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

Similar documents
Breadth First Search. cse2011 section 13.3 of textbook

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

COP 4531 Complexity & Analysis of Data Structures & Algorithms

CS 206 Introduction to Computer Science II

CSE 100: GRAPH ALGORITHMS

Introduction to Graphs. common/notes/ppt/

Figure 1: A directed graph.

Understand graph terminology Implement graphs using

LECTURE 17 GRAPH TRAVERSALS

Graph Search Methods. Graph Search Methods

3.1 Basic Definitions and Applications

Outlines: Graphs Part-2

CS 310 Advanced Data Structures and Algorithms

Applications of BFS and DFS

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

Homework Assignment #3 Graph

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017

Outline. Graphs. Divide and Conquer.

Graph Representation

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

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

Algorithm Design and Analysis

Graph. Vertex. edge. Directed Graph. Undirected Graph

(Re)Introduction to Graphs and Some Algorithms

Advanced Data Structures and Algorithms

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

Graph Algorithms. Andreas Klappenecker

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

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

Graph Search Methods. Graph Search Methods

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

Graphs & Digraphs Tuesday, November 06, 2007

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

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

CSE 2331/5331. Topic 9: Basic Graph Alg. Representations Basic traversal algorithms Topological sort CSE 2331/5331

Unweighted Graphs & Algorithms

Algorithm Design and Analysis

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Graph: representation and traversal

CS 3410 Ch 14 Graphs and Paths

Dijkstra s Algorithm

CS4800: Algorithms & Data Jonathan Ullman

Copyright 2000, Kevin Wayne 1

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

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

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

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

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

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

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

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

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

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

Algorithms: Lecture 10. Chalmers University of Technology

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

Graph Algorithms: Chapters Part 1: Introductory graph concepts

CSE 373: Data Structures and Algorithms

Trees. Arash Rafiey. 20 October, 2015

Unit #9: Graphs. CPSC 221: Algorithms and Data Structures. Will Evans 2012W1

Sample Solutions to Homework #4

CS61BL. Lecture 5: Graphs Sorting

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 Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

Analysis of Algorithms Prof. Karen Daniels

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

CS 491 CAP Introduction to Graphs and Search

Graphs. Ouline. Graphs. An Introduction. Agraph G = (V,E) is composed of: Anedge e = (u,v) is a pair of vertices

L22-23: Graph Algorithms

Week 5. 1 Analysing BFS. 2 Depth-first search. 3 Analysing DFS. 4 Dags and topological sorting. 5 Detecting cycles. CS 270 Algorithms.

Graph Traversal. Section Dr. Chris Mayfield. Oct 24, Department of Computer Science James Madison University

Chapter 22 Elementary Graph Algorithms

Today s Outline. Graph ADT? Graph Definitions. Trees as Graphs. Graphs Chapter 9 in Weiss. More Definitions: Simple Paths and Cycles.

Minimum spanning trees

Lecture 9 Graph Traversal

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

The Shortest Path Problem

Graphs. Ouline. Graphs 10/3/2018. An Introduction. A graph G = (V,E) is composed of: An edge e = (u,v) is a pair of vertices

SELF-BALANCING SEARCH TREES. Chapter 11

W4231: Analysis of Algorithms

Minimum Spanning Trees Ch 23 Traversing graphs

Announcements Problem Set 4 is out!

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

IS 709/809: Computational Methods in IS Research. Graph Algorithms: Introduction

Graphs: Topological Sort / Graph Traversals (Chapter 9)

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington

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

Graph Search. Adnan Aziz

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

Module 11: Additional Topics Graph Theory and Applications

Chapter 9 Graph Algorithms

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

22.1 Representations of graphs

CSE 100: GRAPH SEARCH

Lecture 3: Graphs and flows

Shortest Path Routing Communications networks as graphs Graph terminology Breadth-first search in a graph Properties of breadth-first search

Graph traversal is a generalization of tree traversal except we have to keep track of the visited vertices.

Graph Representations and Traversal

Algorithms Sequential and Parallel: A Unified Approach; R. Miller and L. Boxer 3rd Graph Algorithms

COT 6405 Introduction to Theory of Algorithms

Transcription:

Breadth irst Search CSE Winter Graph raversal Application examples Given a graph representation and a vertex s in the graph ind all paths from s to the other vertices wo common graph traversal algorithms Breadth-irst Search (BS) Depth-irst Search (DS)

BS and Shortest Path Problem Given any 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 Examples: www.student.seas.gwu.edu/~idsv/idsv.html Code: similar to code for topological sort (see the next slide) flag[v] = false: we have not visited v flag[v] = true: we already visited v Why does BS need a flag for each vertex (topological sort does not)? he code works for both directed and undirected graphs

opological Sort ind all starting points Decrement indegree(w) Place new start vertices on the Q BS Algorithm flag[ ]: visited or not

BS Example Visited able (/) Q = { } Initialize visited table (all alse) Initialize Q to be empty Visited able (/) Q = { } lag that has been visited Place on the queue

Visited able (/) Mark neighbors as visited,, Q = {} {,, } Dequeue. Place all unvisited neighbors of on the queue Visited able (/) 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!

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

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

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

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

Visited able (/) What did we discover? Q = { } SOP!!! Q is empty!!! Look at visited tables. here exists a path from vertex to all vertices in the graph Applications of BS What can we do with the BS code we just discussed? Is there a path from 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 o output the contents (e.g., the vertices) of a connected (strongly connected) graph What if the graph is not connected (weakly connected)? Add just a little bit of code and invoke method BS( ) discussed later.

Other Applications of BS o find the shortest path from a vertex s to a vertex v in an unweighted graph o find the length of such a path o find out if a graph contains cycles o find the connected components of a graph that is not connected o construct a BS tree/forest from a graph Running ime of BS Assume adjacency list 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 (cont d) 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? Applications of BS Is there a path from s to a vertex v? Is an undirected graph connected? Is a directed graph strongly connected? o output the contents (e.g., the vertices) of a connected (strongly connected) graph Discuss non-connected (weakly connected) graphs later o find the shortest path from a vertex s to a vertex v in an unweighted graph o find the length of such a path o find out if a graph contains cycles o find the connected components of a graph that is not connected o construct a BS tree/forest from a graph