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,

Slides:



Advertisements
Similar presentations
Bellman-Ford algorithm
Advertisements

Graph Algorithms - 3 Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 13Feb 12, 2014Carnegie Mellon University.
Single Source Shortest Paths
Chapter 9: Graphs 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.
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.
§2 Topological Sort 〖 Example 〗 Courses needed for a computer science degree at a hypothetical university How shall we convert this list into a graph?
CSE 373 Graphs 1: Concepts, Depth/Breadth-First Search
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE Lectures 20 – Intro to Graphs.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Introduction to Algorithms 6.046J/18.401J/SMA5503
Data Structures Using C++
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
#1© K.Goczyła GRAPHS Definitions and data structuresDefinitions and data structures Traversing graphsTraversing graphs Searching for paths in graphsSearching.
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
 Graph Graph  Types of Graphs Types of Graphs  Data Structures to Store Graphs Data Structures to Store Graphs  Graph Definitions Graph Definitions.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
Chapter 23 Minimum Spanning Trees
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lectures 3 Tuesday, 9/25/01 Graph Algorithms: Part 1 Shortest.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Graph Algorithms: Part 1
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
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.
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.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
1 Shortest Path Calculations in Graphs Prof. S. M. Lee Department of Computer Science.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
CS 146: Data Structures and Algorithms July 21 Class Meeting
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.
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.
Data Structures Week 9 Towards Weighted BFS So, far we have measured d s (v) in terms of number of edges in the path from s to v. Equivalent to assuming.
Graphs, BFS, DFS and More…
CS200 Algorithms and Data StructuresColorado State University Part 10. Graphs CS 200 Algorithms and Data Structures 1.
Introduction to Algorithms Jiafen Liu Sept
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.
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
Graphs Upon completion you will be able to:
Trees Thm 2.1. (Cayley 1889) There are nn-2 different labeled trees
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graphs and Shortest Paths Using ADTs and generic programming.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
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.
Chapter 11 Graphs.
CSE 417: Algorithms and Computational Complexity
Presentation transcript:

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, where the sum: Weight(w 1,w 2 )+…+Weight(w n-1,w n ) attains its minimal value among all paths that start at u and end at v The length of a path of n vertices is n-1 (the number of edges) If a graph is connected, and the weights are all non-negative, shortest paths exist for any pair of vertices  Similarly for strongly connected digraphs with non-negative weights  Shortest paths may not be unique v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v

Cycles and negative weights The reason is that the simple cycle c that starts and ends in v 8 has negative cost -1  The path v 7,v 8,c,c,…,c, where c repeats t times has cost 2-t, so it can always be decreased, and no shortest path exists In a connected graph, shortest paths exist if and only if no negative cost cycles exist v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v Negative weights may prevent the existence of shortest paths on graphs with cycles In this example, there is no shortest path between v 7 and v 8 2 1

How to compute shortest paths Consider initially the case where the edges are unweighted  Interested in shortest path in terms of minimum number of edges; conceptually equivalent to have all edges of same weight Answer: Breadth-first search v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 1 Shortest path from v 3 to v

BFS for shortest path  struct Vertex{  private:  list adj; // Adjacency list  int dist;  //... data elements, etc.  public: // Assume all vertices with current dist = -1;  int findShortestPath(Vertex & u, Vertex & v){ queue bfs;  u.dist = 0;  bfs.push(u);  for(Vertex curr = bfs.front(); (!(bfs.empty())) && (curr != v); curr = bfs.front()) {  for(list ::iterator iter = curr.adj.begin(); iter!= curr.adj.end(); iter++) {  if((*iter).dist == -1 || (*iter).dist > curr.dist+1) {  (*iter).dist = curr.dist + 1;  bfs.push(*iter);  }  bfs.pop();  }  return(v.dist);  }; Problem: Compute shortest path from u to v Each vertex stores value dist, initially set to -1 Visited queue Q, initially empty Set u.dist=0, insert it in Q While Q is not empty, replace the vertex at the top of Q with its neighbors, after updating their distances to u

Computing paths  void extractShortestPath(Vertex &u, Vertex &v, stack & s) {  s.push(v);  for(Vertex curr = s.top(); curr!= u; curr = s.top()) {  for(list ::iterator iter = curr.adj.begin(); iter!=curr.adj.end(); iter++) {  if( (*iter).dist == curr.dist - 1) {  s.push(*iter);  break; }  } In the preceding, we learned how to compute minimum distances To compute shortest paths, we need to maintain an auxiliary structure Alternatively, if the graph is undirected, after computing the distances, one may extract the paths by tracing back from the destination If w is the last vertex found to be in the path, recover the predecessor of w as the neighbor z of w such that z.dist+weight(z,w)==w.dist

Weighted graphs: Dijkstra’s algorithm Explicit changes:  Change adjacency list to store pairs of (Vertex neigh, int edgeWeight)  Update weight with the following test:  if((*iter).vert.dist == -1 || (*iter).vert.dist > curr.dist+(*iter).edgeWeight) {  (*iter).dist= curr.dist+1; bfs.push(*iter);  }  The bfs must be now a priority queue, not a simple queue. Instead of iterating through bfs, the vertex of minimum distance is extracted at each time. Since we are assuming no negative-weight edges, the minimum distance-vertex cannot have its distance decreased by newfound paths from other vertices. The weighted-edge version of the previous algorithm is called Dijkstra’s algorithm for shortest paths The process is equivalent, except that the update of distance function uses the weight of the edge, instead of assuming it 1 For simplicity, assume no negative weights exist  Then as before, use -1 to indicate an uncomputed distance.