# CS502 Fundamentals of Algorithms Assignment 3 solution Fall 2012

Question 1:                                                                                                                               Marks 10

After reviewing the activity scheduling with greedy approach, suppose that instead of always selecting the first activity to finish, we instead select the last activity to start that is compatible with all previously selected activities. Describe how this approach is greedy algorithm and prove that it yields an optimal solution.

Greedy algorithms are fast. Therefore, if it can be proven that they yield the global optimum for a certain problem, they will be the method of choice. The technique is used in the following graph algorithms which have many practical applications:

1. Dijkstra’s algorithm for finding shortest path to all nodes given a start node
2. Prim’s algorithm for finding a minimum spanning tree
3. Huffman trees for data compression

The problem with the greedy approach is that most of the time you obtain only some local optimum and not the global one. In such cases you might want to consider a more powerful “tool” like Dynamic Programming. There is a time trade-of, though.

There are cases when, even though greedy algorithms does not guarantee the global optimum, we still might want to use them to get a good approximation for the answer to the problem. For example consider the following situation:

You have a set of n items and you want to place them in several fixed-size boxes. Your goal is to minimize the number of boxes used. A greedy approach where you process the items in the order they are first given, placing them in the first box they fit in, is not optimum but it never takes more than twice the actual minimum number of boxes

The proposed approach: selecting the last activity to start that is compatible with all previously selected activities:  is really the greedy algorithm but starting from the end rather than the beginning.

Another way to look at it is as follows. We are given a set S = {a1, a2.  . . an} of activities, where ai = [si , fi] , and we propose to find an optimal solution by selecting the last activity to start that is compatible with all previously selected activities. Instead, let us create a set Sˊ = {aˊ1, aˊ2. . .aˊn}, where aˊi= [ fi , si]. That is, aˊi is ai in reverse. Clearly, a subset of {ai1, ai2. . . aik} ⊆ S is mutually compatible if and only if the corresponding subset {aˊi1 , aˊi2 , . . . , aˊik} ⊆ Sˊ is also mutually compatible. Thus, an optimal solution for S maps directly to an optimal solution for Sˊ and vice versa.

The proposed approach of selecting the last activity to start that is compatible with all previously selected activities, when run on S, gives the same answer as the greedy algorithm from the text-selecting the first activity to finish that is compatible with all previously selected activities-when run on Sˊ. The solution that the proposed approach finds for S corresponds to the solution that the text’s greedy algorithm finds for Sˊ, and so it is optimal.

Question 2:                                                                                                                               Marks 10

Mr. Mohsin drives a car from Lahore to Rahim Yar Khan along National Highway, His car’s gas tank, when full, holds enough gas to travel n miles, and his map gives the distance between gas station on his route. Mohsin wishes to make as few gas stops as possible along the way.

Give an efficient method by which Mohsin can determine at which gas stations he should stop, and prove that your strategy yields an optimal solution.