Parallel Breadth First Search

Similar documents
Jessica Su (some parts copied from CLRS / last quarter s notes)

Lecture 17: Breadth-First Search (BFS) built on 2017/06/16 at 11:05:40

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

CPS 102: Discrete Mathematics. Quiz 3 Date: Wednesday November 30, Instructor: Bruce Maggs NAME: Prob # Score. Total 60

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

Matching Algorithms. Proof. If a bipartite graph has a perfect matching, then it is easy to see that the right hand side is a necessary condition.

Graph Contraction. Graph Contraction CSE341T/CSE549T 10/20/2014. Lecture 14

Trees. Arash Rafiey. 20 October, 2015

Lecture 3: Graphs and flows

Week 12: Minimum Spanning trees and Shortest Paths

Minimum Spanning Trees

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.

CPSC 536N: Randomized Algorithms Term 2. Lecture 10

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

COMP 182: Algorithmic Thinking Prim and Dijkstra: Efficiency and Correctness

Problem Set 2 Solutions

Graph theory - solutions to problem set 1

Discrete Wiskunde II. Lecture 6: Planar Graphs

22.1 Representations of graphs

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

Context: Weighted, connected, undirected graph, G = (V, E), with w : E R.

PRAM Divide and Conquer Algorithms

Number Theory and Graph Theory

Treaps. 1 Binary Search Trees (BSTs) CSE341T/CSE549T 11/05/2014. Lecture 19

Network flows and Menger s theorem

HW Graph Theory SOLUTIONS (hbovik)

Faster parameterized algorithms for Minimum Fill-In

CSE 331: Introduction to Algorithm Analysis and Design Graphs

Lecture 10,11: General Matching Polytope, Maximum Flow. 1 Perfect Matching and Matching Polytope on General Graphs

implementing the breadth-first search algorithm implementing the depth-first search algorithm

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science Algorithms For Inference Fall 2014

LECTURES 3 and 4: Flows and Matchings

Scan and its Uses. 1 Scan. 1.1 Contraction CSE341T/CSE549T 09/17/2014. Lecture 8

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

CS261: Problem Set #2

Lecture 11: Analysis of Algorithms (CS ) 1

CS261: Problem Set #1

Divide and Conquer Algorithms

Greedy Algorithms 1. For large values of d, brute force search is not feasible because there are 2 d

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

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

Lecture 2 - Graph Theory Fundamentals - Reachability and Exploration 1

Homework 4 Solutions

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

CS781 Lecture 2 January 13, Graph Traversals, Search, and Ordering

Matching Theory. Figure 1: Is this graph bipartite?

Network Design and Optimization course

Graph Algorithms (part 3 of CSC 282),

Bipartite Roots of Graphs

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

Reducing Directed Max Flow to Undirected Max Flow and Bipartite Matching

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

Ma/CS 6b Class 5: Graph Connectivity

CS4800: Algorithms & Data Jonathan Ullman

Definition For vertices u, v V (G), the distance from u to v, denoted d(u, v), in G is the length of a shortest u, v-path. 1

DO NOT RE-DISTRIBUTE THIS SOLUTION FILE

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

15 210: Parallel and Sequential Data Structures and Algorithms

Greedy algorithms is another useful way for solving optimization problems.

Shortest path problems

and 6.855J March 6, Maximum Flows 2

GRAPH THEORY: AN INTRODUCTION

Graph Definitions. In a directed graph the edges have directions (ordered pairs). A weighted graph includes a weight function.

Introduction to Graph Theory

Jana Kosecka. Red-Black Trees Graph Algorithms. Many slides here are based on E. Demaine, D. Luebke slides

3.1 Basic Definitions and Applications

CSci 231 Final Review

Broadcast: Befo re 1

Solutions for the Exam 6 January 2014

Bipartite Matching & the Hungarian Method

Paths, Flowers and Vertex Cover

1 Start with each vertex being its own component. 2 Merge two components into one by choosing the light edge

Parallel Pointers: Graphs

DO NOT RE-DISTRIBUTE THIS SOLUTION FILE

Algorithm Design and Analysis

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

Faster parameterized algorithms for Minimum Fill-In

Lecture 6 Sequences II. Parallel and Sequential Data Structures and Algorithms, (Fall 2013) Lectured by Danny Sleator 12 September 2013

