Algorithm Design and Analysis

Similar documents
Algorithm Design and Analysis

Algorithm Design and Analysis

Single Source Shortest Path

Lecture 11: Analysis of Algorithms (CS ) 1

Theory of Computing. Lecture 7 MAS 714 Hartmut Klauck

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

Dijkstra s Shortest Path Algorithm

Shortest Path Problem

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

1 Dijkstra s Algorithm

Minimum Spanning Trees

Single Source Shortest Path (SSSP) Problem

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 20. Example. Shortest Paths Definitions

Shortest path problems

(Refer Slide Time: 00:18)

Outline. Computer Science 331. Definitions: Paths and Their Costs. Computation of Minimum Cost Paths

Introduction to Algorithms. Lecture 11

Shortest Paths. Shortest Path. Applications. CSE 680 Prof. Roger Crawfis. Given a weighted directed graph, one common problem is finding the shortest

Algorithms for Data Science

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

COMP251: Single source shortest paths

CS4800: Algorithms & Data Jonathan Ullman

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

Week 12: Minimum Spanning trees and Shortest Paths

CS 4349 Lecture October 23rd, 2017

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

Shortest Paths. Nishant Mehta Lectures 10 and 11

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

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

Algorithms on Graphs: Part III. Shortest Path Problems. .. Cal Poly CSC 349: Design and Analyis of Algorithms Alexander Dekhtyar..

Shortest Paths. Nishant Mehta Lectures 10 and 11

1 The Shortest Path Problem

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

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

Single Source Shortest Paths

Lecture I: Shortest Path Algorithms

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

COT 6405 Introduction to Theory of Algorithms

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

Algorithms for Data Science

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

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

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

CS711008Z Algorithm Design and Analysis

Dijkstra s Algorithm

Minimum Spanning Trees

CS 270 Algorithms. Oliver Kullmann. Analysing BFS. Depth-first search. Analysing DFS. Dags and topological sorting.

TIE Graph algorithms

Analysis of Algorithms, I

CS711008Z Algorithm Design and Analysis

Algorithm Design, Anal. & Imp., Homework 4 Solution

Name: Lirong TAN 1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G.

CSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

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

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

Introduction to Algorithms

Week 5. 1 Analysing BFS. 2 Depth-first search. 3 Analysing DFS. 4 Dags and topological sorting. 5 Detecting cycles. CS 270 Algorithms.

Data Structure Lecture#24: Graphs 2 (Chapter 11) U Kang Seoul National University

MST & Shortest Path -Prim s -Djikstra s

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

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

Minimum Spanning Trees

CSci 231 Final Review

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

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

Unit 2: Algorithmic Graph Theory

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

Dijkstra s Algorithm. Single source shortest paths for a directed acyclic graph

Classical Shortest-Path Algorithms

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

DIJKSTRA'S ALGORITHM. By Laksman Veeravagu and Luis Barrera

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

2pt 0em. Computer Science & Engineering 423/823 Design and Analysis of Algorithms. Lecture 04 Minimum-Weight Spanning Trees (Chapter 23)

Week 11: Minimum Spanning trees

Outline. Computer Science 331. Analysis of Prim's Algorithm

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

Shortest Path Algorithms

More Graph Algorithms: Topological Sort and Shortest Distance

Section authors: Hamilton Clower, David Scott, Ian Dundore.

Algorithms and Data Structures 2014 Exercises and Solutions Week 9

Introduction to Algorithms

Introduction Single-source shortest paths All-pairs shortest paths. Shortest paths in graphs

Lecture 17: Weighted Graphs, Dijkstra s Algorithm. Courtesy of Goodrich, Tamassia and Olga Veksler

G205 Fundamentals of Computer Engineering. CLASS 21, Mon. Nov Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

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

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

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

Taking Stock. Last Time Flows. This Time Review! 1 Characterize the structure of an optimal solution

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

Nikos Anastopoulos, Konstantinos Nikas, Georgios Goumas and Nectarios Koziris

