Given a graph G = (V, E)
, using DFS, how do I label each edge with the number of simple cycles it participates in? I am already labeling the nodes with a post-order when I extract the strongly connected components from the graph, so maybe I can use that information somehow.
private Integer labelEdges(Node currentNode, Set<Node> component) {
Integer numLoops = 0;
currentNode.onStack = true;
for (Edge outEdge : currentNode.getEdges()) {
Node nextNode = outEdge.getEnd();
if (component.contains(nextNode)) {
if (nextNode.onStack) {
// loop
numLoops += 1;
}
else {
numLoops += labelEdges(nextNode, component);
}
outEdge.cycles = numLoops;
}
}
currentNode.onStack = false;
return numLoops;
}
I can't seem to reason clearly about this. Can anyone point me in the right direction?