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

Similar documents
Single Source Shortest Path

Shortest Path Problem

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

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

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

Analysis of Algorithms, I

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

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

Dr. Alexander Souza. Winter term 11/12

CSC 373 Lecture # 3 Instructor: Milad Eftekhar

Algorithms for Data Science

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

Single Source Shortest Path (SSSP) Problem

Introduction to Algorithms. Lecture 11

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

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

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

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

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

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 7 Lecturer: David Wagner February 13, Notes 7 for CS 170

Dijkstra s Shortest Path Algorithm

Algorithms for Data Science

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

The Shortest Path Problem

Classical Shortest-Path Algorithms

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

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.

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

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

(Refer Slide Time: 00:18)

Minimum Spanning Trees

COMP251: Single source shortest paths

Shortest path problems

Shortest Path Algorithms

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

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

Lecture 11: Analysis of Algorithms (CS ) 1

CS420/520 Algorithm Analysis Spring 2009 Lecture 14

Dijkstra s Algorithm

COL 702 : Assignment 1 solutions

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

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

Shortest Paths. Nishant Mehta Lectures 10 and 11

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

Shortest Paths. Nishant Mehta Lectures 10 and 11

6.1 Minimum Spanning Trees

1 Dijkstra s Algorithm

Design and Analysis of Algorithms

Design and Analysis of Algorithms

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

Single Source Shortest Paths

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

CS161 - Minimum Spanning Trees and Single Source Shortest Paths

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

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

Outlines: Graphs Part-2

Description of The Algorithm

DIJKSTRA'S ALGORITHM. By Laksman Veeravagu and Luis Barrera

Design and Analysis of Algorithms

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

Binary Heaps in Dynamic Arrays

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

CS 4349 Lecture October 23rd, 2017

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

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

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

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

Final Exam. Jonathan Turner 5/12/2010. CS 542 Advanced Data Structures and Algorithms

09 B: Graph Algorithms II

Algorithm Design and Analysis

Priority Queues. Meld(Q 1,Q 2 ) merge two sets

CSE 548: Analysis of Algorithms. Lectures 14 & 15 ( Dijkstra s SSSP & Fibonacci Heaps )

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

Data Structures and Algorithms

CSC Intro to Intelligent Robotics, Spring Graphs

Lecture I: Shortest Path Algorithms

Three Graph Algorithms

Three Graph Algorithms

Priority Queues and Binary Heaps

Binary heaps (chapters ) Leftist heaps

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

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

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

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 8: Negative Edges

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

Algorithms (VI) Greedy Algorithms. Guoqiang Li. School of Software, Shanghai Jiao Tong University

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

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

Trees. Arash Rafiey. 20 October, 2015

COT 6405 Introduction to Theory of Algorithms

CS350: Data Structures Dijkstra s Shortest Path Alg.

1 The Shortest Path Problem

Data Structures and Algorithms. Werner Nutt

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

CS521 \ Notes for the Final Exam

CS 310 Advanced Data Structures and Algorithms

CS4800: Algorithms & Data Jonathan Ullman

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

Graph Representations and Traversal

Transcription:

Breadth-First Search, Slides for CIS DPV Chapter Jim Royer EECS October, 00 Definition In an undirected graph, the distance between two vertices is the length of the shortest path between them. (If there is no path, the distance is.) Breadth-First Search is based on distance from the starting vertex (The distance-d vertices are all explored before the (d + )-distance ones). procedure bfs(g, s) // Input: G = (V, E), directed or undirected; s V // Output: For all verts u, dist[u] = the distance from s to u. dist[u] dist[s] = 0; Q [s] // = a queue containing just s while Q is not empty do u dequeue(q) for each v adjacent to u do if dist[v] = then enqueue(q, v); dist[v] = dist[u] + return dist Royer CIS Slides / Royer CIS Slides / Breadth-First Search, Breadth-First Search, procedure bfs(g, s) // Input: G = (V, E), s V // Output: u, dist[u] = the // distance from s to u dist[u] Q [s] while Q is not empty do u dequeue(q) for each v adjacent to u do if dist[v] = then enqueue(q, v); dist[v] = dist[u] + return dist Lemma bfs is correct. Proof outline. Proof by induction on distance d. Base case: d = 0. OK since dist[s] = 0. Induction step: d > 0. IH: bfs is correct for (d )-distant verts. When visiting the (d )-distant verts, bfs enqueues all and only the d-distant verts. each d-distant vert, u has dist[u] = d. each d-distant vert, u is visited. procedure bfs(g, s) // Input: G = (V, E), s V // Output: u, dist[u] = the // distance from s to u dist[u] Q [s] while Q is not empty do u dequeue(q) for each v adjacent to u do if dist[v] = then enqueue(q, v); dist[v] = dist[u] + return dist Lemma bfs runs in O( V + E ) time. Proof outline. Every u V enters Q at most once. Each edge in E is used at most twice (in the inner for loop). Royer CIS Slides / Royer CIS Slides /

