Dijkstra’s Algorithm Shortest Path (Single Source)

Slides:



Advertisements
Similar presentations
Problem solving with graph search
Advertisements

Chapter 9: Graphs Shortest Paths
CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
Graph Theory Arnold Mesa. Basic Concepts n A graph G = (V,E) is defined by a set of vertices and edges v3 v1 v2Vertex (v1) Edge (e1) A Graph with 3 vertices.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
* Bellman-Ford: single-source shortest distance * O(VE) for graphs with negative edges * Detects negative weight cycles * Floyd-Warshall: All pairs shortest.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Design and Analysis of Algorithms Single-source shortest paths, all-pairs shortest paths Haidong Xue Summer 2012, at GSU.
CSE 373: Data Structures and Algorithms Lecture 19: Graphs III 1.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
Management Science 461 Lecture 2b – Shortest Paths September 16, 2008.
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
Discussion #34 1/17 Discussion #34 Warshall’s and Floyd’s Algorithms.
Chapter 3 The Greedy Method 3.
Chapter 7: Greedy Algorithms 7.4 Finding the Shortest Path Dijkstra’s Algorithm pp
Floyd’s Algorithm (shortest-path problem) Section 8.2.
CSC 213 Lecture 23: Shortest Path Algorithms. Weighted Graphs Each edge in weighted graph has numerical weight Weights can be distances, building costs,
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
Shortest path algorithm. Introduction 4 The graphs we have seen so far have edges that are unweighted. 4 Many graph situations involve weighted edges.
ASC Program Example Part 3 of Associative Computing Examining the MST code in ASC Primer.
More Chapter 7: Greedy Algorithms Kruskal’s Minimum Spanning Tree Algorithm.
Dijkstra’s Algorithm and Heuristic Graph Search David Johnson.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Greedy methods Prudence Wong
CS 146: Data Structures and Algorithms July 21 Class Meeting
More Dynamic Programming Floyd-Warshall Algorithm.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.
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.
Dr. Naveed Ahmad Assistant Professor Department of Computer Science University of Peshawar.
Representing and Using Graphs
Shortest Path Problem Weight of the graph –Nonnegative real number assigned to the edges connecting to vertices Weighted graphs –When a graph.
Dijkstra’s Algorithm. This algorithm finds the shortest path from a source vertex to all other vertices in a weighted directed graph without negative.
Dijkstra’s Algorithm Supervisor: Dr.Franek Ritu Kamboj
1 The Floyd-Warshall Algorithm Andreas Klappenecker.
Shortest Path in Weighted Graph : Dijkstra’s Algorithm.
Shortest Path Problems Dijkstra’s Algorithm. Introduction Many problems can be modeled using graphs with weights assigned to their edges: Airline flight.
Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.
1 Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible b locally optimal.
1 Ch20. Dynamic Programming. 2 BIRD’S-EYE VIEW Dynamic programming The most difficult one of the five design methods Has its foundation in the principle.
All-Pairs Shortest Paths
A Introduction to Computing II Lecture 16: Dijkstra’s Algorithm Fall Session 2000.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
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.
Graphs – Part III CS 367 – Introduction to Data Structures.
Shortest Path Problems
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Greedy Technique.
Chapter 7: Greedy Algorithms
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
CS330 Discussion 6.
Shortest Path Graph represents highway system Edges have weights
Shortest Path.
Analysis and design of algorithm
CSE 373: Data Structures and Algorithms
Shortest Path Algorithms
Floyd’s Algorithm (shortest-path problem)
CSE 373: Data Structures and Algorithms
Shortest Path Algorithm for Weighted Non-negative Undirected Graphs
Weighted Graphs & Shortest Paths
CSE 373 Data Structures and Algorithms
Dijkstra's Shortest Path Algorithm
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Graphs: Shortest path and mst
Directed Graphs (Part II)
Presentation transcript:

