Workbook For Algorithms: Set 1 No Author Given No Institute Given
1
Disc Discla laim imer er
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.
2
Dynamic Programming
In genomics, given two gene sequences, we try to find if parts of one gene are same as the other. Thus its is important to find the longest common subsequence of two sequences. Write an algorithm for finding the longest common subsequence of two strings. Problem 1
Given an array of integers A of length n, find the longest sequence i1 , ldots, ik such that ij < i j +1 and A[ij ] ≤ A[ij +1 ] for any j ∈ [1, k − 1]. Problem 2
Problem 3
Design an algorithm to find the longest path in a Directed Acyclic Graph.
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: Problem 4
1. Given an estimation of the budget, your algorithm should be able to suggest the customer the combination of products one can buy within one’s budget. Can you help your friends to design the algorithm ?
Problem 5
Chain Matrix Multiplication.
3
Greedy Technique
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 efficient way to detect “pattern” in them- for example, they want to know if the four events Problem 6
“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 .
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 7
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 8
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 nlookups are processed sequentially but can be processed in any order. We wish to minimize the total waiting time i=1 T i where T i 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 ij =1 tj milliseconds. Design an efficient algorithm for computing an optimum order for processing the queries. Problem 9
The United States Postal Service makes fixed size mail shipping boxes-you pay a fixed price for a given box and can ship anything you want that fits 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 first-fit heuristics of the greedy algorithm for the problem works as follows: Problem 10
1. Process the items in a sequence given and place them in the first box in which they fit scanning through the boxes in 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 .
4
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).