Graph Traversals Depth-First Traversals. Algorithms. Example.

Slides:



Advertisements
Similar presentations
Breadth First Search AB F I EH DC G FIFO Queue - front.
Advertisements

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs - II CS 2110, Spring Where did I leave that book?
Breadth-First Search Text Read Weiss, § 9.3 (pp ) Breadth-First Search Algorithms.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
1 Breadth First Search AB F I EH DC G FIFO Queue - front.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Graph Traversals Introduction Breadth-First Traversal. The Algorithm.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Alyce Brady CS 510: Computer Algorithms Breadth-First Graph Traversal Algorithm.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
Graph Traversals Depth-First Traversal. The Algorithm.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
COMP171 Depth-First Search.
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.
Graph Traversals General Traversal Algorithm Depth-First Traversals. –Algorithms. –Example. –Implementation. Breadth-First Traversal. –The Algorithm. –Example.
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.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
Breadth First Search (BFS) Part 2 COMP171. Graph / Slide 2 Shortest Path Recording * BFS we saw only tells us whether a path exists from source s, to.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
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.
Search Related Algorithms. Graph Code Adjacency List Representation:
Graphs – Part II CS 367 – Introduction to Data Structures.
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.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Breadth-First Search (BFS)
Graphs A New Data Structure
CSE 373 Data Structures and Algorithms
Chapter 28 Graphs and Applications
CS212: Data Structures and Algorithms
Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals
CSE 373 Topological Sort Graph Traversals
Data Structures 13th Week
Graph Traversals Some algorithms require that every vertex of a graph be visited exactly once. The order in which the vertices are visited may be important,
Chapter 15 Lists Objectives
Csc 2720 Instructor: Zhuojun Duan
Graph Search Lecture 17 CS 2110 Fall 2017.
Depth-First Search.
Data Structures and Algorithms for Information Processing
Graph Traversals Depth-First Traversals. Algorithms. Example.
CSC 172 DATA STRUCTURES.
Graph Traversals Depth-First Traversals. Algorithms. Example.
Graph Search Lecture 17 CS 2110 Spring 2018.
Graph & BFS.
Search Related Algorithms
Can you get there from here?
6.1.3 Graph representation.
Graphs.
Depth-First Search D B A C E Depth-First Search Depth-First Search
Graphs Part 2 Adjacency Matrix
CSE 373 Data Structures Lecture 16
Spanning Trees Longin Jan Latecki Temple University based on slides by
COMP171 Depth-First Search.
Breadth First Search - A B C D E F G H I front FIFO Queue.
Depth-First Search CSE 2011 Winter April 2019.
Graph Traversal Lecture 18 CS 2110 — Spring 2019.
Depth-First Search CSE 2011 Winter April 2019.
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
CMSC 341 Graphs.
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 Depth-First Traversals. Algorithms. Example. Implementation. Breadth-First Traversal. The Algorithm. 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. Algorithm:- #define TRUE 1 #define FALSE 0 short int visited[20]; void dfs(int v,GNODEPTR *graph) { GNODEPTR w; visited[v]=TRUE; printf(“%d\t”,v); for(w=graph[v];w;w=w->next) if(!visited[w->vertex]) dfs(w->vertex,graph); } Note: Adjacent vertices can be pushed in any order; but to obtain a unique traversal, we will push them in reverse alphabetical order.

