Shortest Path Algorithm for Weighted Non-negative Undirected Graphs

Slides:



Advertisements
Similar presentations
Traffic assignment.
Advertisements

ITS THE FINAL LECTURE! (SING IT, YOU KNOW YOU WANT TO) Operations Research.
CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Shortest paths in edge-weighted digraph Krasin Georgiev Technical University of Sofia g.krasin at gmail com Assistant Professor.
Internet Engineering Czesław Smutnicki Discrete Mathematics – Graphs and Algorithms.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
CS171 Introduction to Computer Science II Graphs Strike Back.
Graphs By JJ Shepherd. Introduction Graphs are simply trees with looser restrictions – You can have cycles Historically hard to deal with in computers.
D IJKSTRA ' S ALGORITHM By Laksman Veeravagu and Luis Barrera Edited by: Manuela Caicedo, Francisco Morales, Rafael Feliciano, and Carlos Jimenez.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
Shortest Paths and Dijkstra's Algorithm CS 110: Data Structures and Algorithms First Semester,
Graph II MST, Shortest Path. Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
Breadth First Search (BFS) Part 2 COMP171. Graph / Slide 2 Shortest Path Recording * BFS we saw only tells us whether a path exists from source s, to.
Shortest Path Problem For weighted graphs it is often useful to find the shortest path between two vertices Here, the “shortest path” is the path that.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
1 Shortest Path Calculations in Graphs Prof. S. M. Lee Department of Computer Science.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Greedy methods Prudence Wong
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Fundamentals, Terminology, Traversal, Algorithms Graph Algorithms Telerik Algo Academy
ALG0183 Algorithms & Data Structures Lecture 21 d Dijkstra´s algorithm 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks Chapter 14 Weiss.
Dr. Naveed Ahmad Assistant Professor Department of Computer Science University of Peshawar.
Shortest Path Problem Weight of the graph –Nonnegative real number assigned to the edges connecting to vertices Weighted graphs –When a graph.
Data Structures and Algorithms Ver. 1.0 Session 17 Objectives In this session, you will learn to: Implement a graph Apply graphs to solve programming problems.
CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Design and Analysis of Algorithms.
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.
Shortest Path in Weighted Graph : Dijkstra’s Algorithm.
D IJKSTRA ' S ALGORITHM. S INGLE -S OURCE S HORTEST P ATH P ROBLEM Single-Source Shortest Path Problem - The problem of finding shortest paths from a.
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
COMP108 Algorithmic Foundations Greedy methods
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Fundamentals, Terminology, Traversal, Algorithms
Shortest Path from G to C Using Dijkstra’s Algorithm
Some applications of graph theory
Single-Source Shortest Paths
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Minimum Spanning Trees and Shortest Paths
Discussion section #2 HW1 questions?
Network Routing.
Party-by-Night Problem
Graph Algorithm.
Shortest Path.
Shortest Path.
A* Path Finding Ref: A-star tutorial.
Shortest-Paths Trees Kun-Mao Chao (趙坤茂)
CSE 373: Data Structures and Algorithms
Chapter 11 Graphs.
Graphs Part 2 Adjacency Matrix
Shortest Path Algorithms
A path that uses every vertex of the graph exactly once.
Case study: Strava + Waze
Slide Courtesy: Uwash, UT
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Shortest Path.
Weighted Graphs & Shortest Paths
CE 221 Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
Slide Courtesy: Uwash, UT
Dijkstra's Shortest Path Algorithm
CE 221 Data Structures and Algorithms
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
CSE 373: Data Structures and Algorithms
Graphs: Shortest path and mst
Graph Algorithm.
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:

Shortest Path Algorithm for Weighted Non-negative Undirected Graphs Dijkstra’s Example By Joe James

Dijkstra Example Problem: Find the shortest distance from source S to all other vertices given in the graph. Step 1: Initialize distances from source (S) to all other vertices. We immedietely see the neighbors A, D,and C of S.

Dijkstra Example Step 2: Relax all out-edges from S. Trace the neighbours of S and compute the shortest distance to them Step 3: Choose the closest vertex to move to, which is D, i.e., 5 < 6 < 8.

Dijkstra Example Step 4: Relax all out-edges from D Step 5: Check out (relax) the shortest out-edge out of D, which is D-A = 2.

Dijkstra Example Step 6: Add the current distance (S-D) to the shortest out-edge of D, which is D-A = 2. Now the shortest distance from S to A is 7 via vertex D

Dijkstra Example Step 8: C is the next nearest vertex from S C has anly one out-edge remaining, which is C-F. The distance S-F via C is 9+6 and via D is 5+4. Thus, C is also done. Step 7: Process all the distances from S to all out-edges of D.

Dijkstra Example Step 9: The next shortest distance from S to its neighbors is now A. Step 10: A has one out-edge (A-E) left. Hence, Distance from S to E over A is 8. A is done and we jump to E, which has two out-edges to relax, E-B and E-G.

Dijkstra Example Step 11: We are done with E and move to F, because it is the closest node to the source that we have’nt visited yet. Step 12: F has one out-edge (F-G). So we move to G. G has no edge to relax so we move to B.

Dijkstra Example Step 13: G has no edge to relax so we move to B, which also has no edges to relax. So, we are done. This finalizes the shortest path from sorurce S to every other vertices in the weighted graph.

Another Example of Dijkstra's Shortest Path Algorithm Find shortest path from s to t. 2 24 3 9 s 18 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44

