Thursday, 10 October 2013


AIM: Write a program using C/C++/Java for implementing the Depth First Search Algorithm

Post by: Sohrab Vakharia

Source Code:
import java.io.*; 
class DepthFirst 

static void dfs1(int a[ ][ ], int m[ ], int i, int n) 

    int j; 
    System.out.println("\t" + (i+1)); 
    m[i] = 1; 
    for(j=0; j<n; j++) 
        if(a[i][j]==1 && m[j]==0) 
            dfs1(a,m,j,n); 

public static void main(String args[]) throws IOException 

    int  n, i, j; 
    System.out.println("How many vertices do you want ? : "); 
    BufferedReader br= new BufferedReader (new InputStreamReader(System.in)); 
     n =Integer.parseInt(br.readLine()); 
    int m[]= new int[n]; 
    int a[][] = new int[n][n]; 
    for (i=0; i<n; i++) 
    { 
        m[i] = 0; 
    } 
    System.out.println("\n\nEnter 1 if edge is present between vertices else 0 if not"); 
    for (i=0; i<n; i++) 
    { 
        System.out.println("\n"); 
        for (j=i; j<n; j++) 
        { 
            System.out.println("Edge between " + (i+1) + " and " +  (j+1)+ " : "); 
            a[i][j] =Integer.parseInt(br.readLine()); 
            a[j][i]=a[i][j]; 
        } 
        a[i][i] = 0; 
    } 
    System.out.println("\nOrder of accessed nodes according to Depth First Search : \t"); 
    for (i=0; i<n; i++) 
        if (m[i]==0) 
            dfs1(a,m,i,n);      

}

Output:

Related Posts :



1 comment: