The Shortest Path Problem

Similar documents
CS 3410 Ch 14 Graphs and Paths

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Shortest path problems

22 Elementary Graph Algorithms. There are two standard ways to represent a

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

5.4 Shortest Paths. Jacobs University Visualization and Computer Graphics Lab. CH : Algorithms and Data Structures 456

Shortest Path Problem

CSE 502 Class 23. Jeremy Buhler Steve Cole. April BFS solves unweighted shortest path problem. Every edge traversed adds one to path length

09 B: Graph Algorithms II

1 Dijkstra s Algorithm

Basic Graph Definitions

Design and Analysis of Algorithms

Design and Analysis of Algorithms

Outline. (single-source) shortest path. (all-pairs) shortest path. minimum spanning tree. Dijkstra (Section 4.4) Bellman-Ford (Section 4.

Artificial Intelligence

CSE 100: GRAPH ALGORITHMS

1 The Shortest Path Problem

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

Unit #9: Graphs. CPSC 221: Algorithms and Data Structures. Will Evans 2012W1

Data Structures in Java. Session 17 Instructor: Bert Huang

Shortest Paths. CSE 373 Data Structures Lecture 21

CS350: Data Structures Dijkstra s Shortest Path Alg.

Single Source Shortest Path

Analysis of Algorithms, I

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

Breadth-First Search, 1. Slides for CIS 675 DPV Chapter 4. Breadth-First Search, 3. Breadth-First Search, 2

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

Classical Shortest-Path Algorithms

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

Lecture 11: Analysis of Algorithms (CS ) 1

4/8/11. Single-Source Shortest Path. Shortest Paths. Shortest Paths. Chapter 24

22 Elementary Graph Algorithms. There are two standard ways to represent a

Algorithms and Data Structures

Data Structures and Algorithms

Algorithms for Data Science

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Graphs. Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale Room - Faner 3131

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

Directed Graphs. DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 1

(Refer Slide Time: 00:18)

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

PATH FINDING AND GRAPH TRAVERSAL

Graph Algorithms. 1 Preliminary Denitions. CSci 335 Software Design and Analysis III Chapter 9 Graph Algorithms. Prof.

CSE 373: Data Structures and Algorithms

Dijkstra s Shortest Path Algorithm

Graph Applications. Topological Sort Shortest Path Problems Spanning Trees. Data Structures 1 Graph Applications

Shortest Paths. Nishant Mehta Lectures 10 and 11

Dijkstra s Algorithm and Priority Queue Implementations. CSE 101: Design and Analysis of Algorithms Lecture 5

Theory of Computing. Lecture 7 MAS 714 Hartmut Klauck

CS 206 Introduction to Computer Science II

Title. Ferienakademie im Sarntal Course 2 Distance Problems: Theory and Praxis. Nesrine Damak. Fakultät für Informatik TU München. 20.

CS420/520 Algorithm Analysis Spring 2009 Lecture 14

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

Minimum Spanning Trees

Shortest Paths. Nishant Mehta Lectures 10 and 11

Ma/CS 6a Class 27: Shortest Paths

Introduction to Algorithms. Lecture 11

The Shortest Path Problem. The Shortest Path Problem. Mathematical Model. Integer Programming Formulation

CSE 326: Data Structures Dijkstra s Algorithm. James Fogarty Autumn 2007

Graph Representation DFS BFS Dijkstra A* Search Bellman-Ford Floyd-Warshall Iterative? Non-iterative? MST Flow Edmond-Karp

Introduction. I Given a weighted, directed graph G =(V, E) with weight function

Dr. Alexander Souza. Winter term 11/12

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

Chapter 9 Graph Algorithms

CSE 100: GRAPH ALGORITHMS

Solution. violating the triangle inequality. - Initialize U = {s} - Add vertex v to U whenever DIST[v] decreases.

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

Graphs. Part II: SP and MST. Laura Toma Algorithms (csci2200), Bowdoin College

Graphs & Digraphs Tuesday, November 06, 2007

Minimum Spanning Tree

Introduction. I Given a weighted, directed graph G =(V, E) with weight function

Algorithms for Data Science

CSci 231 Final Review

CS61BL. Lecture 5: Graphs Sorting

CS 206 Introduction to Computer Science II

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017

CHAPTER 13 GRAPH ALGORITHMS

Algorithm Design and Analysis

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

Lecture 28: Graphs: Shortest Paths (Part 1)

COMP251: Single source shortest paths

1 i n (p i + r n i ) (Note that by allowing i to be n, we handle the case where the rod is not cut at all.)

Homework Assignment #3 Graph

Trees. Arash Rafiey. 20 October, 2015

Dijkstra's algorithm Prim's algorithm

CS350: Data Structures Heaps and Priority Queues

Chapter 24. Shortest path problems. Chapter 24. Shortest path problems. 24. Various shortest path problems. Chapter 24. Shortest path problems

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

CS/COE

CS 310 Advanced Data Structures and Algorithms

Dijkstra s Algorithm

Elementary Graph Algorithms: Summary. Algorithms. CmSc250 Intro to Algorithms

2.3 Optimal paths. E. Amaldi Foundations of Operations Research Politecnico di Milano 1

Description of The Algorithm

from notes written mostly by Dr. Carla Savage: All Rights Reserved

Dijkstra s Algorithm. Dijkstra s algorithm is a natural, greedy approach towards


CS 512: Comments on Graph Search 16:198:512 Instructor: Wes Cowan

Transcription:

The Shortest Path Problem 1

Shortest-Path Algorithms Find the shortest path from point A to point B Shortest in time, distance, cost, Numerous applications Map navigation Flight itineraries Circuit wiring Network routing 2

Shortest Path Problems Weighted graphs: Input is a weighted graph where each edge (v i,v j ) has cost c i,j to traverse the edge Cost of a path v 1 v 2 v N is N 1 c i, i+ 1 i= 1 Goal: to find a smallest cost path Unweighted graphs: Input is an unweighted graph i.e., all edges are of equal weight Goal: to find a path with smallest number of hops 3

Shortest Path Problems Single-source shortest path problem Given a weighted graph G=(V,E), and a source vertex s, find the minimum weighted path from s to every other vertex in G Some algorithms: Weighted: Dijkstra s algo s: source Unweighted: Simple BFS 4

Point to Point SP problem Given G(V,E) and two vertices A and B, find a shortest path from A (source) to B (destination). Solution: 1) Run the code for Single Source Shortest Path using source as A. 2) Stop algorithm when B is reached.