Dijkstra's Shortest Path Algorithm PQ = { s, 2, 3, 4, 5, 6, 7, t }   2 24 3 9 s 18 14  2 6 6  4 30  19 11 15 5 5 6 20 16 t 7 44  distance label 

Dijkstra's Shortest Path Algorithm PQ = { s, 2, 3, 4, 5, 6, 7, t } Here   2 24 3 9 s 18 14  2 6 6  30  4 19 11 15 5 5 6 20 16 t 7 44  distance label 

Dijkstra's Shortest Path Algorithm PQ = { 2, 3, 4, 5, 6, 7, t } Node discovered   X 9 2 24 3 9 s 18 14  X 14 2 6 6  4 30  19 11 15 5 5 6 20 16 t 7 44  distance label  15 X

Dijkstra's Shortest Path Algorithm PQ = { 2, 3, 4, 5, 6, 7, t } Here   X 9 2 24 3 9 s 18 14  X 14 2 6 6  4 30  19 11 15 5 5 6 20 16 t 7 44  distance label  15 X

Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, 6, 7, t }   X 9 2 24 3 9 s 18 14  X 14 2 6 6  30  4 19 11 15 5 5 6 20 16 t 7 44   15 X

Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, 6, 7, t } Node discovered  X 33  X 9 2 24 3 9 s 18 14  X 14 2 6 6   4 30 19 11 15 5 5 6 20 16 t 7 44   15 X

Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, 6, 7, t }  X 33  X 9 2 24 3 9 Here s 18 14  X 14 2 6 6   4 30 19 11 15 5 5 6 20 16 t 7 44   15 X

Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, 7, t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6  44 30  X 4 19 11 15 5 5 6 20 16 t 7 44   15 X

Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, 7, t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6  44 30  X 4 19 11 15 5 5 6 20 16 t 7 44   15 Here X

Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6  44 X 35  X 4 30 19 11 15 5 5 6 20 16 t 7 44 59   15 X X

Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, t } Here 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6  44 X 35 4 30  X 19 11 15 5 5 6 20 16 t 7 44 59   15 X X

Dijkstra's Shortest Path Algorithm PQ = { 4, 5, t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6  44 X 35 X 34 4 30  X 19 11 15 5 5 6 20 16 t 7 44 51 59   15 X X X

Dijkstra's Shortest Path Algorithm PQ = { 4, 5, t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6  44 X 35 X 34 30  X 4 19 11 15 5 5 6 20 Here 16 t 7 44 51 59   15 X X X

Dijkstra's Shortest Path Algorithm PQ = { 4, t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6 45  44 X 35 X 34 X 30  X 4 19 11 15 5 5 6 20 16 t 7 44 50 51 X 59   15 X X X

Dijkstra's Shortest Path Algorithm PQ = { 4, t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6 45  44 X 35 X 34 X 30  X 4 19 11 15 5 Here 5 6 20 16 t 7 44 50 51 X 59   15 X X X

Dijkstra's Shortest Path Algorithm PQ = { t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6 45  44 X 35 X 34 X 30  X 4 19 11 15 5 5 6 20 16 t 7 44 50 51 X 59   15 X X X

Dijkstra's Shortest Path Algorithm PQ = { t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6 45  44 X 35 X 34 X 30  X 4 19 11 15 5 5 6 20 16 t 7 44 Here 50 51 X 59   15 X X X

Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 4, 5, 6, 7, t } PQ = { } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6 45  44 X 35 X 34 X  X 4 30 19 11 15 5 5 6 20 16 t 7 44 50 51 X 59   15 X X X

Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 4, 5, 6, 7, t } PQ = { } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6 45  44 X 35 X 34 X  X 4 30 19 11 15 5 5 6 20 16 t 7 44 50 51 X 59   15 X X X

The Algoritm 1. Assign to every node a distance value. Set it to zero for our initial node and to infinity (-9999) for all other nodes. 2. Mark all nodes as unvisited. Set initial node as current. 3. For current node, consider all its unvisited neighbors and calculate their distance (from the initial node). For example, if current node (A) has distance of 6, and an edge connecting it with another node (B) is 2, the distance to B through A will be 6+2=8. If this distance is less than the previously recorded distance (infinity in the beginning, zero for the initial node), overwrite the distance. 4. When we are done considering all neighbors of the current node, mark it as visited. A visited node will not be checked ever again; its distance recorded now is final and minimal. 5. If all nodes have been visited, finish. Otherwise, set the unvisited node with the smallest distance (from the initial node) as the next "current node" and continue from step 3.

Pseudo Code of the Dijkstra’s Alg Algorithm Dijkstra(Graph, source): for each vertex v in Graph: // Initializations dist[v] := infinity // Unknown distance function from source to v previous[v] := undefined // Previous node in optimal path from source dist[source] := 0 // Distance from source to source Q := the set of all nodes in Graph // All nodes in the graph are unoptimized - thus are in Q while Q is not empty: // The main loop u:= dequeue (Q) // u := vertex in Q with smallest dist[] if dist[u] = infinity: break // all remaining vertices are inaccessible from source dequeue(u,Q) // remove u from Q for each neighbor v of u: // where v has not yet been removed from Q. alt := dist[u] + dist_between(u, v) // Dist. from start point to neighbor if alt < dist[v]: // Relax (u,v) dist[v] := alt previous[v] := u return dist

Izmir University of Economics Homework Assignments 9.5, 9.7, 9.10, 9.40, 9.42, 9.44, 9.46, 9.47, 9.52 You are requested to study and solve the exercises. Note that these are for you to practice only. You are not to deliver the results to me. Izmir University of Economics