问题
I am trying to repair graphs by deleting one edges. The only problem I am running in to is, when there are multiple cycles in the graph for example: 0 3, 2 3, 0 2, 1 2, 3 1. This can be fixed by extracting 3 1, but how do I let the program no that 3 1 is the edge that has to be removed?
Any suggestions? :)
Formatted code from comment...
...
else if (backedges.Count > 1)
{
foreach (Side side in backedges)
{
Node end = Side.node2;
Node begin = Side.node1;
List<Side> allsidesycle = new List<Side>();
while (begin != Side.node2)
{
end = begin;
begin = begin.pi;
Side be = new Side(begin, end);
allsidescycle.Add(be);
}
回答1:
To find cycles you may want to use breadth first search (bfs).
Using bfs on unweighted (or equally weighted) graph grantees that then the first time a node is visited is the shortest path to that node.
If you visit it for the second time - you have a cycle. To remove it modify the graph by deleting that 2nd edge.
来源:https://stackoverflow.com/questions/51115625/finding-cycles-depth-first-search