Finding cycles Depth-first-search

落爺英雄遲暮 提交于 2019-12-11 23:17:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!