Minimum spanning trees Kruskals algorithm Prims algorithm April

  • Slides: 15
Download presentation
Minimum spanning trees Kruskal’s algorithm Prim’s algorithm April 03, 2020 Cinda Heeren / Andy

Minimum spanning trees Kruskal’s algorithm Prim’s algorithm April 03, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1

Announcements • Reminder: TA evaluations and course/instructor evaluations are to be completed online –

Announcements • Reminder: TA evaluations and course/instructor evaluations are to be completed online – Check e-mail for instructions • All open course activities due Thursday, April 09 April 03, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 2

Minimum spanning tree Kruskal’s algorithm • • How are we certain that such a

Minimum spanning tree Kruskal’s algorithm • • How are we certain that such a simple algorithm is guaranteed to produce a minimum spanning tree? April 03, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 3

Kruskal’s algorithm Proof of correctness • April 03, 2020 Cinda Heeren / Andy Roth

Kruskal’s algorithm Proof of correctness • April 03, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 4

Kruskal’s algorithm Correctness • April 03, 2020 Cinda Heeren / Andy Roth / Geoffrey

Kruskal’s algorithm Correctness • April 03, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 5

Kruskal’s algorithm Correctness • April 03, 2020 Cinda Heeren / Andy Roth / Geoffrey

Kruskal’s algorithm Correctness • April 03, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 6

Kruskal’s algorithm Correctness • April 03, 2020 Cinda Heeren / Andy Roth / Geoffrey

Kruskal’s algorithm Correctness • April 03, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 7

Kruskal’s algorithm Correctness • April 03, 2020 Cinda Heeren / Andy Roth / Geoffrey

Kruskal’s algorithm Correctness • April 03, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 8

Kruskal’s algorithm Correctness • April 03, 2020 Cinda Heeren / Andy Roth / Geoffrey

Kruskal’s algorithm Correctness • April 03, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 9

Kruskal’s algorithm Correctness • April 03, 2020 Cinda Heeren / Andy Roth / Geoffrey

Kruskal’s algorithm Correctness • April 03, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 10

Recall: BFS spanning tree Starting from vertex A A 6 16 B 2 3

Recall: BFS spanning tree Starting from vertex A A 6 16 B 2 3 C 8 D 9 G G B (A, 2), (C, 3), (E, 7), (G, 13) C (A, 6), (B, 3), (D, 8), (E, 17), (F, 13) D (A, 16), (C, 8), (F, 12), (G, 9) E (B, 7), (C, 17), (F, 5), (H, 16) F (C, 13), (D, 12), (E, 5), (G, 10), (H, 11) G (D, 9), (F, 10), (H, 4) H (E, 16), (F, 11), (G, 4) E 5 H 13 16 11 H 4 Queue: (B, 2), (C, 6), (D, 16) F 17 F 10 A E 7 13 12 B C D A B C D E G F H Identified: T T T T A B C D E F G H What if we use a priority queue (with neighbours' edge weights) instead of an ordinary queue? April 03, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 11

Prim's algorithm • Greedy algorithm • Builds a spanning tree from initially one vertex.

Prim's algorithm • Greedy algorithm • Builds a spanning tree from initially one vertex. • Repeatedly chooses the minimum-weight edge from a vertex in the tree, to a vertex outside the tree – adds that vertex to the tree Prims. Algorithm(v) { mark v as visited, add v to spanning tree while (graph has unvisited vertices) { Find least cost edge (w, u) from a visited vertex w to unvisited vertex u Mark u as visited Add vertex u and edge (w, u) to the minimum spanning tree } } April 03, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 12

Prim's algorithm A 6 16 B 2 3 C 8 D 7 17 E

Prim's algorithm A 6 16 B 2 3 C 8 D 7 17 E 13 12 9 pr. Q: 16 11 10 G 5 F 4 H 13 B A (B, 2), (C, 6), (D, 16) C E B (A, 2), (C, 3), (E, 7), (G, 13) D C (A, 6), (B, 3), (D, 8), (E, 17), (F, 13) G D (A, 16), (C, 8), (F, 12), (G, 9) F E (B, 7), (C, 17), (F, 5), (H, 16) F (C, 13), (D, 12), (E, 5), (G, 10), (H, 11) G (D, 9), (F, 10), (H, 4) H (E, 16), (F, 11), (G, 4) H (A, B, 2) (A, C, 6) (A, D, 16) (B, C, 3) (B, E, 7) (B, G, 13) (C, D, 8) (C, E, 17) (C, F, 13) (E, F, 5) Visited: T T T T A B C D E F G H (E, H, 16) (F, D, 12) (F, G, 10) (F, H, 11) (D, G, 9) All vertices visited MST weight: 38 (G, H, 4) April 03, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 13

Prim's algorithm Complexity • Actually we can describe Kruskal's algorithm the same way April

Prim's algorithm Complexity • Actually we can describe Kruskal's algorithm the same way April 03, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 14

Readings for this lesson • Carrano & Henry – Chapters 20. 4. 2 –

Readings for this lesson • Carrano & Henry – Chapters 20. 4. 2 – 20. 4. 3 (Spanning trees, minimum spanning trees) • Next class: – Carrano & Henry, Chapter 20. 4. 4 (Shortest path) April 03, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 15