Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prim’s MST Djikstra SP. PRIM’s - Minimum Spanning Tree -A spanning tree of a graph is a tree that has all the vertices of the graph connected by some.

Similar presentations


Presentation on theme: "Prim’s MST Djikstra SP. PRIM’s - Minimum Spanning Tree -A spanning tree of a graph is a tree that has all the vertices of the graph connected by some."— Presentation transcript:

1 Prim’s MST Djikstra SP

2 PRIM’s - Minimum Spanning Tree -A spanning tree of a graph is a tree that has all the vertices of the graph connected by some edges. -A graph can have one or more number of spanning trees. -If the graph has N vertices then the spanning tree will have N-1 edges.

3 Prim MarkedSet = {A}; (0 or starting vertex) UnMarkedSet = {B,C,D}; (1 to N-1 where N is the total number of vertices and 0 is the starting(marked) vertex) while( UnMarkedSet != empty) { Find edge e = (x,y) such that: 1. x ϵ MarkedSet 2. y ϵ UnMarkedSet 3. e has the smallest weight SpanningTree = Spanningtree U {e}; MarkedSet = MarkedSet U {y}; UnMarkedSet = UnMarkedSet – {y}; }

4 Here is our Graph B AC D 11 4 5 5 10 12 8 4

5 Step 1: Remove all loops B AC D 11 4 5 5 10 12 8 4

6 Step 1: Remove all loops B AC D 11 4 5 5 10 12

7 Step 2: Remove all parallel edges connecting the same vertices except for the least weighted one. B AC D 11 4 5 5 10 12

8 Step 2: Remove all parallel edges connecting the same vertices except for the least weighted one. B AC D 11 4 5 5 10

9 Step 3: We are ready to create our MST. Draw a table with rows = columns = number of vertices B AC D 11 4 5 5 10

10 Step 3: We are ready to create our MST. Draw a table with rows = columns = number of vertices B AC D 11 4 5 5 10 ABCD A B C D

11 Step 4: Put 0 in cells having the same row and column name B AC D 11 4 5 5 10 ABCD A0 B0 C0 D0

12 Step 5: Find the edge directly connecting vertex A to other vertices and fill up the table values for cells “AB” as well as “BA” B AC D 11 4 5 5 10 ABCD A05 B50 C0 D0

13 Step 5: Similarly for “AC” and “CA”. B AC D 11 4 5 5 10 ABCD A05 B50 C 0 D0

14 Step 5: The cell “AD” is put as ∞ since no direct path from A to D B AC D 11 4 5 5 10 ABCD A05 ∞ B50 C 0 D∞0

15 Step 5: Fill other rows similarly B AC D 11 4 5 5 10 ABCD A05 ∞ B50411 C10405 D∞1150

16 Step 6: Find the smallest value in row A except for ‘0’ and mark it as a minimum edge for both vertices that it connects to. B AC D 11 4 5 5 10 ABCD A05 ∞ B50411 C10405 D∞1150

17 Step 6: Find the smallest value in row A except for ‘0’ and mark it as a minimum edge for both vertices that it connects to. B AC D 11 4 5 5 10 ABCD A05 ∞ B50411 C10405 D∞1150

18 Step 7: Now looked for the smallest unmarked value in all rows that have a marked edge. In this case, rows A and B that have marked edge ‘5’. B AC D 11 4 5 5 10 ABCD A05 ∞ B50411 C10405 D∞1150

19 Step 7: Smallest unmarked value out of all values in rows A and B is ‘4’ corresponding to “BC” and “CB”. B AC D 11 4 5 5 10 ABCD A05 ∞ B50411 C10405 D∞1150

20 Step 7: Mark “BC” = “CB” = 4 B AC D 11 4 5 5 10 ABCD A05 ∞ B50411 C10405 D∞1150

21 Step 7: Now look for the smallest unmarked value in all three rows A,B,C since all now have marked values in them. In this case, “CD” = 5. B AC D 11 4 5 5 10 ABCD A05 ∞ B50411 C10405 D∞1150

22 Step 7: Mark “CD” = “DC” = 5. B AC D 11 4 5 5 10 ABCD A05 ∞ B50411 C10405 D∞1150

