Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Depth-First Search1 DB A C E.
Advertisements

Depth-First Search1 Part-H2 Depth-First Search DB A C E.
© 2004 Goodrich, Tamassia Breadth-First Search1 CB A E D L0L0 L1L1 F L2L2.
Depth-First Search1 DB A C E. 2 Outline and Reading Definitions (§6.1) Subgraph Connectivity Spanning trees and forests Depth-first search (§6.3.1) Algorithm.
Breadth-First Search1 Part-H3 Breadth-First Search CB A E D L0L0 L1L1 F L2L2.
Alyce Brady CS 510: Computer Algorithms Breadth-First Graph Traversal Algorithm.
© 2004 Goodrich, Tamassia Depth-First Search1 DB A C E.
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.
1 Graphs: Concepts, Representation, and Traversal CSC401 – Analysis of Algorithms Lecture Notes 13 Graphs: Concepts, Representation, and Traversal Objectives:
GRAPHS 1. Outline 2  Undirected Graphs and Directed Graphs  Depth-First Search  Breadth-First Search.
Depth-First Search1 DB A C E. 2 Depth-first search (DFS) is a general technique for traversing a graph A DFS traversal of a graph G – Visits all the vertices.
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.
Depth-First Search Lecture 21: Graph Traversals
© 2010 Goodrich, Tamassia Breadth-First Search1 CB A E D L0L0 L1L1 F L2L2.
Representing Graphs Depth First Search Breadth First Search Graph Searching Algorithms.
Graphs ORD SFO LAX DFW Graphs 1 Graphs Graphs
CSC 172 DATA STRUCTURES.
Breadth-First Search L0 L1 L2
Graphs – Breadth First Search
Graphs 10/24/2017 6:47 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Directed Graphs Directed Graphs Shortest Path 12/7/2017 7:10 AM BOS
Thinking about Algorithms Abstractly
A vertex u is reachable from vertex v iff there is a path from v to u.
Searching Graphs ORD SFO LAX DFW Spring 2007
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
CSC317 Graph algorithms Why bother?
Csc 2720 Instructor: Zhuojun Duan
Breadth-First Search L0 L1 L2
COMP9024: Data Structures and Algorithms
COMP9024: Data Structures and Algorithms
Graph Search Lecture 17 CS 2110 Fall 2017.
Chapter 14 Graph Algorithms
Directed Graphs 5/1/15 12:25:22 PM
CS120 Graphs.
Graphs Part 1.
Graphs.
Graphs.
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Depth-First Search D B A C E Depth-First Search Depth-First Search
CSE 421: Introduction to Algorithms
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Breadth-First Search (BFS)
Depth-First Search D B A C E Depth-First Search Depth-First Search
Elementary Graph Algorithms
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Depth-First Search D B A C E Depth-First Search Depth-First Search
Lecture 13 CSE 331 Sep 27, 2017.
Depth-First Search Graph Traversals Depth-First Search DFS.
Graphs ORD SFO LAX DFW Graphs Graphs
CSE 373 Data Structures Lecture 16
Spanning Trees Longin Jan Latecki Temple University based on slides by
Subgraphs, Connected Components, Spanning Trees
Depth-First Search D B A C E Depth-First Search Depth-First Search
Searching Graphs ORD SFO LAX DFW Spring 2007
COMP171 Depth-First Search.
Graphs Part 1 ORD SFO LAX DFW
Copyright © Aiman Hanna All rights reserved
Depth-First Search CSE 2011 Winter April 2019.
Depth-First Search D B A C E 4/13/2019 5:23 AM Depth-First Search
Breadth-First Search L0 L1 L2 C B A E D F 4/25/2019 3:12 AM
Depth-First Search CSE 2011 Winter April 2019.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
A vertex u is reachable from vertex v iff there is a path from v to u.
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
Breadth-First Search L0 L1 L2 C B A E D F 5/14/ :22 AM
Lecture 11 Graph Algorithms
Lecture 13 CSE 331 Sep 28, 2016.
Breadth-First Search L0 L1 L2 C B A E D F 7/28/2019 1:03 PM
Prof. Ramin Zabih Graph Traversal Prof. Ramin Zabih
Presentation transcript:

Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search 4/18/2019 5:55 PM Breadth-First Search C B A E D L0 L1 F L2 Breadth-First Search

Breadth-First Search Breadth-first search (BFS) is a general technique for traversing a graph Similar to breadth-first traversal in trees “level by level” “children” -> “neighbors” Level is number of edges from the starting node Visit each node only once Breadth-First Search

Example unexplored vertex visited vertex unexplored edge C B A E D L0 L1 F A unexplored vertex A visited vertex unexplored edge discovery edge cross edge L0 L0 A A L1 L1 B C D B C D E F E F Breadth-First Search

Example (cont.) L0 L1 L0 L1 L2 L0 L1 L2 L0 L1 L2 C B A E D F C B A E D Breadth-First Search

Example (cont.) L0 L1 L2 L0 L1 L2 L0 L1 L2 C B A E D F A B C D E F C B Breadth-First Search

BFS Algorithm Algorithm BFS(G, s) Queue  new empty queue enqueue(Queue, s) while Queue.isEmpty() v  dequeue(Queue) if getLabel(v) != VISITED // ** visit v setLabel(v, VISITED) neighbors  getAdjacentVertices(G, v) for each w  neighbors if getLabel(w) != VISITED // “children” enqueue(Queue, w) The algorithm uses a mechanism for setting and getting “labels” of vertices and edges Container C in the book is a queue. ** An unexplored vertex might be might be put on the queue more than once After the first one is visited, the rest can be skipped. A queue doesn’t usually have an operation to find an entry on the queue What is another way to check if an entry is already on the queue? Hint: getLabel() Breadth-First Search

Analysis Setting/getting a vertex/edge label takes O(1) time Each vertex is labeled twice once as UNEXPLORED once as VISITED O(n) getAdjacentVertices(v) is called once for each vertex that is not visited Each edge in the graph is used at most twice Each edge has two vertices O(m) BFS runs in O(n + m) time provided the graph is represented by the adjacency list structure [less precisely, since m is O(n2), BFS is also O(n2)] Breadth-First Search

Properties Notation Property 1 Property 2 Property 3 (not in DFS) Gs: connected component of s Property 1 BFS(G, s) visits all the vertices and edges of Gs Property 2 The discovery edges labeled by BFS(G, s) form a spanning tree Ts of Gs Property 3 (not in DFS) For each vertex v in Li The path of Ts from s to v has i edges Every path from s to v in Gs has at least i edges A B C D E F L0 A L1 B C D L2 E F Breadth-First Search