Design and Analysis of Algorithms

Similar documents
Design and Analysis of Algorithms

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

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 5: SCC/BFS

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

09 B: Graph Algorithms II

The Shortest Path Problem

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

Shortest path problems

Shortest Path Routing Communications networks as graphs Graph terminology Breadth-first search in a graph Properties of breadth-first search

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

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

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

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

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

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

CSE 100: GRAPH ALGORITHMS

(Re)Introduction to Graphs and Some Algorithms

Algorithm Design and Analysis

Theory of Computing. Lecture 7 MAS 714 Hartmut Klauck

CS 3410 Ch 14 Graphs and Paths

Shortest Paths. CSE 373 Data Structures Lecture 21

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

Homework Assignment #3 Graph

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.

Algorithms (V) Path in Graphs. Guoqiang Li. School of Software, Shanghai Jiao Tong University

Lecture 28: Graphs: Shortest Paths (Part 1)

Graph Algorithms. Andreas Klappenecker

Introduction to Parallel & Distributed Computing Parallel Graph Algorithms

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

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

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

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

Algorithms and Data Structures (INF1) Lecture 15/15 Hua Lu

Shortest Path Algorithms

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

Description of The Algorithm

Single Source Shortest Path

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

Week 11: Minimum Spanning trees

CS 310 Advanced Data Structures and Algorithms

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

Search: Advanced Topics and Conclusion

CSE 101, Winter Design and Analysis of Algorithms. Lecture 11: Dynamic Programming, Part 2

TIE Graph algorithms

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

Trees, Trees and More Trees

Solutions to relevant spring 2000 exam problems

Last week: Breadth-First Search

1 The Shortest Path Problem

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

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

CS473-Algorithms I. Lecture 14-A. Graph Searching: Breadth-First Search. Cevdet Aykanat - Bilkent University Computer Engineering Department


CS420/520 Algorithm Analysis Spring 2009 Lecture 14

(Refer Slide Time: 00:18)

Minimum Spanning Trees Shortest Paths

CS 361 Data Structures & Algs Lecture 13. Prof. Tom Hayes University of New Mexico

Algorithms for Data Science

Week 12: Minimum Spanning trees and Shortest Paths

CSE 417: Algorithms and Computational Complexity. 3.1 Basic Definitions and Applications. Goals. Chapter 3. Winter 2012 Graphs and Graph Algorithms

CMU-Q Lecture 2: Search problems Uninformed search. Teacher: Gianni A. Di Caro

CSE 373: Data Structures and Algorithms

Chapter 10. Fundamental Network Algorithms. M. E. J. Newman. May 6, M. E. J. Newman Chapter 10 May 6, / 33

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.)

Graph Representations and Traversal

Graph Traversal CSCI Algorithms I. Andrew Rosenberg

Homework 2. Sample Solution. Due Date: Thursday, May 31, 11:59 pm

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

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

Unit-5 Dynamic Programming 2016

Lecture 11: Analysis of Algorithms (CS ) 1

CS 206 Introduction to Computer Science II

CSE 332: Data Abstractions Lecture 15: Topological Sort / Graph Traversals. Ruth Anderson Winter 2013

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

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

Single Source Shortest Path (SSSP) Problem

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

Algorithm Design and Analysis

Artificial Intelligence

22.3 Depth First Search

All Shortest Paths. Questions from exercises and exams

CS170 Discussion Section 4: 9/18

COMPSCI 311: Introduction to Algorithms First Midterm Exam, October 3, 2018

Unit 2: Algorithmic Graph Theory

CSC 373 Lecture # 3 Instructor: Milad Eftekhar

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

Graphs. The ultimate data structure. graphs 1

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Trees Rooted Trees Spanning trees and Shortest Paths. 12. Graphs and Trees 2. Aaron Tan November 2017

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Sources for this lecture 2. Shortest paths and minimum spanning trees

Graph Algorithms. Definition

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

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

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND

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

CSE 373: Data Structures and Algorithms

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

1 More on the Bellman-Ford Algorithm

Transcription:

CSE 101, Winter 2018 Design and Analysis of Algorithms Lecture 6: BFS, SPs, Dijkstra Class URL: http://vlsicad.ucsd.edu/courses/cse101-w18/

Distance in a Graph The distance between two nodes is the length of the shortest path between them E.g., d(s,b) = 2; d(s,d) = 1; etc.

Limitations of DFS From given starting node s, DFS finds All nodes reachable from s Explicit paths to these nodes (= a DFS tree) So, DFS determines whether a path exists between nodes in a graph But it does not always find the shortest path F A B Explore(G,A) F A B E D C E D C DFS finds a path of length 5 from A to F, but the shortest A-F path has length 1.

