LECTURE 17 GRAPH TRAVERSALS

Similar documents
LECTURE 11 TREE TRAVERSALS

LECTURE 26 PRIM S ALGORITHM

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017

Breadth First Search. cse2011 section 13.3 of textbook

implementing the breadth-first search algorithm implementing the depth-first search algorithm

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

23 DECOMPOSITION OF GRAPHS

CSC Design and Analysis of Algorithms. Lecture 4 Brute Force, Exhaustive Search, Graph Traversal Algorithms. Brute-Force Approach

Advanced Data Structures and Algorithms

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

Understand graph terminology Implement graphs using

Figure 1. A breadth-first traversal.

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

Unweighted Graphs & Algorithms

lecture27: Graph Traversals

CSE 100: GRAPH SEARCH

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD

08 STACKS DATA STRUCTURES AND ALGORITHMS IMPLEMENTATION & APPLICATIONS IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

To list all vertices connected to a vertex u in graph G we can use the following loop:

Chapter 5. Decrease-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

22.1 Representations of graphs

Breadth First Search. Graph Traversal. CSE 2011 Winter Application examples. Two common graph traversal algorithms

CSE 100: GRAPH ALGORITHMS

Outline. Graphs. Divide and Conquer.

CS 206 Introduction to Computer Science II

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

Graphs and Algorithms

Lecture 8: PATHS, CYCLES AND CONNECTEDNESS

Inf 2B: Graphs, BFS, DFS

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

I A graph is a mathematical structure consisting of a set of. I Formally: G =(V, E), where V is a set and E V V.

Title Description Participants Textbook

CS 206 Introduction to Computer Science II

Graph Search Methods. Graph Search Methods

Module 11: Additional Topics Graph Theory and Applications

Graph Search Methods. Graph Search Methods

Trees. Arash Rafiey. 20 October, 2015

CS24 Week 8 Lecture 1

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

Graphs. Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale Room - Faner 3131

CS350: Data Structures Dijkstra s Shortest Path Alg.

Graphs: Graph Data Structure:

Some major graph problems

Lecture 3: Graphs and flows

I want an Oompa-Loompa! screamed Veruca. Roald Dahl, Charlie and the Chocolate Factory

Graph: representation and traversal

Fundamental Algorithms

Graph Algorithms. Imran Rashid. Jan 16, University of Washington

Homework 2. Sample Solution. Due Date: Thursday, May 31, 11:59 pm

Lecture 24 Notes Search in Graphs

Graph. Vertex. edge. Directed Graph. Undirected Graph

Algorithms and Data Structures Lesson 4

Introduction to Graphs. common/notes/ppt/

Joint Advanced Student School 2007 Martin Dummer

Data Structure Advanced

Mystery Algorithm! ALGORITHM MYSTERY( G = (V,E), start_v ) mark all vertices in V as unvisited mystery( start_v )

Graph Traversals BFS & DFS 1 CS S-16

COMP 250 Fall graph traversal Nov. 15/16, 2017

Plan. CMPSCI 311: Introduction to Algorithms. Recall. Adjacency List Representation. DFS Descriptions. BFS Description

Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis

Figure 1: A directed graph.

CS61BL. Lecture 5: Graphs Sorting

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

Algorithms: Lecture 10. Chalmers University of Technology

Learning Objectives. c D. Poole and A. Mackworth 2010 Artificial Intelligence, Lecture 3.2, Page 1

TIE Graph algorithms

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of MCA

Lecture 26: Graphs: Traversal (Part 1)

CS 361 Data Structures & Algs Lecture 13. Prof. Tom Hayes University of New Mexico

TIE Graph algorithms

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

Data Structures and Algorithms

Uninformed Search Strategies

Graphs Data Structures

Data Structure. IBPS SO (IT- Officer) Exam 2017

Graph Representations and Traversal

Implementing Algorithms

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Computational Optimization ISE 407. Lecture 19. Dr. Ted Ralphs

Fundamentals of Data Structure

22.3 Depth First Search

12 Abstract Data Types

csci 210: Data Structures Graph Traversals

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

CSE 332: Data Abstractions Lecture 15: Topological Sort / Graph Traversals. Ruth Anderson Winter 2013

CS 491 CAP Introduction to Graphs and Search

Algorithm Design and Analysis

Analysis of Algorithms

Applications of BDF and DFS

Abstract Data Types CHAPTER 12. Review Questions

LECTURE 13 BINARY TREES

CSC 8301 Design and Analysis of Algorithms: Graph Traversal

CS4800: Algorithms & Data Jonathan Ullman

GRAPHS Lecture 17 CS2110 Spring 2014

PA3 Design Specification

Definition Example Solution

Transcription:

