Title: Implement Depth-First Search: Department of Computer Science and Engineering
Title: Implement Depth-First Search: Department of Computer Science and Engineering
2 Problem analysis
Two of the most popular tree traversal algorithms are breadth-first search (BFS) and depth-first search (DFS).
Both methods visit all vertices and edges of a graph; however, they are different in the way in which they
perform the traversal. This difference determines which of the two algorithms is better suited for a specific
purpose.
Adjacency List:
Vertices are labelled (or re-labelled) from 0 to V(G)- 1. Corresponding to each vertex is a list (either an array
or linked list) of its neighbours. Table: 1 represents the adjacency list of figure1.
DFS:
Depth-first Search or Depth-first traversal is a recursive algorithm for searching all the vertices of a graph or tree
data structure. Traversal means visiting all the nodes of a graph. Figure 1 shows the DFS graph traversal.As
in the example given above, the DFS algorithm traverses from S to A to D to G to E to B first, then to F, and
lastly to C. It employs the following rules.
3 Algorithm (DFS)
A standard DFS implementation puts each vertex of the graph into one of two categories:
1. Visited
2. Not Visited
The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.
The DFS algorithm works as follows:
Step 1. Start by putting any one of the graph’s vertices on top of a stack.
Step 2. Take the top item of the stack and add it to the visited list.
Step 3. Create a list of that vertex’s adjacent nodes. Add the ones which aren’t in the visited list to the top of
the stack.
Step 4. Keep repeating steps 2 and 3 until the stack is empty.
4 Flowchart
5 Implementation in Java
1 import java.util.*;
2 public class DFS {
3 static char[] c={’A’,’B’,’C’,’D’,’E’,’F’,’G’,’S’};
4 static int e[]={2,2,2,2,2,2,3,3};
5 static int list[][]={{3,7},{4,7},{5,7},{0,6},{1,6},{2,6},{3,4,5},{0,1,2}};//
adjacency list of graph figure 1.
6 static int[] checked=new int[20];
7 static int[] stk=new int[20];
8 static int top=0;
9
10 public static void main(String[] args) {
11 int i,n,f=0;//f is a flag that tells when to pop
12 push(7);//inserting the first node from where the traversal starts.
13 while(top!=0){//loop will execute till the stack is not empty.
14 n=stk[top−1];
15 for(i=0;i<e[n];i++){
16 f=0;
17 if(notChecked(list[n][i])==1){
18 push(list[n][i]);
19 f=1;
20 break;
21 }
22
23 }
24 if(f==0)
25 pop();
26 }
27 }
28
29 static int notChecked(int n){// this method checks the node visited or not
30 if(checked[n]==1)
31 return 0;
32 return 1;
33 }
34
35
36 static int pop(){// this method is used to pop a node from stack
37 //System.out.print(c[stk[top−1]]+" ");//print popping sequence
38 top−−;
39 return stk[top];
40 }
41
42 static void push(int n){//this method is used to push a node from stack
43 checked[n]=1;
44 System.out.print(c[n]+" ");
45 stk[top]=n;
46 top++;
47 }
48
49 }
7 Lab Task (Please implement yourself and show the output to the
instructor)
1. Write a program to perform DFS traversal on a dynamic graph from user.
2. Write a program to find the path from source to destination using DFS.
8 Lab Exercise (Submit as a report)
• Write a program to perform topological search using BFS.
9 Policy
Copying from internet, classmate, seniors, or from any other source is strongly prohibited. 100% marks will be
deducted if any such copying is detected.