Greedy Algorithm Nattee Niparnan Greedy If solving problem
Greedy Algorithm Nattee Niparnan
Greedy �If solving problem is a series of steps �Simply pick the one that “maximize” the immediate outcome � Instead of looking for the long run result
Example of Greedy Algorithm �Rational Knapsack �Step �You have to select items to be put into your sack �Greedy �Simply pick the one having highest price/weight ratio �We know that this approach does not work in the case of 0 -1 knapsack
Minimal Spanning Tree
Minimal Spanning Tree �Given an undirected connected graph �With weighted edges �Find a subgraph of that graph �That is connected �Having smallest sum of edges’ weights
Example graph MST
Observation �MST should not have a cycle �Why? �Removing edge in a cycle does not destroy the connectivity problem �So why bother having an edge in a cycle in the MST
Property of Trees �A tree with N nodes has N – 1 edges �A connected graph having |V| - 1 = |E| is a tree
MST Problem �Input �An undirected connected graph �Weighted edges �Output �A set of edges that constitute the MST of the given graph �(notice that all nodes must be in the tree, so we don’t need to select them)
Kruskal’s Algorithm �Idea �We need |V|- 1 edges �Simply pick them �At each step, just select one edge having smallest value �That does not cause a cycle
Example A C E B D F
Example A B 1 C E D F
Example A B 1 1 C E D F
Example A 1 C E 1 B 1 D F
Example 1 A C E 2 B 1 1 D F
Example 1 A C 3 2 B E 1 D 1 F
Cut Property �Why Kruskal’s works? �It is because of the “cut property” �Suppose a set of edges X are part of a MST of G = (V, E), pick any subset of nodes S for which X does not cross between S and V – S, and let e be the lightest edge across this partition. Then X U {e} is part of some MST
Cut Property
Implementing Kruskal’s �Need something to check the connected component of the graph �We could do CC problem �But that takes too much time �Try some data structure that represent “disjoint set” �It should be able to �makeset(x) create a set containing just x �find(x) find a set where x is a member �union(x, y) union a set find(x) and a set find(y)
Implementing Kruskal’s procedure kruskal(G; w) //Input: A connected undirected graph G = (V, E) with edge weights we //Output: A minimum spanning tree defined by the edges X for all u V : makeset(u) X = {} Sort the edges E by weight for all edges (u, v) E, in increasing order of weight: if find(u) != find(v): add edge (u, v) to X union(u, v)
Analysis �What is O of kruskal? �It sorts the edges �It needs � |V| makeset � 2|E| find � |V| - 1 union �What is the eventual complexity?
Prim’s Algorithm �Instead of selecting the “minimal” edge of all edges that does not create a cycle �Select the “minimal” edge of all edges that connects to the original graph
Prim’s Implementation procedure kruskal(G; w) //Input: A connected undirected graph G = (V, E) with edge weights we //Output: A minimum spanning tree defined by the array prev for all u V : cost(u) = 1 prev(u) = nil Pick any initial node u 0 cost(u 0) = 0 H = makequeue(V ) (priority queue, using cost-values as keys) while H is not empty: v = deletemin(H) for each (v, z) E: if cost(z) > w(v, z): cost(z) = w(v, z) prev(z) = v
Example A C E B D F
Example A B 1 C E D F
Example 1 A C E D F 2 B
Example 1 A C E D F 2 B 1
Example 1 A C 3 2 B E 1 D F
Example 1 A C 3 2 B E 1 D 1 F
- Slides: 30