Graph Algorithms (part 3 of CSC 282),

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

Fundamental Graph Algorithms Part Three

Paths, Flowers and Vertex Cover

Homework #5 Algorithms I Spring 2017

CS4800: Algorithms & Data Jonathan Ullman

Single Source Shortest Path

Minimum Spanning Trees My T. UF

Chapter 22. Elementary Graph Algorithms

Algorithms Activity 6: Applications of BFS

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Lecture Notes for Chapter 23: Minimum Spanning Trees

Chapter 5 Graph Algorithms Algorithm Theory WS 2012/13 Fabian Kuhn

Lecture 10 Graph algorithms: testing graph properties

Union/Find Aka: Disjoint-set forest. Problem definition. Naïve attempts CS 445

Weighted Graph Algorithms Presented by Jason Yuan

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

3.1 Basic Definitions and Applications. Chapter 3. Graphs. Undirected Graphs. Some Graph Applications

THE LEAFAGE OF A CHORDAL GRAPH

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

Transcription:

CSE341T/CSE549T 11/03/2014 Lecture 18 Parallel Breadth First Search Today, we will look at a basic graph algorithm, breadth first search (BFS). BFS can be applied to solve a variety of problems including: finding all the vertices reachable from a vertex v, finding if an undirected graph is connected, finding the shortest path from a vertex v to all other vertices, determining if a graph is bipartite, bounding the diameter of an undirected graph, partitioning graphs, and as a subroutine for finding the maximum flow in a flow network (using Ford-Fulkerson s algorithm). 1 BFS BFS, as with the other graph searches, can be applied to both directed and undirected graph. In the following discussion the distance δ G (u, v) from a vertex u to a vertex v in a graph G is the shortest path (minimum number of edges) from u to v. The idea of breadth first search is to start at a source vertex s and explore the graph level by level, first visiting all vertices that are the (out) neighbors of s (i.e. have distance 1 from s), then vertices that have distance two from s, then distance three, etc. It should be clear that a vertex at distance i + 1 must have an in-neighbor from a vertex a distance i. Therefore if we know all vertices at distance i, then we can find the vertices at distance i + 1 by just considering their out-neighbors. The BFS approach therefore works as follows. As with all the search approaches, the approach needs to keep track of what vertices have already been visited so that it does not visit them more than once. Let s call the set of all visited vertices at the end of step i, X i. On each step the search also needs to keep the set of new vertices that are exactly distance i from v. We refer to these as the frontier vertices F i X i. To generate the next set of frontier vertices the search simply takes the neighborhood of F and removes any vertices that have already been visited, i.e., N G (F ) \ X. Recall that for a vertex u, N G (u) are the neighbors of u in the graph G (the out-neighbors for a directed graph) and for a set of vertices U, that N G (U) = u U N G (u). This gives us the following code where F is the set of frontier vertices for iteration i and X is the set of vertices that has already been processed. 1

BFS(G, X, F, i) 1 if F = 0 2 then return 3 F N G (F )\X 4 label all vertices in F as i + 1 5 X X F 6 BFS(G, X, F, i + 1) The original call is with X = {s}, F = {s} and i = 0. It might be easier to look at the iterative version of this algorithm. BFS(G, s) 1 X {s} 2 F {s} 3 i 0 4 while F Φ 5 do F N G (F )\X 6 label all vertices in F as i + 1 7 X X F 8 F F 9 i i + 1 To prove that the algorithm is correct we need to prove the assumptions that are stated in the algorithm. In particular: Lemma 1 In algorithm BFS when calling BFS(G, X, F, i), we have X = {u V G : δ G (v, u) i} F = {u V G : δ G (v, u) = i} Proof. This can be proved by induction on the step i. For the base case (the initial call) we have X = F = {s} and i = 0. This is true since only s has distance 0 from s. For the inductive step we note that, if all vertices F at step i have distance i from s, then a neighbor of F must have minimum path of length d i + 1 from s since we are adding just one more edge to the path. However, if a neighbor of F has a path d < i + 1 then it must be in X, by the inductive hypothesis so it is not added to F. Therefore F on step i + 1 will contain vertices with distance exactly d = i + 1 from s. Furthermore since the neighbors of F are unioned with X, X at step i + 1 will contain exactly the vertices with distance d i + 1. To argue that the algorithm returns all reachable vertices we note that if a vertex u is reachable from u and has distance d = δ(s, u) then there must be another x vertex with distance δ(s, x) = d 1. Therefore BFS will not terminate without finding it. Furthermore for any vertex u δ(s, u) < V so the algorithm will terminate in at most V steps. 2

