简单的图中循环删除算法

原文出处

问题:


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)
   end
end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值