Breadth-First Search, procedure bfs(g, s) // Input: G = (V, E), s V // Output: u, dist[u] = the F: E: // distance from s to u dist[u] Q [s] D: while Q is not empty do u dequeue(q) for each v adjacent to u do if dist[v] = then enqueue(q, v); dist[v] = dist[u] + return dist B: S:0 C: Single Source Shortest Paths Problem: Single Source Shortest Paths Given: G = (V, E), len: E R +, and s V. (G directed or undirected) Find: For each u, dist[u] = the shortest distance from s. 0 Royer CIS Slides / Royer CIS Slides / Dijkstra s Algorithm, Digression: Priority Queues Problem: Single Source Shortest Paths Given: G = (V, E), len: E R +, and s V. (G directed or undirected) Find: For each u, dist[u] = the shortest distance from s. // v has priority dist[v] dist[v] dist[u] + len(u, v) decreasekey(pq, v, dist[v]) Dijkstra s algorithm BFS with a priority queue dist[u] = the shortest distance through solved vertices Always choose the unsolved u with minimal dist[u] Declare the u solved and update dist[v] for v adjacent to u Each element has two fields: datum and priority (an integer) Priority queue operations: newqueue(... ): makes a new priority queue deletemin(pq): returns item with smallest priority and removes it from the priority queue add(pq,d,p): add a datum d with priority p decreasekey(pq,d,p): change the priority of datum d to p More later. Royer CIS Slides / Royer CIS Slides 8 /

Dijkstra s Algorithm, Dijkstra s Algorithm, 0 0 node dist 0 prev null null null null null null node dist 0 prev null null null Royer CIS Slides / Royer CIS Slides 0 / Dijkstra s Algorithm, Dijkstra s Algorithm, 0 0 node dist 0 prev null node dist 0 prev null Royer CIS Slides / Royer CIS Slides /

Dijkstra s Algorithm, Dijkstra s Algorithm, 0 0 node dist 0 8 prev null node dist 0 8 prev null Royer CIS Slides / Royer CIS Slides / Dijkstra s Algorithm, 8 Dijkstra s Algorithm, 0 node dist 0 8 prev null while (PQ is not empty) do Lemma For each step of the while-loop: (a) If w is not in PQ, then dist[w] = the shortest distance from s to w. (b) If w is in PQ, then dist[w] = the shortest distance from s to w in which all the vertices on the path (except w) are not in PQ. Proof: Base case. Initially, all vertices are in PQ, dist[s] = 0, and for w = s, dist[w] =. (a) holds vacuously (as all verts are in PQ). Part (b) also holds. (Why?) Royer CIS Slides / Royer CIS Slides /

