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

Similar documents
Single Source, Shortest Path Problem

CSE 373 Data Structures & Algorithms

CSE 373: Data Structures and Algorithms

Today s Outline CSE 221: Algorithms and Data Structures Graphs (with no Axes to Grind)

Shortest Paths. CSE 373 Data Structures Lecture 21

CS350: Data Structures Dijkstra s Shortest Path Alg.

Section 7: Dijkstra s

The Shortest Path Problem

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

Minimum Spanning Trees and Shortest Paths

Problem: Large Graphs

CSE 332: Data Structures & Parallelism Lecture 22: Minimum Spanning Trees. Ruth Anderson Winter 2018

Algorithms for Data Science

Spanning Tree. Lecture19: Graph III. Minimum Spanning Tree (MSP)

CSE 100: GRAPH ALGORITHMS

Shortest path problems

CSE 373: Data Structures and Algorithms

Undirected graph is a special case of a directed graph, with symmetric edges

(Refer Slide Time: 00:18)

Lecture 11: Analysis of Algorithms (CS ) 1

Classical Shortest-Path Algorithms

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15

Algorithms and Data Structures

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

Module 5 Graph Algorithms

Spanning Trees. CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Motivation. Observations. Spanning tree via DFS

Minimum Spanning Trees

CS 4349 Lecture October 23rd, 2017

Minimum Spanning Trees and Shortest Paths

Luke. Leia 2. Han Leia

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

CSC 8301 Design & Analysis of Algorithms: Warshall s, Floyd s, and Prim s algorithms

Shortest Paths. Nishant Mehta Lectures 10 and 11

09 B: Graph Algorithms II

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

DIJKSTRA'S ALGORITHM. By Laksman Veeravagu and Luis Barrera

Shortest Paths. Nishant Mehta Lectures 10 and 11

Dijkstra s algorithm for shortest paths when no edges have negative weight.

Single Source Shortest Path (SSSP) Problem

Algorithms in a Nutshell. Session 4 Recap Algorithm Themes 11:20 11:40

Dynamic-Programming algorithms for shortest path problems: Bellman-Ford (for singlesource) and Floyd-Warshall (for all-pairs).

Dijkstra s Algorithm Last time we saw two methods to solve the all-pairs shortest path problem: Min-plus matrix powering in O(n 3 log n) time and the

1 Dijkstra s Algorithm

1 The Shortest Path Problem

CS4800: Algorithms & Data Jonathan Ullman

CSE 373: Data Structures and Algorithms

Final Examination. Algorithms & Data Structures II ( )

Analysis of Algorithms, I

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

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

Theory of Computing. Lecture 7 MAS 714 Hartmut Klauck

Undirected graph is a special case of a directed graph, with symmetric edges

Description of The Algorithm

Graphs. The ultimate data structure. graphs 1

6.1 Minimum Spanning Trees

CS 561, Lecture 10. Jared Saia University of New Mexico

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 6: BFS and Dijkstra s

CS 561, Lecture 10. Jared Saia University of New Mexico

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016

MST & Shortest Path -Prim s -Djikstra s

Single Source Shortest Path

Basic Graph Definitions

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

Data Structures in Java. Session 17 Instructor: Bert Huang

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Chapter 9 Graph Algorithms

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

More Graph Algorithms: Topological Sort and Shortest Distance

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

Algorithms for Data Science

Data Structures and Algorithms

Dr. Alexander Souza. Winter term 11/12

Theorem: The greedy algorithm for RKP will always output the optimal solution under Selection Criterion 3. Proof sketch: Assume that the objects have

Shortest Paths and Minimum Spanning Trees

CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms

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

