简单的图中循环删除算法

来源:互联网 发布:天正制图软件 编辑:程序博客网 时间:2024/05/22 17:16

原文出处

问题:


My problem should be pretty simple, given a graph (BGL adjacency_list) is there a simple algorithm to remove cycles? My first attempt was to use the DFS visitor to detect an edge that'd close the cycle and then remove it but I was unable to implement it correctly.
我的问题应该很简单,给定一个图(BGL adjacency_list)有一个简单的算法来消除周期?我的第一次尝试是使用DFS来检测边缘,会关闭周期,然后删除它,但我却无法正确实施。

Any suggestions? Code samples would be best.
有什么建议吗?代码示例将是最好的。

回答1:


Boost is great. It has a depth_first_search method that accepts a visitor.You can see more information about it here.
提升是伟大的。它有一个depth_first_search方法接受一个访客。你可以看到更多关于它的信息。

All you need to do is implement a visitor like this:
你所需要做的就是实现一个这样的访问者:

class CycleTerminator : public boost::dfs_visitor<> {    template <class Edge, class Graph>    void back_edge(Edge e, Graph& g) {        //implement    }};

remembering of course that a back edge is an edge that closes a cycle in the graph.
记住,一个后边缘是一个在图中关闭一个周期的边缘。

回答2:


It's a simple DFS, as you said. Each time you come to a node you visited before, there's a cycle. Just remove the last edge.
这是一个简单的DFS,正如你所说的。每次你来到一个你访问过的节点之前,都有一个周期。刚刚删除最后一个边缘。

A pseudocode in no particular language.
在没有特定语言的伪代码。

void walk(current_node, previous_node)   if visited[current_node]      remove edge between current_node and previous_node      return   end   visited[current_node] = true   for (each adjacent node)       walk(adjacent_node, current_node)   endend
0 0
原创粉丝点击