Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 373: Data Structures and Algorithms

Similar presentations


Presentation on theme: "CSE 373: Data Structures and Algorithms"— Presentation transcript:

1 CSE 373: Data Structures and Algorithms
Lecture 24: Graphs VI

2 Minimum Spanning Tree Problem
Input: Undirected Graph G = (V, E) and a cost function C from E to non-negative real numbers. C(e) is the cost of edge e. Output: A spanning tree T with minimum total cost. That is: T that minimizes

3 Observations about Spanning Trees
For any spanning tree T, inserting an edge enew not in T creates a cycle But Removing any edge eold from the cycle gives back a spanning tree If enew has a lower cost than eold we have progressed!

4 Find the MST A C B D F H G E 1 7 6 5 11 4 12 13 2 3 9 10 The previous problem is a minimum spanning tree problem. We have two primary ways of solving this problem deterministically. BTW, Which of these three is the minimum spanning tree? (The one in the middle)

5 Two Different Approaches
Prim’s Algorithm Looks familiar! Kruskals’s Algorithm Completely different! One node, grow greedily Forest of MSTs, Union them together. I wonder how to union…

6 Prim’s algorithm Idea: Grow a tree by adding an edge from the “known” vertices to the “unknown” vertices. Pick the edge with the smallest weight. G v Pick one node as the root, Incrementally add edges that connect a “new” vertex to the tree. Pick the edge (u,v) where u is in the tree, v is not AND where the edge weight is the smallest of all edges (where u is in the tree and v is not). known

7 Prim’s algorithm A B C D F E 10 1 5 8 3 6 2 4
Starting from empty T, choose a vertex at random and initialize V = {A}, T ={} G 6

8 Prim’s algorithm A Choose the vertex u not in V such that edge weight from u to a vertex in V is minimal (greedy!) V = {A,C} T = { (A,C) } 10 5 1 3 8 B C D 1 1 6 4 2 F E G 6

9 Prim’s algorithm Repeat until all vertices have been chosen
V = {A,C,D} T= { (A,C), (C,D)} A B C D F E 10 1 5 8 3 6 2 4 G

10 Prim’s algorithm V = {A,C,D,E} T = { (A,C), (C,D), (D,E) } A B C D F E
10 1 5 8 3 6 2 4 G 6

11 Prim’s algorithm V = {A,C,D,E,B} T = { (A,C), (C,D), (D,E), (E,B) } A
F E 10 1 5 8 3 6 2 4 G 6

12 Prim’s algorithm V = {A,C,D,E,B,F}
T = { (A,C), (C,D), (D,E), (E,B), (B,F) } A B C D F E 10 1 5 8 3 6 2 4 G 6

13 Prim’s algorithm V = {A,C,D,E,B,F,G}
T = { (A,C), (C,D), (D,E), (E,B), (B,F), (E,G) } A B C D F E 10 1 5 8 3 6 2 4 G 6

14 Prim’s algorithm Final Cost: 1 + 3 + 4 + 1 + 1 + 6 = 16 A B C D F E 10
5 8 3 6 2 4 G 6

15 Prim's Algorithm Implementation
for each vertex v: // Initialization v's distance := infinity. v's previous := none. mark v as unknown. choose random node v1. v1's distance := 0. List := {all vertices}. T := {}. while List is not empty: v := remove List vertex with minimum distance. add edge {v, v's previous} to T. mark v as known. for each unknown neighbor n of v: if distance(v, n) is smaller than n's distance: n's distance := distance(v, n). n's previous := v. return T.

16 Prim’s algorithm Analysis
How is it different from Djikstra's algorithm? If the step that removes unknown vertex with minimum distance is done with binary heap the running time is: O(|E|log |V|)

17 Kruskal’s MST Algorithm
Idea: Grow a forest out of edges that do not create a cycle. Pick an edge with the smallest weight. G=(V,E) v

18 Example of Kruskal 1 1 6 5 4 7 2 3 {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5}

19 Example of Kruskal 2 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5}

20 Example of Kruskal 2 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5}

21 Example of Kruskal 3 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5}

22 Example of Kruskal 4 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5}

23 Example of Kruskal 5 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5}

24 Example of Kruskal 6 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5}

25 Example of Kruskal 7 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5}

26 Example of Kruskal 7 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5}

27 Example of Kruskal 8,9 1 2 3 1 3 3 3 7 3 4 1 6 4 2 2 5 {7,4} {2,1} {7,5} {5,6} {5,4} {1,6} {2,7} {2,3} {3,4} {1,5}

28 Kruskal's Algorithm Implementation
sort edges in increasing order of length (e1, e2, e3, ..., em). T := {}. for i = 1 to m if ei does not add a cycle: add ei to T. return T. But how can we determine that adding ei to T won't add a cycle?


Download ppt "CSE 373: Data Structures and Algorithms"

Similar presentations


Ads by Google