Breadth First Search. cse2011 section 13.3 of textbook

Similar documents
Breadth First Search. Graph Traversal. CSE 2011 Winter Application examples. Two common graph traversal algorithms

Graphs. Terminology. Graphs & Breadth First Search (BFS) Extremely useful tool in modeling problems. Consist of: Vertices Edges

LECTURE 17 GRAPH TRAVERSALS

COP 4531 Complexity & Analysis of Data Structures & Algorithms

CS 206 Introduction to Computer Science II

Applications of BFS and DFS

Understand graph terminology Implement graphs using

Outlines: Graphs Part-2

CSE 100: GRAPH ALGORITHMS

CS 310 Advanced Data Structures and Algorithms

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

Figure 1: A directed graph.

To list all vertices connected to a vertex u in graph G we can use the following loop:

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

COMP 250 Fall graph traversal Nov. 15/16, 2017

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017

Algorithm Design and Analysis

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Algorithm Design and Analysis

Graphs & Digraphs Tuesday, November 06, 2007

Algorithms: Lecture 10. Chalmers University of Technology

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

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

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

Advanced Data Structures and Algorithms

CS61BL. Lecture 5: Graphs Sorting

Graph Search Methods. Graph Search Methods

ECE 242 HOMEWORK 5. In the figure below, one can go from node B to A but not from A to B.

Graphs. Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale Room - Faner 3131

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

LECTURE 26 PRIM S ALGORITHM

Graphs Introduction and Depth first algorithm

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

Module 11: Additional Topics Graph Theory and Applications

Introduction to Graphs. common/notes/ppt/

Graph: representation and traversal

Dijkstra s Algorithm

CSE 100 Minimum Spanning Trees Prim s and Kruskal

3.1 Basic Definitions and Applications

Unweighted Graphs & Algorithms

(Re)Introduction to Graphs and Some Algorithms

Copyright 2000, Kevin Wayne 1

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

Outline. Graphs. Divide and Conquer.

CSE 100: GRAPH SEARCH

Graph Algorithms. Andreas Klappenecker

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

Announcements Problem Set 4 is out!

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

Graph Traversal CSCI Algorithms I. Andrew Rosenberg

CS2 Algorithms and Data Structures Note 9

Basic Graph Algorithms (CLRS B.4-B.5, )

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

ECE 242 Data Structures and Algorithms. Graphs II. Lecture 27. Prof.

Trees. Arash Rafiey. 20 October, 2015

Lecture 5: Graphs & their Representation

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

Homework Assignment #3 Graph

Graphs Chapter 24. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Graph Search Methods. Graph Search Methods

Graph. Vertex. edge. Directed Graph. Undirected Graph

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.

Graph Algorithms. Definition

Graph Representation

Lecture 3: Graphs and flows

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

Algorithm Design and Analysis

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

CS4800: Algorithms & Data Jonathan Ullman

csci 210: Data Structures Graph Traversals

L22-23: Graph Algorithms

CS 270 Algorithms. Oliver Kullmann. Analysing BFS. Depth-first search. Analysing DFS. Dags and topological sorting.

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Minimum spanning trees

LECTURE 11 TREE TRAVERSALS

Lecture 26: Graphs: Traversal (Part 1)

CSE 332: Data Abstractions Lecture 15: Topological Sort / Graph Traversals. Ruth Anderson Winter 2013

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Sample Solutions to Homework #4

Searching in Graphs (cut points)

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

W4231: Analysis of Algorithms

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

Minimum Spanning Trees Ch 23 Traversing graphs

The Shortest Path Problem

CS 3410 Ch 14 Graphs and Paths

CSE 417: Algorithms and Computational Complexity. 3.1 Basic Definitions and Applications. Goals. Chapter 3. Winter 2012 Graphs and Graph Algorithms

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

- Logic and Algorithms - Graph Algorithms

CSC Design and Analysis of Algorithms. Lecture 4 Brute Force, Exhaustive Search, Graph Traversal Algorithms. Brute-Force Approach

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

UNDIRECTED GRAPHS BBM 201 DATA STRUCTURES DEPT. OF COMPUTER ENGINEERING

Quiz 2 Solutions. [1 points] Write your name on top of each page.

22.1 Representations of graphs

Today s Outline. CSE 326: Data Structures. Topic #15: Cool Graphs n Pretty Pictures. A Great Mathematician. The Bridges of Königsberg

