1 / 30

Greedy Algorithm

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

lupita
Download Presentation

Greedy Algorithm

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Greedy Algorithm NatteeNiparnan

  2. 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

  3. 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

  4. Minimal Spanning Tree

  5. 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

  6. Example graph MST

  7. 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

  8. Property of Trees • A tree with N nodes has N – 1 edges • A connected graph having |V| - 1 = |E| is a tree

  9. 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)

  10. 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

  11. Example A C E B D F

  12. Example 1 A C E B D F

  13. Example 1 A C E 1 B D F

  14. Example 1 A C E 1 1 B D F

  15. Example 1 A C E 2 1 1 B D F

  16. Example 1 A C E 3 2 1 1 B D F

  17. 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

  18. Cut Property

  19. 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)

  20. 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)

  21. 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?

  22. 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

  23. 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 u0 cost(u0) = 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

  24. Example A C E B D F

  25. Example 1 A C E B D F

  26. Example 1 A C E 2 B D F

  27. Example 1 A C E 2 1 B D F

  28. Example 1 A C E 3 2 1 B D F

  29. Example 1 A C E 3 2 1 1 B D F

More Related