Graphs Breadth First Search & Depth First Search by Shailendra Upadhye.

Slides:



Advertisements
Similar presentations
Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
Advertisements

Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
CS 473Lecture 141 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
Graph Traversals. For solving most problems on graphs –Need to systematically visit all the vertices and edges of a graph Two major traversals –Breadth-First.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture eight Dr. Hamdy M. Mousa.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.
Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:
Breadth First Search. Two standard ways to represent a graph –Adjacency lists, –Adjacency Matrix Applicable to directed and undirected graphs. Adjacency.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
1 Graph Programming Gordon College. 2 Graph Basics A graph G = (V, E) –V = set of vertices, E = set of edges –Dense graph: |E|  |V| 2 ; Sparse graph:
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 traversals / cutler1 Graph traversals Breadth first search Depth first search.
Shortest Path Problems
Lecture 14: Graph Algorithms Shang-Hua Teng. Undirected Graphs A graph G = (V, E) –V: vertices –E : edges, unordered pairs of vertices from V  V –(u,v)
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 12 Graphs and basic search algorithms Motivation Definitions and properties Representation.
Lecture 10 Graph Algorithms. Definitions Graph is a set of vertices V, with edges connecting some of the vertices (edge set E). An edge can connect two.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Graph Algorithms Introduction to Algorithms Graph Algorithms CSE 680 Prof. Roger Crawfis Partially from io.uwinnipeg.ca/~ychen2.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
COSC 3101A - Design and Analysis of Algorithms 10
Elementary Graph Algorithms CSc 4520/6520 Fall 2013 Slides adapted from David Luebke, University of Virginia and David Plaisted, University of North Carolina.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Elementary Graph Algorithms Comp 122, Fall 2004.
Elementary Graph Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Graph Algorithms Searching. Review: Graphs ● A graph G = (V, E) ■ V = set of vertices, E = set of edges ■ Dense graph: |E|  |V| 2 ; Sparse graph: |E|
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Graph. Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices.
Graph. Graph Usage I want to visit all the known famous places starting from Seoul ending in Seoul Knowledge: distances, costs Find the optimal(distance.
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture nine Dr. Hamdy M. Mousa.
Chapter 05 Introduction to Graph And Search Algorithms.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
G RAPH A LGORITHMS Dr. Tanzima Hashem Assistant Professor CSE, BUET.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
CS138A Elementary Graph Algorithms Peter Schröder.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Introduction to Algorithms
Graphs Breadth First Search & Depth First Search
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Data Structures, Algorithms & Complexity
CSC317 Graph algorithms Why bother?
Graphs Breadth First Search & Depth First Search
Graph: representation and traversal CISC4080, Computer Algorithms
Graph.
Elementary Graph Algorithms
Graphs A graph G = (V, E) V = set of vertices, E = set of edges
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Finding Shortest Paths
BFS,DFS Topological Sort
Graph Representation (23.1/22.1)
Basic Graph Algorithms
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Algorithms Searching in a Graph.
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

Graphs Breadth First Search & Depth First Search by Shailendra Upadhye

Contents Overview of Graph terminology. Graph representation. Breadth first search. Depth first search. – if time permits Pseudocode walkthrough using sample graphs. Applications of BFS and DFS. References. Q & A Working example for BFS.

Data structures with C++ using STL by Ford, William; Topp, William; Prentice Hall Graph terminology - overview A graph consists of  set of vertices V = {v 1, v 2, ….. v n }  set of edges that connect the vertices E ={e 1, e 2, …. e m } Two vertices in a graph are adjacent if there is an edge connecting the vertices. Two vertices are on a path if there is a sequences of vertices beginning with the first one and ending with the second one Graphs with ordered edges are directed. For directed graphs, vertices have in and out degrees. Weighted Graphs have values associated with edges.

ref. Introduction to Algorithms by Thomas Cormen Graph representation – undirected graphAdjacency listAdjacency matrix

ref. Introduction to Algorithms by Thomas Cormen Graph representation – directed graphAdjacency listAdjacency matrix

Some notes Adjacency list representation is usually preferred since it is more efficient in representing sparse graphs.  Graphs for which |E| is much less than |V| 2 Adjacency list requires memory of the order of θ(V+E) Searching a graph means systematically following the edges of the graph so as to visit the vertices.

ref. Introduction to Algorithms by Thomas Cormen Breadth first search Given  a graph G=(V,E) – set of vertices and edges  a distinguished source vertex s Breadth first search systematically explores the edges of G to discover every vertex that is reachable from s. It also produces a ‘breadth first tree’ with root s that contains all the vertices reachable from s. For any vertex v reachable from s, the path in the breadth first tree corresponds to the shortest path in graph G from s to v. It works on both directed and undirected graphs. However, we will explore only directed graphs.

ref. Introduction to Algorithms by Thomas Cormen Breadth first search It is so named because It discovers all vertices at distance k from s before discovering vertices at distance k+1. Animation:

ref. Introduction to Algorithms by Thomas Cormen Breadth first search - concepts To keep track of progress, it colors each vertex - white, gray or black. All vertices start white. A vertex discovered first time during the search becomes nonwhite. All vertices adjacent to black ones are discovered. Whereas, gray ones may have some white adjacent vertices. Gray represent the frontier between discovered and undiscovered vertices.

BFS – How it produces a Breadth first tree The tree initially contains only root. – s Whenever a vertex v is discovered in scanning adjacency list of vertex u  Vertex v and edge (u,v) are added to the tree.

ref. Introduction to Algorithms by Thomas Cormen BFS - algorithm BFS(G, s)// G is the graph and s is the starting node 1 for each vertex u ∈ V [G] - {s} 2 do color[u] ← WHITE// color of vertex u 3 d[u] ← ∞// distance from source s to vertex u 4 π[u] ← NIL// predecessor of u 5 color[s] ← GRAY 6 d[s] ← 0 7 π[s] ← NIL 8 Q ← Ø// Q is a FIFO - queue 9 ENQUEUE(Q, s) 10 while Q ≠ Ø// iterates as long as there are gray vertices. Lines do u ← DEQUEUE(Q) 12 for each v ∈ Adj[u] 13 do if color[v] = WHITE// discover the undiscovered adjacent vertices 14 then color[v] ← GRAY// enqueued whenever painted gray 15 d[v] ← d[u] π[v] ← u 17 ENQUEUE(Q, v) 18 color[u] ← BLACK// painted black whenever dequeued

ref. Introduction to Algorithms by Thomas Cormen Breadth First Search - example

ref. Introduction to Algorithms by Thomas Cormen Breadth first search - analysis Enqueue and Dequeue happen only once for each node. - O(V). Sum of the lengths of adjacency lists – θ(E) (for a directed graph) Initialization overhead O(V) Total runtime O(V+E)

ref. Introduction to Algorithms by Thomas Cormen Depth first search It searches ‘deeper’ the graph when possible. Starts at the selected node and explores as far as possible along each branch before backtracking. Vertices go through white, gray and black stages of color.  White – initially  Gray – when discovered first  Black – when finished i.e. the adjacency list of the vertex is completely examined. Also records timestamps for each vertex  d[v]when the vertex is first discovered  f[v]when the vertex is finished

ref. Introduction to Algorithms by Thomas Cormen Depth first search - algorithm DFS(G) 1 for each vertex u ∈ V [G] 2 do color[u] ← WHITE// color all vertices white, set their parents NIL 3 π[u] ← NIL 4 time ← 0// zero out time 5 for each vertex u ∈ V [G]// call only for unexplored vertices 6 do if color[u] = WHITE// this may result in multiple sources 7 then DFS-VISIT(u) DFS-VISIT(u) 1 color[u] ← GRAY ▹ White vertex u has just been discovered. 2 time ← time +1 3 d[u] time// record the discovery time 4 for each v ∈ Adj[u] ▹ Explore edge(u, v). 5 do if color[v] = WHITE 6 then π[v] ← u// set the parent value 7 DFS-VISIT(v)// recursive call 8 color[u] BLACK ▹ Blacken u; it is finished. 9 f [u] ▹ time ← time +1

ref. Introduction to Algorithms by Thomas Cormen Depth first search – example

ref. Introduction to Algorithms by Thomas Cormen Depth first search - analysis Lines 1-3, initialization take time Θ(V). Lines 5-7 take time Θ(V), excluding the time to call the DFS-VISIT. DFS-VISIT is called only once for each node (since it’s called only for white nodes and the first step in it is to paint the node gray). Loop on line 4-7 is executed |Adj(v)| times. Since, ∑ vєV |Adj(v)| = Ө (E), the total cost of DFS-VISIT it θ(E) The total cost of DFS is θ(V+E)

BFS and DFS - comparison Space complexity of DFS is lower than that of BFS. Time complexity of both is same – O(|V|+|E|). The behavior differs for graphs where not all the vertices can be reached from the given vertex s. Predecessor subgraphs produced by DFS may be different than those produced by BFS. The BFS product is just one tree whereas the DFS product may be multiple trees.

BFS and DFS – possible applications Exploration algorithms in Artificial Intelligence Possible to use in routing / exploration wherever travel is involved. E.g.,  I want to explore all the nearest pizza places and want to go to the nearest one with only two intersections.  Find distance from my factory to every delivery center.  Most of the mapping software (GOOGLE maps, YAHOO(?) maps) should be using these algorithms.  Companies like Waste Management, UPS and FedEx? Applications of DFS  Topologically sorting a directed acyclic graph. List the graph elements in such an order that all the nodes are listed before nodes to which they have outgoing edges.  Finding the strongly connected components of a directed graph. List all the subgraphs of a strongly connected graph which themselves are strongly connected.

References Data structures with C++ using STL by Ford, William; Topp, William; Prentice Hall. Introduction to Algorithms by Cormen, Thomas et. al., The MIT press.

Data Structures in C++ using STL, William Ford Working example for BFS Seattle Olympia Everett Portland Spokane North Bend Centralia * Assume – edge value 1 for all Cases: 1.Start with Everett 2.Start with Olympia