



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




A greedy algorithm is one of general algorithmic techniques that can be used for problems where you are trying to find some sort of maximum of minimum type of solution, and it is supposed to be the best solution. One way to solve these kinds of problems is to look at all possible sets of solutions and see which one is the best. But, as you might imagine there can be lots of solutions, and this technique can be very slow. It turns out that for many problems there is no need to analyze all possible solutions, rather you can make a very quick and greedy choice just by looking at some small local portion of data, without being worry about the rest of the data source. So, the idea behind a greedy algorithm is to make the āchoice that seems best at the timeā, and if we can prove the correctness of our solution, we often have an unbeatable solution with respect of efficiency. However often times, greedy algorithms simply donāt produce the optimal solution. As an example, imagine that you are giving driving directions from point A to point B, and the only possible choices are move along the X axis, and the Y axis. In this case, a greedy algorithm recommends that you make a greedy choice and move to the direction that looks closer to point B. According to this algorithm you have to go all the way down to point C, and then come back all the way to D to turn around. In this case the greedy algorithm doesnāt work, since it would have been easier to go to point D first, and then to B!
Change problem is an example of greedy algorithm in which you try to make change for n cents using the minimum number of coins possible. Problem : Let {d1, d2, d3, d4, 1} be a set of denomination of coins, where d1 has the largest value, and d4 | d3, d3 | d2, d2 | d1. Prove that the following greedy algorithm gives the optimal solution for the change problem. Algorithm: Give the maximum possible number of d1 coins. Give the maximum possible number of d2 coins for what is left. Give the maximum possible number of d3 coins for what is left. Give the maximum possible number of d4 coins for what is left. Proof: Let S be a set of coins that this greedy algorithm produce. We know to find other solutions for the change problem from this set of values, we can take out any coin of a particular value and replace it with other coins. But, notice that you we are not allowed to increase the number of d1 coins, otherwise we will end up having too much change. The other way that one can think is to take out a d1 coin and replace it with some number of coins of smaller value. However, our goal was to minimize the number of coins, and this will contradict our goal! So, we proved that optimal configuration to make change for n cents must have c = ļ«n / d1ļ» coins of denomination d1. It can not have any more (we will end up having extra change), or any less (we will increase the number of coins). Once we proved that, the problem is reduced to make change for (n ā c.d1) cents using {d3, d2, d1, 1} coins. As you see, the problem is recursive and the same argument is true.
Problem: Given a range of time to schedule a single room, and a list of starting and ending times for events, e(s, f), create a schedule for the room that maximizes the number of events for which the room is granted. Room Scheduling Algorithm:
Let G = <N, A> be a connected, undirected graph where N is the set of nodes and A is the set of edges. Each edge has a given nonnegative length. Problem: Find a subset T of edges of G such that all the nodes remain connected when only the edges in T are used, and the sum of the lengths of edges in T is as small as possible. If G has edges of length 0, given solutions with equal total length, we prefer the one with least edges. Minimum spanning tree: Let Gā = <N, T> be a partial graph formed by the nodes of G and the edges in T, suppose there are n nodes in N. T: number of edges in Gā. If Gā as (n ā 1) edges connected um sum of the lengths of edges, then Gā is called Minimum spanning tree. Lemma Let G = < N,A > be a connected undirected graph where the length of each edge is given. Let be a strict subset of the nodes of G. Let be a promising set of edges such that no edge in T leaves B. Let be the shortest edge that leaves B (or one of the shortest if ties exist), thenis promising. B N \ B u in U v of minimal length
{initialization} Sort A by increasing length
Initialize n sets, each containing a different element of N {greedy loop} repeat
merge(ucomp, vcomp)
until T contains n ā 1 edges return T Show how this algorithm works. Increasing order of length the edges n are: {3,5},{4,6},{3,7},{4,7},{5,6},{3,6},{2,5}, {1,2},{3,4},{1,6},{2,3},{1,5},{1,4}
Theorem Kruskalās algorithm finds a minimum spanning tree.