13
Workbook For Algorithms: Set 1 No Author Given No Institute Given 1 Disclaimer A small collection of 20 to 30 problems can hardly replace the need of a text book. Henceforth, we make no such daring claims like “all you need is this workbook”. This booklet is merely a pointer to the set of problems we will be discussing in the class or tutorials. Anyone considering this workbook to be a quick guide to problem solving can be accused of misinterpretation of the purpose of the booklet. (BTW, what is the purpose of writing the booklet ! Maybe just to show we are working overtime :-)) All the questions mentioned in the booklet are chosen from various sources and we cannot ensure the accuracy of the questions. In case you wish to bring some issues regarding the problems or you want us to include some new problems that you have in possession, please write back to us. Also the faculties for the course are merely mortal human beings and in short may not be able to solve all the problems mentioned in the booklet. In case of failure on our part, show the generosity of forgiving us. Finally, to err is human and to forgive is divine.

Workbook for algorithms

Embed Size (px)

Citation preview

Page 1: Workbook for algorithms

Workbook For Algorithms: Set 1

No Author Given

No Institute Given

1 Disclaimer

A small collection of 20 to 30 problems can hardly replace the need of a text book. Henceforth, we make no suchdaring claims like “all you need is this workbook”. This booklet is merely a pointer to the set of problems we willbe discussing in the class or tutorials. Anyone considering this workbook to be a quick guide to problem solving canbe accused of misinterpretation of the purpose of the booklet. (BTW, what is the purpose of writing the booklet !Maybe just to show we are working overtime :-))

All the questions mentioned in the booklet are chosen from various sources and we cannot ensure the accuracy ofthe questions. In case you wish to bring some issues regarding the problems or you want us to include some newproblems that you have in possession, please write back to us. Also the faculties for the course are merely mortalhuman beings and in short may not be able to solve all the problems mentioned in the booklet. In case of failure onour part, show the generosity of forgiving us. Finally, to err is human and to forgive is divine.

Page 2: Workbook for algorithms

2 Dynamic Programming

Problem 1 In genomics, given two gene sequences, we try to find if parts of one gene are same as the other. Thusits is important to find the longest common subsequence of two sequences. Write an algorithm for finding the longestcommon subsequence of two strings.

Page 3: Workbook for algorithms

Problem 2 Given an array of integers A of length n, find the longest sequence 〈i1, ldots, ik〉 such that ij < ij+1 andA[ij ] ≤ A[ij+1] for any j ∈ [1, k − 1].

Page 4: Workbook for algorithms

Problem 3 Design an algorithm to find the longest path in a Directed Acyclic Graph.

Page 5: Workbook for algorithms

Problem 4 Consider that some of your non-engineer friends are starting an e-commerce business where people canbuy gifts for their loved ones. They are trying to design an algorithm for the following purpose:

1. Given an estimation of the budget, your algorithm should be able to suggest the customer the combination ofproducts one can buy within one’s budget.

Can you help your friends to design the algorithm ?

Page 6: Workbook for algorithms

Problem 5 Chain Matrix Multiplication.

Page 7: Workbook for algorithms

3 Greedy Technique

Problem 6 Some of your friends need an algorithm for time -series data mining in which one looks for patterns insequence of events that occur over time. Stock exchange data seems to be a natural source of such data. Given a longsequence S of such events, your friends want an efficient way to detect “pattern” in them- for example, they want toknow if the four events“buy yahoo, buy ebay, buy yahoo, buy oracle”occur in this sequence S, in order but not necessarily consecutively.

They begin with a collection of possible events and a sequence S of n these events. A given event may occur multipletimes in S . We will say that a sequence S′ is a subsequence of S if there is a way to delete some of the events fromS so that the remaining events in order are equal to S′. Say for example, the sequence of the four events above is asubsequence of“buy amazon, buy yahoo, buy ebay, buy yahoo, buy yahoo, buy oracle”

The goal is to be able to dream up short sequences and quickly detect whether they are subsequences of S. So the goalis to solve the following problem: Give an algorithm that takes two sequences of events S′ of length m and S of lengthn, each possibly containing an event more than once and decide whether S′ is a sequence of S.

Page 8: Workbook for algorithms

Problem 7 Let us consider a quite road with houses scattered very sparsely along it. (Picture the road as a long linesegment, with an eastern point and an western point.) The residents of all these houses are avid cell phone users.You want to place cell phone base station at certain points along the road, so that every house is withing four milesof one of the base stations. Give an algorithm to achieve the goal using as few base stations as possible.

Page 9: Workbook for algorithms

Problem 8 Suppose you are given a connected graph G, with edge costs that are all distinct. Prove that G has aunique minimum spanning tree.

Page 10: Workbook for algorithms

Problem 9 A database has to respond to n simultaneous client SQL queries. The service time required for query iis ti milliseconds and is known in advance. The lookups are processed sequentially but can be processed in any order.We wish to minimize the total waiting time

∑ni=1 Ti where Ti is the time client i takes to return. For example if the

lookups are served in order of increasing i, then the client making the ith query has to wait for∑i

j=1 tj milliseconds.Design an efficient algorithm for computing an optimum order for processing the queries.

Page 11: Workbook for algorithms

Problem 10 The United States Postal Service makes fixed size mail shipping boxes-you pay a fixed price for a givenbox and can ship anything you want that fits in a box. Suppose you have a set of n items that you need to ship andhave a large supply of some x× y× z inch priority mail shipping boxes. Naturally, you want to minimize the numberof boxes you use.

The first-fit heuristics of the greedy algorithm for the problem works as follows:

1. Process the items in a sequence given and place them in the first box in which they fit scanning through the boxesin increasing order.

The first fit is never optimal but it never takes more than twice the optimal.(Will show it in the class).

Your task: Implement the first-fit technique to run in O(n log n) time.

Page 12: Workbook for algorithms

4 Randomized Algorithm

Problem 11 P. that the expected height of a skip list is O(log n).

Page 13: Workbook for algorithms

Problem 12 P. that the expected number of comparisons in the quick sort algorithm is O(n log n).