1 Shortest Path Algorithms Given a graph G = (V, E) and a distinguished vertex s, find the shortest weighted path from s to every other vertex in G. weighted.

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

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.
CSC 2300 Data Structures & Algorithms April 13, 2007 Chapter 9. Graph Algorithms.
The Greedy Approach Chapter 8. The Greedy Approach It’s a design technique for solving optimization problems Based on finding optimal local solutions.
Graph Algorithms. Jaruloj Chongstitvatana Chapter 3: Greedy Algorithms 2 Outline Graph Representation Shortest path Minimum spanning trees.
§2 Topological Sort 〖 Example 〗 Courses needed for a computer science degree at a hypothetical university How shall we convert this list into a graph?
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
1 9.3 Shortest Path Algorithms Improvement –Use a queue to keep all vertices whose shortest distance to s is known. –Initialize d v to  for all vertices,
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
CMSC 341 Graphs 2. 2 Weighted Shortest Path Problem Single-source shortest-path problem: Given as input a weighted graph, G = (V,E), and a distinguished.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
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.
Shortest Paths Definitions Single Source Algorithms
Shortest Path Algorithm By Weston Vu CS 146. What is Shortest Paths? Shortest Paths is a part of the graph algorithm. It is used to calculate the shortest.
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
Tirgul 13. Unweighted Graphs Wishful Thinking – you decide to go to work on your sun-tan in ‘ Hatzuk ’ beach in Tel-Aviv. Therefore, you take your swimming.
CSE 326: Data Structures Graphs Ben Lerner Summer 2007.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Dijkstra's algorithm.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
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.
Graph Algorithms Terminology Topological sort Shortest-path algorithms
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
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.
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.
CMSC 380 Graph Traversals and Search. 2 Graph Traversals Graphs can be traversed breadth-first, depth- first, or by path length We need to specifically.
CS 61B Data Structures and Programming Methodology Aug 5, 2008 David Sun.
CMSC 341 Graphs. 8/3/2007 UMBC CMSC 341 Graphs 2 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
CMSC 341 Graphs. 5/5/20062 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of edges, E. Each edge.
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.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Graph Algorithms CS 202 – Fundamental Structures of Computer Science.
C.Eng 213 Data Structures Graphs Fall Section 3.
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Introduction to Graphs
Graphs.
CSC 172 DATA STRUCTURES.
MST - Prim’s Algorithm Greedy algorithm that “grows” an MST by repeatedly finding the lowest cost edge that will connect a new vertex to MST For every.
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Shortest Path Algorithms
CMSC 341 Lecture 20.
Shortest Path Algorithms
Topological Sort Algorithm
Fundamental Structures of Computer Science II
Chapter 24: Single-Source Shortest Paths
CE 221 Data Structures and Algorithms
Fundamental Structures of Computer Science II
Chapter 24: Single-Source Shortest Paths
CSE 417: Algorithms and Computational Complexity
CMSC 341 Graphs 2.
CE 221 Data Structures and Algorithms
Lecture 12 Shortest Path.
Richard Anderson Spring 2016
Chapter 9: Graphs Shortest Paths
GRAPH – Definitions A graph G = (V, E) consists of
Directed Graphs (Part II)
Presentation transcript:

1 Shortest Path Algorithms Given a graph G = (V, E) and a distinguished vertex s, find the shortest weighted path from s to every other vertex in G. weighted path length of v 1, v 2, …, v N : applications: –fastest way to get from one place to another –cheapest way to fly/send data from one city to another., where c i,j is the cost of edge (v i, v j )

2 Unweighted Shortest Path Special case of the weighted problem: all weights are 1. Solution: breadth-first search. Similar to level-order traversal for trees. v3v3 v6v6 v1v1 v2v2 v4v4 v7v7 v5v5 v0v0 s

3 void Graph::unweighted (Vertex s) { Queue q(NUM_VERTICES); Vertex v, w; q.enqueue(s); s.dist = 0; while (!q.isEmpty()) { v = q.dequeue(); for each w adjacent to v if (w.dist == INFINITY) { w.dist = v.dist + 1; w.path = v; q.enqueue(w); } each edge examined at most once each vertex enqueued at most once total running time: O( )

4 Weighted Shortest Path no negative weight edges. Dijkstra’s algorithm: uses similar ideas as the unweighted case. algorithms: do what seems to be best at every decision point. S “known” s V - S “unknown” v

5 Example v3v3 v6v6 v1v1 v2v2 v4v4 v5v5 v0v0 s

6 void Graph::dijkstra(Vertex s) { Vertex v,w; s.dist = 0; while (true) { v = smallest unknown dist. vert; if (v == NOT_A_VERTEX) break; v.known = true; for each w adjacent to v if (!w.known) if (v.dist + cvw < w.dist) { decrease(w.dist to v.dist + cvw); w.path = v; } while loop executes |V| times each edge examined once

7 Analysis How long does it take to find the smallest unknown distance? –simple scan using an array: O(|V|) –binary heap: O(log |V|) Total running time: –simple scan: O(|V| 2 + |E|) –binary heap: O(|V| log |V| + |E| log |V|) = O(|E| log |V|)

8 Proof of Correctness By induction on |S|, at end of loop: 1.For all v  S, v.dist is equal to the length of the shortest path from s to v. 2.For all v  V – S, v.dist is the length of the shortest path from s to v that lies wholly within S, except v itself. S “known” s V - S “unknown” G=(V,E) v

9 The proof Basis step. |S| = 1. The shortest path from s to itself has length 0. A path from s to v  V – S wholly within S except for v consists of a single edge (s, v). v.dist = cvw in the algorithm. Ind. step. Suppose v is chosen. If v.dist is not the length of a shortest path from s to v, then there must be a shorter path P. The path P must contain some vertex other than v that is not in S. (By ind. hyp. #2.)

10 Induction step, cont’d Let x be the first such vertex in P. But then x.dist < v.dist (why?), and so x should have been chosen, not v. Contradiction. Therefore, P must not exist, and v.dist is the length of the shortest path from s to v. #2 is true because of the decrease() operation. (why?) s x v S P

11 Negative Edge Costs Dijkstra’s algorithm fails. Idea: Get rid of the idea of known and unknown vertices. –Place s on the queue. –Dequeue a vertex and update distances of adjacent vertices, and place an adjacent vertex in the queue if its distance was lowered. s x v S P

12 Acyclic Graphs Idea: We do not need to choose a vertex with minimum cost from an unknown set. Instead, choose the vertices in topological order. Running time: O(|V| + |E|) s