Presentation is loading. Please wait.

Presentation is loading. Please wait.

Minimum spanning tree Shortest path algorithms

Similar presentations


Presentation on theme: "Minimum spanning tree Shortest path algorithms"— Presentation transcript:

1 Minimum spanning tree Shortest path algorithms

2 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

3 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 14 2 10 15 3 8 Run on example graph

4 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 14 2 10 15 3 8 Run on example graph

5 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 14 2 10 15 r 3 8 Pick a start vertex r

6 Red vertices have been removed from Q
Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 14 2 10 15 u 3 8 Red vertices have been removed from Q

7 Red arrows indicate parent pointers
Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 14 2 10 15 u 3 8 3 Red arrows indicate parent pointers

8 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 14 14 2 10 15 u 3 8 3

9 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 14 14 2 10 15 3 8 3 u

10 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 14 14 2 10 15 8 3 8 3 u

11 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 10 14 2 10 15 8 3 8 3 u

12 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 10 14 2 10 15 8 3 8 3 u

13 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 10 2 14 2 10 15 8 3 8 3 u

14 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 10 2 14 2 10 15 8 15 3 8 3 u

15 Prim’s Algorithm u MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 10 2 14 2 10 15 8 15 3 8 3

16 Prim’s Algorithm u MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6 4 9 5 10 2 9 14 2 10 15 8 15 3 8 3

17 Prim’s Algorithm u MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 6 4 9 5 10 2 9 14 2 10 15 8 15 3 8 3

18 Prim’s Algorithm u MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 6 4 9 5 5 2 9 14 2 10 15 8 15 3 8 3

19 Prim’s Algorithm u MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 6 4 9 5 5 2 9 14 2 10 15 8 15 3 8 3

20 Prim’s Algorithm u MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 6 4 9 5 5 2 9 14 2 10 15 8 15 3 8 3

21 Prim’s Algorithm u MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 6 4 9 5 5 2 9 14 2 10 15 8 15 3 8 3

22 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 6 4 9 5 5 2 9 14 2 u 10 15 8 15 3 8 3

23 Review: Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); What is the hidden cost in this code?

24 Review: Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; DecreaseKey(v, w(u,v));

25 Review: Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); What will be the running time? A: Depends on queue binary heap: O(E lg V) Fibonacci heap: O(V lg V + E)

26 Walk-Through F C A B D H G E Initialize array K dv pv A F   B C D E
2 Initialize array 3 F C K dv pv A F B C D E G H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

27 F C A B D H G E Start with any node, say D K dv pv A B C D T  E F G H
2 Start with any node, say D 3 F C K dv pv A B C D T E F G H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

28 F C A B D H G E Update distances of adjacent, unselected nodes K dv pv
2 3 F C K dv pv A B C 3 D T E 25 F 18 G 2 H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

29 F C A B D H G E Select node with minimum distance K dv pv A B C 3 D T
2 3 F C K dv pv A B C 3 D T E 25 F 18 G 2 H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

30 F C A B D H G E Update distances of adjacent, unselected nodes K dv pv
2 3 F C K dv pv A B C 3 D T E 7 G F 18 2 H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

31 F C A B D H G E Select node with minimum distance K dv pv A B C T 3 D
2 3 F C K dv pv A B C T 3 D E 7 G F 18 2 H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

32 F C A B D H G E Update distances of adjacent, unselected nodes K dv pv
2 3 F C K dv pv A B 4 C T 3 D E 7 G F 2 H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

33 F C A B D H G E Select node with minimum distance K dv pv A B 4 C T 3
2 3 F C K dv pv A B 4 C T 3 D E 7 G F 2 H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

34 F C A B D H G E Update distances of adjacent, unselected nodes K dv pv
2 3 F C K dv pv A 10 F B 4 C T 3 D E 2 G H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

35 F C A B D H G E Select node with minimum distance K dv pv A 10 F B 4 C
2 3 F C K dv pv A 10 F B 4 C T 3 D E 2 G H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

36 F C A B D H G E Update distances of adjacent, unselected nodes
2 3 F C K dv pv A 10 F B 4 C T 3 D E 2 G H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7 Table entries unchanged

37 F C A B D H G E Select node with minimum distance K dv pv A 10 F B 4 C
2 3 F C K dv pv A 10 F B 4 C T 3 D E 2 G H 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

38 F C A B D H G E Update distances of adjacent, unselected nodes K dv pv
2 3 F C K dv pv A 4 H B C T 3 D E 2 F G 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

39 F C A B D H G E Select node with minimum distance K dv pv A T 4 H B C
2 3 F C K dv pv A T 4 H B C 3 D E 2 F G 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

40 F C A B D H G E Update distances of adjacent, unselected nodes
2 3 F C K dv pv A T 4 H B C 3 D E 2 F G 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7 Table entries unchanged

41 F C A B D H G E Select node with minimum distance K dv pv A T 4 H B C
2 3 F C K dv pv A T 4 H B C 3 D E 2 F G 10 A 7 3 8 4 18 4 B D 9 H 10 25 2 3 G E 7

42 F C A B D H G E Done Cost of Minimum Spanning Tree =  dv = 21 K dv pv
3 F C K dv pv A T 4 H B C 3 D E 2 F G A 3 4 4 B D H 2 3 G E Done

43 Example a b c d e h g f i 4 8 7 11 1 2 14 9 10 6


Download ppt "Minimum spanning tree Shortest path algorithms"

Similar presentations


Ads by Google