0% found this document useful (0 votes)
23 views

Graph

The document discusses breadth-first search (BFS) and depth-first search (DFS) algorithms for traversing graphs. BFS uses a queue to examine neighboring nodes level-by-level, while DFS uses a stack to explore nodes as deeply as possible first. Both algorithms impose a spanning tree on the graph and use node statuses to track processing state. Pseudocode for BFS and DFS traversal algorithms is provided.

Uploaded by

sunynx
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Graph

The document discusses breadth-first search (BFS) and depth-first search (DFS) algorithms for traversing graphs. BFS uses a queue to examine neighboring nodes level-by-level, while DFS uses a stack to explore nodes as deeply as possible first. Both algorithms impose a spanning tree on the graph and use node statuses to track processing state. Pseudocode for BFS and DFS traversal algorithms is provided.

Uploaded by

sunynx
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Traversing a Graph

Many graph algorithms require one to systematically examine the nodes and edges of a
graph G. There are two standard ways to do this. They are:

Breadth first traversal (BFT)


Depth first traversal (DFT)
The BFT will use a queue as an auxiliary structure to hold nodes for future processing
and the DFT will use a STACK.

During the execution of these algorithms, each node N of G will be in one of three
states, called the status of N, as follows:

1. STATUS = 1 (Ready state): The initial state of the node N.

2. STATUS = 2 (Waiting state): The node N is on the QUEUE or STACK, waiting to


be processed.

3. STATUS = 3 (Processed state): The node N has been processed.

Both BFS and DFS impose a tree (the BFS/DFS tree) on the structure of graph. So, we
can compute a spanning tree in a graph. The computed spanning tree is not a minimum
spanning tree. The spanning trees obtained using depth first search are called depth
first spanning trees. The spanning trees obtained using breadth first search are called
Breadth first spanning trees.

Breadth first search and traversal:

The general idea behind a breadth first traversal beginning at a starting node A is as
follows. First we examine the starting node A. Then we examine all the neighbors of A.
Then we examine all the neighbors of neighbors of A. And so on. We need to keep track
of the neighbors of a node, and we need to guarantee that no node is processed more
than once. This is accomplished by using a QUEUE to hold nodes that are waiting to be
processed, and by using a field STATUS that tells us the current status of any node.
The spanning trees obtained using BFS are called Breadth first spanning trees.

Breadth first traversal algorithm on graph G is as follows:

This algorithm executes a BFT on graph G beginning at a starting node A.

Initialize all nodes to the ready state (STATUS = 1).

1. Put the starting node A in QUEUE and change its status to the waiting
state (STATUS = 2).

2. Repeat the following steps until QUEUE is empty:

a. Remove the front node N of QUEUE. Process N and change the


status of N to the processed state (STATUS = 3).

b. Add to the rear of QUEUE all the neighbors of N that are in the
ready state (STATUS = 1), and change their status to the waiting
state (STATUS = 2).

3. Exit.
Depth first search and traversal:

Depth first search of undirected graph proceeds as follows: First we examine the
starting node V. Next an unvisited vertex 'W' adjacent to 'V' is selected and a depth
first search from 'W' is initiated. When a vertex 'U' is reached such that all its adjacent
vertices have been visited, we back up to the last vertex visited, which has an unvisited
vertex 'W' adjacent to it and initiate a depth first search from W. The search terminates
when no unvisited vertex can be reached from any of the visited ones.

This algorithm is similar to the inorder traversal of binary tree. DFT algorithm is similar
to BFT except now use a STACK instead of the QUEUE. Again field STATUS is used to
tell us the current status of a node.

The algorithm for depth first traversal on a graph G is as follows.

This algorithm executes a DFT on graph G beginning at a starting node A.

5. Initialize all nodes to the ready state (STATUS = 1).

6. Push the starting node A into STACK and change its status to the waiting state
(STATUS = 2).

7. Repeat the following steps until STACK is empty:

Pop the top node N from STACK. Process N and change the status of N to
the processed state (STATUS = 3).

Push all the neighbors of N that are in the ready state (STATUS = 1), and
change their status to the waiting state (STATUS = 2).
8. Exit.

Example 1:

Consider the graph shown below. Traverse the graph shown below in breadth first
order and depth first order.

A Node Adjacency List


A F, C, B
B A, C, G
F C B
C A, B, D, E, F, G
D C, F, E, J
D E G E C, D, G, J, K
F A, C, D

J K A Gra ph G G B, C, E, K
J D, E, K
K E, G, J
Adjacency list for graph G
Breadth-first search and traversal:

The steps involved in breadth first traversal are as follows:

Current Status
QUEUE Processed Nodes
Node
A B C D E F G J K
1 1 1 1 1 1 1 1 1
A 2 1 1 1 1 1 1 1 1
A FCB A 3 2 2 1 1 2 1 1 1
F CBD AF 3 2 2 2 1 3 1 1 1
C BDEG AFC 3 2 3 2 2 3 2 1 1
B DEG AFCB 3 3 3 2 2 3 2 1 1
D EGJ AFCBD 3 3 3 3 2 3 2 2 1
E GJK AFCBDE 3 3 3 3 3 3 2 2 2
G JK AFCBDEG 3 3 3 3 3 3 3 2 2
J K AFCBDEGJ 3 3 3 3 3 3 3 3 2
K EMPTY AFCBDEGJK 3 3 3 3 3 3 3 3 3

For the above graph the breadth first traversal sequence is: A F C B D E G J K.

Depth-first search and traversal:

The steps involved in depth first traversal are as follows:

Current Status
Stack Processed Nodes
Node
A B C D E F G J K
1 1 1 1 1 1 1 1 1
A 2 1 1 1 1 1 1 1 1
A BCF A 3 2 2 1 1 2 1 1 1
F BCD AF 3 2 2 2 1 3 1 1 1
D BCEJ AFD 3 2 2 3 2 3 1 2 1
J BCEK AFDJ 3 2 2 3 2 3 1 3 2
K BCEG AFDJK 3 2 2 3 2 3 2 3 3
G BCE AFDJKG 3 2 2 3 2 3 3 3 3
E BC AFDJKGE 3 2 2 3 3 3 3 3 3
C B AFDJKGEC 3 2 3 3 3 3 3 3 3
B EMPTY AFDJKGECB 3 3 3 3 3 3 3 3 3

For the above graph the depth first traversal sequence is: A F D J K G E C B.

You might also like