Graph Traversals Introduction Breadth-First Traversal. The Algorithm.

Slides:



Advertisements
Similar presentations
Topological Sort Topological sort is the list of vertices in the reverse order of their finishing times (post-order) of the depth-first search. Topological.
Advertisements

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.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
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.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
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.
Graphs – Depth First Search ORD DFW SFO LAX
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
GRAPHS AND GRAPH TRAVERSALS 9/26/2000 COMP 6/4030 ALGORITHMS.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
Testing for Connectedness and Cycles
1 Data Structures DFS, Topological Sort Dana Shapira.
1 Testing for Connectedness and Cycles Connectedness of Undirected Graphs Implementation of Connectedness detection Algorithm for Undirected Graphs. Implementation.
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
Graph Traversals Depth-First Traversal. The Algorithm.
COMP171 Depth-First Search.
Graph Traversals Depth-First Traversals. –Algorithms. –Example. –Implementation. Breadth-First Traversal. –The Algorithm. –Example. –Implementation. Review.
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.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Graph Traversals General Traversal Algorithm Depth-First Traversals. –Algorithms. –Example. –Implementation. Breadth-First Traversal. –The Algorithm. –Example.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
1 Topological Sort Introduction. Definition of Topological Sort. Topological Sort is Not Unique. Topological Sort Algorithms. An Example. Implementation.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Depth-First Search Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck. v G G G G is completely traversed.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
CS261 Data Structures DFS and BFS – Edge List Representation.
COSC 3101A - Design and Analysis of Algorithms 10
Spring 2015 Lecture 10: Elementary Graph Algorithms
CSC 331: Algorithm Analysis Decompositions of Graphs.
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
Discussion #32 1/13 Discussion #32 Properties and Applications of Depth-First Search Trees.
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.
Graphs.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
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.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
COSC 2007 Data Structures II
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Properties and Applications of Depth-First Search Trees and Forests
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.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
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.
Graph Algorithms – 2. graphs Parenthesis Theorem Theorem 22.7 For all u, v, exactly one of the following holds: 1. d[u] < f [u] < d[v] < f [v] or.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Introduction to Algorithms
Breadth-First Search (BFS)
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals
Graph Algorithms Using Depth First Search
Graph Traversals Depth-First Traversals. Algorithms. Example.
Graph Traversals Depth-First Traversals. Algorithms. Example.
Lecture 10 Algorithm Analysis
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Graph Traversals Depth-First Traversals. Algorithms. Example.
Graph Traversals Depth-First Traversals. Algorithms. Example.
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Graph Traversals Introduction Breadth-First Traversal. The Algorithm. Example. Implementation. Depth-First Traversals. Algorithms. Some traversal applications: Connected Components Strongly Connected Components Edge Classification Review Questions.

Introduction A free tree is a connected undirected graph without a cycle. Note: This definition of tree is different from the one of a rooted tree In a free tree |E| = |V| - 1 Example of a free tree: A forest is an acyclic directed or undirected graph consisting of two or more trees The trees in a directed forest are rooted trees The trees in an undirected forest are free trees