Dijkstra s Algorithm, 0 Dijkstra s Algorithm, while (PQ is not empty) do Lemma For each step of the while-loop: (a) If w is not in PQ, then dist[w] = the shortest distance from s to w. (b) If w is in PQ, then dist[w] = the shortest distance from s to w in which all the vertices on the path (except w) are not in PQ. Proof: Induction step. Induction Hypothesis: The Lemma holds up to the current step of the while-loop. Suppose u is the vertex deletemin removes from PQ in this step. Claim : (a) holds for this u. Why? Draw the picture! Claim : (b) also holds for this step. Why? Draw the picture! while (PQ is not empty) do Runtime analysis. This depends on the implementation of priority queue used. Royer CIS Slides / Royer CIS Slides 8 / Simply implementing Priority Queues via an Array A simple minded array implementation. PQ[..n] : array of items last : index Implementing Priority Queues via a Binary Min-Heap Definition A binary tree has the min-heap property when each node has the min-value in the subtree below it. v add(pq,dat,p): last last+; PQ[item] newitem(dat, p) decreasekey(pq,i,p): PQ[i].priority p Θ() time Θ() time For example: values >= v values >= v deletemin(pq): Find and remove the item with smallest priority O(n) time Royer CIS Slides / Royer CIS Slides 0 /

Binary Min-Heap: Adding an Element & DecreaseKey Binary Min-Heap: DeleteMin procedure bubbleup(ptr) while (ptr = root) & (ptr s key ptr s parent s key) do swap ptr with its parent ptr ptr s parent procedure add(ky) ptr a new leaf node in the tree ptr.key ky bubbleup(ptr) procedure decreasekey(ptr, nk) ptr.key nk bubbleup(ptr) procedure sinkdown(ptr) q ptr; l ptr.left; r ptr.right while (q is not a leaf) do if l = null & q.key > l.key then q l if r = null & q.key > r.key then q r if q = ptr then return swap ptr.key and q.key ptr q procedure deletemin() if the heap is empty then error v root.key Pick a leaf node p root.key p.key delete p from the tree sinkdown(p) Animation: http://people.ksp.sk/~kuko/bak/index.html Royer CIS Slides / Royer CIS Slides / Binary Min-Heap: Complete Binary Trees Dijkstra s Algorithm, Run Time Analysis Definition A complete binary tree is one where all levels are complete except perhaps for the lowest level which is filled in left to right. Fact: A complete binary tree of n elements is of height + log n. If the heap tree is complete, then add: O(log n) time decreasekey: O(log n) time deletemin: O(log n) time In fact: There is a very simple array representation of a min-heap. (See Text.) while (PQ is not empty) do Runtime analysis. If a binary min-heap is used to implement priority queues, the run time is O(( V + E ) log V ). (Why?) Using Fibonacci heaps can get this down to O( V log V + E ). (See text.) Royer CIS Slides / Royer CIS Slides /

Negative weight edges Relaxation Dijkstra s Algorithm breaks if edges are allows to have negative weights.... and shortest paths may not exists if we have negative weight cycles. Consider: B A 00 - E D - - - procedure for each v V do dist[v] ; prev[v] null // Try improving dist[v] via (u, v) E // for Dijkstra s Alg, add: // while (PQ is not empty) do relax(v, u, len) C F Royer CIS Slides / Royer CIS Slides / Properties of Shortest Paths and Relaxation, Notation: u v there is no path from u to v u v there is a path from u to v u w v a path from u to v with final edge (w, v) δ(u, v) = the length of a shortest path from u to v. (= u v) Triangle inequality For each (u, v) E, δ(s, v) δ(s, u) + len(u, v). Why: Any s u v cannot be any smaller than δ(s, v). Upper-bound property For all v V, dist[v] δ(s, v) at every point in the algorithm. Why: If dist[v] <, then it s value comes from finding the length of some path s v. Properties of Shortest Paths and Relaxation, Notation: u v there is no path from u to v u v there is a path from u to v u w v a path from u to v with final edge (w, v) δ(u, v) = the length of a shortest path from u to v. (= if u v) No-path property If s v, then we always have dist[v] = δ(s, v) =. Why: (dist[v] < ) the algorithm found some path s v. Convergence property If s u v is a shortest path in G and if dist[u] = δ(s, u) before relaxing (u, v), then dist[v] = δ(s, v) afterwards. Why: Shortest paths have optimal substructure: Subpaths of shortest paths are themselves shortest paths. Royer CIS Slides / Royer CIS Slides 8 /

