I want to find all the connected components in a graph. which is different from Solution For example my graph is as follows:
I want to find the connected component with the minimum cost.
I am trying to use DFS to find the connected component and then add those component cost which covers all the vertices in an array find minimum of them and return it.
But when I use DFS I always get the "path" instead of "connected component" that covers all the vertices.
Example:
Component with edges A-B,A-C,A-E,B-E having sum 10 is not coming as the output for my code as it is giving the paths. Below is my recursive code.
public void dfs(int node,int[][] visited,int[] v,int p){
//System.out.println("DFS call on node "+node+" with pathsum "+p);
//DFS call on Node "node" so making this node visited
v[node]=1;
//Checking whether the all the nodes in the graph are vsited.
//if yes add this path sum to array.
if(wp(v)){
System.out.println("Adding path sum "+p);
res.add(p);
return;
}
for(int i=0;i<n;i++){
//calling dfs on all the non visited nodes which have a path to them from node "node"
if(v[i]==0 && visited[node][i]!=0){
int[] cl=v.clone();
//p=p+visited[node][i];
dfs(i,visited,cl,p+visited[node][i]);
}
}
return;
}
Can anyone help me out for this.