CS420/520 Algorithm Analysis Spring 2009 Lecture 14

lecture29: Shortest Path Algorithms

Lecture Notes for Chapter 23: Minimum Spanning Trees

SHORTEST PATHS. Weighted Digraphs. Shortest paths. Shortest Paths BOS ORD JFK SFO LAX DFW MIA

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

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

Lecture #7. 1 Introduction. 2 Dijkstra s Correctness. COMPSCI 330: Design and Analysis of Algorithms 9/16/2014

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

Problem Set 8 Solutions

Transcription:

Algorithm Design and Analysis LECTURE 6 Greedy Graph Algorithms Shortest paths Adam Smith 9/8/14

The (Algorithm) Design Process 1. Work out the answer for some examples. Look for a general principle Does it work on *all* your examples? 3. Write pseudocode 4. Test your algorithm by hand or computer Does it work on *all* your examples? Python is a great language for testing algorithms 5. Prove your algorithm is always correct 6. Check running time Be prepared to go back to step 1!

Writing algorithms Clear and unambiguous Test: You should be able to hand it to any student in the class, and have them convert it into working code. Homework pitfalls: remember to specify data structures (list, stack, hash table, etc) writing recursive algorithms: don t confuse the recursive subroutine with the first call label global variables clearly

Writing proofs Purpose Determine for yourself that algorithm is correct Convince reader Who is your audience? Yourself Your classmates Not the TA/grader Main goal: Find your own mistakes

Homework Goals: Reinforce and clarify material from lecture Develop your skills problem-solving and Communication Make sure you understand the solution Use the feedback If you don t understand something, ask! Me or the TA or on Piazza

Shortest Paths

Single-source Shortest Path Problem Input: Directed graph G = (V, E). Source node s, destination node t. for each edge e, length (e) = length of e. length path = sum of edge lengths Find: shortest directed path from s to t. s 9 14 6 3 18 6 3 15 5 0 30 5 11 16 4 6 19 Length of path (s,,3,5,t) is 9 + 3 + + 16 = 50. 7 44 t

When is there a shortest path? Edge weights nonnegative: shortest path always exists Negative weight edges: shortest path may exist or not If no negative cycles in G, then shortest path exists If negative cycles in G, then no shortest path

Generic idea: relaxing edges For each vertex v V, maintain Distance estimate d v from s to v (initially ) Predecessor π(v) on shortest path from s to v (initially NIL) Invariant: d(v) is the length of some path from s to v that ends with edge π v v. An edge u v is tense if d v > d u + l(u, v) This means that d(v) is too big! Relax(u, v) d v = d u + l(u, v) π v = u Note: edges leaving v may now be tense s d(u) u shortest path to some u in explored part, followed by a single edge (u, v) (e) v

Generic algorithm Maintain a bag of nodes T to be considered. Initialize T = {s}, d(s) = 0. d v = for all v s While T is not empty Take a vertex u from T For each edge u v leaving u If u v is tense, Relax(u,v) Put or update v in T s Proof of correctness: If no tense edges, then all d(u) are correct. (Proof on board.) d(u) u (e) v

Dikstra s aglorithm Priority queue to maintain the bag T Key = d(v) Process unexplored node in ascending order of estimate d(v) Usual implementation: Never process a vertex twice! Mark vertices as done (using set S) Generic correctness proof breaks down S s While T is not empty Take a vertex u from T Add u to S For each edge u v leaving u If v S and (u, v) is tense, Relax(u,v) Put (or update) v in T d(u) u (e) v

Demo of Dijkstra s Algorithm Graph with nonnegative edge lengths: A 10 B D 8 1 4 7 9 3 C E

Demo of Dijkstra s Algorithm Initialize: 10 B D 0 A 8 1 4 7 9 Q: A B C D E 0 3 C E S: {}

Demo of Dijkstra s Algorithm A EXTRACT-MIN(Q): 10 B D 0 A 8 1 4 7 9 Q: A B C D E 0 3 C E S: { A }

