graph traversals n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Graph Traversals PowerPoint Presentation
Download Presentation
Graph Traversals

Loading in 2 Seconds...

play fullscreen
1 / 19

Graph Traversals - PowerPoint PPT Presentation


  • 96 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Graph Traversals' - shyla


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
graph traversals
Graph Traversals
  • Depth-First Traversals.
    • Algorithms.
    • Example.
    • Implementation.
  • Breadth-First Traversal.
    • The Algorithm.
    • Example.
    • Implementation.
  • Review Questions.
depth first traversal algorithm
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

A B C F E G D H I

Stack

Example
  • Demonstrate depth-first traversal using an explicit stack.
breadth first traversal algorithm
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
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;}}}
example1

Queue front

Order of

Traversal

A B D E C G F H I

Queue rear

Example
  • Demonstrating breadth-first traversal using a queue.
example2
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

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

example4
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

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

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

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

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

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

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

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

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

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

example14
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 =