DATA STRUCTURES AND ALGORITHMS LECTURE 17 GRAPH TRAVERSALS IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD STRATEGIES Traversals of graphs are also called searches We can use either breadth-first or depth-first traversals Breadth-first requires a queue Depth-first requires a stack 2 Lecture 17 - Graph Traversals 1

BREADTH-FIRST TRAVERSAL Consider implementing a breadth-first traversal on a graph: Choose any vertex, mark it as visited and push it onto queue While the queue is not empty: Pop to top vertex v from the queue For each vertex adjacent to v that has not been visited: Mark it visited, and Push it onto the queue This continues until the queue is empty Note: if there are no unvisited vertices, the graph is connected, 3 BREADTH-FIRST TRAVERSAL IMPLEMENTATION Consider implementing a depth-first traversal on a graph: Choose any vertex, mark it as visited From that vertex: If there is another adjacent vertex not yet visited, go to it Otherwise, go back to the most previous vertex that has not yet had all of its adjacent vertices visited and continue from there Continue until no visited vertices have unvisited adjacent vertices Two implementations: Recursive Iterative 4 Lecture 17 - Graph Traversals 2

BREADTH-FIRST TRAVERSAL Consider this graph BREADTH-FIRST TRAVERSAL Performing a breadth-first traversal Push the first vertex onto the queue A Lecture 17 - Graph Traversals 3

BREADTH-FIRST TRAVERSAL Performing a breadth-first traversal Pop A and push B, C and E A B C E BREADTH-FIRST TRAVERSAL Pop B and push D A, B C E D Lecture 17 - Graph Traversals 4

BREADTH-FIRST TRAVERSAL Pop C and push F A, B, C E D F BREADTH-FIRST TRAVERSAL Pop E and push G and H A, B, C, E D F G H Lecture 17 - Graph Traversals 5

BREADTH-FIRST TRAVERSAL Pop D A, B, C, E, D F G H BREADTH-FIRST TRAVERSAL Pop F A, B, C, E, D, F G H Lecture 17 - Graph Traversals 6

BREADTH-FIRST TRAVERSAL Pop G and push I A, B, C, E, D, F, G H I BREADTH-FIRST TRAVERSAL Pop H A, B, C, E, D, F, G, H I Lecture 17 - Graph Traversals 7

BREADTH-FIRST TRAVERSAL Pop I A, B, C, E, D, F, G, H, I BREADTH-FIRST TRAVERSAL The queue is empty: we are finished A, B, C, E, D, F, G, H, I Lecture 17 - Graph Traversals 8

DEPTH-FIRST TRAVERSAL Perform a recursive depth-first traversal on this same graph DEPTH-FIRST TRAVERSAL Visit the first node A Lecture 17 - Graph Traversals 9

DEPTH-FIRST TRAVERSAL A has an unvisited neighbor A, B DEPTH-FIRST TRAVERSAL B has an unvisited neighbor A, B, C Lecture 17 - Graph Traversals 10

DEPTH-FIRST TRAVERSAL C has an unvisited neighbor A, B, C, D DEPTH-FIRST TRAVERSAL D has no unvisited neighbors, so we return to C A, B, C, D, E Lecture 17 - Graph Traversals 11

DEPTH-FIRST TRAVERSAL E has an unvisited neighbor A, B, C, D, E, G DEPTH-FIRST TRAVERSAL F has an unvisited neighbor A, B, C, D, E, G, I Lecture 17 - Graph Traversals 12

DEPTH-FIRST TRAVERSAL H has an unvisited neighbor A, B, C, D, E, G, I, H EXADEPTH-FIRST TRAVERSALMPLE We recurse back to C which has an unvisited neighbour A, B, C, D, E, G, I, H, F Lecture 17 - Graph Traversals 13

DEPTH-FIRST TRAVERSAL We recurse finding that no other nodes have unvisited neighbours A, B, C, D, E, G, I, H, F COMPARISON We recurse finding that no other nodes have unvisited neighbours A, B, C, D, E, G, I, H, F Lecture 17 - Graph Traversals 14

COMPARISON The order in which vertices can differ greatly An iterative depth-first traversal may also be different again A, B, C, E, D, F, G, H, I A, B, C, D, E, G, I, H, F APPLICATIONS Applications of tree traversals include: Determining connectiveness and finding connected sub-graphs Determining the path length from one vertex to all others Testing if a graph is bipartite Determining maximum flow Cheney s algorithm for garbage collection Wikipedia, http://en.wikipedia.org/wiki/graph_traversal http://en.wikipedia.org/wiki/depth-first_search http://en.wikipedia.org/wiki/breadth-first_search 30 Lecture 17 - Graph Traversals 15