210 likes | 346 Views
Graph Traversals. Depth-First Traversals. Algorithms. Example. Implementation. Breadth-First Traversal. The Algorithm. Example. Implementation. Review Questions. Depth-First Traversal Algorithm.
E N D
Graph Traversals • Depth-First Traversals. • Algorithms. • Example. • Implementation. • Breadth-First Traversal. • The Algorithm. • Example. • Implementation. • 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.
A B C F E G D H I Stack Example • Demonstrate depth-first traversal using an explicit stack.
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;}}}
Queue front Order of Traversal A B D E C G F H I Queue rear Example • Demonstrating breadth-first traversal using a queue.
Example 0 F 1 F 0 2 F 8 3 F 4 F 2 9 5 F 1 6 F 7 F 7 3 8 F 6 9 F 4 5 Adjacency List Visited Table (T/F) source Initialize visited table (all False) { } Q = Initialize Q to be empty
Example 0 F 1 F 0 2 T 8 3 F 4 F 2 9 5 F 1 6 F 7 F 7 3 8 F 6 9 F 4 5 Adjacency List Visited Table (T/F) source Flag that 2 has been visited. { 2 } Q = Place source 2 on the queue.
Example 0 F 1 T 0 2 T 8 3 F 4 T 2 9 5 F 1 6 F 7 F 7 3 8 T 6 9 F 4 5 Adjacency List Visited Table (T/F) Neighbors source Mark neighbors as visited. {2} → { 8, 1, 4 } Q = Dequeue 2. Place all unvisited neighbors of 2 on the queue
Example 0 T 1 T 0 2 T 8 3 F 4 T 2 9 5 F 1 6 F 7 F 7 3 8 T 6 9 T 4 5 Adjacency List Visited Table (T/F) 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 0 T 1 T 0 2 T 8 3 T 4 T 2 9 5 F 1 6 F 7 T 7 3 8 T 6 9 T 4 5 Adjacency List Visited Table (T/F) 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 0 T 1 T 0 2 T 8 3 T 4 T 2 9 5 F 1 6 F 7 T 7 3 8 T 6 9 T 4 5 Adjacency List Visited Table (T/F) Neighbors source { 4, 0, 9, 3, 7 } → { 0, 9, 3, 7 } Q = Dequeue 4. -- 4 has no unvisited neighbors!
Example 0 T 1 T 0 2 T 8 3 T 4 T 2 9 5 F 1 6 F 7 T 7 3 8 T 6 9 T 4 5 Adjacency List Visited Table (T/F) Neighbors source { 0, 9, 3, 7 } → { 9, 3, 7 } Q = Dequeue 0. -- 0 has no unvisited neighbors!
Example 0 T 1 T 0 2 T 8 3 T 4 T 2 9 5 F 1 6 F 7 T 7 3 8 T 6 9 T 4 5 Adjacency List Visited Table (T/F) source Neighbors { 9, 3, 7 } → { 3, 7 } Q = Dequeue 9. -- 9 has no unvisited neighbors!
Example 0 T 1 T 0 2 T 8 3 T 4 T 2 9 5 T 1 6 F 7 T 7 3 8 T 6 9 T 4 5 Adjacency List Visited Table (T/F) Neighbors source Mark new visited Vertex 5. { 3, 7 } → { 7, 5 } Q = Dequeue 3. -- place neighbor 5 on the queue.
Example 0 T 1 T 0 2 T 8 3 T 4 T 2 9 5 T 1 6 T 7 T 7 3 8 T 6 9 T 4 5 Adjacency List Visited Table (T/F) source Neighbors Mark new visited Vertex 6. { 7, 5 } → { 5, 6 } Q = Dequeue 7. -- place neighbor 6 on the queue.
Example 0 T 1 T 0 2 T 8 3 T 4 T 2 9 5 T 1 6 T 7 T 7 3 8 T 6 9 T 4 5 Adjacency List Visited Table (T/F) source Neighbors { 5, 6} → { 6 } Q = Dequeue 5. -- no unvisited neighbors of 5.
Example 0 T 0 1 T 2 T 8 3 T 4 T 2 9 5 T 1 T 6 7 T 7 3 8 T 6 4 9 T 5 Adjacency List Visited Table (T/F) source Neighbors { 6 } → { } Q = Dequeue 6. -- no unvisited neighbors of 6.
Example 0 T 1 T 0 2 T 8 3 T 4 T 2 9 5 T 1 6 T 7 T 7 3 8 T 6 9 T 4 5 Adjacency List Visited Table (T/F) source What did we discover? Look at “visited” tables. There exists a path from source vertex 2 to all vertices in the graph. { } STOP!!! Q is empty!!! Q =