Shortest Path Algorithm What is the Shortest Path Problem? Is the shortest path problem well defined? The Dijkstra's Algorithm for Shortest Path Problem.

Slides:



Advertisements
Similar presentations
Bellman-Ford algorithm
Advertisements

Single Source Shortest Paths
§3 Shortest Path Algorithms Given a digraph G = ( V, E ), and a cost function c( e ) for e  E( G ). The length of a path P from source to destination.
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.
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Shortest Path Algorithm What is the Shortest Path Problem? Is the shortest path problem well defined? The Dijkstra's Algorithm for Shortest Path Problem.
1 Theory I Algorithm Design and Analysis (10 - Shortest paths in graphs) T. Lauer.
Design and Analysis of Algorithms Single-source shortest paths, all-pairs shortest paths Haidong Xue Summer 2012, at GSU.
§2 Topological Sort 〖 Example 〗 Courses needed for a computer science degree at a hypothetical university How shall we convert this list into a graph?
chapter Single-Source Shortest Paths Problem Definition Shortest paths and Relaxation Dijkstra’s algorithm (can be viewed as a greedy algorithm)
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CSE 101- Winter ‘15 Discussion Section January 26th 2015.
Shortest Paths On weighted graphs. Weighted Shortest Paths The shortest path from a vertex u to a vertex v in a graph is a path w 1 = u, w 2,…,w n = v,
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints.
Minimum Spanning Tree What is a Minimum Spanning Tree. Constructing a Minimum Spanning Tree. Prim’s Algorithm. –Animated Example. –Implementation. Kruskal’s.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
1 Minimum Spanning Trees (MSTs) and Minimum Cost Spanning Trees (MCSTs) What is a Minimum Spanning Tree? Constructing Minimum Spanning Trees. What is a.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
1 8a-ShortestPathsMore Shortest Paths in a Graph (cont’d) Fundamental Algorithms.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Graph Traversals Depth-First Traversal. The Algorithm.
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
Shortest Paths Definitions Single Source Algorithms
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.
Minimum Spanning Tree What is a Minimum Spanning Tree. Constructing a Minimum Spanning Tree. What is a Minimum-Cost Spanning Tree. Prim’s Algorithm. –Animated.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
Shortest Paths Introduction to Algorithms Shortest Paths CSE 680 Prof. Roger Crawfis.
Copyright © Cengage Learning. All rights reserved.
Dijkstra’s Algorithm. 2 Shortest-path Suppose we want to find the shortest path from node X to node Y It turns out that, in order to do this, we need.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
CSCI-455/552 Introduction to High Performance Computing Lecture 18.
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
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
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
ALG0183 Algorithms & Data Structures Lecture 21 d Dijkstra´s algorithm 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks Chapter 14 Weiss.
COSC 2007 Data Structures II Chapter 14 Graphs III.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
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.
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
Dijkstra’s Algorithm Supervisor: Dr.Franek Ritu Kamboj
Shortest Paths and Dijkstra’s Algorithm CS 105. SSSP Slide 2 Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine.
The single-source shortest path problem (SSSP) input: a graph G = (V, E) with edge weights, and a specific source node s. goal: find a minimum weight (shortest)
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.
Minimum Spanning Tree What is a Minimum Spanning Tree.
Dijkstra animation. Dijksta’s Algorithm (Shortest Path Between 2 Nodes) 2 Phases:initialization;iteration Initialization: 1. Included:(Boolean) 2. Distance:(Weight)
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Graphs – Part III CS 367 – Introduction to Data Structures.
Unit 11 Graphs (2) King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Minimum Spanning Trees
Minimum Spanning Tree What is a Minimum Spanning Tree.
Shortest Path Algorithm
Shortest Path Graph represents highway system Edges have weights
Minimum Spanning Trees
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Shortest Path.
Shortest path algorithm
Fundamental Structures of Computer Science II
Single-Source Shortest Paths
Graph Algorithms: Shortest Path
Shortest Path Algorithm
Graphs: Shortest path and mst
Shortest Path Algorithm
Presentation transcript:

Shortest Path Algorithm What is the Shortest Path Problem? Is the shortest path problem well defined? The Dijkstra's Algorithm for Shortest Path Problem. Animated Example. Data structures required for implementation. Implementation. Review Questions.

What is the shortest path problem? In an edge-weighted graph, the weight of an edge measures the cost of traveling that edge. For example, in a graph representing a network of airports, the weights could represent: distance, cost or time. Such a graph could be used to answer any of the following: –What is the fastest way to get from A to B? –Which route from A to B is the least expensive? –What is the shortest possible distance from A to B? Each of these questions is an instance of the same problem: The shortest path problem!