1 Non greedy algorithms (which we should have covered

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

Graphs. There is no restriction on the relation E. Directed graphs also known as digraphs

CS 310 Advanced Data Structures and Algorithms

Algorithm Analysis Graph algorithm. Chung-Ang University, Jaesung Lee

CS 146 Spring 2017 Homework 4 Sections 5 and 6. The homework assignment is available at:

CSC 373 Lecture # 3 Instructor: Milad Eftekhar

CSE332: Data Abstractions Lecture 25: Minimum Spanning Trees. Ruth Anderson via Conrad Nied Winter 2015

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

Recitation 10. Shortest Paths Announcements. ShortLab has been released, and is due Friday afternoon. It s worth 125 points.

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

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

Practice Problems for the Final

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Dynamic Programming. December 15, CMPE 250 Dynamic Programming December 15, / 60

Data Structures and Algorithms

Weighted Graph Algorithms Presented by Jason Yuan

Lecture 14: Shortest Paths Steven Skiena

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

Content. 1 Algorithms. 2 Assignment and Knapsack problem 3 Bin Packing. 4 Scheduling

Dynamic Programming: 1D Optimization. Dynamic Programming: 2D Optimization. Fibonacci Sequence. Crazy 8 s. Edit Distance

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

In this chapter, we consider some of the interesting problems for which the greedy technique works, but we start with few simple examples.

Algorithms. All-Pairs Shortest Paths. Dong Kyue Kim Hanyang University

Transcription:

SE 6: Data Structures Dijkstra s lgorithm James Fogarty utumn 007

Dijkstra, Edsger Wybe Legendary figure in computer science; was a professor at University of Texas. Supported teaching introductory computer courses without computers (pencil and paper programming) Supposedly wouldn t (until very late in life) read his e-mail; so, his staff had to print out messages and put them in his box. E.W. Dijkstra (90-00) 97 Turning ward Winner, Programming Languages, semaphores, and

Dijkstra s lgorithm: Idea dapt FS to handle weighted graphs Two kinds of vertices: Finished or known vertices Shortest distance has been computed Unknown vertices Have tentative distance

Dijkstra s lgorithm: Idea t each step: ) Pick closest unknown vertex ) dd it to known vertices ) Update distances

Dijkstra s lgorithm: Pseudocode Initialize the cost of each node to Initialize the cost of the source to 0 While there are unknown nodes left in the graph Select an unknown node b with the lowest cost Mark b as known For each node a adjacent to b a s cost = min(a s old cost, b s cost + cost of (b, a)) a s prev path node = b 5

Important Features Once a vertex is made known, the cost of the shortest path to that node is known While a vertex is still not known, another shorter path to it might still be found The shortest path itself can found by following the backward pointers stored in node.path 6

Dijkstra s lgorithm in action D 0 F H 9 7 0 E G Vertex Visited? ost Found by 0???? D?? E?? F?? G?? H?? 7

Dijkstra s lgorithm in action D 0 F H 9 7 0 E G Vertex Visited? ost Found by Y 0 <= <= D <= E?? F?? G?? H?? 8

Dijkstra s lgorithm in action D 0 F H 9 7 0 E G Vertex Visited? ost Found by Y 0 <= Y D <= E <= F?? G?? H?? 9

Dijkstra s lgorithm in action D 0 F H 9 7 0 E G Vertex Visited? ost Found by Y 0 Y Y D <= E <= F <= G?? H?? 0

Dijkstra s lgorithm in action D 0 F H 9 7 0 E G Vertex Visited? ost Found by Y 0 Y Y D Y E <= F <= G?? H??

Dijkstra s lgorithm in action D 0 7 F H 9 7 0 E G Vertex Visited? ost Found by Y 0 Y Y D Y E <= F Y G?? H <=7 F

Dijkstra s lgorithm in action D 0 7 F H 9 7 0 E G Vertex Visited? ost Found by Y 0 Y Y D Y E <= F Y G <=8 H Y 7 H F 8

Dijkstra s lgorithm in action D 0 7 F H 9 7 0 E G Vertex Visited? ost Found by Y 0 Y Y D Y E <= F Y G Y 8 H Y 7 G H F 8

Dijkstra s lgorithm in action D 0 7 F H 9 7 0 E G Vertex Visited? ost Found by Y 0 Y Y D Y E Y F Y G Y 8 H Y 7 G H F 8 5