Demo of Dijkstra s Algorithm Explore edges leaving A: 0 A 10 10 B D 8 1 4 7 9 Q: A B C D E 0 10 3 3 C S: { A } E 3

Demo of Dijkstra s Algorithm C EXTRACT-MIN(Q): 0 A 10 10 B D 8 1 4 7 9 Q: A B C D E 0 10 3 3 C S: { A, C } E 3

Demo of Dijkstra s Algorithm Explore edges leaving C: 0 A 10 7 B 11 D 8 1 4 7 9 Q: A B C D E 0 10 3 7 11 5 3 C S: { A, C } E 3 5

Demo of Dijkstra s Algorithm E EXTRACT-MIN(Q): 0 A 10 7 B 11 D 8 1 4 7 9 Q: A B C D E 0 10 3 7 11 5 3 C S: { A, C, E } E 3 5

Demo of Dijkstra s Algorithm Explore edges leaving E: 0 A 10 7 B 11 D 8 1 4 7 9 Q: A B C D E 0 10 3 7 11 5 7 11 3 C S: { A, C, E } E 3 5

Demo of Dijkstra s Algorithm B EXTRACT-MIN(Q): 0 A 10 7 B 11 D 8 1 4 7 9 Q: A B C D E 0 10 3 7 11 5 7 11 3 C S: { A, C, E, B } E 3 5

Demo of Dijkstra s Algorithm Explore edges leaving B: 0 A 10 7 B 9 D 8 1 4 7 9 Q: A B C D E 0 10 3 7 11 5 7 11 9 3 C S: { A, C, E, B } E 3 5

Demo of Dijkstra s Algorithm D EXTRACT-MIN(Q): 0 A 10 7 B 9 D 8 1 4 7 9 Q: A B C D E 0 10 3 7 11 5 7 11 9 3 C E 3 5 S: { A, C, E, B, D }

New Proof of Correctness (Greedy Stays Ahead) Invariant. For each node u that has been processed, d(u) = length of shortest path from s to u. P' x Proof: (by induction on S ) s Base case: S = 1 is trivial. S u Inductive hypothesis: Assume for S = k 1. Let v be next node added to S, and let (u, v) be the chosen edge. The shortest s-u path plus (u, v) is an s-v path of length d(v). Consider any s-v path P. We will show that it's no shorter than d(v). Let (x, y) be the first edge in P that leaves S, and let P' be the subpath to x. P + (x, y) has length = d x + l x, y = d y d(v) So P has length at least d(v). P v y

Physical intuition System of pipes filling with water Vertices are intersections Edge length = pipe length d(v) = time at which water reaches v Balls and strings Vertices balls Edge e string of length l(e) Hold ball s up in the air d v = (height of s)-(height of v) Nature uses greedy algorithms

Review Is Dijsktra s algorithm correct with negative edge weights? Give either a proof of correctness, or an example of a graph where Dijkstra fails

Implementation: Priority Queue Maintain a set of items with priorities (= keys ) Example: jobs to be performed Operations: Insert Increase key Decrease key Extract-min: find and remove item with least key Common data structure: binary heap Time: O(log n) per operation

Pseudocode for Dijkstra(G,, s) d[s] 0 for each v V {s} do d[v] S Q V Q is a priority queue maintaining V S, keyed on d[v] while Q do u EXTRACT-MIN(Q) S S {u} for each v Adjacency-list[u] do if d[v] > d[u] + (u, v) then d[v] d[u] + (u, v) explore edges leaving v Implicit DECREASE-KEY

Analysis of Dijkstra n times degree(u) times while Q do u EXTRACT-MIN(Q) S S {u} for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) m implicit DECREASE-KEY s. PQ Operation ExtractMin DecreaseKey Dijkstra n m Array n 1 Binary heap log n log n d-way Heap HW3 HW3 Fib heap log n 1 Total n m log n m log m/n n m + n log n Individual ops are amortized bounds

Further reading Erickson s lecture notes: http://web.engr.illinois.edu/~jeffe/teaching/algorithms/notes/1-sssp.pdf