Is the shortest path problem well defined? If all the edges in a graph have non-negative weights, then it is possible to find the shortest path from any two vertices. For example, in the figure below, the shortest path from B to F is { B, A, C, E, F } with a total cost of nine. Thus, the problem is well defined for a graph that contains non- negative weights.

Is the shortest path problem well defined? - Cont'd Things get difficult for a graph with negative weights. For example, the path D, A, C, E, F costs 4 even though the edge (D, A) costs 5 -- the longer the less costly. The problem gets even worse if the graph has a negative cost cycle. e.g. {D, A, C, D} A solution can be found even for negative-weight graphs but not for graphs involving negative cost cycles. {D, A, C, D, A, C, E, F} = 2 {D, A, C, D, A, C, D, A, C, E, F} = 0

The Dijkstra's Algorithm Dijkstra's algorithm solves the single-source shortest path problem for a non-negative weights graph. It finds the shortest path from an initial vertex, say v 0, to all the other vertices. For each vertex v, it keeps track of three things: –k v : a boolean variable indicating whether the shortest path to v is known, initially false for all vertices. –d v : the shortest known distance from v0 to v, initially infinity for all vertices except that of v0 which is 0. –p v : the predecessor of v on the path from v0 to v, initially unknown for all vertices.

The Dijkstra's Algorithm Cont'd The algorithm repeats the following steps until all vertices are processed: 1.From the set of vertices for which k v = false, select the vertex v having the smallest known distance dv. 2.set k v to true. 3.for each vertex w adjacent to v for which kv is false, test if the known distance, d w is greater than d v + C(v, w). If it is, set d w to d v + C(v, w) and set p w to v.

Animated Example BCD A F E BCD A F E

Data structures required The implementation of Dijkstra's algorithm uses the Entry structure below. Each Entry instance has three fields: know, distance and previous, corresponding to the three variables, kv, dv & pv. Since in each pass, the vertex with the smallest known distance is chosen, a priority queue is used to store the vertices. 1 public class Algorithms 2 { 3 static final class Entry 4 { 5 boolean known = false; 6 int distance = Integer.MAX_VALUE; 7 int predecessor = Integer.MAX_VALUE; 8 } 9 }

Implementation of Dijkstra's Algorithm The Dijkstra’s Algorithm method shown below takes two arguments, a directed graph and the starting vertex. The method returns a vertex-weighted Digraph from which the shortest path from v0 to any vertex can be found. 1 public class Algorithms { 2 3 public static Digraph dijkstrasAlgorithm (Digraph g, 4 int s) { 5 int n = g.getNumberOfVertices (); 6 Entry[] table = new Entry [n]; 7 for (int v = 0; v < n; ++v) 8 table [v] = new Entry (); 9 table [s].distance = 0; 10 PriorityQueue queue = 11 new BinaryHeap (g.getNumberOfEdges()); 12 queue.enqueue ( 13 new Association (new Int (0), g.getVertex (s)));

Implementation of Dijkstra's Algorithm - Cont'd 14 while (!queue.isEmpty ()) { 15 Association assoc=(Association) queue.dequeueMin(); 16 Vertex v0 = (Vertex) assoc.getValue (); 17 int n0 = v0.getNumber (); 18 if (!table [n0].known) { 19 table [n0].known = true; 20 Enumeration p = v0.getEmanatingEdges (); 21 while (p.hasMoreElements ()){ 22 Edge edge = (Edge) p.nextElement (); 23 Vertex v1 = edge.getMate (v0); 24 int n1 = v1.getNumber (); 25 Int wt = (Int) edge.getWeight (); 26 int d=table [n0].distance + wt.intValue (); 27 if (table [n1].distance > d) { 28 table [n1].distance = d; 29 table [n1].predecessor = n0; 30 queue.enqueue(new Association(new Int(d),v1)); 31 } 32 } 33 } 34 }

Implementation of Dijkstra's Algorithm Cont'd 35 Digraph result = new DigraphAsLists (n); 36 for (int v = 0; v < n; ++v) 37 result.addVertex (v, new Int (table [v].distance)); 38 for (int v = 0; v < n; ++v) 39 if (v != s) 40 result.addEdge (v, table [v].predecessor); 41 return result; 42 } 43 }

Review Questions Use the graph Gc shown above to trace the execution of Dijkstra's algorithm as it solves the shortest path problem starting from vertex a. Dijkstra's algorithm works as long as there are no negative edge weights. Given a graph that contains negative edge weights, we might be tempted to eliminate the negative weights by adding a constant weight to all of the edges. Explain why this does not work. Dijkstra's algorithm can be modified to deal with negative edge weights (but not negative cost cycles) by eliminating the known flag and by inserting a vertex back into the queue every time its tentative distance decreases. Implement this modified algorithm.