Exercise 1 So far we have specified a routine that returns the set of vertices reachable from s and the length of all shortest paths to these vertices. Often we would like to know the shortest path itself, not just its length. How would you extend BFS for these purposes? The important operations in the above algorithm is 1. calculating N G (F ) = u F N G(u), 2. calculating N G (F )\X 3. calculating X X F. How we calculate these depends on the data structure we use to keep the adjacency information of the graph. Using Sets: If we keep the neighbors of each vertex using a set data structure, then we can simply use the difference and union operators to do the last two operations. For the first operation, we can do a reduction on the union operation since union of sets is an associative operation. Using Arrays: We can also simply use sorted adjacency arrays and use merge operations to do the union and get sorted merged arrays. You then have to run a remove duplicates algorithm that uses scan. For the difference, again, one can use a modified merge where you mark each node as coming from either N G (F ) or X and then run a version of remove duplicates. 2 Analysis The number of iterations of the while loop in the iterative code is the diameter of G, namely d. Now we just need to compute the cost of each iteration. Again, recall, that the important operations are: 1. calculating N G (F ) = u F N G(u), 2. calculating N G (F )\X 3. calculating X X F. In order to calculate the cost of computing N G (F ) we can use the following lemma. 3

2.1 Reduction lemma So far we have only analyzed reduce when each operation is a constant time operation. For breadth first search, we need a more general cost for reduction, since computation of N G (F ) is basically a reduction on the union operation. Consider a combine operator f where the cost of combining x and y depends on some measure, generally size, of x and y. Say work of the combine is W (f(x, y)) and the span is S(f(x, y)). Lemma 2 For any combine function f and a monotone size measure s, if for any x, y, 1. s(f(x, y)) s(x) + s(y) and 2. W (f(x, y)) c f (s(x) + s(y)) for some universal constant c f depending on the function f, and say (reduce f S) is reduction based on operator f of the set S. Then, we have ( ) W (reduce f S) = O log S x S s(x). Using the reduction lemma, we can calculate the cost computing N G (F ). The cost of union of two nodes v 1 and v 2 is N(v 2 ) lg( N(v 1 ) + N(v 2 ) ). Note that this function is always O( N(v 1 ) + N(v 2 ) ). Therefore, the conditions of the above lemma are satisfied. Therefore, the cost of merging the entire frontier is W = O(lg F v F N(v) = N G (F ) lg F 2.2 Without using the reduction lemma Just to give you some intuition about the reduction lemma, let us analyze the reduction operation from scratch using arrays. We can simply merge the adjacency arrays for all elements in F pairwise in a tree (just as we do a reduction). Cost at every level of the tree is O( N G (F ) ) and the number of levels is lg F, giving us the total cost of N G (F ) lg F. 2.3 Analysis of Breadth First Search As we analyzed above, the work of the reduction in each iteration is O( N G (F ) lg F ). Similarly, X + N the cost of calculating N G (F )\X is also O(min{ X, N G (F ) } log( G (F ) ) and the cost X + F max{ X, N G (F ) } of calculating X X F is O(min{ X, F } log( ). The second two quantities is max{ X, F } are also O( N G (F ) lg n), since F N G (F ). The span of each iteration is O(lg F lg max u F { N G (u) }) for the reduction. Therefore, we can upper bound it by lg 2 n. 4

Naive Analysis We can upper bound N G (F ) as m in each iteration giving us a per-iteration work of m lg n and the total work O(dm lg n). The total span is O(d lg 2 n). Tighter Analysis of Work We can do a tighter analysis of work by noting that each node is in exactly one frontier. Therefore, we can add up the work over the diameter. W = O log n N(v) v F i i=[0,...,d] = O log n i=[0,...,d] = O ((log n) E ) = O(m log n) N(v) v F i Now we increased the work a little bit sequential BFS has the work of O(m). One can do better, but you need concurrent writes. 5