Dijkstra’s Algorithm Shortest Path (Single Source) Comparison to Floyd’s Algorithm: Solves a simpler problem O(n2) vs. O(n3)

Dijkstra’s: A Greedy Algorithm What does greedy mean? At the beginning of each step, the algorithm picks the best option. Q: What other problem used a greedy algorithm? A: Knapsack problem Recall that there were 3 greedy strategies Highest $$$ Lowest weight Highest $$$/weight ratio

Dijkstra’s: A Dynamic Programming Algorithm Recall that dynamic programming refers to an algorithm that chooses to store information rather than re-compute it. To really be considered Dynamic Programming Store or update information at each step Use the collective information to compute the next step

Recall Floyd’s Algorithm for (int k = 0; k < N; k++) // For each vertex // Update the matrix for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) // Allow vertex k to be an intermediate hop if (M[i][k]+M[k][j] < M[i][j]) then M[i][j] = M[i][k]+M[k][j]

Recall Floyd’s Algorithm The beauty of Floyd’s algorithm is that it progressively minimizes the distance between vertices Each step incorporates more and more possibilities, i.e., we add another intermediate hop. Step 0 i j k1 Step 1 k2 k3 Step 2 Step 3

Recall Floyd’s Algorithm However, at the end of each step, the algorithm can rule out inferior possibilities. if (M[i][k]+M[k][j] < M[i][j]) then M[i][j] = M[i][k]+M[k][j] If the i  k  j is a better option than i  j then we never consider i  j in the future. We always know that hopping through k is better

Recall Floyd’s Algorithm Implicitly Floyd’s algorithm computes the minimum path between any two vertices given O(n!) possibilities. But, it actually does NOT compute all the possibilities hops that are not optimal are quickly identified and never considered in the future. k 4 5 i j 8

How is Dijkstra’s algorithm better? Its really not better. Its more appropriate when you have a designated starting vertex. Since you know the source vertex, you can limit your exploration. If you limit your exploration properly, you can compute the minimum distance to all vertices in O(n2) computations.

a b c d e f g h i j Initial Step: Given a graph Adjacency List a null 28 13 17 8 14 38 6 7 32 5 20 21 4 26 11 30 10 45 3 46 Initial Step: Given a graph Adjacency List V Prev Dist a null b  c d e f g h i j a d b 5 28 c 8 13 f 6 17 g 11 14 32 e i 4 26 38 46 3 7 h j 45 10 20 21 30

Pick the V with the smallest dist 28 8 Step 1: Pick the V with the smallest dist V Prev Dist a null b  c d e f g h i j a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a d b 5 28 c 8 13 f 6 17 g 11 14 32 e i 4 26 38 46 3 7 h j 45 10 20 21 30 g h i 11 45 10 5 j

Iterate over its adj. list and update the table 28 8 Step 1: Mark it as visited. Iterate over its adj. list and update the table V Prev Dist a null b 28 c  d 5 e f g h i j a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i

Pick v with smallest dist. 28 8 Step 2: Pick v with smallest dist. V Prev Dist a null b 28 c  d 5 e f g h i j a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i

Iterate over its adj. list and update the table 28 8 Step 2: Mark it as visited. Iterate over its adj. list and update the table V Prev Dist a null b d 19 c  5 e f g 16 h i j a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i

Pick v with smallest dist. 28 8 Step 3: Pick v with smallest dist. V Prev Dist a null b d 19 c  5 e f g 16 h i j a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i

Iterate over its adj. list and update the table 28 8 Step 3: Mark it as visited. Iterate over its adj. list and update the table V Prev Dist a null b d 19 c  5 e f g 16 h 21 i j 61 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i

Pick v with smallest dist. 28 8 Step 4: Pick v with smallest dist. V Prev Dist a null b d 19 c  5 e f g 16 h 21 i j 61 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i

Iterate over its adj. list and update the table 28 8 Step 4: Mark it as visited. Iterate over its adj. list and update the table V Prev Dist a null b d 19 c 27 5 e  f g 16 h 21 i j 61 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i

