Breadth-First Search (BFS)

Slides:



Advertisements
Similar presentations
Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University.
Advertisements

What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
Depth-First Search1 Part-H2 Depth-First Search DB A C E.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
CS 312 – Graph Algorithms1 Graph Algorithms Many problems are naturally represented as graphs – Networks, Maps, Possible paths, Resource Flow, etc. Ch.
Graph Traversals Introduction Breadth-First Traversal. The Algorithm.
Graph Traversals Reading Material: Chapter 9. Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application.
1 Data Structures DFS, Topological Sort Dana Shapira.
CS344: Lecture 16 S. Muthu Muthukrishnan. Graph Navigation BFS: DFS: DFS numbering by start time or finish time. –tree, back, forward and cross edges.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
IS 2610: Data Structures Graph April 5, 2004.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Discussion #32 1/13 Discussion #32 Properties and Applications of Depth-First Search Trees.
Lecture 13 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
Properties and Applications of Depth-First Search Trees and Forests
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Graph Search Applications, Minimum Spanning Tree
Introduction to Algorithms
Graphs A New Data Structure
Directed Graphs 12/7/2017 7:15 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Tracing An Algorithm for Strongly Connected Components that uses Depth First Search Graph obtained from Text, page a-al: Geetika Tewari.
Graphs-Part II Depth First Search (DFS)
Graphs Representation, BFS, DFS
Main algorithm with recursion: We’ll have a function DFS that initializes, and then calls DFS-Visit, which is a recursive function and does the depth first.
Lecture 11 Graph Algorithms
CSC317 Graph algorithms Why bother?
Csc 2720 Instructor: Zhuojun Duan
Lecture 12 Graph Algorithms
Chapter 14 Graph Algorithms
Directed Graphs 9/20/2018 1:45 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Depth-First Search.
CS120 Graphs.
Graph Algorithms Using Depth First Search
CSCE 411 Design and Analysis of Algorithms
Graph Search Applications
Lecture 15 CSE 331 Sep 29, 2014.
CSE 421: Introduction to Algorithms
Graph Algorithms – 2 DAGs Topological order
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Graph & BFS.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Advanced Algorithms Analysis and Design
Lecture 14 CSE 331 Sep 30, 2016.
Search Related Algorithms
Lectures on Graph Algorithms: searching, testing and sorting
Graph Traversals Depth-First Traversals. Algorithms. Example.
Connected Components, Directed Graphs, Topological Sort
Richard Anderson Autumn 2016 Lecture 5
Subgraphs, Connected Components, Spanning Trees
Directed Graphs Directed Graphs Directed Graphs 2/23/ :12 AM BOS
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Richard Anderson Winter 2009 Lecture 6
Algorithms: Design and Analysis
Lecture 6 Graph Traversal
Important Problem Types and Fundamental Data Structures
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Richard Anderson Winter 2019 Lecture 6
Richard Anderson Lecture 5 Graph Theory
Lecture 11 Graph Algorithms
Richard Anderson Winter 2019 Lecture 5
Richard Anderson Autumn 2015 Lecture 6
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application may require that vertices be visited in some.
Presentation transcript:

BFS, DFS, Applications of DFS Trees and Forests, Strongly-connected Components

Breadth-First Search (BFS)

Undirected edges: each edge twice BFS Algorithm Mark start node and enqueue While queue is not empty Dequeue N For each neighbor X of N If X is not marked Mark X and enqueue Marked N Queue a - a a - b a b c a b c b c c - d c d d - b c d a = O(n+2m) = O(m) if m>>n Undirected edges: each edge twice Search order: a b c d

Depth-First Search (DFS)

DFS Algorithm Search order: a b c d Marked N DFS a a a a b b b b c c c DFS (start node) Proc DFS (N) Mark N For each neighbor X of N If X is not marked DFS (X) Marked N DFS a a a a b b b b c c c c d d d c b b c d a Search order: a b c d = O(n+2m) = O(m) if m>>n

BFS vs. DFS BFS Order: a b c e d f a b e c f d Marked N Queue a - a b a b c a b c e a b c e b c e d b c e d c e d f c e d f e d f d f f - BFS Order: a b c e d f

BFS vs. DFS DFS Order: a b c f d e (BFS Order: a b c e d f) a b e c f Marked N DFS a a a a b b b b c c c c f f f c b d d d b a e e e DFS Order: a b c f d e (BFS Order: a b c e d f)

BFS & DFS with Directed Graphs j k l m n a b c d e f g h i j k l m n BFS DFS a,b,c,d,e,f,g,h,i,j,k,l,m,n Same as before, by chance a,b,e,j,f,k,l,h,c,g,d,i,m,n Not same as before