Introduction (Cont’d) To traverse a graph is to systematically visit and process each node in the graph exactly once. There are two common graph traversal algorithms that are applicable to both directed and undirected graphs : BreadthFirst Traversal (BFS) DepthFirst Traversal (DFS) PreOrder DepthFirst Traversal PostOrder DepthFirst Traversal Since some graph algorithms do not require all vertices of a graph to be visited we will define both BFS and DFS such that it is possible the algorithms: starting from any vertex, will not visit all vertices if the traversed graph is disconnected starting from a particular vertex, may not visit all vertices if the traversed graph is weakly connected We shall define General traversal algorithms [dfsAllVertices and bfsAllVertices] that will visit each vertex of a graph G in those algorithms that require each vertex to be visited. These algorithms will be of the form: for(i = 0; i < numberOfVertices; i++){ if(vertexi is not visited) dfsPreOrder(G, vertexi); // or dfsPostOrder(G, vertexi) or bsf(G, vertexi) }

Introduction (Cont’d) The BFS and DFS traversal of a graph G is not unique. A traversal depends both on the starting vertex, and on the order of traversing the adjacent vertices of each node.

Breadth-First Traversal Algorithm In this method, After visiting a vertex v, we must visit all its adjacent vertices w1, w2, w3, ..., before going down to the next level to visit vertices adjacent to w1 etc. The method can be implemented using a queue. A boolean array is used to ensure that a vertex is enqueued only once. enqueue the starting vertex while(queue is not empty){ dequeue a vertex v from the queue; visit v. enqueue vertices adjacent to v that were never enqueued; } Note: Adjacent vertices can be enqueued in any order; but to obtain a unique traversal, we will enqueue them in alphabetical order. A BFS traversal of a graph results in a breadth-first tree or in a forest of such trees

Example Breadth-first traversal using a queue. Queue front Order of Queue rear A B D E C G F H I Queue front BFS-tree: Note: The BFS-tree for undirected graph is a free tree

Breadth-First Traversal Implementation public void breadthFirstTraversal(Visitor visitor, Vertex start){ boolean enqueued[] = new boolean[numberOfVertices]; for(int i = 0; i < numberOfVertices; i++) enqueued[i] = false; Queue queue = new QueueAsLinkedList(); enqueued[getIndex(start)] = true; queue.enqueue(start); while(!queue.isEmpty() && !visitor.isDone()) { Vertex v = (Vertex) queue.dequeue(); visitor.visit(v); Iterator it = v.getSuccessors(); while(it.hasNext()) { Vertex to = (Vertex) it.next(); int index = getIndex(to); if(!enqueued[index]) { enqueued[index] = true; queue.enqueue(to); }

Analysis of BFS For a Graph G=(V, E) and n = |V| and m=|E| When Adjacency List is used Complexity is O(m + n) When Adjacency Matrix is used Scanning each row for checking the connectivity of a Vertex is in order O(n). So, Complexity is O(n2)

Depth-First Traversal Algorithm A DFS starting at a vertex v first visits v, then some neighbour w of v, then some neighbour x of w that has not been visited before, etc. When it gets stuck, the DFS backtracks until it finds the first vertex that still has a neighbour that has not been visited before. It continues with this neighbour until it has to backtrack again. Eventually, it will visit all vertices reachable from v Must keep track of vertices already visited to avoid cycles. The method can be implemented using recursion or iteration. The iterative preorder depth-first algorithm is: push the starting vertex onto the stack while(stack is not empty){ pop a vertex off the stack, call it v if v is not already visited, visit it push vertices adjacent to v, not visited, onto the stack } Note: Adjacent vertices can be pushed in any order; but to obtain a unique traversal, we will push them in reverse alphabetical order. A DFS traversal of a graph results in a depth-first tree or in a forest of such trees

Example Depth-first traversal using an explicit stack. Order of The Preorder Depth First Tree: A B C F E G D H I Note: The DFS-tree for undirected graph is a free tree

Recursive preorder Depth-First Traversal Implementation dfsPreorder(v){ visit v; for(each neighbour w of v) if(w has not been visited) dfsPreorder(w); } The following is the code for the recursive preorderDepthFirstTraversal method of the AbstractGraph class: public void preorderDepthFirstTraversal(Visitor visitor, Vertex start) { boolean visited[] = new boolean[numberOfVertices]; for(int v = 0; v < numberOfVertices; v++) visited[v] = false; preorderDepthFirstTraversal(visitor, start, visited); }

Recursive preorder Depth-First Traversal Implementation (cont’d) private void preorderDepthFirstTraversal(Visitor visitor, Vertex v, boolean[] visited) { if(visitor.isDone()) return; visitor.visit(v); visited[getIndex(v)] = true; Iterator p = v.getSuccessors(); while(p.hasNext()) { Vertex to = (Vertex) p.next(); if(! visited[getIndex(to)]) preorderDepthFirstTraversal(visitor, to, visited); }

Recursive preorder Depth-First Traversal Tracing At each stage, a set of unvisited adjacent vertices of the current vertex is generated. The Preorder Depth First Tree:

Recursive postorder Depth-First Traversal Implementation dfsPostorder(v){ mark v; for(each neighbour w of v) if(w is not marked) dfsPostorder(w); visit v; } The following is the code for the recursive postorderDepthFirstTraversal method of the AbstractGraph class: public void postorderDepthFirstTraversal(Visitor visitor, Vertex start) { boolean visited[] = new boolean[numberOfVertices]; for(int v = 0; v < numberOfVertices; v++) visited[v] = false; postorderDepthFirstTraversal(visitor, start, visited); }

Recursive postorder Depth-First Traversal Implementation (cont’d) private void postorderDepthFirstTraversal( Visitor visitor, Vertex v, boolean[] visited) { if(visitor.isDone()) return; // mark v visited[getIndex(v)] = true; Iterator p = v.getSuccessors(); while(p.hasNext()){ Vertex to = (Vertex) p.next(); if(! visited[getIndex(to)]) postorderDepthFirstTraversal(visitor, to, visited); } // visit v visitor.visit(v);

Recursive postorder Depth-First Traversal Tracing At each stage, a set of unmarked adjacent vertices of the current vertex is generated. The PostOrder Depth First Tree:

Analysis of DFS For a Graph G=(V, E) and n = |V| and m=|E| When Adjacency List is used Complexity is O(m + n) When Adjacency Matrix is used Scanning each row for checking the connectivity of a Vertex is in order O(n). So, Complexity is O(n2) DFS uses space O(|V|) in the worst case to store the stack of vertices on the current search path as well as the set of already-visited vertices.

Connected Components (CCs) The connected components of an undirected graph are the separate ``pieces'' of the graph such that there is no connection between the pieces. Connected components can easily be found using depth-first search or breadth-first search. Anything we discover during this search must be part of the same connected component. We then repeat the search from any undiscovered vertex (if one exists) to define the next component, until all vertices have been found: dfsAllVertices(G) { c = 0; /* component number */ for (i = 0; i < numberOfVertices; i++) if (vertexi is not visited) { c++; output("Component ” + c); dfsPreorder(G, vertexi); }

DFS Numbering Let G be a directed or undirected graph. Let Ti be a DFS (Depth First Search) tree. Let d[v] be the discovery time and f[v] be the finishing time of a vertex v of G. The first timestamp d[v] records when v is first discovered, and the second timestamp f[v] records when the search finishes examining v's adjacent vertices. The following algorithm builds DFS tree(s) of a graph G and calculates the discovery and finishing times of each vertex: dfsAllVertices(G){ time = 0; // Let time be a global variable mark each vertex of G as NOT VISITED; for(i = 0; i < numberOfVertices; i++){ if(vertexi is not visited){ Add vertexi to tree Ti; dfsPreOrder(vertexi, G, Ti); } dfsPreOrder(v, G, T){ visit(v); mark v as VISITED; d[v] = ++time; for(each neighbour w of v){ if(w is NOT VISITED){ add edge (v, w) to tree T; dfsPreOrder(w, G, T); f[v] = ++time;

Strongly Connected Components (SCCs) A strongly connected component in  a directed graph G = (V, E) is a maximal set of vertices such that for every pair of vertices u and v in the component,  vertices u and v are reachable from each other. If G = (V, E) is a directed graph, its transpose, GT = (V, ET) is the same as G with all arrows reversed. KOSARAJU’S ALGORITHM FOR FINDING SCCs of a graph G: Call dfsAllVertices(G) to compute finishing time for each vertex Create the transpose of G i.e., GT Call dfsAllVertices(GT) but this time consider the vertices in order of decreasing finish time [i.e, start at the vertex with highest finish time, then next highest etc.] Output the vertices of each tree in the DFS-forest of step 3 as a separate strongly connected component The complexity of this algorithm is twice the time of DFS ( G ) which is O ( | V | + | E | ) Note: G and GT have the same Strongly Connected Components

Strongly Connected Components (Cont’d) We can translate Kosaraju’s algorithm to the following algorithm: Perform a DFS on G, each time a vertex is assigned finish time, push it in a stack S Create GT, the transpose of G Initialize each vertex in GT as not visited while(stack S is not empty){ Pop a vertex v from S If(v is not visited){ Mark v as visited dfsPreOrder(v, GT) // mark each unvisited reachable vertex as visited Output all visited vertices in the above dfs as a strongly connected component }

Strongly Connected Components (Cont’d) Example: Find the SCCs of the graph below. Use A as the start vertex and DFS traversal: Perform dfsAllVertices(G) and compute the discovery and finish times of each vertex, push each finished vertex in a stack: 2. Create GT 3. Perform dfsAllVertices(GT) in decreasing order of finish times obtained in step (1). Pop a vertex from the stack; start a traversal from the vertex if it has not been visited; output all reachable unvisited vertices in each traversal as a strongly connected component DFS(D) = {D, F, G, H}, DFS(C) = {C}, DFS(A) = {A}, DFS(B) = {B, E}

Graph Edge Classification using DFS Let G be a directed graph. Let Ti be a DFS (Depth First Search) tree for G. Note: G may have one or several DFS trees. We can classify edges on G as: • Tree-Edge is an edge in a DFS tree. • Back-Edge is a non-tree edge from a vertex u to a proper ancestor of u in a DFS tree. Note: Self loops in directed graphs are considered to be back edges. • Forward-Edge is a non-tree edge from a vertex u to a proper descendant of u in a DFS tree. • Cross-Edge is a non-tree edge that connects vertices in two different DFS-trees or two vertices in the same DFS-tree neither of which is the ancestor nor the descendant of the other. Note: The way we classify the edges is not unique; it depends on what node we start from and in what order the algorithm happens to select successors to visit. Forward- and Cross-Edges only apply to directed graphs; this implies in a DFS tree of an undirected graph G, every edge of G is either a tree edge or a back edge.

Edge Classification using DFS (Cont’d) Example1: Classify the edges in the following disconnected undirected graph. Use vertex A as the starting vertex. If at any point in the DFS search it is possible to visit more than one vertex, visit the vertices in increasing alphabetical order.

Edge Classification using DFS (Cont’d) Example2: Classify the edges in the following directed graph. Use vertex A as the starting vertex. If at any point in the DFS search it is possible to visit more than one vertex, visit the vertices in increasing alphabetical order.

Edge Classification using DFS (Cont’d) DFS can be modified to classify edges as it encounters them During the traversal the discovery and finish times are computed and vertices are colored as: white: if the vertex is not visited gray: when the vertex is first encountered during the traversal black: if the vertex and all its adjacent vertices have been visited An edge e = (v, w) is classified based on the color of vertex w when e is first explored: Tree edge – if w is white Back edge – if w is gray Forward or cross - if w is black: Forward edge – if w is black and d[v] < d[w] (w was discovered after v) Cross edge – if w is black and d[v] > d[w] (v discovered after w)

Edge Classification using DFS (Cont’d) The DFS Edge classification algorithm for directed graphs: dfsAllVertices(G){ color all vertices white; time = 0; for( each v  V) if (v is white) DFS(v); } DFS(v){ color v gray; d[v] = ++time; [previsit(v)] for (each w adjacent to v){ if(w is white) edge(v, w).Type = “treeEdge”; else if(w is gray) edge(v, w).Type = “backEdge”; else if(d[v] < d[w]) edge(v, w).Type = “forwardEdge”; // w is black else edge(v, w).Type = “crossEdge”; // w is black if (w is white){ [ Add edge (v, w) to DFS tree] DFS(w) f[v] = ++time; [postvisit(v)] color v black;

Edge Classification using DFS (Cont’d) Exercise: Run the DFS edge classification algorithm on the following graph, starting at vertex 1, to obtain the classification given below:

Edge Classification using BFS BFS classifies edges as follows: Directed Graphs tree edges, back edges, and cross edges [There are no forward edges] Undirected Graphs tree edges and cross edges Compare this classification with that of DFS classification: Directed Graphs: tree edges, back edges, forward edges, and cross edges Undirected Graphs: tree edges and back edges BFS has the useful feature that its tree edges from a given vertex s define paths from s that have a minimum number of edges. Exercise: Repeat Example1 and 2 in slides 24 and 25 using BFS

Review Questions 1. Consider a depth-first traversal of the undirected graph GA shown above, starting from vertex a. List the order in which the nodes are visited in a preorder traversal showing the depth-first traversal tree. List the order in which the nodes are visited in a postorder traversal 2. Repeat exercise 1 above for a depth-first traversal starting from vertex d. 3. List the order in which the nodes of the undirected graph GA shown above are visited by a breadth first traversal that starts from vertex a, showing the breadth-first traversal tree. Repeat this exercise for a breadth-first traversal starting from vertex d. 4. Repeat Exercises 1 and 3 for the directed graph GB.