1 Applications of BFS and DFS CSE 2011 Winter 2011 27 May 2016.

Slides:



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

© 2010 Goodrich, Tamassia Graphs1 Graph Data Structures ORD DFW SFO LAX
CS203 Lecture 15.
Depth-First Search1 Part-H2 Depth-First Search DB A C E.
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
© 2004 Goodrich, Tamassia Breadth-First Search1 CB A E D L0L0 L1L1 F L2L2.
Graph Searching CSE 373 Data Structures Lecture 20.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Breadth-First and Depth-First Search
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Graphs By JJ Shepherd. Introduction Graphs are simply trees with looser restrictions – You can have cycles Historically hard to deal with in computers.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lectures 3 Tuesday, 9/25/01 Graph Algorithms: Part 1 Shortest.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Breadth-First Search1 Part-H3 Breadth-First Search CB A E D L0L0 L1L1 F L2L2.
DAST 2005 Tirgul 11 (and more) sample questions. DAST 2005 Q.Let G = (V,E) be an undirected, connected graph with an edge weight function w : E→R. Let.
Lecture 12 CSE 331 Sep 30, Announcements Final exam: Dec 16, 11:45am-2:45pm, NSC 210 HW 2 solutions at the end of the lecture Mid term: Oct 16,
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
COMP171 Depth-First Search.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
Graph Traversals CSC 172 SPRING 2002 LECTURE 26. Traversing graphs Depth-First Search like a post-order traversal of a tree Breath-First Search Less like.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Depth-first search COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar to pre-order.
Breadth First Search (BFS) Part 2 COMP171. Graph / Slide 2 Shortest Path Recording * BFS we saw only tells us whether a path exists from source s, to.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Depth-First Search Lecture 24 COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar.
Lecture 13 CSE 331 Oct 2, Announcements Mid term in < 2 weeks Graded HW2 at the END of the class.
Review – Part 1 CSE 2011 Winter September 2015.
Search Related Algorithms. Graph Code Adjacency List Representation:
Graph Search Computing 2 COMP s1. P ROBLEMS ON G RAPHS What kinds of problems do we want to solve on/via graphs? Is there a simple path from A to.
CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,
COSC 2007 Data Structures II Chapter 14 Graphs III.
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
GRAPHS 1. Outline 2  Undirected Graphs and Directed Graphs  Depth-First Search  Breadth-First Search.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
1 Subgraphs A subgraph S of a graph G is a graph such that The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges.
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
Depth-First Search Lecture 21: Graph Traversals
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Introduction to Graph Theory Lecture 17: Graph Searching Algorithms.
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
Graphs and Paths : Chapter 15 Saurav Karmakar
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
CSC 213 – Large Scale Programming Lecture 31: Graph Traversals.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
© 2010 Goodrich, Tamassia Breadth-First Search1 CB A E D L0L0 L1L1 F L2L2.
Lecture 12 Graph Algorithms
Depth-First Search.
CS120 Graphs.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Spanning Trees Longin Jan Latecki Temple University based on slides by
Richard Anderson Autumn 2016 Lecture 5
Subgraphs, Connected Components, Spanning Trees
COMP171 Depth-First Search.
Depth-First Search CSE 2011 Winter April 2019.
Depth-First Search CSE 2011 Winter April 2019.
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
Lecture 11 Graph Algorithms
Presentation transcript:

1 Applications of BFS and DFS CSE 2011 Winter May 2016

2 Some Applications of BFS and DFS BFS  To find the shortest path from a vertex s to a vertex v in an unweighted graph  To find the length of such a path  To find out if a strongly connected directed graph contains cycles  To construct a BSF tree/forest from a graph DFS  To find a path from a vertex s to a vertex v.  To find the length of such a path.  To construct a DSF tree/forest from a graph.

3 Computing Spanning Trees

4 Trees Tree: a connected graph without cycles. Given a connected graph, remove the cycles  a tree. The paths found by BFS(s) form a rooted tree (called a spanning tree), with the starting vertex as the root of the tree. BFS tree for vertex s = 2 What would a level-order traversal of the tree tell you?

5 Computing a BFS Tree Use BFS on a vertex BFS( v ) with array prev[ ] The paths from source s to the other vertices form a tree

6 Computing Spanning Forests

7 Computing a BFS Forest A forest is a set of trees. A connected graph gives a tree (which is itself a forest). A connected component also gives us a tree. A graph with k components gives a forest of k trees.

8 Example D E A C F B G K H L N M O R Q P s A graph with 3 components

9 Example of a Forest D E A C F B G K H L N M O R Q P s A forest with 3 trees We removed the cycles from the previous graph.

10 Computing a BFS Forest Use BFS method on a graph BFSearch( G ), which calls BFS( v ) Use BFS( v ) with array prev[ ].  The paths originating from v form a tree. BFSearch( G ) examines all the components to compute all the trees in the forest.

11 Applications of DFS Is there a path from source s to a vertex v? Is an undirected graph connected? Is a directed graph strongly connected? To output the contents (e.g., the vertices) of a graph To find the connected components of a graph To find out if a graph contains cycles and report cycles. To construct a DSF tree/forest from a graph

12 DFS Algorithm Flag all vertices as not visited Flag yourself as visited For unvisited neighbors, call RDFS(w) recursively We can also record the paths using prev[ ]. Where do we insert the code for prev[ ]?

13 DFS Path Tracking Adjacency List Visited Table (T/F)TT T T T T T T T T Pred Try some examples. Path(0) -> Path(6) -> Path(7) -> DFS find out path too source

14 DFS Tree Resulting DFS-tree. Notice it is much “deeper” than the BFS tree. Captures the structure of the recursive calls - when we visit a neighbor w of v, we add w as child of v - whenever DFS returns from a vertex v, we climb up in the tree from v to its parent

Finding Cycles Using DFS Similar to using BFS. For undirected graphs, classify the edges into 3 categories during program execution: unvisited edge, discovery edge, and back (cross) edge.  Code Fragment 13.1, p  If there exists a back edge, the undirected graph contains a cycle. 15

16 Applications – DFS vs. BFS What can BFS do and DFS can’t?  Finding shortest paths (in unweighted graphs) What can DFS do and BFS can’t?  Finding out if a connected undirected graph is biconnected A connected undirected graph is biconnected if there are no vertices whose removal disconnects the rest of the graph

17 DFS vs. BFS CB A E D L0L0 L1L1 F L2L2 CB A E D F DFSBFS ApplicationsDFSBFS Spanning forest, connected components, paths, cycles  Shortest paths  Biconnected components 

18 Final Exam  Sunday, April 17, 10:00-13:00 Materials:  All lectures notes and corresponding sections in the textbook from the beginning to today’s lecture  All assignments  Homework questions