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

Problem solving with graph search
Single Source Shortest Paths
CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
§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
November 14, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Aalborg University
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.
Shortest Paths and Dijkstra's Algorithm CS 110: Data Structures and Algorithms First Semester,
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.
1 Minimum Spanning Trees (MSTs) and Minimum Cost Spanning Trees (MCSTs) What is a Minimum Spanning Tree? Constructing Minimum Spanning Trees. What is a.
CSC 213 Lecture 23: Shortest Path Algorithms. Weighted Graphs Each edge in weighted graph has numerical weight Weights can be distances, building costs,
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.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
Shortest Path Algorithm What is the Shortest Path Problem? Is the shortest path problem well defined? The Dijkstra's Algorithm for Shortest Path Problem.
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
Shortest path algorithm. Introduction 4 The graphs we have seen so far have edges that are unweighted. 4 Many graph situations involve weighted edges.
Shortest Paths Definitions Single Source Algorithms
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.
The Shortest Path Problem
Shortest Paths Introduction to Algorithms Shortest Paths CSE 680 Prof. Roger Crawfis.
CS 146: Data Structures and Algorithms July 21 Class Meeting
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.
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.
1 Chapter 28 Weighted Graph Applications. 2 Objectives F To represent weighted edges using adjacency matrices and priority queues (§28.2). F To model.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 28 Weighted Graph.
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.
Graphs Chapter 8 from Drozdek. Definitions A graph is a generalization of a tree. A simple graph consists of a nonempty set of vertices and possibly an.
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.
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)
Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.
1 Dijkstra’s Algorithm Dr. Ying Lu RAIK 283 Data Structures & Algorithms.
Minimum Spanning Tree What is a Minimum Spanning Tree.
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.
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:
Fundamental Structures of Computer Science II
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. Implementation Dijkstra's Algorithm

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 s, to all the other vertices.

The Dijkstra's Algorithm Cont'd // Let V be the set of all vertices in G, and s the start vertex. for(each vertex v){ currentDistance(s-v) = ∞; predecessor(v) = undefined; } currentDistance(s-s) = 0; T = V; while(T   ){ v = a vertex in T with minimal currentDistance from s; T= T – {v}; for(each vertex u adjacent to v and in T){ if(currentDistance(s-u) > currentDistance(s-v) + weight(edge(vu)){ currentDistance(s-u) = currentDistance(s-v) + weight(edge(vu)); predecessor(u) = v; } For each vertex, the algorithm keeps track of its current estimated shortest distance from the starting vertex and its predecessor on the current path

Example Tracing Dijkstra’s algorithm starting at vertex B: The resulting vertex-weighted graph is:

Data structures required The implementation of Dijkstra's algorithm uses the Entry structure, which contains the following three fields: –know: a boolean variable indicating whether the shortest path to v is known, initially false for all vertices. –distance : the shortest known distance from s to v, initially infinity for all vertices except that of s which is 0. –predecessor : the predecessor of v on the path from s to v, initially unknown for all vertices. public class Algorithms{ static final class Entry{ boolean known; int distance; Vertex predecessor; Entry(){ known = false; distance = Integer.MAX_VALUE; predecessor = null; }

Implementation of Dijkstra's Algorithm The dijkstrasAlgorithm 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 s to any vertex can be found. Since in each pass, the vertex with the smallest known distance is chosen, a minimum priority queue is used to store the vertices. public static Graph dijkstrasAlgorithm(Graph g, Vertex start){ int n = g.getNumberOfVertices(); Entry table[] = new Entry[n]; for(int v = 0; v < n; v++) table[v] = new Entry(); table[g.getIndex(start)].distance = 0; PriorityQueue queue = new BinaryHeap( g.getNumberOfEdges()); queue.enqueue(new Association(new Integer(0), start));

Implementation of Dijkstra's Algorithm - Cont'd while(!queue.isEmpty()) { Association association = (Association)queue.dequeueMin(); Vertex v1 = (Vertex) association.getValue(); int n1 = g.getIndex(v1); if(!table[n1].known){ table[n1].known = true; Iterator p = v1.getEmanatingEdges(); while (p.hasNext()){ Edge edge = (Edge) p.next(); Vertex v2 = edge.getMate(v1); int n2 = g.getIndex(v2); Integer weight = (Integer) edge.getWeight(); int d = table[n1].distance + weight.intValue(); if(table[n2].distance > d){ table[n2].distance = d; table[n2].predecessor = v1; queue.enqueue(new Association(d, v2)); }

Implementation of Dijkstra's Algorithm Cont'd Graph result = new GraphAsLists(true);//Result is Digraph Iterator it = g.getVertices(); while (it.hasNext()){ Vertex v = (Vertex) it.next(); result.addVertex(v.getLabel(), new Integer(table[g.getIndex(v)].distance)); } it = g.getVertices(); while (it.hasNext()){ Vertex v = (Vertex) it.next(); if (v != start){ String from = v.getLabel(); String to = table[g.getIndex(v)].predecessor.getLabel(); result.addEdge(from, to); } return result; }

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.