private static Scanner scanner;
    import java.util.Scanner;


public class Dfs {



    public static void dfs(int a[][],int m[],int i,int n)
    {

       int j;
    m[i]=1;
 for ( j = 0; j < n && i<n ; j++ )
       {
          if ( a[i][j] == 1 && m[j]==0)       
          {
             dfs(a,m,j,n);     
          }
       }
 }  
    public static void main(String[] args) {
    int n;
    int sum=0;
    int counter=0;

scanner = new Scanner(System.in);
n=scanner.nextInt();
int [][] graph=new int[n][n];
int i,j;
int m[]=new int[n];

for(i=0;i<n;i++){
    m[i]=0;
}
for(i=0;i<n;i++){
    for(j=0;j<n;j++){
        graph[i][j]=scanner.nextInt();

    }
}

        }
    }

        for(i=0;i<n;i++){
            if(m[i]==0){
                counter++;
            dfs(graph, m,i, n);


            }
        }

    System.out.println(counter);

    }

}

}

i want this programfirst print number of the components of the graph topology and then at other line the tops of in each component topology for example:
input:
3
0 1 0
1 0 0
0 0 0
output:
2
1 2
3

Recommended Answers

All 2 Replies

honestly, I don't entirely understand what you are trying to do.
but besides that: what is your actual question?

the program first get the number of the vertex of the graph and the Adjacency Matrix Representation of the graph then print the number of the components of the graph topology and then in other lines print the vertices in each
component

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.