Example Demonstrate depth-first traversal using an explicit stack. A B C F E G D H I

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 while(queue is not empty){ 3 dequeue a vertex v from the queue; 4 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.

Bfs algorithm void bfs(int v,GNODEPTR *graph){ GNODEPTR q[20],w; int front=0,rear=-1; printf(“%d\t”,v); visited[v]=TRUE; addq(&front,&rear,q,graph[v]); while(front<=rear) { w=deleteq(&front,&rear,q); for(; w; w=w->next) If(!visited[w->vertex]){printf(“%d\t”,w->vertex); addq(&front,&rear,q,graph[w->vertex]); visited[w->vertex]=TRUE;}}}

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

Example Adjacency List Visited Table (T/F)‏ 2 4 3 5 1 7 6 9 8 F 2 4 3 5 1 7 6 9 8 source Initialize visited table (all False)‏ { } Q = Initialize Q to be empty

Example Adjacency List Visited Table (T/F)‏ 8 2 9 1 7 3 6 4 5 F T 2 4 3 5 1 7 6 9 8 source Flag that 2 has been visited. { 2 } Q = Place source 2 on the queue.

Example Adjacency List Visited Table (T/F)‏ 8 2 9 1 7 3 6 4 5 F T 2 4 3 5 1 7 6 9 8 Neighbors source Mark neighbors as visited. Q = {2} → { 8, 1, 4 } Dequeue 2. Place all unvisited neighbors of 2 on the queue

Example Adjacency List Visited Table (T/F)‏ 2 4 3 5 1 7 6 9 8 T F 2 4 3 5 1 7 6 9 8 source Neighbors Mark new visited Neighbors. { 8, 1, 4 } → { 1, 4, 0, 9 } Q = Dequeue 8. -- Place all unvisited neighbors of 8 on the queue. -- Notice that 2 is not placed on the queue again, it has been visited!

Example Adjacency List Visited Table (T/F)‏ 8 2 9 1 7 3 6 4 5 T F 2 4 3 5 1 7 6 9 8 Neighbors source Mark new visited Neighbors. { 1, 4, 0, 9 } → { 4, 0, 9, 3, 7 } Q = Dequeue 1. -- Place all unvisited neighbors of 1 on the queue. -- Only nodes 3 and 7 haven’t been visited yet.

Example Adjacency List Visited Table (T/F)‏ 8 2 9 1 7 3 6 4 5 T F 2 4 3 5 1 7 6 9 8 Neighbors source { 4, 0, 9, 3, 7 } → { 0, 9, 3, 7 } Q = Dequeue 4. -- 4 has no unvisited neighbors!

Example Adjacency List Visited Table (T/F)‏ 8 2 9 1 7 3 6 4 5 T F Neighbors 2 4 3 5 1 7 6 9 8 source { 0, 9, 3, 7 } → { 9, 3, 7 } Q = Dequeue 0. -- 0 has no unvisited neighbors!

Example Adjacency List Visited Table (T/F)‏ 8 2 9 1 7 3 6 4 5 T F 2 4 3 5 1 7 6 9 8 source Neighbors { 9, 3, 7 } → { 3, 7 } Q = Dequeue 9. -- 9 has no unvisited neighbors!

Example Adjacency List Visited Table (T/F)‏ 8 2 9 1 7 3 6 4 5 T F 2 4 3 5 1 7 6 9 8 Neighbors source Mark new visited Vertex 5. { 3, 7 } → { 7, 5 } Q = Dequeue 3. -- place neighbor 5 on the queue.

Example Adjacency List Visited Table (T/F)‏ 8 2 9 1 7 3 6 4 5 T 2 4 3 5 1 7 6 9 8 source Neighbors Mark new visited Vertex 6. { 7, 5 } → { 5, 6 } Q = Dequeue 7. -- place neighbor 6 on the queue.

Example Adjacency List Visited Table (T/F)‏ 8 2 9 1 7 3 6 4 5 T 2 4 3 5 1 7 6 9 8 source Neighbors { 5, 6} → { 6 } Q = Dequeue 5. -- no unvisited neighbors of 5.

Example Adjacency List Visited Table (T/F)‏ 8 2 9 1 7 3 6 4 5 T 2 4 3 5 1 7 6 9 8 source Neighbors { 6 } → { } Q = Dequeue 6. -- no unvisited neighbors of 6.

Example STOP!!! Q is empty!!! Adjacency List Visited Table (T/F)‏ 2 4 9 8 7 6 5 4 3 2 1 T 2 4 3 5 1 7 6 9 8 source What did we discover? Look at “visited” tables. There exists a path from source vertex 2 to all vertices in the graph. { } Q = STOP!!! Q is empty!!!