Label an edge with number of cycles it participates in

Aidenhjj :

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?

Josef Ginerman :

It is hard to give you a complete answer without seeing your whole code, but I think this will help. Note that the links provided are for undirected graphs.

I think you should divide the problem in two:

1. Finding all the cycles in a graph (save them on a hash-table or similar)

2. Finding which of those cycles contain certain node.

Solution for 1: For the first step there are a lot of algorithms on-line, like this one that would work with minor tweaks or this one that counts the number of cycles and you can change it to save the cycles it finds.

Solution for 2: This depends on how you save the cycles, but it's a simple search algorithm.

Note that this solution is not optimal if you only want to find the answer for one node one time, but it is really good if you want to be able to find the number of cycles for any given node and any given time.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=77897&siteId=1