1 / 42

Minimum Spanning Trees

Minimum Spanning Trees. CS 146 Prof. Sin-Min Lee Regina Wang. Spanning Tree properties:. On a connected graph G=(V, E), a spanning tree: is a connected subgraph acyclic is a tree (|E| = |V| - 1) contains all vertices of G (spanning). A connected, undirected graph.

temima
Download Presentation

Minimum Spanning Trees

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. Minimum Spanning Trees CS 146 Prof. Sin-Min Lee Regina Wang

  2. Spanning Tree properties: On a connected graph G=(V, E), a spanning tree: • is a connected subgraph • acyclic • is a tree (|E| = |V| - 1) • contains all vertices of G (spanning)

  3. A connected,undirected graph Four of the spanning trees of the graph Spanning trees • Suppose you have a connected undirected graph • Connected: every node is reachable from every other node • Undirected: edges do not have an associated direction • ...then a spanning tree of the graph is a connected subgraph in which there are no cycles • Total of 16 different spanning trees for the graph below

  4. Result of a BFSstarting from top An undirected graph Result of a DFSstarting from top Finding a spanning tree • BFS • DFS • Greedy algorithm

  5. Minimum weight spanning tree (MST) On a weighted graph, A MST: • connects all vertices through edges with least weights. • has w(T) ≤ w(T’) for every other spanning tree T’ in G • Adding one edge not in MST will create a cycle

  6. Finding Minimum Spanning Tree Two “greedy” algorithms • Kruskal’s and Prim’s • make best decision at every stage • locally optimal choice  globally optimal solution. • Greedy algorithms do not always yields optimal solutions.

  7. Kruskal’s algorithm  Edge first 1. Arrange all edges in a list (L) in non-decreasing order • Select edges from L, and include that in set T, avoid cycle. • Repeat 3 until T becomes a tree that covers all vertices

  8. Kruskal’s Algorithm 15 8 5 13 13 16 1 4 14 14 12 12 2 3 16 14 14 7 6 15

  9. Kruskal’s Algorithm 15 8 5 13 13 16 1 4 14 14 12 12 2 3 16 14 14 7 6 15

  10. Kruskal’s Algorithm 15 8 5 13 13 16 1 4 14 14 12 12 2 3 16 14 14 7 6 15

  11. Kruskal’s Algorithm 15 8 5 13 13 16 1 4 14 14 12 12 2 3 16 14 14 7 6 15

  12. Kruskal’s Algorithm 15 8 5 13 13 16 1 4 14 14 12 12 2 3 16 14 14 7 6 15

  13. Kruskal’s Algorithm 15 8 5 13 13 16 1 4 14 14 12 12 2 3 16 14 14 7 6 15

  14. Kruskal’s Algorithm 15 8 5 13 13 16 1 4 14 14 12 12 2 3 16 14 14 7 6 15

  15. Kruskal’s Algorithm 15 8 5 13 13 16 1 4 14 14 12 12 Skip 2 3 16 14 14 7 6 15 Skip {7,8} to avoid cycle

  16. Kruskal’s Algorithm 15 8 5 13 13 16 1 4 14 14 12 12 Skip 2 3 16 Skip 14 14 7 6 15 Skip {5,6} to avoid cycle

  17. Kruskal’s Algorithm 15 8 5 13 13 16 1 4 14 14 12 12 2 3 16 14 14 7 6 15 MST is formed

  18. Prim’s algorithm • Start form any arbitrary vertex • Find the edge that has minimum weight form all known vertices • Stop when the tree covers all vertices

  19. Prim’s algorithm 15 Start from any arbitrary vertex 8 5 13 13 16 1 4 1 14 14 12 12 2 3 16 14 14 7 6 15

  20. Prim’s algorithm 15 8 5 13 13 16 1 4 1 The best choice is {1,2} 14 14 12 12 What’s next? 2 3 2 16 14 14 7 6 Weight:12 15

  21. Prim’s algorithm 15 8 5 8 After {1,8} we may choose {2,7} or {7,8} There are more than one MST 13 13 16 1 4 1 14 14 12 12 What’s next? 2 3 2 16 14 14 7 6 Weight:12+13 15

  22. Prim’s algorithm 15 8 5 8 13 13 Let’s choose {2, 7} at this moment 16 1 4 1 14 14 12 12 We are free to choose {5,8} or {6,7} but not {7,8} because we need to avoid cycle 2 3 2 16 14 14 7 6 7 15

  23. Prim’s algorithm 15 8 5 8 5 13 13 16 1 4 1 Let’s choose {5,8} at this moment 14 14 12 12 2 3 2 16 14 14 The best choice is now {4,5} 7 6 7 15

  24. Prim’s algorithm 15 8 5 8 5 13 13 16 1 4 1 4 14 14 12 12 The best choice is now {4,3} 2 3 2 16 14 14 7 6 7 15

  25. Prim’s algorithm 15 8 5 8 5 13 13 16 1 4 1 4 The best choice is now {3,6} or {5,6} 14 14 12 12 2 3 2 3 16 14 14 7 6 7 15

  26. Prim’s algorithm 15 8 5 8 5 13 13 16 1 4 1 4 A MST is formed Weight = 93 14 14 12 12 2 3 2 3 16 14 14 7 6 7 6 15

  27. Prim’s algorithm – more complex 3 20 54 5 0 100 44 8 14 32 99 31 14 11 19

  28. Prim’s algorithm – more complex 3 20 54 5 0 100 44 8 14 32 99 31 14 11 19

  29. Prim’s algorithm 3 20 54 5 0 100 44 8 14 32 99 31 14 11 19

  30. Prim’s algorithm 3 20 54 5 0 100 44 8 14 32 99 31 14 11 19

  31. Prim’s algorithm 3 20 54 5 0 100 44 8 14 32 99 31 14 11 19

  32. Prim’s algorithm 3 20 54 5 0 100 44 8 14 32 99 31 14 11 19

  33. Prim’s algorithm 3 20 54 5 0 100 44 8 14 32 99 31 14 11 19

  34. Prim’s algorithm 3 20 54 5 0 100 44 8 14 32 99 31 14 11 19

  35. Prim’s algorithm 3 20 54 5 0 MST is formed 100 44 8 14 32 99 31 14 11 19

  36. Compare Prim and Kruskal • Both have the same output  MST • Kruskal’s begins with forest and merge into a tree • Prim’s always stays as a tree • If you don’t know all the weight on edges  use Prim’s algorithm • If you only need partial solution on the graph  use Prim’s algorithm

  37. Compare Prim and Kruskal Complexity Kruskal: O(NlogN) comparison sort for edges Prim: O(NlogN) search the least weight edge for every vertice

  38. Why do we need MST? • a reasonable way for clustering points in space into natural groups • can be used to give approximate solutions to hard problems

  39. Minimizing costs • Suppose you want to provide solution to : • electric power, Water, telephone lines, network setup • To minimize cost, you could connect locations using MST • However, MST is not necessary the shortest path and it does not apply to cycle

  40. MST don’t solve TSP • Travel salesman problem (TSP) can not be solved by MST :  salesman needs to go home (what’s the cost going home?)  TSP is a cycle  Use MST to approximate  solve TSP by exhaustive approach try every permutation on cyclic graph

  41. More practice Kruskal's Algorithm applet Prim’s Algorithm applet

  42. The End Thank you

More Related