DFS Trees and Forests 1 3 5 4 6 6 5 4 3 1 DFS Tree DFS Trees Forest 2 5 4 1 6 Convention: Roots in decreasing order; other nodes in increasing order Convention: increasing order 1 3 5 4 6 6 5 4 3 1 Can choose any order, so when there is a choice, choose smallest. When starting w/1, there are two choices, 3,5. Choose the smallest, 3. DFS Tree DFS Trees Forest Note: O(m) to create (check each edge once)

Edge Classification 3 2 5 4 1 6 Add all edges  but make them dashed if a marked node is encountered. 1 3 5 4 6 6 5 4 3 cross 1 backward cross cross forward cross backward cross backward cross Tree edge if y is a child of x in DFS forest. Forward edge if y is a descendent of x, but not a child. Backward edge if y is an ancestor of x or if x = y. Cross edge if y is not x and is neither a descendent nor an ancestor of x. backward cross

Postorder Numbers Given a DFS tree, do a postorder traversal of the tree edges and number the nodes as they are visited. 1 3 5 2 4 6 (6) 1 6 5 2 4 3 (5) (1) (6) (5) (4) (4) (2) (3) Order of number determines if forward or backward arc void dfs(int node) { notes(nodes) boolean TestAcyclic() dfsForest(); for (int u=0; j<MAX; u++) { List p = nodes[u].succ; while (; != null) (2) (3) (1) Can be added as the tree is built, so O(m) (assuming m >> n)

Observations about DFS and Postorder Numbers There are many interesting applications of all these ideas  all of which take O(m) time, assuming m >> n. Left to Right Always directing back

Cycles A directed graph G is cyclic iff a DFS-tree of G has a backward edge This implies cycle detection can be done in O(m) (O(n2) in the worst case for a complete graph), which beats O(n3) in Warshall’s algorithm, i.e. compute matrix and see if there is a 1 on the diagonal.

(for acyclic directed graphs) Topological Sorting (for acyclic directed graphs) Recall that partial orders and Hasse diagrams are acyclic directed graphs. A topological sort of a partial ordering R is an imposed total ordering over the elements such that x precedes y if xRy. Algorithm: List the nodes in reverse postorder (i.e. push nodes on a stack whenever we assign a postorder number; then pop all of them.)

Topological Sort Example b c f d e Topological Sort Example Alphabetical: a b c f d e (1) (2) (3) (4) (5) (6) b6 f5 d4 c3 e2 a1 Reverse Alphabetical: Notes: (1) All edges go to the right. (2) The nodes are correctly and totally ordered. (3) More than one ordering is possible. f b (6) (5) d c (1) (4) b6 f5 c4 a3 e2 d1 e a (2) (3)

Reachability To test if we can reach y from x, build a DFS tree starting at x; y is reachable iff y is in the tree rooted at x. O(m), (O(n2) in the worst case). Beats Warshall’s O(n3), but Warshall’s tests reachability for all pairs. Can start the DFS reachability algorithm at every node, but then the algorithm is O(nm), which is O(n3) in the worst case. Which is better depends on whether the graph is sparse: DFS reachability, O(nm), beats Warshall’s, O(n3); dense: Warshall’s is cheaper to setup and run.

Strongly-connected Components Definition: Maximal set of vertices such that every vertex in the set can reach every other vertex in the set through some path that never leaves the set The SCCs in a graph partition the vertices into disjoint sets

SCC Algorithm 1. Create the reverse graph (all the edges are reversed) 2. Compute the post-order traversal number for each vertex using the reversed graph (the order of leaving the vertices in the depth-search search) 3. Following the post-order traversal numbers on vertices from the last vertex to the first vertex, start a DFS on the original graph, and any vertex visited in that search belongs to the SCC. Repeat until all the vertices are visited.

SCC Example (1) 1. Create the reverse graph (all the edges are reversed) a b c f d e a b c f d e

SCC Example (2) 2. Compute the post-order traversal number for each vertex using the reversed graph (the order of leaving the vertices in the depth-search search) Starting with a a b c f d e f: 0 b: 1 c: 2 a: 3 d: 4 e: 5

SCC Example (3) 3. Following the post-order traversal numbers on vertices from the last vertex to the first vertex, start a DFS on the original graph, and any vertex visited in that search belongs to the SCC. Repeat until all the vertices are visited. a b c f d e Start with e: {e} Start with d: {d} Start with a: {a,b,c,f}

Connected Components for Undirected Graphs Replace each edge with two directed edges and then run the DFS forest algorithm. O(m) + O(m) = O(m) We can prove: x and y are in the same connected component of G iff x and y are in the same DFS tree. (left as an exercise). We can observe the proof – not worth the time to formally prove.