All Pairs Shortest Path Problem Given G(V,E), find a shortest path between all pairs of vertices. Solutions: (brute-force) Solve Single Source Shortest Path for each vertex as source There are more efficient ways of solving this problem (e.g., Floyd-Warshall algo).

Negative Weights Graphs can have negative weights E.g., arbitrage Shortest positive-weight path is a net gain Path may include individual losses Problem: Negative weight cycles Allow arbitrarily-low path costs Solution Detect presence of negative-weight cycles 7

Unweighted Shortest Paths No weights on edges Find shortest length paths Same as weighted shortest path with all weights equal Breadth-first search 1 2 source O( E + V ) 0 2 3 1 3 8

Unweighted Shortest Paths For each vertex, keep track of Whether we have visited it (known) Its distance from the start vertex (d v ) Its predecessor vertex along the shortest path from the start vertex (p v ) 9

Unweighted Shortest Paths Solution 1: Repeatedly iterate through vertices, looking for unvisited vertices at current distance from start vertex s. Running time: O( V 2 ) 10

Unweighted Shortest Paths Solution: Ignore vertices that have already been visited by keeping only unvisited vertices (distance = ) on the queue. Running time: O( E + V ) source Queue: v 3 v 1, v 6 v 2, v 4 v 7, v 5 11

Unweighted Shortest Paths 12

Weighted Shortest Paths Dijkstra s algorithm GREEDY strategy: Always pick the next closest vertex to the source Use priority queue to store unvisited vertices by distance from s After deletemin v, update distances of remaining vertices adjacent to v using decreasekey Does not work with negative weights 13

Dijkstra s Algorithm 14

source Dijkstra 15

BuildHeap: O( V ) DeleteMin: O( V log V ) DecreaseKey: O( E log V ) Total running time: O( E log V ) 16

Why Dijkstra Works Hypothesis This is called the Optimal Substructure property 100 A D 20 100 100 10 B C A least-cost path from X to Y contains least-cost paths from X to every city on the path to Y E.g., if X C1 C2 C3 Y is the least-cost path from X to Y, then 10 X C1 C2 C3 is the least-cost path from X to C3 X C1 C2 is the least-cost path from X to C2 X C1 is the least-cost path from X to C1 17

Why Dijkstra Works PROOF BY CONTRADICTION: Assume hypothesis is false I.e., Given a least-cost path P from X to Y that goes through C, there is a better path P from X to C than the one in P Show a contradiction But we could replace the subpath from X to C in P with this lesser-cost path P The path cost from C to Y is the same Thus we now have a better path from X to Y But this violates the assumption that P is the least-cost path from X to Y Therefore, the original hypothesis must be true X P C Y 18

Printing Shortest Paths 19

What about graphs with negative edges? Will the O( E log V ) Dijkstra s algorithm work as is? deletemin Updates to dist source v 1 3 v4 1 v 2-10 2 v 3 Solution: Do not mark any vertex as known. Instead allow multiple updates. V 1 V 2 V 4 V 3 V 2.dist = 3 V 4.dist = 4, V 3.dist = 5 No change No change and so v 4. dist will remain 4. Correct answer: v 4.dist should be updated to -5

Negative Edge Costs Running time: O( E V ) source v 1 3 1 v 2 2 v4-10 v 3 Queue V 1 V 2 V 4, V 3 V 3 V 4 Dequeue V 1 V 2 V 4 V 3 V 4 Updates to dist V 2.dist = 3 V 4.dist = 4, V 3.dist = 5 No updates V 4.dist = -5 No updates 21

Negative Edge Costs Running time: O( E V ) Negative weight cycles? 22

Shortest Path Problems Unweighted shortest-path problem: O( E + V ) Weighted shortest-path problem No negative edges: O( E log V ) Negative edges: O( E V ) Acyclic graphs: O( E + V ) No asymptotically faster algorithm for singlesource/single-destination shortest path problem 23

Course Evaluation Site in now Open! http://skylight.wsu.edu/s/053eadf6-6157-44ce- 92ad-cbc26bde3b53.srv