Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graph Traversals Depth-First Traversals. Algorithms. Example.

Similar presentations


Presentation on theme: "Graph Traversals Depth-First Traversals. Algorithms. Example."— Presentation transcript:

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

2 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.

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

4 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){ dequeue a vertex v from the queue; 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.

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

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

7 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

8 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.

9 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

10 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!

11 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.

12 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!

13 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!

14 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!

15 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.

16 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.

17 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.

18 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.

19 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!!!


Download ppt "Graph Traversals Depth-First Traversals. Algorithms. Example."

Similar presentations


Ads by Google