Pick v with smallest dist. 28 8 Step 5: Pick v with smallest dist. V Prev Dist a null b d 19 c 27 5 e  f g 16 h 21 i j 61 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i

Iterate over its adj. list and update the table 28 8 Step 5: Mark it as visited. Iterate over its adj. list and update the table V Prev Dist a null b d 19 c 27 5 e h 42 f  g 16 21 i 51 j 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i

Pick v with smallest dist. 28 8 Step 6: Pick v with smallest dist. V Prev Dist a null b d 19 c 27 5 e h 42 f  g 16 21 i 51 j 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i

Iterate over its adj. list and update the table 28 8 Step 6: Mark it as visited. Iterate over its adj. list and update the table V Prev Dist a null b d 19 c 27 5 e h 42 f 33 g 16 21 i 51 j 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i

Pick v with smallest dist. 28 8 Step 7: Pick v with smallest dist. V Prev Dist a null b d 19 c 27 5 e h 42 f 33 g 16 21 i 51 j 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i

Iterate over its adj. list and update the table 28 8 Step 7: Mark it as visited. Iterate over its adj. list and update the table V Prev Dist a null b d 19 c 27 5 e h 42 f 33 g 16 21 i j 36 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i

Pick v with smallest dist. 28 8 Step 8: Pick v with smallest dist. V Prev Dist a null b d 19 c 27 5 e h 42 f 33 g 16 21 i j 36 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i

Iterate over its adj. list and update the table 28 8 Step 8: Mark it as visited. Iterate over its adj. list and update the table V Prev Dist a null b d 19 c 27 5 e f 36 33 g 16 h 21 i j 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i

Pick v with smallest dist. 28 8 Step 9: Pick v with smallest dist. V Prev Dist a null b d 19 c 27 5 e f 36 33 g 16 h 21 i j 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i

Iterate over its adj. list and update the table 28 8 Step 9: Mark it as visited. Iterate over its adj. list and update the table V Prev Dist a null b d 19 c 27 5 e f 36 33 g 16 h 21 i j 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i

Pick v with smallest dist. 28 8 Step 10: Pick v with smallest dist. V Prev Dist a null b d 19 c 27 5 e f 39 33 g 16 h 21 i j 36 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i

Iterate over its adj. list and update the table 28 8 Step 10: Mark it as visited. Iterate over its adj. list and update the table V Prev Dist a null b d 19 c 27 5 e f 39 33 g 16 h 21 i j 36 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i

28 8 Traceback The table can be used to recursively trace all the shortest paths from vertex a V Prev Dist a null b d 19 c 27 5 e f 39 33 g 16 h 21 i j 36 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 g h i 11 45 10 5 j

What is the shortest path to vertex e? 28 8 Traceback What is the shortest path to vertex e? V Prev Dist a null b d 19 c 27 5 e f 36 33 g 16 h 21 i j 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 g h i 11 45 10 5 e j

What is the shortest path to vertex e? 28 8 Traceback What is the shortest path to vertex e? V Prev Dist a null b d 19 c 27 5 e f 39 33 g 16 h 21 i j 36 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 g h i 11 45 10 5 f e j

What is the shortest path to vertex e? 28 8 Traceback What is the shortest path to vertex e? V Prev Dist a null b d 19 c 27 5 e f 39 33 g 16 h 21 i j 36 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 g h i 11 c 45 10 5 f e j

What is the shortest path to vertex e? 28 8 Traceback What is the shortest path to vertex e? V Prev Dist a null b d 19 c 27 5 e f 39 33 g 16 h 21 i j 36 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 b g h i 11 c 45 10 5 f e j

What is the shortest path to vertex e? 28 8 Traceback What is the shortest path to vertex e? V Prev Dist a null b d 19 c 27 5 e f 39 33 g 16 h 21 i j 36 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 d 5 30 b g h i 11 c 45 10 5 f e j

