Graph Theory Deep
## src//graph/Components.java file code:
package .graph;
import .graph.read.Graph;
/**
* Depth-First Traversal
*/
public class Components {
Graph G;// Reference to the graph
private boolean[] visited;// Records whether a node has been visited during the dfs process
private int ccount;// Records the number of connected components
private int[] id;// Connected component label corresponding to each node
// Depth-first traversal of the graph
void dfs(int v ){
visited=true;
id= ccount;
for(int i: G.adj(v)){
if(!visited)
dfs(i);
}
}
// Constructor, finds the connected components of an unweighted graph
public Components(Graph graph){
// Algorithm initialization
G = graph;
visited =new boolean[G.V()];
id =new int[G.V()];
ccount =0;
for(int i =0; i < G.V(); i ++){
visited=false;
id=-1;
}
// Find the connected components of the graph
for(int i =0; i =0&& v =0&& w < G.V();
return id== id;
}
}
YouTip