Graph Traversal Text Weiss, § 9.6 Depth-First Search Think Stack Breadth-First Search Think Queue.

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

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.
Breadth-First Search Text Read Weiss, § 9.3 (pp ) Breadth-First Search Algorithms.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Traversals A systematic method to visit all nodes in a tree Binary tree traversals: Pre-order: root, left, right In-order: left, root, right Post-order:
Breadth-First and Depth-First Search
Lecture 16: Tree Traversal.
Chapter 5 Decrease and Conquer. Homework 7 hw7 (due 3/17) hw7 (due 3/17) –page 127 question 5 –page 132 questions 5 and 6 –page 137 questions 5 and 6.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
CS 261 – Recitation 9 & 10 Graphs & Final review
Chap 5: Decrease & conquer. Objectives To introduce the decrease-and-conquer mind set To show a variety of decrease-and-conquer solutions: Depth-First.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Dan Grossman Spring 2010.
Graph Traversals Introduction Breadth-First Traversal. The Algorithm.
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.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
Design and Analysis of Algorithms - Chapter 5
Breath First Searching & Depth First Searching C and Data Structures Baojian Hua
Graph Traversals CSC 172 SPRING 2002 LECTURE 26. Traversing graphs Depth-First Search like a post-order traversal of a tree Breath-First Search Less like.
Alyce Brady CS 510: Computer Algorithms Depth-First Graph Traversal Algorithm.
Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.
Depth-first search COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar to pre-order.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Preorder Traversal with a Stack Push the root onto the stack. While the stack is not empty n pop the stack and visit it.
Depth-First Search Lecture 24 COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
2013-T2 Lecture 22 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
Review for Final Andy Wang Data Structures, Algorithms, and Generic Programming.
Lecture 13 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Graph Traversal BFS & DFS. Review of tree traversal methods Pre-order traversal In-order traversal Post-order traversal Level traversal a bc d e f g hi.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
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.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
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 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
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.
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
COSC 2P03 Week 21 Tree Traversals – reminder Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first.
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.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Hello Everyone!!! 1. Tree And Graphs 2 Features of Trees  Tree Nodes Each node have 0 or more children A node have must one parent  Binary tree Tree.
COSC 2P03 Week 21 Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: –push : insert data item onto top of stack –pop : remove data.
Breadth-first and depth-first traversal CS1114
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.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
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.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
Graphs - II CS 2110, Spring Where did David leave that book? 2.
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.
Lecture No.14 Data Structures Dr. Sohail Aslam
Trees Definitions Implementation Traversals K-ary Trees
Binary Tree Traversals
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

Graph Traversal Text Weiss, § 9.6 Depth-First Search Think Stack Breadth-First Search Think Queue

Overview Goal –To systematically visit the nodes of a graph A tree is a directed, acyclic, graph (DAG) If the graph is a tree, –DFS is exhibited by preorder, postorder, and (for binary trees) inorder traversals –BFS is exhibited by level-order traversal

Depth-First Search // recursive, preorder, depth-first search void dfs (Node v) { if (v == null) return; if (v not yet visited) visit&mark(v); // visit node before adjacent nodes for (each w adjacent to v) if (w has not yet been visited) dfs(w); } // dfs

Depth-First Search // recursive, postorder, depth-first search void dfs (Node v) { if (v == null) return; tag(v); // mark v as having been considered for (each w adjacent to v) if (w has not yet been tagged) dfs(w); visit(v); // postorder traversal: visit node after // adjacent nodes } // dfs

Depth-First Search // non-recursive, preorder, depth-first search void dfs (Node v) { if (v == null) return; push(v); while (stack is not empty) { pop(v); if (v has not yet been visited) mark&visit(v); for (each w adjacent to v) if (w has not yet been visited) push(w); } // while } // dfs

Depth-First Search // non-recursive, postorder, depth-first search void dfs (Node v) { // Lex 20 (Do not need to submit) } // dfs

Example Policy: Visit adjacent nodes in increasing index order

Preorder DFS: Start with Node

Preorder DFS: Start with Node Push 5

Preorder DFS: Start with Node Pop/Visit/Mark 5

Preorder DFS: Start with Node Push 2, Push 1

Preorder DFS: Start with Node Pop/Visit/Mark 1

Preorder DFS: Start with Node Push 4, Push 2, Push 0

Preorder DFS: Start with Node Pop/Visit/Mark 0

Preorder DFS: Start with Node Push 7, Push 3

Preorder DFS: Start with Node Pop/Visit/Mark 3

Preorder DFS: Start with Node Push 2

Preorder DFS: Start with Node Pop/Mark/Visit 2

Preorder DFS: Start with Node Pop/Mark/Visit 7

Preorder DFS: Start with Node Push 6

Preorder DFS: Start with Node Pop/Mark/Visit 6

Preorder DFS: Start with Node Pop (don’t visit) 2

Preorder DFS: Start with Node Pop/Mark/Visit 4

Preorder DFS: Start with Node Pop (don’t visit) 2

Preorder DFS: Start with Node Done

Preorder DFS: Start with Node 5 Note: edge (0,3) removed

Depth-First Search Policy: Don’t push nodes twice // non-recursive, preorder, depth-first search void dfs (Node v) { if (v == null) return; push(v); while (stack is not empty) { pop(v); if (v has not yet been visited) mark&visit(v); for (each w adjacent to v) if (w has not yet been visited && not yet stacked) push(w); } // while } // dfs

Preorder DFS (Don’t push nodes twice). Start with Node

Postorder DFS: Start with Node

Breadth-first Search Ripples in a pond Visit designated node Then visited unvisited nodes a distance i away, where i = 1, 2, 3, etc. For nodes the same distance away, visit nodes in systematic manner (eg. increasing index order)

Breadth-First Search // non-recursive, preorder, breadth-first search void bfs (Node v) { if (v == null) return; enqueue(v); while (queue is not empty) { dequeue(v); if (v has not yet been visited) mark&visit(v); for (each w adjacent to v) if (w has not yet been visited && has not been queued) enqueue(w); } // while } // bfs

BFS: Start with Node

BFS: Start with Node

BFS: Node one-away

BFS: Visit 1 and

BFS: Nodes two-away

BFS: Visit 0 and

BFS: Nodes three-away

BFS: Visit nodes 3 and

BFS: Node four-away

BFS: Visit