Lecture 9 Graph Traversal

Transcription:

Breadth irst Search cse section. of textbook

Graph raversal (.) Application example Given a graph representation and a vertex s in the graph, find all paths from s to the other vertices. wo common graph traversal algorithms: Breadth-irst Search (BS) Idea is similar to level-order traversal for trees. Implementation uses a queue. Gives shortest path from a vertex to another. Depth-irst Search (DS) Idea is similar to preorder traversal for trees (visit a node then visit its children recursively). Implementation uses a stack (implicitly via recursion).

BS and Shortest Path Problem Given any source vertex s, BS visits the other vertices at increasing distances away from s. In doing so, BS discovers shortest paths from s to the other vertices. What do we mean by distance? he number of edges on a path from s (unweighted graph). Example Consider s=vertex s Nodes at distance?,,, Nodes at distance?,,, Nodes at distance?

How Does BS Work? Similarly to level-order traversal for trees. he BS code we will discuss works for both directed and undirected graphs.

Skeleton of BS Algorithm output v;

BS Algorithm flag[ ]: visited or not output v;

BS Example Adjacency List Visited able (/) source Initialize visited table (all alse) Q = { } Initialize Q to be empty

Adjacency List source Visited able (/) Q = { } lag that has been visited Place source on the queue

Adjacency List Visited able (/) Neighbors source Mark neighbors as visited,, Q = {} {,, } Dequeue. Place all unvisited neighbors of on the queue

Adjacency List Visited able (/) source Neighbors Mark newly visited neighbors, Q = {,, } {,,, } Dequeue. -- Place all unvisited neighbors of on the queue. -- Notice that is not placed on the queue again, it has been visited!

Adjacency List Visited able (/) Neighbors source Mark newly visited neighbors, Q = {,,, } {,,,, } Dequeue. -- Place all unvisited neighbors of on the queue. -- Only nodes and haven t been visited yet.

Adjacency List Visited able (/) source Neighbors Q = {,,,, } {,,, } Dequeue. -- has no unvisited neighbors!

Adjacency List Visited able (/) Neighbors source Q = {,,, } {,, } Dequeue. -- has no unvisited neighbors!

Adjacency List Visited able (/) source Neighbors Q = {,, } {, } Dequeue. -- has no unvisited neighbors!

Adjacency List Visited able (/) Neighbors source Mark new visited Vertex Q = {, } {, } Dequeue. -- place neighbor on the queue.

Adjacency List Visited able (/) source Neighbors Mark new visited Vertex Q = {, } {, } Dequeue. -- place neighbor on the queue

Adjacency List Visited able (/) source Neighbors Q = {, } { } Dequeue. -- no unvisited neighbors of

Adjacency List Visited able (/) source Neighbors Q = { } { } Dequeue. -- no unvisited neighbors of

Adjacency List Visited able (/) source What did we discover? Q = { } SOP!!! Q is empty!!! Look at visited tables. here exists a path from source vertex to all vertices in the graph

Assume adjacency list Running ime of BS V = number of vertices; E = number of edges Each vertex will enter Q at most once. dequeue is O(). he for loop takes time proportional to deg(v).

Running ime of BS () Recall: Given a graph with E edges Σ vertex v deg(v) = E he total running time of the while loop is: O( Σ vertex v ( + deg(v)) ) = O(V+E) his is the sum over all the iterations of the while loop! Homework: What is the running time of BS if we use an adjacency matrix?

BS and Unconnected Graphs L N P O Q R A graph may not be connected (strongly connected) enhance the above BS code to accommodate this case. M D E s A C G B K A graph with components H

Recall the BS Algorithm output ( v );

Enhanced BS Algorithm A graph with components N L M A B C H K We can re-use the previous BS(s) method to compute the connected components of a graph G. BSearch( G ) { i = ; // component number for every vertex v flag[v] = false; for every vertex v if ( flag[v] == false ) { print ( Component + i++ ); BS( v ); } }

Applications of BS What can we do with the BS code we just discussed? Is there a path from source s to a vertex v? Check flag[v]. Is an undirected graph connected? Scan array flag[ ]. If there exists flag[u] = false then Is a directed graph strongly connected? Scan array flag[ ]. If there exists flag[u] = false then

Next lecture Depth irst Search (DS) Review inal exam