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.

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

Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
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.
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.
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 Breadth First Search & Depth First Search by Shailendra Upadhye.
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:
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
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:
Graphs - Definition G(V,E) - graph with vertex set V and edge set E
Cs420dl lecture 7 Graph traversals wim bohm cs csu.
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.
Tirgul 11 DFS Properties of DFS Topological sort.
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
Shortest Path Problems
1 Data Structures DFS, Topological Sort Dana Shapira.
Tirgul 11 BFS,DFS review Properties Use. Breadth-First-Search(BFS) The BFS algorithm executes a breadth search over the graph. The search starts at a.
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.
Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.
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.
November 6, Algorithms and Data Structures Lecture XI Simonas Šaltenis Aalborg University
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.
1 Depth-First Search Idea: –Starting at a node, follow a path all the way until you cannot move any further –Then backtrack and try another branch –Do.
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.
 2004 SDU Lectrue4-Properties of DFS Properties of DFS Classification of edges Topological sort.
CSC 201: Design and Analysis of Algorithms Lecture # 18 Graph Algorithms Mudasser Naseer 1 12/16/2015.
1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very.
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.
David Luebke 1 1/25/2016 CSE 207: Algorithms Graph Algorithms.
Liaquat Majeed Sheikh 1 1/25/2016 Graph Algorithms.
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.
CS138A Elementary Graph Algorithms Peter Schröder.
Introduction to Algorithms
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
CS200: Algorithm Analysis
Graph: representation and traversal CISC4080, Computer Algorithms
Binhai Zhu Computer Science Department, Montana State University
Elementary Graph Algorithms
Graph Representation Adjacency list representation of G = (V, E)
Finding Shortest Paths
Advanced Algorithms Analysis and Design
Algorithms Searching in a Graph.
Elementary Graph Algorithms
Presentation transcript:

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 Search (BFS) –Depth-First Search(DFS)

BFS Starts at some source vertex s Discover every vertex that is reachable from s Also produces a BFS tree with root s and including all reachable vertices Discovers vertices in increasing order of distance from s –Distance between v and s is the minimum number of edges on a path from s to v i.e. discovers vertices in a series of layers

BFS : vertex colors stored in color[] Initially all undiscovered: white When first discovered: gray –They represent the frontier of vertices between discovered and undiscovered –Frontier vertices stored in a queue –Visits vertices across the entire breadth of this frontier When processed: black

Additional info stored (some applications of BFS need this info) pred[u]: points to the vertex which first discovered u d[u]: the distance from s to u

BFS(G=(V,E),s) for each (u in V) color[u] = white d[u] = infinity pred[u] = NIL color[s] = gray d[s] = 0 Q.enqueue(s) while (Q is not empty) do u = Q.dequeue() for each (v in Adj[u]) do if (color(v] == white) then color[v] = gray //discovered but not yet processed d[v] = d[u] + 1 pred[v] = u Q.enqueue(v) //added to the frontier color[u] = black //processed

Chapter 12: Graphs7 Example C B A E D discovery edge cross edge A visited vertex A identified vertex unexplored edge F CB A E D F CB A E D F

Chapter 12: Graphs8 Example (cont.) CB A E D F CB A E D F CB A E D F CB A E D F

Chapter 12: Graphs9 Example (cont.) CB A E D F L2L2 CB A E D F CB A E D F CB A E D F

Analysis Each vertex is enqued once and dequeued once : Θ(n) Each adjacency list is traversed once: Total: Θ(n+m)

BFS Tree predecessor pointers after BFS is completed define an inverted tree –Reverse edges: BFS tree rooted at s –The edges of the BFS tree are called : discovery (tree) edges –Remaining graph edges are called: cross edges

BFS and shortest paths Theorem: Let G=(V,E) be a directed or undirected graph, and suppose BFS is run on G starting from vertex s. During its execution BFS discovers every vertex v in V that is reachable from s. Let δ(s,v) denote the number of edges on the shortest path form s to v. Upon termination of BFS, d[v] = δ(s,v) for all v in V.

DFS Start at a source vertex s Search as far into the graph as possible and backtrack when there is no new vertices to discover –recursive

color[u] and pred[u] as before color[u] –Undiscovered: white –Discovered but not finished processing: gray –Finished: black pred[u] –Pointer to the vertex that first discovered u

Time stamps, We store two time stamps: –d[u]: the time vertex u is first discovered (discovery time) –f[u]: the time we finish processing vertex u (finish time)

DFS(G) for each (u in V) do color[u] = white pred[u] = NIL time = 0 for each (u in V) do if (color[u] == white) DFS-VISIT(u) //vertex u is just discovered color[u] = gray time = time +1 d[u] = time for each (v in Adj[u]) do //explore neighbor v if (color[v] == white) then //v is discovered by u pred[v] = u DFS-VISIT(v) color[u] = black // u is finished f[u] = time = time+ 1 DFS-VISIT invoked by each vertex exactly once. Θ(V+E)

DFS Forest Tree edges: inverse of pred[] pointers –Recursion tree, where the edge (u,v) arises when processing a vertex u, we call DFS-VISIT(v) Remaining graph edges are classified as: Back edges: (u, v) where v is an ancestor of u in the DFS forest (self loops are back edges) Forward edges: (u, v) where v is a proper descendent of u in the DFS forest Cross edges: (u,v) where u and v are not ancestors or descendents of one another. D B A C E s

If DFS run on an undirected graph No difference between back and forward edges: all called back edges No cross edges: can you see why?

Parenthesis Theorem In any DFS of a graph G= (V,E), for any two vertices u and v, exactly one of the following three conditions holds: –The intervals [d[u],f[u]] and [d[v],f[v]] are entirely disjoint and neither u nor v is a descendent of the other in the DFS forest – The interval [d[u],f[u]] is contained within interval [d[v],f[v]] and u is a descendent of v in the DFS forest –The interval [d[v],f[v]] is contained within interval [d[u],f[u]] and v is a descendent of u in the DFS forest

How can we use DFS to determine whether a graph contains any cycles?