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.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
Minimum Spanning Trees (MSTs) Prim's Algorithm For each vertex not in the tree, keep track of the lowest cost edge that would connect it to the tree This.
1 Theory I Algorithm Design and Analysis (10 - Shortest paths in graphs) T. Lauer.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the path begins is the source vertex.
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
External-Memory MST (Arge, Brodal, Toma). Minimum-Spanning Tree Given a weighted, undirected graph G=(V,E), the minimum-spanning tree (MST) problem is.
Minimum spanning tree Prof Amir Geva Eitan Netzer.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
ADA: 10. MSTs1 Objective o look at two algorithms for finding mimimum spanning trees (MSTs) over graphs Prim's algorithm, Kruskal's algorithm Algorithm.
Week -7-8 Topic - Graph Algorithms CSE – 5311 Prepared by:- Sushruth Puttaswamy Lekhendro Lisham.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 5 Graph Algorithms.
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
Dijkstra’s Algorithm Supervisor: Dr.Franek Ritu Kamboj
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
CSCI2100 Data Structures Tutorial 12
Contest Algorithms January 2016 Describe shortest path trees, SSSP, APSP, and three algorithms: Dijkstra, Bellman-Ford, Floyd-Warshall 9. Shortest Paths.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
© 2010 Goodrich, Tamassia Shortest Paths1 C B A E D F
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Shortest Path -Prim’s -Djikstra’s. PRIM’s - Minimum Spanning Tree -A spanning tree of a graph is a tree that has all the vertices of the graph connected.
Lecture ? The Algorithms of Kruskal and Prim
Minimum Spanning Trees
COMP108 Algorithmic Foundations Greedy methods
Minimum Spanning Trees
Programming Abstractions
Eager Prim Dijkstra.
Minimum Spanning Trees and Shortest Paths
Ellen Walker CPSC 201 Data Structures Hiram College
Ford-Fulkerson.
Shortest Path Problems
Shortest Path Graph represents highway system Edges have weights
CSCE350 Algorithms and Data Structure
Minimum Spanning Trees
Eager Prim Dijkstra.
Graphs & Graph Algorithms 2
CSE373: Data Structures & Algorithms Lecture 12: Minimum Spanning Trees Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 20: Minimum Spanning Trees Linda Shapiro Spring 2016.
CSE 373 Data Structures and Algorithms
Graphs.
Chapter 11 Graphs.
Shortest Path Problems
Shortest path algorithm
Minimum Spanning Tree Algorithms
Slide Courtesy: Uwash, UT
Minimum Spanning Tree.
Algorithms: Design and Analysis
Algorithms Searching in a Graph.
CSE 373: Data Structures and Algorithms
Shortest Path Problems
Shortest Paths.
CSE 373 Data Structures and Algorithms
Slide Courtesy: Uwash, UT
Graph Algorithms: Shortest Path
CSE 373: Data Structures and Algorithms
Algorithm Course Dr. Aref Rashad
Graphs: Shortest path and mst
Single-Source Shortest Path & Minimum Spanning Trees
Topological Sorting Minimum Spanning Trees Shortest Path
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

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 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.

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}; }

Here is our Graph B AC D

Step 1: Remove all loops B AC D

Step 1: Remove all loops B AC D

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

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

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

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

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

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 ABCD A05 B50 C0 D0

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

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

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

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 ABCD A05 ∞ B50411 C10405 D∞1150

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 ABCD A05 ∞ B50411 C10405 D∞1150

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 ABCD A05 ∞ B50411 C10405 D∞1150

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

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

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 ABCD A05 ∞ B50411 C10405 D∞1150

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

Step 7: We have our final Minimum Spanning Tree. B AC D

Weight of MST = Sum of marked edges. AB + BC + CD = = 14 B AC D

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); }

B AC D 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); }

B AC D 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); }

B AC D 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); }

B AC D 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); }

B AC D 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); }

B AC D 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); }

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|)

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

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

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)

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

Worked Example: a b c d e gf

Step 1: Draw a table with the set of vertices (v) a b c d e gf

a b c d e gf Vabcdefg a

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

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 Vabcdefg a0a0a 3a3a 5a5a 6a6a ∞a∞a ∞a∞a ∞a∞a

a b c d e gf Vabcdefg a0a0a 3a3a 5a5a 6a6a ∞a∞a ∞a∞a ∞a∞a b0a0a 3a3a

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 Vabcdefg a0a0a 3a3a 5a5a 6a6a ∞a∞a ∞a∞a ∞a∞a b0a0a 3a3a

a b c d e gf Vabcdefg a0a0a 3a3a 5a5a 6a6a ∞a∞a ∞a∞a ∞a∞a b0a0a 3a3a 5b5b

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 Vabcdefg a0a0a 3a3a 5a5a 6a6a ∞a∞a ∞a∞a ∞a∞a b0a0a 3a3a 5b5b

a b c d e gf Vabcdefg a0a0a 3a3a 5a5a 6a6a ∞a∞a ∞a∞a ∞a∞a b0a0a 3a3a 5a5a 5b5b ∞a∞a ∞a∞a ∞a∞a

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

Step 4: Next lowest unmarked weight is selected. So any of “5a” or “5b” will be valid. a b c d e gf 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!

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 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

Step 6: Next lowest “8c”. f->e = 8c+5f = 13f ! g = 8c+1f = 9f <12c a b c d e gf 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

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 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

Shortest Path weight from ‘a’ to ‘g’ is = 9f, where f=8c, where c = 5a. a b c d e gf 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

So, the shortest path from ‘a’ to ‘g’ would be: ac gf 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

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]); }

a b c d e gf 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

a b c d e gf 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

a b c d e gf 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

a b c d e gf 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

a b c d e gf 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

a b c d e gf 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

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

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.