Properties of Shortest Paths and Relaxation, Notation: u v there is no path from u to v u v there is a path from u to v u w v a path from u to v with final edge (w, v) δ(u, v) = the length of a shortest path from u to v. (= if u v) Convergence property If s u v is a shortest path in G and if dist[u] = δ(s, u) before relaxing (u, v), then dist[v] = δ(s, v) afterwards. Properties of Shortest Paths and Relaxation, Notation: u v there is no path from u to v u v there is a path from u to v u w v a path from u to v with final edge (w, v) δ(u, v) = the length of a shortest path from u to v. (= if u v) Convergence property If s u v is a shortest path in G and if dist[u] = δ(s, u) before relaxing (u, v), then dist[v] = δ(s, v) afterwards. Path-relaxation property If s, v,..., v k is a shortest path & (s, v ), (v, v ),..., (v k, v k ) are relaxed in that order, then dist[v k ] = δ(s, v k ). Why: This follows from the Convergence property. Predecessor-subgraph property Once dist[v] = δ(s, v) for each v V, then the prev[v] s give a shortest-path tree rooted at s. Why: This also follows from the Convergence property. Royer CIS Slides / Royer CIS Slides 0 / Bellman-Ford: Just Relax (a lot) Bellman-Ford: Example, procedure for each v V do dist[v] ; prev[v] null // Try improving dist[v] via (u, v) E Run time analysis: Θ( V E ) time. for i to V do relax(u, v, len) (Why?) procedure for each v V do dist[v] ; prev[v] null ; for i to V do relax(u, v, len) Just before the relaxation loop Images from http://areshsaharkhiz.blogspot.com/ Royer CIS Slides / Royer CIS Slides /

Bellman-Ford: Example, Bellman-Ford: Example, procedure for each v V do dist[v] ; prev[v] null procedure for each v V do dist[v] ; prev[v] null ; ; for i to V do relax(u, v, len) for i to V do relax(u, v, len) Just after the st pass through the relaxation loop Just after the nd pass through the relaxation loop Royer CIS Slides / Royer CIS Slides / Bellman-Ford: Example, Bellman-Ford: Example, procedure for each v V do dist[v] ; prev[v] null ; procedure for each v V do dist[v] ; prev[v] null ; for i to V do relax(u, v, len) for i to V do relax(u, v, len) Just after the nd pass through the relaxation loop Just after the th pass through the relaxation loop Royer CIS Slides / Royer CIS Slides /

Bellman-Ford: Correctness, Bellman-Ford: Correctness, for i to V do // Relax loop relax(u, v, len) // Neg.wght s? Convergence property If s u v is a shortest path in G and if dist[u] = δ(s, u) before relaxing (u, v), then dist[v] = δ(s, v) afterwards. Path-relaxation property If P = s, v,..., v k is a shortest path & (s, v ), (v, v ),..., (v k, v k ) are relaxed in that order, then dist[v k ] = δ(s, v k ). Case: There are no negative weight cycles in G. Suppose v 0, v, v,..., v k is a shortest path in G where s = v 0. Note: k V. (Why?) After the i-th iteration of the relaxation loop, (v i, v i ) is relaxed. So by the Path-relaxation and convergence properties, after iteration k, dist[v k ] = δ(s, v k ). Moreover, Bellman-Ford returns (TRUE, dist) for i to V do // Relax loop relax(u, v, len) // Neg.wght s? Convergence property If s u v is a shortest path in G and if dist[u] = δ(s, u) before relaxing (u, v), then dist[v] = δ(s, v) afterwards. Path-relaxation property If P = s, v,..., v k is a shortest path & (s, v ), (v, v ),..., (v k, v k ) are relaxed in that order, then dist[v k ] = δ(s, v k ). Claim: G has a neg. wght cycle B.F. returns (FALSE, dist) Proof: Suppose there is a neg. wght cycle. Then every time we relax each edge in G, some dist[v] improves. (Why?) The function returns (FALSE, dist). Suppose B.F. returns (FALSE, dist) Then some length- V path to a vertex gives a cheaper path than the length-( V ) paths. Hence, this length- V path must include a cycle. Since this length- V path gave an improvement, the cycle must be negative weight. Royer CIS Slides / Royer CIS Slides 8 /