Docsity

Greedy Algorithms: Technique for Maximum/Minimum Solutions, Study notes of Computer Science

Toggle sidebar
bg1
COT 5405: Design and Analysis of Algorithms
Instructor Mr. Arup Guha
Fall 2003
Lecture 11 October 07, 2003
Note-Takers: Yasaman Eghbali, Jinhua Xia
Topic: Greedy Algorithms
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!
D
A C B
1
pf3
pf4
pf5

Partial preview of the text

Download Greedy Algorithms: Technique for Maximum/Minimum Solutions and more Study notes Computer Science in PDF only on Docsity!

COT 5405: Design and Analysis of Algorithms

Instructor Mr. Arup Guha

Fall 2003

Lecture 11 October 07, 2003

Note-Takers: Yasaman Eghbali, Jinhua Xia

Topic: Greedy Algorithms

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!

D

A C B

Change problem

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.

Room Scheduling

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:

Minimum Spanning Tree

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

Kruskal’s algorithm

function Kruskal (G = <N, A>: graph; length: A ļ‚®^ R ): set of edge

{initialization} Sort A by increasing length

n ^ the number of nodes in N

T ^ Ƙ {will contain the edges of the minimum spanning tree}

Initialize n sets, each containing a different element of N {greedy loop} repeat

e ^ ^ u ,^ v ^  shortest edge not yet considered

ucomp ^ find(u)

vcomp ^ find(v)

if ucomp vcomp then

merge(ucomp, vcomp)

T  T   e 

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.