What is the shortest path to vertex e? 28 8 Traceback What is the shortest path to vertex e? V Prev Dist a null b d 19 c 27 5 e f 39 33 g 16 h 21 i j 36 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 a 11 20 21 4 26 d 5 30 b g h i 11 c 45 10 5 f e j

How to code Dijkstra’s Algorithm Data Structures: AdjacencyList (adjList) adjList.find(label) Finds a vertex in constant time Uses the vertex label as the hash index Sets an iterator to point to label’s adjacency list

Vertex v = new Vertex(‘h’); adjList.find(v.getLabel()) 5 28 c 8 13 f 6 17 g 11 14 32 e i 4 26 38 46 3 7 h j 45 10 20 21 30

Vertex v = new Vertex(‘h’); adjList.find(v.getLabel()) // Finds h 5 28 a d b 8 13 b c a 6 17 c f b 11 14 32 d g b a 4 26 38 46 e i d b f 3 7 f e c 5 45 g h j 10 20 21 30 h j d e i 11 26 i h f 5 j i

Vertex v = new Vertex(‘h’); adjList.find(v.getLabel()) // Moves iterator to front of h’s adjacency list 5 28 a d b 8 13 b c a 6 17 c f b 11 14 32 d g b a 4 26 38 46 e i d b f 3 7 f e c 5 45 g h j 10 20 21 30 h j d e i 11 26 i h f 5 j i

How to code Dijkstra’s Algorithm Data Structures: AdjacencyList (adjList) adjList.getNext(); Returns the edge Moves the iterator to the next edge Returns null if it reaches the end of label’s adjacency list

while (e = adjList.getNext()) print(e.getLabel()); 5 28 a d b 8 13 b c a 6 17 c f b 11 14 32 d g b a 4 26 38 46 e i d b f 3 7 f e c 5 45 g h j 10 20 21 30 h j d e i 11 26 i h f 5 j i

How to code Dijkstra’s Algorithm Data Structures: Edge (e) e.getLabel() Returns the label of the vertex that the edge points to e.getDist() Returns the edge’s distance (weight of the edge)

j while (e = adjList.getNext()) print(e.getLabel()); a d b b c a c f b 5 28 a d b 8 13 j b c a 6 17 c f b 11 14 32 d g b a 4 26 38 46 e i d b f 3 7 f e c 5 45 g h j 10 20 21 30 h j d e i 11 26 i h f 5 j i

j d while (e = adjList.getNext()) println(e.getLabel()); a d b b c a c 5 28 a d b 8 13 j d b c a 6 17 c f b 11 14 32 d g b a 4 26 38 46 e i d b f 3 7 f e c 5 45 g h j 10 20 21 30 h j d e i 11 26 i h f 5 j i

j d e while (e = adjList.getNext()) println(e.getLabel()); a d b b c a 5 28 a d b 8 13 j d e b c a 6 17 c f b 11 14 32 d g b a 4 26 38 46 e i d b f 3 7 f e c 5 45 g h j 10 20 21 30 h j d e i 11 26 i h f 5 j i

j d e i while (e = adjList.getNext()) println(e.getLabel()); a d b b c 5 28 a d b 8 13 j d e i b c a 6 17 c f b 11 14 32 d g b a 4 26 38 46 e i d b f 3 7 f e c 5 45 g h j 10 20 21 30 h j d e i 11 26 i h f 5 j i

j d e i while (e = adjList.getNext()) println(e.getLabel()); a d b b c 5 28 a d b 8 13 j d e i loop terminates b c a 6 17 c f b 11 14 32 d g b a 4 26 38 46 e i d b f 3 7 f e c 5 45 g h j 10 20 21 30 h j d e i null 11 26 i h f 5 j i

How to code Dijkstra’s Algorithm Data Structures: PathTable (pTable) pTable.getMin() Finds the minimum distance vertex and sets it as visited. Returns the vertex Returns null if all vertices are marked visited.

