Graph Traversals

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

description

Graph Traversals. Depth-First Traversals. Algorithms. Example. Implementation. Breadth-First Traversal. The Algorithm. Example. Implementation. Review Questions. Depth-First Traversal Algorithm. - PowerPoint PPT Presentation

Transcript of Graph Traversals

Page 1: Graph Traversals

Graph Traversals

• Depth-First Traversals.– Algorithms.– Example.– Implementation.

• Breadth-First Traversal.– The Algorithm.– Example.– Implementation.

• Review Questions.

Page 2: Graph Traversals

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.

Page 3: Graph Traversals

Example

• Demonstrate depth-first traversal using an explicit stack.

StackA B C F E G D H I

Page 4: Graph Traversals

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. 5 enqueue vertices adjacent to v that were never enqueued;6 }

• Note: Adjacent vertices can be enqueued in any order; but to obtain a unique

traversal, we will enqueue them in alphabetical order.

Page 5: Graph Traversals

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

Page 6: Graph Traversals

Example

• Demonstrating breadth-first traversal using a queue.

Order ofTraversal Queue rearA B D E C G F H I

Queue front

Page 7: Graph Traversals

Example

2

4

3

5

1

76

9

8

0

Adjacency List

source

9

8

7

6

5

4

3

2

1

0

Visited Table (T/F)

F

F

F

F

F

F

F

F

F

F

Q = { }

Initialize visitedtable (all False)

Initialize Q to be empty

Page 8: Graph Traversals

Example

2

4

3

5

1

76

9

8

0

Adjacency List

source

9

8

7

6

5

4

3

2

1

0

Visited Table (T/F)

F

F

F

F

F

F

F

T

F

F

Q = { 2 }

Flag that 2 has been visited.

Place source 2 on the queue.

Page 9: Graph Traversals

Example

2

4

3

5

1

76

9

8

0

Adjacency List

source

9

8

7

6

5

4

3

2

1

0

Visited Table (T/F)

F

T

F

F

F

T

F

T

T

F

Q = {2} → { 8, 1, 4 }

Mark neighborsas visited.

Dequeue 2. Place all unvisited neighbors of 2 on the queue

Neighbors

Page 10: Graph Traversals

Example

2

4

3

5

1

76

9

8

0

Adjacency List

source

9

8

7

6

5

4

3

2

1

0

Visited Table (T/F)

T

T

F

F

F

T

F

T

T

T

Q = { 8, 1, 4 } → { 1, 4, 0, 9 }

Mark new visitedNeighbors.

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!

Neighbors

Page 11: Graph Traversals

Example

2

4

3

5

1

76

9

8

0

Adjacency List

source

9

8

7

6

5

4

3

2

1

0

Visited Table (T/F)

T

T

T

F

F

T

T

T

T

T

Q = { 1, 4, 0, 9 } → { 4, 0, 9, 3, 7 }

Mark new visitedNeighbors.

Dequeue 1. -- Place all unvisited neighbors of 1 on the queue. -- Only nodes 3 and 7 haven’t been visited yet.

Neighbors

Page 12: Graph Traversals

Example

2

4

3

5

1

76

9

8

0

Adjacency List

source

9

8

7

6

5

4

3

2

1

0

Visited Table (T/F)

T

T

T

F

F

T

T

T

T

T

Q = { 4, 0, 9, 3, 7 } → { 0, 9, 3, 7 }

Dequeue 4. -- 4 has no unvisited neighbors!

Neighbors

Page 13: Graph Traversals

Example

2

4

3

5

1

76

9

8

0

Adjacency List

source

9

8

7

6

5

4

3

2

1

0

Visited Table (T/F)

T

T

T

F

F

T

T

T

T

T

Q = { 0, 9, 3, 7 } → { 9, 3, 7 } Dequeue 0. -- 0 has no unvisited neighbors!

Neighbors

Page 14: Graph Traversals

Example

2

4

3

5

1

76

9

8

0

Adjacency List

source

9

8

7

6

5

4

3

2

1

0

Visited Table (T/F)

T

T

T

F

F

T

T

T

T

T

Q = { 9, 3, 7 } → { 3, 7 } Dequeue 9. -- 9 has no unvisited neighbors!

Neighbors

Page 15: Graph Traversals

Example

2

4

3

5

1

76

9

8

0

Adjacency List

source

9

8

7

6

5

4

3

2

1

0

Visited Table (T/F)

T

T

T

F

T

T

T

T

T

T

Q = { 3, 7 } → { 7, 5 } Dequeue 3. -- place neighbor 5 on the queue.

Neighbors

Mark new visitedVertex 5.

Page 16: Graph Traversals

Example

2

4

3

5

1

76

9

8

0

Adjacency List

source

9

8

7

6

5

4

3

2

1

0

Visited Table (T/F)

T

T

T

T

T

T

T

T

T

T

Q = { 7, 5 } → { 5, 6 } Dequeue 7. -- place neighbor 6 on the queue.

Neighbors

Mark new visitedVertex 6.

Page 17: Graph Traversals

Example

2

4

3

5

1

76

9

8

0

Adjacency List

source

9

8

7

6

5

4

3

2

1

0

Visited Table (T/F)

T

T

T

T

T

T

T

T

T

T

Q = { 5, 6} → { 6 } Dequeue 5. -- no unvisited neighbors of 5.

Neighbors

Page 18: Graph Traversals

Example

2

4

3

5

1

76

9

8

0

Adjacency List

source

9

8

7

6

5

4

3

2

1

0

Visited Table (T/F)

T

T

T

T

T

T

T

T

T

T

Q = { 6 } → { } Dequeue 6. -- no unvisited neighbors of 6.

Neighbors

Page 19: Graph Traversals

Example

2

4

3

5

1

76

9

8

0

Adjacency List

source

9

8

7

6

5

4

3

2

1

0

Visited Table (T/F)

T

T

T

T

T

T

T

T

T

T

Q = { }

STOP!!! Q is empty!!!

What did we discover?

Look at “visited” tables.

There exists a path from sourcevertex 2 to all vertices in the graph.