23 Step 7: We have our final Minimum Spanning Tree. B AC D 11 4 5 5 10

24 Weight of MST = Sum of marked edges. AB + BC + CD = 5 + 4 + 5 = 14 B AC D 11 4 5 5 10

25 LazyPrimMST public lazyPrimMST(EdgeWeightedGraph G) { pq = new MinPQ (); marked = new boolean[G.V()]; mst = new Queue (); visit(G,0); while(!pq.isEmpty()) { Edge e = pq.delMin(); int v = e.either(), w = e.other(v); If (marked[v] && marked[w]) continue; mst.enqueue(e); If (!marked[v]) visit(G, v); If (!marked[w]) visit(G, w); } private void visit(EdgeWeightedGraph G, int v) { marked[v] = true; for (Edge e: G.adj(v)) { if (!marked[e.other(v)]) pq.insert(e); }

26 B AC D 11 4 5 5 10 public lazyPrimMST(EdgeWeightedGraph G) { pq = new MinPQ (); marked = new boolean[G.V()]; mst = new Queue (); visit(G,0); while(!pq.isEmpty()) { Edge e = pq.delMin(); int v = e.either(), w = e.other(v); If (marked[v] && marked[w]) continue; mst.enqueue(e); If (!marked[v]) visit(G, v); If (!marked[w]) visit(G, w); } pq: null Marked: A pq: :5, :10 private void visit(EdgeWeightedGraph G, int v) { marked[v] = true; for (Edge e: G.adj(v)) { if (!marked[e.other(v)]) pq.insert(e); }

27 B AC D 11 4 5 5 10 public lazyPrimMST(EdgeWeightedGraph G) { pq = new MinPQ (); marked = new boolean[G.V()]; mst = new Queue (); visit(G,0); while(!pq.isEmpty()) { Edge e = pq.delMin(); int v = e.either(), w = e.other(v); If (marked[v] && marked[w]) continue; mst.enqueue(e); If (!marked[v]) visit(G, v); If (!marked[w]) visit(G, w); } pq: :5, :10 pq.delMin: :5 B is not visited yet, so visit(G,B) Marked: A,B pq: :4, :10, :11 private void visit(EdgeWeightedGraph G, int v) { marked[v] = true; for (Edge e: G.adj(v)) { if (!marked[e.other(v)]) pq.insert(e); }

28 B AC D 11 4 5 5 10 public lazyPrimMST(EdgeWeightedGraph G) { pq = new MinPQ (); marked = new boolean[G.V()]; mst = new Queue (); visit(G,0); while(!pq.isEmpty()) { Edge e = pq.delMin(); int v = e.either(), w = e.other(v); If (marked[v] && marked[w]) continue; mst.enqueue(e); If (!marked[v]) visit(G, v); If (!marked[w]) visit(G, w); } pq: :4, :10, :11 pq.delMin: :4 C is not visited yet, so visit(G,C) Marked: A,B,C pq: :5, :10, :11 private void visit(EdgeWeightedGraph G, int v) { marked[v] = true; for (Edge e: G.adj(v)) { if (!marked[e.other(v)]) pq.insert(e); }

29 B AC D 11 4 5 5 10 public lazyPrimMST(EdgeWeightedGraph G) { pq = new MinPQ (); marked = new boolean[G.V()]; mst = new Queue (); visit(G,0); while(!pq.isEmpty()) { Edge e = pq.delMin(); int v = e.either(), w = e.other(v); If (marked[v] && marked[w]) continue; mst.enqueue(e); If (!marked[v]) visit(G, v); If (!marked[w]) visit(G, w); } pq: :5, :10, :11 pq.delMin: :5 D is not visited yet, so visit(G,D) Marked: A,B,C,D pq: :10, :11 private void visit(EdgeWeightedGraph G, int v) { marked[v] = true; for (Edge e: G.adj(v)) { if (!marked[e.other(v)]) pq.insert(e); }

30 B AC D 11 4 5 5 10 public lazyPrimMST(EdgeWeightedGraph G) { pq = new MinPQ (); marked = new boolean[G.V()]; mst = new Queue (); visit(G,0); while(!pq.isEmpty()) { Edge e = pq.delMin(); int v = e.either(), w = e.other(v); If (marked[v] && marked[w]) continue; mst.enqueue(e); If (!marked[v]) visit(G, v); If (!marked[w]) visit(G, w); } pq: :10, :11 pq.delMin: :10 A,C are visited Marked: A,B,C,D pq: :11 private void visit(EdgeWeightedGraph G, int v) { marked[v] = true; for (Edge e: G.adj(v)) { if (!marked[e.other(v)]) pq.insert(e); }

31 B AC D 11 4 5 5 10 public lazyPrimMST(EdgeWeightedGraph G) { pq = new MinPQ (); marked = new boolean[G.V()]; mst = new Queue (); visit(G,0); while(!pq.isEmpty()) { Edge e = pq.delMin(); int v = e.either(), w = e.other(v); If (marked[v] && marked[w]) continue; mst.enqueue(e); If (!marked[v]) visit(G, v); If (!marked[w]) visit(G, w); } pq: :11 pq.delMin: :11 B,D are visited Marked: A,B,C,D pq: null ->finished, Q:early finish? private void visit(EdgeWeightedGraph G, int v) { marked[v] = true; for (Edge e: G.adj(v)) { if (!marked[e.other(v)]) pq.insert(e); }

32 Time Complexity: Minimum edge weight data structureTime complexity (total) adjacency matrix, searchingO(|V| 2 ) binary heap and adjacency listO((|V| + |E|) log |V|) = O(|E| log |V|) Fibonacci heap and adjacency listO(|E| + |V| log |V|)

33 Dijkstra’s Shortest Path: - Given: weighted graph, G, and source vertex, v - Compute: shortest path to every other vertex in G - Path length is sum of edge weights along path. Shortest path has smallest length among all possible paths

34 Algorithm: Grow a collection of vertices for which shortest path is known - paths contain only vertices in the set - add as new vertex the one with the smallest distance to the source - shortest path to an outside vertex must contain a current shortest path as a prefix Use a greedy algorithm

35 Edge Relaxation: Maintain value D[u] for each vertex Each starts at infinity, and decreases as we find out about a shorter path from v to u (D[v] = 0) Maintain priority queue, Q, of vertices to be relaxed use D[u] as key for each vertex remove min vertex from Q, and relax its neighbors Relaxation for each neighbor of u: If D[u] + w(u,z) < D[z] then, D[z] = D[u] + w(u,z)

36 Pseudocode: ShortestPath(G, v) init D array entries to infinity D[v]=0 add all vertices to priority queue Q while Q not empty do u = Q.removeMin() for each neighbor, z, of u in Q do if D[u] + w(u,z) < D[z] then D[z] = D[u] + w(u,z) Change key of z in Q to D[z] return D as shortest path lengths

37 Worked Example: a b c d e gf 3 2 6 5 2 9 3 6 75 2 1

38 Step 1: Draw a table with the set of vertices (v) a b c d e gf 3 2 6 5 2 9 3 6 75 2 1

39 a b c d e gf 3 2 6 5 2 9 3 6 75 2 1 Vabcdefg a

40 Step 2: Mark all vertices starting from ‘a’. Subscript denotes the vertex we connect from. a b c d e gf 3 2 6 5 2 9 3 6 75 2 1 Vabcdefg a0a0a 3a3a 5a5a 6a6a ∞a∞a ∞a∞a ∞a∞a

41 Step 3: The lowest weight is marked red (shortest weight determined) and the next lowest weight in the row is looked for – “3a” in this case which is the weight from ‘a’ to ‘b’. So, the next row in the table starts with ‘b’ and “3a” marked red. a b c d e gf 3 2 6 5 2 9 3 6 75 2 1 Vabcdefg a0a0a 3a3a 5a5a 6a6a ∞a∞a ∞a∞a ∞a∞a

42 a b c d e gf 3 2 6 5 2 9 3 6 75 2 1 Vabcdefg a0a0a 3a3a 5a5a 6a6a ∞a∞a ∞a∞a ∞a∞a b0a0a 3a3a

43 Step 3: The next adjacent vertex from b is looked for and compared with the weight when directly reached from a – “6a”. When coming via vertex b, the total weight is a->b(“3a”)+b- >d(“2b”) = “5b”. This new weight will replace the older weight in the ‘d’ column. a b c d e gf 3 2 6 5 2 9 3 6 75 2 1 Vabcdefg a0a0a 3a3a 5a5a 6a6a ∞a∞a ∞a∞a ∞a∞a b0a0a 3a3a

44 a b c d e gf 3 2 6 5 2 9 3 6 75 2 1 Vabcdefg a0a0a 3a3a 5a5a 6a6a ∞a∞a ∞a∞a ∞a∞a b0a0a 3a3a 5b5b

45 Step 3: Cannot reach anywhere else from ‘b’, so rest of the weights – ‘c’,’e’,’f’,’g’ are copied down from the first row as they were a b c d e gf 3 2 6 5 2 9 3 6 75 2 1 Vabcdefg a0a0a 3a3a 5a5a 6a6a ∞a∞a ∞a∞a ∞a∞a b0a0a 3a3a 5b5b

46 a b c d e gf 3 2 6 5 2 9 3 6 75 2 1 Vabcdefg a0a0a 3a3a 5a5a 6a6a ∞a∞a ∞a∞a ∞a∞a b0a0a 3a3a 5a5a 5b5b ∞a∞a ∞a∞a ∞a∞a

47 Step 4: Next lowest value is selected. So any of “5a” or “5b” will be valid. a b c d e gf 3 2 6 5 2 9 3 6 75 2 1 Vabcdefg a0a0a 3a3a 5a5a 6a6a ∞a∞a ∞a∞a ∞a∞a b0a0a 3a3a 5a5a 5b5b ∞a∞a ∞a∞a ∞a∞a

48 Step 4: Next lowest unmarked weight is selected. So any of “5a” or “5b” will be valid. a b c d e gf 3 2 6 5 2 9 3 6 75 2 1 Vabcdefg a0a0a 3a3a 5a5a 6a6a ∞a∞a ∞a∞a ∞a∞a b0a0a 3a3a 5a5a 5b5b ∞a∞a ∞a∞a ∞a∞a c0a0a 3a3a 5a5a 5b5b 11 c 8c8c 12 c -c->d cost: 5a+2c = 7c which is !< 5b, so weight in column d remains 5b. -c->e cost = 5a+6c = 11c which is < ∞, so new weight in column e becomes 11c -Similarly for f and g, new weights are 8c and 12c respectively. -Remember: Subscript is where I’m coming from!

49 Step 5: Next lowest “5b”. d->f = 5b+9d = 14d !< 11c. ‘f’ and ‘g’ are unreachable so their weights remain as they are. a b c d e gf 3 2 6 5 2 9 3 6 75 2 1 Vabcdefg a0a0a 3a3a 5a5a 6a6a ∞a∞a ∞a∞a ∞a∞a b0a0a 3a3a 5a5a 5b5b ∞a∞a ∞a∞a ∞a∞a c0a0a 3a3a 5a5a 5b5b 11 c 8c8c 12 c d0a0a 3a3a 5a5a 5b5b 11 c 8c8c 12 c

50 Step 6: Next lowest “8c”. f->e = 8c+5f = 13f ! g = 8c+1f = 9f <12c a b c d e gf 3 2 6 5 2 9 3 6 75 2 1 Vabcdefg a0a0a 3a3a 5a5a 6a6a ∞a∞a ∞a∞a ∞a∞a b0a0a 3a3a 5a5a 5b5b ∞a∞a ∞a∞a ∞a∞a c0a0a 3a3a 5a5a 5b5b 11 c 8c8c 12 c d0a0a 3a3a 5a5a 5b5b 11 c 8c8c 12 c f0a0a 3a3a 5a5a 5b5b 11 c 8c8c 9f9f

51 Step 7: No more scope for improvement so the last row gives me the shortest path from ‘a’ to any of the other vertices. a b c d e gf 3 2 6 5 2 9 3 6 75 2 1 Vabcdefg a0a0a 3a3a 5a5a 6a6a ∞a∞a ∞a∞a ∞a∞a b0a0a 3a3a 5a5a 5b5b ∞a∞a ∞a∞a ∞a∞a c0a0a 3a3a 5a5a 5b5b 11 c 8c8c 12 c d0a0a 3a3a 5a5a 5b5b 11 c 8c8c 12 c f0a0a 3a3a 5a5a 5b5b 11 c 8c8c 9f9f

52 Shortest Path weight from ‘a’ to ‘g’ is = 9f, where f=8c, where c = 5a. a b c d e gf 3 2 6 5 2 9 3 6 75 2 1 Vabcdefg a0a0a 3a3a 5a5a 6a6a ∞a∞a ∞a∞a ∞a∞a b0a0a 3a3a 5a5a 5b5b ∞a∞a ∞a∞a ∞a∞a c0a0a 3a3a 5a5a 5b5b 11 c 8c8c 12 c d0a0a 3a3a 5a5a 5b5b 11 c 8c8c 12 c f0a0a 3a3a 5a5a 5b5b 11 c 8c8c 9f9f

53 So, the shortest path from ‘a’ to ‘g’ would be: ac gf 5 3 1 Vabcdefg a0a0a 3a3a 5a5a 6a6a ∞a∞a ∞a∞a ∞a∞a b0a0a 3a3a 5a5a 5b5b ∞a∞a ∞a∞a ∞a∞a c0a0a 3a3a 5a5a 5b5b 11 c 8c8c 12 c d0a0a 3a3a 5a5a 5b5b 11 c 8c8c 12 c f0a0a 3a3a 5a5a 5b5b 11 c 8c8c 9f9f

54 DijkstraSP public DijkstraSP(EdgeWeightedDigraph G, int s) { edgeTo = new DirectedEdge[G.V()]; distTo = new double[G.V()]; pq = new IndexMinPQ (G.V()); for (int v = 0; v < G.V(); ++v) { distTo[v] = Double.POSITIVE_INFINITY; } pq.insert(s, 0.0); while (!pq.isEmpty()) { relax(G, pq.delMin()); } Private void relax(EdgeWeightedDigraph G, int v) { for (DirectedEdge e: G.adj(v)) { int w = e.to(); if (distTo[w] > distTo[v] + e.weight()) { distTo[w] = distTo[v] + e.weight(); edgeTo[w] = e; if (pq.contains(w)) { pq.changeKey(w, distTo[w]); } else { pq.insert(w, distTo[w]); }

55 a b c d e gf 3 2 6 5 2 9 3 6 75 2 1 pq.insert(s, 0.0); while (!pq.isEmpty()) { relax(G, pq.delMin()); } Private void relax(EdgeWeightedDigraph G, int v) { for (DirectedEdge e: G.adj(v)) { int w = e.to(); if (distTo[w] > distTo[v] + e.weight()) { distTo[w] = distTo[v] + e.weight(); edgeTo[w] = e; if (pq.contains(w)) { pq.changeKey(w, distTo[w]); } else { pq.insert(w, distTo[w]); } pq: null pq: a:0

56 a b c d e gf 3 2 6 5 2 9 3 6 75 2 1 pq.insert(s, 0.0); while (!pq.isEmpty()) { relax(G, pq.delMin()); } Private void relax(EdgeWeightedDigraph G, int v) { for (DirectedEdge e: G.adj(v)) { int w = e.to(); if (distTo[w] > distTo[v] + e.weight()) { distTo[w] = distTo[v] + e.weight(); edgeTo[w] = e; if (pq.contains(w)) { pq.changeKey(w, distTo[w]); } else { pq.insert(w, distTo[w]); } distTo: a:0, b:inf, c:inf, d:inf, e:inf, f:inf, g:inf pq: a:0 pq.delMin: a:0 relax a: update distance from a distTo: a:0, b:3, c:5, d:6, e:inf, f:inf, g:inf pq: b:3, c: 5, d: 6

57 a b c d e gf 3 2 6 5 2 9 3 6 75 2 1 pq.insert(s, 0.0); while (!pq.isEmpty()) { relax(G, pq.delMin()); } Private void relax(EdgeWeightedDigraph G, int v) { for (DirectedEdge e: G.adj(v)) { int w = e.to(); if (distTo[w] > distTo[v] + e.weight()) { distTo[w] = distTo[v] + e.weight(); edgeTo[w] = e; if (pq.contains(w)) { pq.changeKey(w, distTo[w]); } else { pq.insert(w, distTo[w]); } distTo: a:0, b:3, c:5, d:6, e:inf, f:inf, g:inf pq: b:3, c: 5, d: 6 pq.delMin: b:3 relax b: update distance from b distTo: a:0, b:3, c:5, d:5, e:inf, f:inf, g:inf pq: c:5, d: 5

58 a b c d e gf 3 2 6 5 2 9 3 6 75 2 1 pq.insert(s, 0.0); while (!pq.isEmpty()) { relax(G, pq.delMin()); } Private void relax(EdgeWeightedDigraph G, int v) { for (DirectedEdge e: G.adj(v)) { int w = e.to(); if (distTo[w] > distTo[v] + e.weight()) { distTo[w] = distTo[v] + e.weight(); edgeTo[w] = e; if (pq.contains(w)) { pq.changeKey(w, distTo[w]); } else { pq.insert(w, distTo[w]); } distTo: a:0, b:3, c:5, d:5, e:inf, f:inf, g:inf pq: c:5, d: 5 pq.delMin: c:5 relax c: update distance from c distTo: a:0, b:3, c:5, d:5, e:11, f:8, g:12 pq: d:5, f:8, e:11, g:12

59 a b c d e gf 3 2 6 5 2 9 3 6 75 2 1 pq.insert(s, 0.0); while (!pq.isEmpty()) { relax(G, pq.delMin()); } Private void relax(EdgeWeightedDigraph G, int v) { for (DirectedEdge e: G.adj(v)) { int w = e.to(); if (distTo[w] > distTo[v] + e.weight()) { distTo[w] = distTo[v] + e.weight(); edgeTo[w] = e; if (pq.contains(w)) { pq.changeKey(w, distTo[w]); } else { pq.insert(w, distTo[w]); } distTo: a:0, b:3, c:5, d:5, e:11, f:8, g:12 pq: d:5, f:8, e:11, g:12 pq.delMin: d:5 relax d: update distance from d distTo: a:0, b:3, c:5, d:5, e:11, f:8, g:12 pq: f:8, e:11, g:12

60 a b c d e gf 3 2 6 5 2 9 3 6 75 2 1 pq.insert(s, 0.0); while (!pq.isEmpty()) { relax(G, pq.delMin()); } Private void relax(EdgeWeightedDigraph G, int v) { for (DirectedEdge e: G.adj(v)) { int w = e.to(); if (distTo[w] > distTo[v] + e.weight()) { distTo[w] = distTo[v] + e.weight(); edgeTo[w] = e; if (pq.contains(w)) { pq.changeKey(w, distTo[w]); } else { pq.insert(w, distTo[w]); } distTo: a:0, b:3, c:5, d:5, e:11, f:8, g:12 pq: f:8, e:11, g:12 pq.delMin: f:8 relax f: update distance from f distTo: a:0, b:3, c:5, d:5, e:11, f:8, g:9 pq: g:9, e:11

61 Djikstra Analysis: O(nlogn) time to build priority queue O(nlogn) time removing vertices from queue O(mlogn) time relaxing edges Changing key can be done in O(logn) time Total time: O( (n + m)logn ) which can be O(n 2 logn) for dense graph

62 Prim’s vs Dijkstra’s Prim builds up MST for each adjacent vertex so, will have to go through all previous vertices to get to the desired vertex Dijkstra’s builds up the MST for the shortest possible path to any of the vertices from the source vertex which may or may not include all vertices occurring before the destination vertex. Train that connects to several cities would then use Prim’s Algorithm. But a train that could go from one city to another in the shortest possible time, would use Dijkstra’s.


Download ppt "Prim’s MST Djikstra SP. PRIM’s - Minimum Spanning Tree -A spanning tree of a graph is a tree that has all the vertices of the graph connected by some."

Similar presentations


Ads by Google