Graph Traversals Depth-First Traversal. The Algorithm.

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

Binary Tree Traversals
Data Structures Using C++ 2E1 Topic of this lecture: Topological Order OU CS prerequisite bubble chart
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
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)
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Minimum Spanning Tree What is a Minimum Spanning Tree. Constructing a Minimum Spanning Tree. Prim’s Algorithm. –Animated Example. –Implementation. Kruskal’s.
Graph Traversals Introduction Breadth-First Traversal. The Algorithm.
Testing for Connectedness and Cycles
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
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.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Graph Traversals Depth-First Traversals. –Algorithms. –Example. –Implementation. Breadth-First Traversal. –The Algorithm. –Example. –Implementation. Review.
Shortest Path Algorithm What is the Shortest Path Problem? Is the shortest path problem well defined? The Dijkstra's Algorithm for Shortest Path Problem.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Graph Traversals General Traversal Algorithm Depth-First Traversals. –Algorithms. –Example. –Implementation. Breadth-First Traversal. –The Algorithm. –Example.
1 Binary Tree Traversals Binary Tree Traversal classification Tree traversal animations DepthFirst traversal abstraction Accept method of AbstractTree.
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.
Minimum Spanning Tree What is a Minimum Spanning Tree. Constructing a Minimum Spanning Tree. What is a Minimum-Cost Spanning Tree. Prim’s Algorithm. –Animated.
Testing for Connectedness & Cycles Connectedness of an Undirected Graph Implementation of Connectedness detection Algorithm. Implementation of Strong Connectedness.
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
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.
Data Structures and Algorithms Ver. 1.0 Session 17 Objectives In this session, you will learn to: Implement a graph Apply graphs to solve programming problems.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
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.
SLIDES ADAPTED FROM ALEX MARIAKAKIS, WITH MATERIAL FROM KRYSTA YOUSOUFIAN, MIKE ERNST, KELLEN DONOHUE Section 5: HW6 and Interfaces Justin Bare and Deric.
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.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Minimum Spanning Tree What is a Minimum Spanning Tree.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: Graphs Data Structures.
Breadth-first and depth-first traversal CS1114
Graph Traversal Text Weiss, § 9.6 Depth-First Search Think Stack Breadth-First Search Think Queue.
Lecture 9: Graphs & Graph Models. Definition of a Graph edge vertex cycle path.
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.
CC 215 DATA STRUCTURES TREES TRAVERSALS Dr. Manal Helal - Fall 2014 Lecture 9 AASTMT Engineering and Technology College 1.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Graphs - II CS 2110, Spring Where did David leave that book? 2.
© 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.
Queues By Jimmy M. Lu. Overview Definition Standard Java Queue Operations Implementation Queue at Work References.
Breadth-First Search (BFS)
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
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
Graph Traversals Depth-First Traversals. Algorithms. Example.
Binary Tree Traversals
Binary Tree Traversals
Depth-First Search CSE 2011 Winter April 2019.
Binary Tree Traversals
Graph Traversals Depth-First Traversals. Algorithms. Example.
Presentation transcript:

Graph Traversals Depth-First Traversal. The Algorithm. Animated Example. Implementation. Breadth-First Traversal. Review Questions.

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 stack. The algorithm using stack is as follows: 1 Push the starting vertex onto the stack 2 Repeat the following until the stack is empty 3 pop a vertex off the stack, call it v 4 if v is not already visited, visit it 5 push vertices adjacent to v, not visited, onto the stack

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

Depth-First Traversal Implementation The following shows a code for the depthFirstTraversal method of the AbstractGraph class, using recursion. It involves two methods, one public, shown below, and the other private, shown in the next page. The public method accepts any PrePostVisitor and the number of the starting vertex. 1 public abstract class AbstractGraph extends 2 AbstractContainer implements Graph { 3 4 public void depthFirstTraversal(PrePostVisitor visitor, 5 int start) { 6 boolean[] visited = new boolean [numberOfVertices]; 7 for (int v = 0; v < numberOfVertices; ++v) 8 visited [v] = false; 9 depthFirstTraversal(visitor, vertex[start], visited); 10 }

Depth-First Traversal Implementation - Cont'd This method recursively visits all the vertices in the depth-first order. 11 private void depthFirstTraversal(PrePostVisitor visitor, 12 Vertex v, boolean[] visited) { 13 if (visitor.isDone ()) 14 return; 15 visitor.preVisit (v); 16 visited [v.getNumber ()] = true; 17 Enumeration p = v.getSuccessors (); 18 while (p.hasMoreElements ()) { 19 Vertex to = (Vertex) p.nextElement (); 20 if (!visited [to.getNumber ()]) 21 depthFirstTraversal (visitor, to, visited); 22 } 23 visitor.postVisit (v); 24 } 25 //… 26 }

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. 1 enqueue the starting vertex 2 Repeat the following until the queue is empty. 3 Remove the vertex at the front of the queue, call it v. 4 visit v. 5 enqueue vertices adjacent to v that were never enqueued.

Animated Example Demonstrating breadth-first traversal using a queue. Order of Traversal Queue rear A B D E C G F H I Queue front

Breadth-First Traversal Implementation The following gives the code for the BreadthFirstTraversal method of the AbstractGraph. A boolean-valued array, enqueued, is used to keep track of the vertices enqueued. The array is initialized to false, then a queue is created and the starting vertex in enqueued. 1 public abstract class AbstractGraph extends 2 AbstractContainer implements Graph { 3 4 public void breadthFirstTraversal(Visitor visitor, 5 int start) { 6 boolean[] enqueued = new boolean[numberOfVertices]; 7 for (int v = 0; v < numberOfVertices; ++v) 8 enqueued [v] = false; 9 Queue queue = new QueueAsLinkedList (); 10 enqueued [start] = true; 11 queue.enqueue (vertex [start]);

Breadth-First Traversal Implementation - Cont'd 12 while (!queue.isEmpty () && !visitor.isDone ()) { 13 Vertex v = (Vertex) queue.dequeue (); 14 visitor.visit (v); 15 Enumeration p = v.getSuccessors (); 16 17 while (p.hasMoreElements ()) { 18 Vertex to = (Vertex) p.nextElement (); 19 if (!enqueued [to.getNumber ()]) { 20 queue.enqueue (to); 21 enqueued [to.getNumber ()] = true; 22 } 23 } 24 } 25 } 26 //… 27 }

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. 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. Repeat this exercise for a breadth-first traversal starting from vertex d. 4. Repeat Exercises 1 and 3 for the directed graph GB.