JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Graph Traversal Algorithm

Graph Traversal Algorithm

In this part of the tutorial we will discuss the techniques by using which, we can traverse all the vertices of the graph.
Traversing the graph means examining all the nodes and vertices of the graph. There are two standard methods by using which, we can traverse the graphs. Lets discuss each one of them in detail.
  • Breadth First Search
  • Depth First Search

Breadth First Search (BFS) Algorithm

Breadth first search is a graph traversal algorithm that starts traversing the graph from root node and explores all the neighbouring nodes. Then, it selects the nearest node and explore all the unexplored nodes. The algorithm follows the same process for each of the nearest node until it finds the goal.
The algorithm of breadth first search is given below. The algorithm starts with examining the node A and all of its neighbours. In the next step, the neighbours of the nearest node of A are explored and process continues in the further steps. The algorithm explores all neighbours of all the nodes and ensures that each node is visited exactly once and no node is visited twice.

Algorithm

  • Step 1: SET STATUS = 1 (ready state)
    for each node in G
  • Step 2: Enqueue the starting node A
    and set its STATUS = 2
    (waiting state)
  • Step 3: Repeat Steps 4 and 5 until
    QUEUE is empty
  • Step 4: Dequeue a node N. Process it
    and set its STATUS = 3
    (processed state).
  • Step 5: Enqueue all the neighbours of
    N that are in the ready state
    (whose STATUS = 1) and set
    their STATUS = 2
    (waiting state)
    [END OF LOOP]
  • Step 6: EXIT

Example

Consider the graph G shown in the following image, calculate the minimum path p from node A to node E. Given that each edge has a length of 1.

Breadth First Search Algorithm 

Solution:

Minimum Path P can be found by applying breadth first search algorithm that will begin at node A and will end at E. the algorithm uses two queues, namely QUEUE1 and QUEUE2QUEUE1 holds all the nodes that are to be processed while QUEUE2 holds all the nodes that are processed and deleted from QUEUE1.
Lets start examining the graph from Node A.
1. Add A to QUEUE1 and NULL to QUEUE2.
  1. QUEUE1 = {A}  
  2. QUEUE2 = {NULL}  
2. Delete the Node A from QUEUE1 and insert all its neighbours. Insert Node A into QUEUE2
  1. QUEUE1 = {B, D}  
  2. QUEUE2 = {A}  
3. Delete the node B from QUEUE1 and insert all its neighbours. Insert node B into QUEUE2.
  1. QUEUE1 = {D, C, F}   
  2. QUEUE2 = {A, B}  
4. Delete the node D from QUEUE1 and insert all its neighbours. Since F is the only neighbour of it which has been inserted, we will not insert it again. Insert node D into QUEUE2.
  1. QUEUE1 = {C, F}  
  2. QUEUE2 = { A, B, D}  
5. Delete the node C from QUEUE1 and insert all its neighbours. Add node C to QUEUE2.
  1. QUEUE1 = {F, E, G}  
  2. QUEUE2 = {A, B, D, C}  
6. Remove F from QUEUE1 and add all its neighbours. Since all of its neighbours has already been added, we will not add them again. Add node F to QUEUE2.
  1. QUEUE1 = {E, G}  
  2. QUEUE2 = {A, B, D, C, F}  
7. Remove E from QUEUE1, all of E's neighbours has already been added to QUEUE1 therefore we will not add them again. All the nodes are visited and the target node i.e. E is encountered into QUEUE2.
  1. QUEUE1 = {G}  
  2. QUEUE2 = {A, B, D, C, F,  E}  
Now, backtrack from E to A, using the nodes available in QUEUE2.
The minimum path will be A → B → C → E.

.