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.

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

Chapter 9: Graphs 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.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Topological Sort Topological sort is the list of vertices in the reverse order of their finishing times (post-order) of the depth-first search. Topological.
1 Theory I Algorithm Design and Analysis (10 - Shortest paths in graphs) T. Lauer.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CSE 373: Data Structures and Algorithms Lecture 19: Graphs III 1.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
Graphs. John Edgar  Understand graph terminology  Implement graphs using  Adjacency lists and  Adjacency matrices  Perform graph searches  Depth.
Minimum Spanning Tree Algorithms
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
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.
Shortest Paths Definitions Single Source Algorithms
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
Data Structures and Algorithms1 Graphs 3 Adapted From: Data Structures and Their Algorithms, by Harry R. Lewis and Larry Denenberg (Harvard University:
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
Course notes CS2606: Data Structures and Object-Oriented Development Graphs Department of Computer Science Virginia Tech Spring 2008 (The following notes.
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.
ALG0183 Algorithms & Data Structures Lecture 21 d Dijkstra´s algorithm 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks Chapter 14 Weiss.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
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.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
Common final examinations When: Wednesday, 12/11, 3:30-5:30 PM Where: Ritter Hall - Walk Auditorium 131 CIS 1166 final When: Wednesday, 12/11, 5:45-7:45.
Lecture 14 Graph Representations. Graph Representation – How do we represent a graph internally? – Two ways adjacency matrix list – Adjacency Matrix For.
Graphs. Made up of vertices and arcs Digraph (directed graph) –All arcs have arrows that give direction –You can only traverse the graph in the direction.
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.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
COSC 2007 Data Structures II
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
– Graphs 1 Graph Categories Strong Components Example of Digraph
Graphs Upon completion you will be able to:
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Proof of correctness of Dijkstra’s algorithm: Basically, we need to prove two claims. (1)Let S be the set of vertices for which the shortest path from.
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.
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
Minimum Spanning Trees and Shortest Paths
Cinda Heeren / Geoffrey Tien
Shortest Path Problems
Short paths and spanning trees
Graphs Representation, BFS, DFS
Chapter 11 Graphs.
Shortest Path Problems
Shortest Path Algorithms
Minimum Spanning Tree Algorithms
Slide Courtesy: Uwash, UT
CSE 373: Data Structures and Algorithms
Shortest Path Algorithm for Weighted Non-negative Undirected Graphs
CSE 373 Data Structures and Algorithms
Slide Courtesy: Uwash, UT
Graph Algorithms: Shortest Path
Dijkstra's Shortest Path Algorithm
Chapter 9: Graphs Shortest Paths
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

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 has the smallest sum of its edge weights Dijkstra’s algorithm determines the shortest path between a given vertex and all other vertices The algorithm is named after its discoverer, Edgser Dijkstra it is assumed that weight of edges are positive The shortest path between B and G is: B–D–E–F–G and not B–G (or B–A–E–F–G) B A C D E G F

Dijkstra’s Algorithm Finds the shortest path to all nodes from the start node Performs a modified BFS that accounts for cost of vertices The cost of a vertex (to reach a start vertex) is weight of the shortest path from the start node to the vertex using only the vertices which are already visited (except for the last one) Selects the node with the least cost from unvisited vertices In an unweighted graph (weight of each edge is 1) this reduces to a BFS To be able to quickly find an unvisited vertex with the smallest cost, we will use a priority queue (highest priority = smallest cost) When we visit a vertex and remove it from the queue, we might need to update the cost of some vertices (neighbors of the vertex) in queue (decrease it) The shortest path to any node can be found by backtracking in the results array (the results array contains, for each node, the minumum cost and a “parent” node from which one can get to this node achieving the minimum cost)

Dijkstra’s Algorithm – Initialization Initialization – insert all vertices in a priority queue ( PQ ) Set the cost of the start vertex to zero Set the costs of all other vertices to infinity and their parent vertices to the start node Note that because the cost to reach the start vertex is zero it will be at the head of the PQ Special requirement on priority queue PQ : we can use min-heap a cost of an item (vertex) can decrease, and in such case we need to bubble-up the item (in time O(log n)) another complication is that we need to locate the item in the queue which cost has changed, but we know its value (vertex number) but not its location in the queue – therefore, we need to keep reversed index array mapping vertices to positions in the heap

Priority Queue Interface public class Vertex { private int number; // vertex number: 0..|V|-1 private double cost; private int parent; // constructors, accessors, mutators } public interface VertexPQInterface { // using min-heap // costs of the vertices are used as priorities (keys) public boolean isEmpty(); public void insert(Vertex v); // O(log n) public Vertex extractMin(); // O(log n) // remove the vertex with smallest cost public int find(int vertexNumber); // O(1) // return index of vertex v public void decreaseCost(int i, double newCost); // O(log n) // decrease the cost of vertex in position i of the heap }

Dijkstra’s Algorithm – Main Loop Until PQ is empty Remove the vertex with the least cost and insert it in a results array, make that the current vertex (cv) [it can be proved that the cost of this vertex is optimal] Search the adjacency list of cv for neighbors which are still in PQ For each such vertex, v, perform the following comparison If cost[cv] + weight(cv,v) < cost[v] change v’s cost recorded in the PQ to cost[cv] + weight(cv,v) and change v’s parent vertex to cv Repeat with the next vertex at the head of PQ

Dijkstra Algorithm Outline public class Dijkstra { Vertex results[]; Dijkstra(WeightedGraphList G,int start) { // create a VertexPQ and insert all vertices // loop: // extract min-cost vertex and put it to results // update costs of neighbors in PQ } double costTo(int v) { return results[v].getCost(); } void printPathTo(int v); }

Dijkstra’s Algorithm – Final Stage When the priority queue is empty the results array contains all of the shortest paths from the start vertex Note that a vertex’s cost in the results array represents the total cost of the shortest path from the start to that vertex To find the shortest path to a vertex from start vertex look up the goal vertex in the results array The vertex’s parent vertex represents the previous vertex in the path A complete path can be found by backtracking through all of the parent vertices until the start vertex is reached

Find the Shortest Path … The shaded squares are inaccessible Square 13 is the start square Moves can be made vertically or horizontally (but not diagonally) one square at a time The cost to reach an adjacent square is indicated by the wall thickness:

Only vertices that can be reached are to be represented Graph is undirected As the cost to move from one square to another differs, the graph is weighted The graph is fairly sparse, suggesting that the edges should be stored in an adjacency list Graph Representation

Only vertices that can be reached are to be represented Graph is undirected As the cost to move from one square to another differs, the graph is weighted The graph is fairly sparse, suggesting that the edges should be stored in an adjacency list Graph Representation

The cost to reach each vertex from the start (st) is set to infinity For vertex v let's call this cost c[st][v] All nodes are entered in a priority queue, in cost priority The cost to reach the start node is set to 0, and the priority queue is updated The results list is shown in the sidebar Dijkstra’s Algorithm Start            

Dijkstra’s Algorithm Demonstration             13, 0, 13 vertex, cost, parent remove start from PQ 07, 1, 13 update cost to adjacent vertex, v, via removed vertex, u, if: c[u][v] + c[st][u] < c[st][v]     12, 1, 13   19, 1, 13  14, 2, 13 06, 2, 12 08, 2, 07 18, 2, 12  

       Dijkstra’s Algorithm Demonstration         13, 0, 13 vertex, cost, parent 07, 1, 13 12, 1, 13 19, 1, 13 14, 2, 13 06, 2, 12 08, 2, 07 18, 2, 12 20, 3, 19 02, 5, 08 09, 5, 08 26, 5, 20 27, 7, 26 10, 9, 09 32, 9, 26 04, 11, 10 28, 10, 27 33, 11, 32 34, 12, 33 22, 13, 28 35, 13, 34 05, 14, 04 11, 14, 10 29, 14, 35 23, 16, 29             

Retrieving the Shortest Path 13, 0, 13 vertex, cost, parent 07, 1, 13 12, 1, 13 19, 1, 13 14, 2, 13 06, 2, 12 08, 2, 07 18, 2, 12 20, 3, 19 02, 5, 08 09, 5, 08 26, 5, 20 27, 7, 26 10, 9, 09 32, 9, 26 04, 11, 10 28, 10, 27 33, 11, 32 34, 12, 33 22, 13, 28 35, 13, 34 05, 14, 04 11, 14, 10 29, 14, 35 23, 16, 29 Having completed the array of results paths from the start vertex can now be retrieved This is done by looking up the end vertex (the vertex to which one is trying to find a path) and backtracking through the parent vertices to the start For example to find a path to vertex 23 backtrack through: 29, 35, 34, 33, 32, 26, 20, 19, 13 Note: there should be some efficient way to search the results array for a vertex

Shortest Path from Vertex 13 to 23                   13, 0, 13 vertex, cost, parent 07, 1, 13 12, 1, 13 19, 1, 13 14, 2, 13 06, 2, 12 08, 2, 07 18, 2, 12 20, 3, 19 02, 5, 08 09, 5, 08 26, 5, 20 27, 7, 26 10, 9, 09 32, 9, 26 04, 11, 10 28, 10, 27 33, 11, 32 34, 12, 33 22, 13, 28 35, 13, 34 05, 14, 04 11, 14, 10 29, 14, 35 23, 16, 29

Dijkstra’s Algorithm Analysis The cost of the algorithm is dependent on  E  and  V  and the data structure used to implement the priority queue How many operations are performed? Whenever a vertex is removed we have to find each adjacent edge and compare its cost There are  V  vertices to be removed and Each of  E  edges will be examined once (in a directed graph) If a heap is used to implement the priority queue Building the heap takes O(  V  ) time Removing each vertex takes O(log  V  ) time, in total: O(  V  *log  V  ) Assuming that the heap is indexed (so that a vertex’s position can be found easily) changing the vertex’s cost takes O(log  V  ), in total: O(  E  *log  V  ) The total cost is O((  V  +  E  )*log  V  )