Breadth-First Search: The Idea Imagine vertices of the graph as balls and edges as strings tied to the balls. To find shortest paths: lift the start vertex off the ground get layered view of the graph IDEA: Find vertices at distance 0, then 1, 2, etc. Suppose we ve found all nodes at distance d A A node is at distance d+1 if: it is adjacent to a node at distance d F D B it hasn t been seen yet E C Lift A Level 0 Level 1 Level 2

Breadth-First Search Algorithm procedure BFS(G,s) Input: Graph G = (V,E), (starting) vertex s V Output: for each u V reachable from s, dist[u] is set to u's distance from s for each u V dist[u] dist[s] 0 Eject Queue Q Inject Q [s] // queue containing just s while Q is not empty u eject(q) for each edge (u,w) E if dist[w] = : // i.e., w not seen yet inject(q,w) dist[w] dist[u] + 1

BFS Example Queue d(a) d(b) d(c) d(d) d(e) d(f) [A] 0 *[B D F] 0 1 1 1 [D F C] 0 1 2 1 1 [F C E] 0 1 2 1 2 1 *[C E] 0 1 2 1 2 1 [E] 0 1 2 1 2 1 *[ ] 0 1 2 1 2 1 F A B E D C BFS Tree F A B E D C * = All nodes at level 0, 1, 2 (respectively, per each *) have been processed

Correctness of BFS by Induction Statement: For any distance d = 0,1,2,... there is a point in time at which 3 conditions hold: BFS has correctly set dist[ ] for all nodes at distance d All other nodes have dist[ ] = Q contains precisely the nodes at distance d Base Case: d = 0 This holds immediately before the first iteration General Case: Assume true for d; will show for d+1 By induction hypothesis, there is a point in time at which our 3 conditions hold for level d. Let Q be the state of the queue at that time. Then the 3 conditions hold for level d+1 when all level d nodes have been ejected from the queue.

BFS Runtime Analysis Initializing the dist[ ] array takes time O( V ) The outer while loop runs V times, once per node u V each node enqueued, dequeued exactly once The time taken on node u is proportional to the degree of u = the number of edges out of u Therefore, the total runtime of the algorithm is O( V + u V deg(u)) = O( V + E ) Food for Thought (already asked in Lecture 1): What would a graph search look like that is in between DFS and BFS?

Edge Lengths: Toward Dijkstra s Algorithm BFS treats all edges as having the same length But this is not true in many applications Notation: length or weight of e = (u,w) l(e) or l(u,w) Sometimes, edge lengths / weights can be negative Can you think of examples?

Extending BFS Suppose graph G has positive, integral edge lengths Simple trick: add dummy nodes to make G with unitlength edges For real nodes, distances in G = distances in G We know how to run BFS on G But BFS on G could waste a lot of time Can we adapt BFS to work only when there is something interesting to do?

Alarm Clocks Idea Snooze through visits to dummy nodes Alarm should wake us up whenever something interesting is happening, i.e., when BFS encounters a real node Alarm for each real node = estimated time of arrival based on edges currently being traversed T = 0: set alarms for A (100), B (200); snooze T = 100: wake up, BFS is at A; set alarm for B (150); snooze T = 150: wake up; done.

Alarm Clock Algorithm Set an alarm for node s at time T = 0 If the next alarm goes off at time T, for node u, then: The shortest path distance to u is T For each edge (u,w) E If no alarm has been set for w, then set an alarm at time T + l(u,w) If an alarm has been set, but at a time later than T + l(u,w), then move it to this earlier time (1) Exactly simulates BFS on G (2) This is also known as Dijkstra s algorithm (3) What data structure helps implementation?

An Excursion on Shortest Paths

Algorithm Design Key Ideas (Shortest Paths) For SOME problems, the following property enables an efficient solution: Principle of Optimality : Any subsolution of an optimal solution is itself an optimal solution For such problems, solution strategies can include: Tabulate (cache) Subproblem Solutions : Avoid recomputation by creating a table of subproblem solutions Relaxation / Successive Approximation : Make multiple passes, each time solving a less restricted version of the original problem Eventually, solve completely unrestricted = original problem instance

Shortest Path Problem Types Given a graph G=(V,E) and w: E (1 to 2) s-t : Find a shortest path from s to t (1 to all) single-source or SSSP : Find a shortest path from a source s to every other vertex v V (All to all) all-pairs or APSP : Find a shortest path from every vertex to every other vertex The weight or cost of path v i,...,v k = l(v i,v i+1 ) Sometimes: no negative edges Examples of negative edges : travel cost incentives, exothermic chemical reactions, unprofitable transactions in arbitrage, Always: no negative cycles Otherwise, the shortest-path problem isn t well-defined <0

SSSP When Edges Have Positive Length Condition 1: d i,j > 0 All edges have positive length (Notation: d i,j = length of (v i,v j ) edge) s Condition 2: d i,j + d j,k d i,k for some i,j,k Otherwise, shortest-path problem is trivial!!! (All shortest paths would be direct edges from the source)