Vertex v = pTable.getMin(); a null b d 19 c  5 e f g 16 h 21 i j 61 Prev Dist a null b d 19 c  5 e f g 16 h 21 i j 61

Vertex v = pTable.getMin(); a null b d 19 c  5 e f g 16 h 21 i j 61 Prev Dist a null b d 19 c  5 e f g 16 h 21 i j 61

Vertex v = pTable.getMin(); a null b d 19 c  5 e f g 16 h 21 i j 61 v Prev Dist a null b d 19 c  5 e f g 16 h 21 i j 61 v label = ‘b’ dist = 19

How to code Dijkstra’s Algorithm Data Structures: PathTable (pTable) pTable.update(vertex, prev, dist) Updates previous vertex and total distance Label used as the hash index Constant time update

pTable.update(‘c’, v.getLabel(), v.getDist() + 10); Prev Dist a null b d 19 c  5 e f g 16 h 21 i j 61 v label = ‘b’ dist = 19 pTable.update(‘c’, v.getLabel(), v.getDist() + 10);

pTable.update(‘c’, v.getLabel(), v.getDist() + 10); Prev Dist a null b d 19 c  5 e f g 16 h 21 i j 61 v label = ‘b’ dist = 19 pTable.update(‘c’, v.getLabel(), v.getDist() + 10);

pTable.update(‘c’, v.getLabel(), v.getDist() + 10); Prev Dist a null b d 19 c 29 5 e  f g 16 h 21 i j 61 v label = ‘b’ dist = 19 pTable.update(‘c’, v.getLabel(), v.getDist() + 10);

How to code Dijkstra’s Algorithm Weight w = 0; Edge e = null; Label v = new Label(‘a’); pTable.update(v, null, 0); while(v = pTable.getMin()) { adjList.find(v.getLabel()); while (e = adjList.getNext()) { w = e.getDist() + v.getDist(); pTable.update(e.getLabel(), v.getLabel(), w); }

How to code Dijkstra’s Algorithm Weight w = 0; Edge e = null; Label v = new Label(‘a’); // variables pTable.update(v, null, 0); while(v = pTable.getMin()) { adjList.find(v.getLabel()); while (e = adjList.getNext()) { w = e.getDist() + v.getDist(); pTable.update(e.getLabel(), v.getLabel(), w); }

How to code Dijkstra’s Algorithm Weight w = 0; Edge e = null; Label v = new Label(‘a’); pTable.update(v, null, 0); // Sets A as the start vertex while(v = pTable.getMin()) { adjList.find(v.getLabel()); while (e = adjList.getNext()) { w = e.getDist() + v.getDist(); pTable.update(e.getLabel(), v.getLabel(), w); }

How to code Dijkstra’s Algorithm V Prev Dist a null b  c d e f g h i j Label v = new Label(‘a’); pTable.update(v, null, 0); while(v = pTable.getMin()) { adjList.find(v.getLabel()); while (e = adjList.getNext()) { w = e.getDist() + v.getDist(); pTable.update(e.getLabel(), v.getLabel(), w); }

How to code Dijkstra’s Algorithm 5 d 28 b Label v = new Label(‘a’); pTable.update(v, null, 0); while(v = pTable.getMin()) { adjList.find(v.getLabel()); while (e = adjList.getNext()) { w = e.getDist() + v.getDist(); pTable.update(e.getLabel(), v.getLabel(), w); } b 8 c 13 a c 6 f 17 b d 11 g 14 b 32 a e 4 i 26 d 38 b 46 f f 3 e 7 c g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i

How to code Dijkstra’s Algorithm V Prev Dist a null b 28 c  d 5 e f g h i j a 5 d 28 b Label v = new Label(‘a’); pTable.update(v, null, 0); while(v = pTable.getMin()) { adjList.find(v.getLabel()); while (e = adjList.getNext()) { w = e.getDist() + v.getDist(); pTable.update(e.getLabel(), v.getLabel(), w); }