Workbook For Algorithms

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Workbook For Algorithms: Set 1

No Author Given
No Institute Given

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.

Dynamic Programming

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

Problem 2 Given an array of integers A of length n, nd the longest sequence i1 , ldots, ik such that ij < ij+1 and A[ij ] A[ij+1 ] for any j [1, k 1].

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

Problem 4 Consider that some of your non-engineer friends are starting an e-commerce business where people can buy 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 of products one can buy within ones budget. Can you help your friends to design the algorithm ?

Problem 5 Chain Matrix Multiplication.

Greedy Technique

Problem 6 Some of your friends need an algorithm for time -series data mining in which one looks for patterns in sequence of events that occur over time. Stock exchange data seems to be a natural source of such data. Given a long sequence S of such events, your friends want an ecient way to detect pattern in them- for example, they want to know 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 multiple times 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 from S so that the remaining events in order are equal to S . Say for example, the sequence of the four events above is a subsequence 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 goal is to solve the following problem: Give an algorithm that takes two sequences of events S of length m and S of length n, each possibly containing an event more than once and decide whether S is a sequence of S.

Problem 7 Let us consider a quite road with houses scattered very sparsely along it. (Picture the road as a long line segment, 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 miles of one of the base stations. Give an algorithm to achieve the goal using as few base stations as possible.

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

Problem 9 A database has to respond to n simultaneous client SQL queries. The service time required for query i is ti milliseconds and is known in advance. The lookups are processed sequentially but can be processed in any order. n We wish to minimize the total waiting time i=1 Ti where Ti is the time client i takes to return. For example if the i lookups are served in order of increasing i, then the client making the ith query has to wait for j=1 tj milliseconds. Design an ecient algorithm for computing an optimum order for processing the queries.

Problem 10 The United States Postal Service makes xed size mail shipping boxes-you pay a xed price for a given box and can ship anything you want that ts in a box. Suppose you have a set of n items that you need to ship and have a large supply of some x y z inch priority mail shipping boxes. Naturally, you want to minimize the number of boxes you use. The rst-t heuristics of the greedy algorithm for the problem works as follows: 1. Process the items in a sequence given and place them in the rst box in which they t scanning through the boxes in increasing order. The rst t is never optimal but it never takes more than twice the optimal.(Will show it in the class). Your task: Implement the rst-t technique to run in O(n log n) time.

Randomized Algorithm

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

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

You might also like