SSSP When Edges Have Positive Length Condition 1: d i,j > 0 All edges have positive length (Notation: d i,j = length of (v i,v j ) edge) s Condition 2: d i,j + d j,k d i,k for some i,j,k Otherwise, shortest-path problem is trivial!!! (All shortest paths would be direct edges from the source) Fact 1: Length of a path > length of any of its subpaths (Because all edges have positive length) v 3 v 9 v 1 v 6 v 4 v 8 P1 P2 v 3 v 9 v 1 v 4 v 8 P4 v 9 v 1 v 6 v 4 Length(P1) > Length(Px), x = 2, 3, 4 P3

SSSP When Edges Have Positive Length Condition 1: d i,j > 0 All edges have positive length (Notation: d i,j = length of (v i,v j ) edge) s Condition 2: d i,j + d j,k d i,k for some i,j,k Otherwise, shortest-path problem is trivial!!! (All shortest paths would be direct edges from the source) Fact 1: Length of a path > length of any of its subpaths Fact 2: Any subpath of a shortest path is itself a shortest path Principle of Optimality s v i If there is a shorter path from v i to v j, then we wouldn t have had a shortest s-t path v j t

SSSP When Edges Have Positive Length Condition 1: d i,j > 0 All edges have positive length (Notation: d i,j = length of (v i,v j ) edge) s Condition 2: d i,j + d j,k d i,k for some i,j,k Otherwise, shortest-path problem is trivial!!! (All shortest paths would be direct edges from the source) Fact 1: Length of a path > length of any of its subpaths Fact 2: Any subpath of a shortest path is itself a shortest path Fact 3: Any shortest path contains V - 1 edges By the Pigeonhole Principle (Note: requires that there are no negative or zero-cost cycles)

Back to Dijkstra s Algorithm

Shortest Paths Dijkstra s Algorithm Scenario: All shortest paths from v 0 = source to other nodes are ordered by increasing length: P 1 P 2 P n-1 Index the nodes accordingly What Dijkstra s Algorithm Does: Find P 1, then find P 2, etc. Question: How many edges are there in P 1? Exactly 1 edge, else can find a subpath that is shorter Question: How many edges are there in P k? At most k edges, else can find k (shorter) subpaths, which would contradict the definition of P k

Shortest Paths Dijkstra s Algorithm Scenario: All shortest paths from v 0 = source to other nodes are ordered by increasing length: P 1 P 2 P n-1 Index nodes accordingly Algorithm Idea: Find P 1, then find P 2, etc. Fact 4: P k contains k edges Why is this picture impossible? v 2 v 0 v 1 To find P 1 : only look at 1-edge paths (min = P 1 ) To find P 2 : only look at 1- and 2-edge paths BUT: need only consider 2-edge paths of form d 01 + d 1i Else more than one path shorter than P 2, a contradiction

Section 4.4.2: Dijkstra s Algorithm Terminology Permanent label: Know true SP distance from v 0 to v i Temporary label: Know restricted SP distance from v 0 to v i (going through only existing permanently-labeled nodes) Set of permanently labeled nodes = R in the textbook Dijkstra s Algorithm 0. All vertices v i, i = 1,, n-1, receive temporary labels l i with value d 0i LOOP: 1. Among all temporary labels, pick l k = min i l i and change l k to l k * (i.e., make vertex v k s label permanent) // stop if no temporary labels left 2. Replace all temporary labels of v k s neighbors, using l i min (l i, l k * + d ki )

Example

Example

Figure 4.10: Single-Edge Extensions of SPs Find shortest path to v k+1 by extending by a single edge the shortest path to one of v 0, v 1,, v k

Added after class Figure 4.10: Single-Edge Extensions of SPs Find shortest path to v k+1 by extending by a single edge the shortest path to one of v 0, v 1,, v k

Proof of Dijkstra Correctness

Proof of Dijkstra Correctness Added after class

Dijkstra key points

Negative Edges Dijkstra s algorithm assumes that the shortest path from s to v must pass through vertices that are closer to s than v. This fails when there are negative edges in G (Figure 4.12)

1-Slide Proof of Dijkstra

Prim s Algorithm vs. Dijkstra s Algorithm Prim: Iteratively add edge e(u,v) to T, such that u T, v T, and d u,v is minimum Dijkstra: Iteratively add edge e(u,v) to T, such that u T, v T, and l u + d u,v is minimum Both are building trees, in similar ways! Prim: Minimum Spanning Tree Dijkstra: Shortest Path Tree INTERESTING QUESTION: What kind of tree does the following algorithm build? Prim-Dijkstra : Iteratively add edge e(u,v) to T, such that u T, v T, and c l u + d u,v is minimum // 0 c 1