Breadth-First and Depth-First Search

Slides:



Advertisements
Similar presentations
Chapter 9: Graphs Topological Sort
Advertisements

Chapter 9: Graphs Shortest Paths
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
Analysis of Algorithms Depth First Search. Graph A representation of set of objects Pairs of objects are connected Interconnected objects are called “vertices.
1 Graphs Traversals In many graph problems, we need to traverse the vertices of the graph in some order Analogy: Binary tree traversals –Pre-order Traversal.
Graph Theory, DFS & BFS Kelly Choi What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic.
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?
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Chapter 8, Part I Graph Algorithms.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
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?
#1© K.Goczyła GRAPHS Definitions and data structuresDefinitions and data structures Traversing graphsTraversing graphs Searching for paths in graphsSearching.
Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graph Traversals Reading Material: Chapter 9. Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application.
Chapter 9: Graphs Summary Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
COMP171 Depth-First Search.
Blind Search-Part 2 Ref: Chapter 2. Search Trees The search for a solution can be described by a tree - each node represents one state. The path from.
Applications of Depth-First Search
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.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
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.
Chapter 9: Graphs Spanning Trees Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
CS261 Data Structures DFS and BFS – Edge List Representation.
Search Related Algorithms. Graph Code Adjacency List Representation:
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,
Data Structures and Algorithms Ver. 1.0 Session 17 Objectives In this session, you will learn to: Implement a graph Apply graphs to solve programming problems.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graph Traversal BFS & DFS. Review of tree traversal methods Pre-order traversal In-order traversal Post-order traversal Level traversal a bc d e f g hi.
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.
COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
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,
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
Depth-First Search Lecture 21: Graph Traversals
Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Introduction to Graph Theory Lecture 17: Graph Searching Algorithms.
Breadth-first and depth-first traversal CS1114
Graph Traversal Text Weiss, § 9.6 Depth-First Search Think Stack Breadth-First Search Think Queue.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Thinking about Algorithms Abstractly
Spanning Trees Longin Jan Latecki Temple University based on slides by
Spanning Trees Longin Jan Latecki Temple University based on slides by
Subgraphs, Connected Components, Spanning Trees
COMP171 Depth-First Search.
Depth-First Search CSE 2011 Winter April 2019.
Lecture 11 Graph Algorithms
Chapter 9: Graphs Shortest Paths
Chapter 9: Graphs Spanning Trees
Presentation transcript:

Breadth-First and Depth-First Search Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Chapter 9: Graphs Breadth-First and Depth-First Search Lydia Sinapova, Simpson College

Breadth-First and Depth-First Search BFS Basic Algorithm BFS Complexity DFS Algorithm DFS Implementation Relation between BFS and DFS

BFS – Basic Idea Given a graph with N vertices and a selected vertex A:  for (i = 1; there are unvisited vertices ; i++) Visit all unvisited vertices at distance i (i is the length of the shortest path between A and currently processed vertices) Queue-based implementation

BFS – Algorithm BFS algorithm 1. Store source vertex S in a queue and mark as processed 2. while queue is not empty Read vertex v from the queue for all neighbors w: If w is not processed Mark as processed Append in the queue Record the parent of w to be v (necessary only if we need the shortest path tree)

Example Breadth-first traversal: 1, 2, 3, 4, 6, 5 1: starting node 2, 3, 4 : adjacent to 1 (at distance 1 from node 1) 6 : unvisited adjacent to node 2. 5 : unvisited, adjacent to node 3 Example 1 Adjacency lists 1: 2, 3, 4 2: 1, 3, 6 3: 1, 2, 4, 5, 6 4: 1, 3, 5 5: 3, 4 6: 2, 3 4 2 3 5 The order depends on the order of the nodes in the adjacency lists 6

Shortest Path Tree Consider the distance table: Nodes A B C D E Distance to A 1 2 Parent The table defines the shortest path tree, rooted at A.

BFS – Complexity Step 1 : read a node from the queue O(V) times. Step 2 : examine all neighbors, i.e. we examine all edges of the currently read node. Not oriented graph: 2*E edges to examine Hence the complexity of BFS is O(V + 2*E)

Depth-First Search Procedure dfs(s) mark all vertices in the graph as not reached invoke scan(s) Procedure scan(s) mark and visit s for each neighbor w of s if the neighbor is not reached invoke scan(w)

Depth-First Search with Stack Initialization: mark all vertices as unvisited, visit(s) while the stack is not empty: pop (v,w) if w is not visited add (v,w) to tree T visit(w) Procedure visit(v) mark v as visited for each edge (v,w) push (v,w) in the stack

Recursive DFS DepthFirst(Vertex v) visit(v); for each neighbor w of v if (w is not visited) add edge (v,w) to tree T DepthFirst(w) Visit(v) mark v as visited

Example Depth first traversal: 1, 2, 6, 3, 5, 4 the particular order is dependent on the order of nodes in the adjacency lists 1 4 Adjacency lists 1: 2, 3, 4 2: 6, 3, 1 3: 1, 2, 6, 5, 4 4: 1, 3, 5 5: 3, 4 6: 2, 3 2 3 5 6

BFS and DFS bfs(G) list L = empty tree T = empty choose a starting vertex x visit(x) while(L nonempty) remove edge (v,w) from beginning of L if w not visited add (v,w) to T visit(w) BFS and DFS dfs(G) list L = empty tree T = empty choose a starting vertex x visit(x) while(L nonempty) remove edge (v,w) from end of L if w not visited add (v,w) to T visit(w) Visit ( vertex v) mark v as visited for each edge (v,w) add edge (v,w) to end of L

Applications of DFS Trees: preorder traversal Graphs: Connectivity Biconnectivity – articulation points Euler graphs