问题
I'm trying to code a program that uses Electrical Mesh Analasys. So I have the nodes of the circuit [A,B,C,D] and the branches that links the nodes [0,1,2,3,4,5,6,7,8].
How can I find the shortest cycles in an undirected graph with multi edges like the example bellow?
Graph image (4 nodes, 9 edges/branches):
Cycles:
0-1-0
5-6-5
6-8-6
1-4-2-1
2-7-3-2
4-7-5-4
The number of cycles that I need are: Cycles = Branches - (Nodes - 1), in this case I need 6 cycles.
I have this data stored in arrays like this:
realNodes = [A,B,C,D];
groupBranches = [[0,1,4,5,6,8], [3,5,6,7,8], [0,1,2,3], [2,4,7]];
// groupArray[0] stores the branch numbers connected to A;
// groupArray[1] stores the branch numbers connected to B;
// groupArray[2] stores the branch numbers connected to C;
// groupArray[3] stores the branch numbers connected to D;
Notes:
I don't need all the possible cycles, I just need to use every single edge (branch) in some cycle.
Also, the final cycles can vary from the presented in the example.
I would be happy to see an algorithm in any language.
回答1:
You can make a spanning tree using any algorithm you like (BFS works).
Then, for every edge that's not in the tree, you make a cycle containing that edge plus the path through the tree from one end to the other.
来源:https://stackoverflow.com/questions/55482573/cycle-enumeration-of-an-undirected-graph-with-multi-edges