Your turn s v 0 v 5 v v v V Visited? ost Found by 6 5 v0 v v 5 0 v 6 v v v v5 v6 6

Dijkstra s lg: Implementation Initialize the cost of each node to Initialize the cost of the source to 0 While there are unknown nodes left in the graph Select the unknown node b with the lowest cost Mark b as known For each node a adjacent to b a s cost = min(a s old cost, b s cost + cost of (b, a)) a s prev path node = b (if we updated a s cost) What data structures should we use? Running time? 7

void Graph::dijkstra(Vertex s){ Vertex v,w; Initialize s.dist = 0 and set dist of all other vertices to infinity Sounds like adjacency lists } while (there exist unknown vertices, find the one b with the smallest distance) b.known = true; } for each a adjacent to b if (!a.known) if (b.dist + weight(b,a) < a.dist){ a.dist = (b.dist + weight(b,a)); a.path = b; } Sounds like deletemin on a heap Sounds like decreasekey Running time: O( E log V ) there are E edges to examine, and each one causes a heap operation of time O(log V ) 8

Dijkstra s lgorithm: Summary lassic algorithm for solving SSSP in weighted graphs without negative weights greedy algorithm (irrevocably makes decisions without considering future consequences) Intuition for correctness: shortest path from source vertex to itself is 0 cost of going to adjacent nodes is at most edge weights cheapest of these must be shortest path to that node update paths for new node and continue picking cheapest path 9

orrectness: The loud Proof V Next shortest path from inside the known cloud etter path to V? No! W The Known loud Source How does Dijkstra s decide which vertex to add to the Known set next? If path to V is shortest, path to W must be at least as long (or else we would have picked W as the next vertex) So the path through W to V cannot be any shorter! 0

orrectness: Inside the loud Prove by induction on # of nodes in the cloud: Initial cloud is just the source with shortest path 0 ssume: Everything inside the cloud has the correct shortest path Inductive step: Only when we prove the shortest path to some node v (which is not in the cloud) is correct, we add it to the cloud When does Dijkstra s algorithm not work?

The Trouble with Negative Weight ycles 0-5 E D What s the shortest path from to E? Problem?

t each step: Dijkstra s vs FS ) Pick closest unknown vertex ) dd it to finished vertices ) Update distances t each step: ) Pick vertex from queue ) dd it to visited vertices ) Update queue with neighbors Dijkstra s lgorithm readth-first Search Some Similarities:

Single-Source Shortest Path Given a graph G = (V, E) and a single distinguished vertex s, find the shortest weighted path from s to every other vertex in G. ll-pairs Shortest Path: Find the shortest paths between all pairs of vertices in the graph. How?

nalysis Total running time for Dijkstra s: O( V log V + E log V ) (heaps) What if we want to find the shortest path from each point to LL other points? 5

Dynamic Programming lgorithmic technique that systematically records the answers to sub-problems in a table and re-uses those recorded results (rather than re-computing them). Simple Example: alculating the Nth Fibonacci number. Fib(N) = Fib(N-) + Fib(N-) 6

Floyd-Warshall for (int k = ; k =< V; k++) for (int i = ; i =< V; i++) for (int j = ; j =< V; j++) if ( ( M[i][k]+ M[k][j] ) < M[i][j] ) M[i][j] = M[i][k]+ M[k][j] Invariant: fter the kth iteration, the matrix includes the shortest paths for all pairs of vertices (i,j) containing only vertices..k as intermediate vertices 7

Initial state of the matrix: - a b - c a b c d e d e a 0 - - - b - 0 - c - - 0 - d - - - 0 e - - - - 0 M[i][j] = min(m[i][j], M[i][k]+ M[k][j]) 8

Floyd-Warshall - for ll-pairs shortest path a b c d e - a d b e - c a 0 0-0 b - 0 - - c - - 0 - Final Matrix ontents d - - - 0 e - - - - 0 9