Graph Traversals General Traversal Algorithm Depth-First Traversals. –Algorithms. –Example. –Implementation. Breadth-First Traversal. –The Algorithm. –Example.

Slides:



Advertisements
Similar presentations
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
Advertisements

CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Breadth-First Search Text Read Weiss, § 9.3 (pp ) Breadth-First Search Algorithms.
Binary Tree Traversals
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Chapter 8, Part I Graph Algorithms.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 27 Graph Applications.
GRAPHS AND GRAPH TRAVERSALS 9/26/2000 COMP 6/4030 ALGORITHMS.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Graph Traversals Introduction Breadth-First Traversal. The Algorithm.
Topological Sort Introduction. Definition of Topological Sort.
Testing for Connectedness and Cycles
1 Testing for Connectedness and Cycles Connectedness of Undirected Graphs Implementation of Connectedness detection Algorithm for Undirected Graphs. Implementation.
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
Graph Traversals Depth-First Traversal. The Algorithm.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Graph Traversals Depth-First Traversals. –Algorithms. –Example. –Implementation. Breadth-First Traversal. –The Algorithm. –Example. –Implementation. Review.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
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.
Testing for Connectedness and Cycles Connectedness of an Undirected Graph Implementation of Connectedness detection Algorithm. Implementation of Strong.
Preorder Traversal with a Stack Push the root onto the stack. While the stack is not empty n pop the stack and visit it.
Topological Sort Introduction. Definition of Topological Sort. Topological Sort is Not Unique. Topological Sort Algorithm. An Example. Implementation.
Search Related Algorithms. Graph Code Adjacency List Representation:
U n i v e r s i t y o f H a i l ICS 202  2011 spring  Data Structures and Algorithms  1.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
Trees – Part 2 CS 367 – Introduction to Data Structures.
Graphs. Made up of vertices and arcs Digraph (directed graph) –All arcs have arrows that give direction –You can only traverse the graph in the direction.
Graphs – Part II CS 367 – Introduction to Data Structures.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
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.
Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
7. Graph Traversal Describe and compare depth-first and breadth-first graph searching, and look at the creation of spanning trees Contest Algorithms: 7.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Minimum Spanning Tree What is a Minimum Spanning Tree.
Graph Traversal Text Weiss, § 9.6 Depth-First Search Think Stack Breadth-First Search Think Queue.
Backtracking Algorithm Depth-First Search Text Read Weiss, § 9.6 Depth-First Search and § 10.5 Backtracking Algorithms.
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
SEARCHING GRAPHS Chapter INTRO  in our tree discussion, learned three ways of traversing a graph: –Preorder (prefix) –Inorder (infix) –Postorder.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Breadth-First Search (BFS)
Minimum Spanning Tree What is a Minimum Spanning Tree.
CS212: Data Structures and Algorithms
Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals
Csc 2720 Instructor: Zhuojun Duan
Graph Traversals Depth-First Traversals. Algorithms. Example.
Graph Traversals Depth-First Traversals. Algorithms. Example.
Testing for Connectedness and Cycles
Topological Sort Introduction. Definition of Topological Sort.
Graph Traversals Depth-First Traversals. Algorithms. Example.
COMP171 Depth-First Search.
Depth-First Search CSE 2011 Winter April 2019.
Topological Sort Introduction. Definition of Topological Sort.
Topological Sort Introduction. Definition of Topological Sort.
Topological Sort Introduction. Definition of Topological Sort.
Graph Traversals Depth-First Traversals. Algorithms. Example.
Presentation transcript:

Graph Traversals General Traversal Algorithm Depth-First Traversals. –Algorithms. –Example. –Implementation. Breadth-First Traversal. –The Algorithm. –Example. –Implementation. Review Questions.

General Traversal Algorithm This algorithm ensures that all vertices are visited in the graph. This is certainly important in the case where the graph is disconnected (in the case of undirected graphs) or not strongly connected (in the case of directed graphs) The method call doTraverse is replaced with all the subsequent traversal methods discussed. dfsPreorder, dfsPostorder, BreadthFirst. GraphTraversal (Graph G) 1 for each vertex v  G do 2 mark v as unvisited; 3 for each vertex v  G do 4 if v is marked as unvisited 5 doTraverse(G, v);

Depth-First Traversal Algorithm In this method, After visiting a vertex v, which is adjacent to w1, w2, w3,...; Next we visit one of v's adjacent vertices, w1 say. Next, we visit all vertices adjacent to w1 before coming back to w2, etc. 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: 1push the starting vertex, v, onto the stack; 2mark v as visited; 3while(stack is not empty) { 4 pop vertex v off the stack; 5 visit v; 6for each vertex w adjacent to v that is not marked visited do { 7 mark w as visited; 8 push w onto the stack;} 9} Note: Adjacent vertices can be pushed in any order; but to obtain a unique traversal, we will push them in reverse alphabetical order.

Example Demonstrates depth-first traversal using an explicit stack. Order of Traversal Stack A B C F D G H I E

Recursive preorder Depth-First Traversal Implementation 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); } dfsPreorder(G, v){ visit v; mark v as visited ; for each adjacent vertex w of v do if (w has not been marked as visited) { dfsPreorder(G, w); }

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 Implementation (cont’d) ABC DEF GHI The Preorder Depth First Traversal Tree is shown below:

Recursive postorder Depth-First Traversal Implementation 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); } dfsPostorder(G, v){ mark v as visited; for(each neighbour w of v) if(w is not marked visited) dfsPostorder(G, w); visit v; } The following is the code for the recursive postorderDepthFirstTraversal method of the AbstractGraph class:

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 Implementation (cont’d) ABC DEF GHI The Postorder Depth First Traversal Tree is shown below:

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 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. BreadthFirst(G, v) 1 enqueue the starting vertex v; mark it as visited; 2 while(queue is not empty){ 3 dequeue a vertex v from the queue; 4 visit v. 5 enqueue vertices adjacent to v that are not marked visited; 6} Note: Adjacent vertices can be enqueued in any order; but to obtain a unique traversal, we will enqueue them in alphabetical order.

Example Demonstrating breadth-first traversal using a queue. Order of Traversal Queue rear A B D E C G F H I Queue front ABC DEF GHI The Breadth- firstTraversal Tree is shown below:

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); }

Review Questions 1. Considera 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.