Presentation is loading. Please wait.

Presentation is loading. Please wait.

Kruskal’s MST Maximum Flow

Similar presentations


Presentation on theme: "Kruskal’s MST Maximum Flow"— Presentation transcript:

1 Kruskal’s MST Maximum Flow

2 Kruskal’s MST Kruskal’s MST: Insert all edges into a PQ
Grab the min edge from the PQ that does not create a cycle in the MST (Union-Find) Remove it from the PQ and add it to the MST

3 Kruskal’s Example PQ: 1: (0, 2) 2: (3, 5) 3: (1, 4) 4: (2, 5) 5: (2, 3) 5: (0, 3) 5: (1, 2) 6: (0, 1) 6: (2, 4) 6: (4, 5) 6 5 1 1 3 5 5 3 2 2 6 4 4 5 6

4 Union-Find UF eciently determines connectedness of two points.
When adding edges one at a time, you can determine that if the endpoints of an edge to be added are already connected in the graph, Then adding this new edge would cause the endpoints to be biconnected, creating a cycle.

5 Union-Find Init Check public UF(int N) { }
count = N; id = new int[N]; for (int i=0; i<N; ++i) id[i] = i; } Check public connected(int p, int q) { return find(p) == find(q);

6 Union-Find Find Union public int find(int p) { }
while (p != id[p]) p = id[p]; } Union public void union(int p, int q) { int i = find(p); int j = find(q); if (i == j) return; id[i] = j; --count;

7 Kruskal’s Example using Union-Find
PQ: 1: (0, 2) 2: (3, 5) 3: (1, 4) 4: (2, 5) 5: (2, 3) 5: (0, 3) 5: (1, 2) 6: (0, 1) 6: (2, 4) 6: (4, 5) ID: 0: 0 1: 1 2: 2 3: 3 4: 4 5: 5 6 5 1 1 3 5 5 3 2 2 6 4 1 2 3 4 5 4 5 6

8 Kruskal’s Example using Union-Find
PQ: 1: (0, 2) 2: (3, 5) 3: (1, 4) 4: (2, 5) 5: (2, 3) 5: (0, 3) 5: (1, 2) 6: (0, 1) 6: (2, 4) 6: (4, 5) ID: 0: 0 1: 1 2: 0 3: 3 4: 4 5: 5 6 5 1 1 3 5 5 3 2 2 6 4 1 3 4 5 4 5 6 2

9 Kruskal’s Example using Union-Find
PQ: 1: (0, 2) 2: (3, 5) 3: (1, 4) 4: (2, 5) 5: (2, 3) 5: (0, 3) 5: (1, 2) 6: (0, 1) 6: (2, 4) 6: (4, 5) ID: 0: 0 1: 1 2: 0 3: 3 4: 4 5: 3 6 5 1 1 3 5 5 3 2 2 6 4 1 3 4 4 5 6 2 5

10 Kruskal’s Example using Union-Find
PQ: 1: (0, 2) 2: (3, 5) 3: (1, 4) 4: (2, 5) 5: (2, 3) 5: (0, 3) 5: (1, 2) 6: (0, 1) 6: (2, 4) 6: (4, 5) ID: 0: 0 1: 1 2: 0 3: 3 4: 1 5: 3 6 5 1 1 3 5 5 3 2 2 6 4 1 3 4 5 6 2 4 5

11 Kruskal’s Example using Union-Find
PQ: 1: (0, 2) 2: (3, 5) 3: (1, 4) 4: (2, 5) 5: (2, 3) 5: (0, 3) 5: (1, 2) 6: (0, 1) 6: (2, 4) 6: (4, 5) ID: 0: 0 1: 1 2: 0 3: 0 4: 1 5: 3 6 5 1 1 3 5 5 3 2 2 1 6 4 3 4 5 2 4 6 5

12 Kruskal’s Example using Union-Find
PQ: 1: (0, 2) 2: (3, 5) 3: (1, 4) 4: (2, 5) 5: (2, 3) 5: (0, 3) 5: (1, 2) 6: (0, 1) 6: (2, 4) 6: (4, 5) ID: 0: 0 1: 1 2: 0 3: 0 4: 1 5: 3 6 5 1 1 3 5 5 3 2 2 1 6 4 3 4 5 2 4 6 5

13 Kruskal’s Example using Union-Find
PQ: 1: (0, 2) 2: (3, 5) 3: (1, 4) 4: (2, 5) 5: (2, 3) 5: (0, 3) 5: (1, 2) 6: (0, 1) 6: (2, 4) 6: (4, 5) ID: 0: 0 1: 1 2: 0 3: 0 4: 1 5: 3 6 5 1 1 3 5 5 3 2 2 1 6 4 3 4 5 2 4 6 5

14 Kruskal’s Example using Union-Find
PQ: 1: (0, 2) 2: (3, 5) 3: (1, 4) 4: (2, 5) 5: (2, 3) 5: (0, 3) 5: (1, 2) 6: (0, 1) 6: (2, 4) 6: (4, 5) ID: 0: 0 1: 0 2: 0 3: 0 4: 1 5: 3 6 5 1 1 3 5 5 3 2 2 6 4 2 3 1 4 5 6 5 4

15 Union-Find Worst case?

16 Max-flow Ford-Fulkerson Edmonds-Karp
Using DFS to find the augmenting path Edmonds-Karp Using BFS to find the augmenting path Augmenting path with smallest number of edges

17 Edmonds Karp’s Max-flow Example
0|2 A D 0|15 0|5 0|10 0|7 s C 0|9 t 0|10 0|3 0|10 0|6 0|5 B E

18 Edmonds Karp’s Max-flow Example
2|2 2 s A t: min(5, 2)=2 A D 0|15 2|5 0|10 2 0|7 s C 0|9 t 0|10 0|3 0|10 0|6 0|5 B E

19 Edmonds Karp’s Max-flow Example
2|2 2 s A t: min(5,2)=2 s B E t: min(10,5,6)=5 A D 0|15 2|5 0|10 2 0|7 s C 0|9 t 0|10 5 0|3 5|6 5|10 5 5|5 B E 5

20 Edmonds Karp’s Max-flow Example
2|2 2 s A t: min(5,2)=2 s B E t: min(10,5,6)=5 s C D t: min(7,10,15)=7 A D 7|15 2|5 7|10 2 7 7 7|7 s C 0|9 t 7 0|10 5 0|3 5|6 5|10 5 5|5 B E 5

21 Edmonds Karp’s Max-flow Example
2|2 2 s A t: min(5,2)=2 s B E t: min(10,5,6)=5 s C D t: min(7,10,15)=7 S B C D t: min(5, 3, 3, 8)=3 A D 10|15 2|5 10|10 2 10 10 7|7 s C 0|9 t 7 0|10 8 3|3 5|6 8|10 3 5 5|5 B E 5

22 Edmonds Karp’s Max-flow Example
2|2 2 s A t: min(5,2)=2 s B E t: min(10,5,6)=5 s C D t: min(7,10,15)=7 S B C D t: min(5, 3, 3, 8)=3 A D 10|15 2|5 10|10 2 10 10 7|7 s C 0|9 t 7 0|10 8 3|3 5|6 8|10 5 5|5 B E 5

23 Edmonds Karp’s Max-flow Example
2|2 2 s A t: min(5,2)=2 s B E t: min(10,5,6)=5 s C D t: min(7,10,15)=7 S B C D t: min(5, 3, 3, 8)=3 Max-Flow=17 A D 10|15 2|5 10|10 2 10 10 7|7 s C 0|9 t 7 0|10 8 3|3 5|6 8|10 5 5|5 B E 5


Download ppt "Kruskal’s MST Maximum Flow"

Similar presentations


Ads by Google