简体   繁体   English

循环有向图的遍历

[英]Traversal of cyclic directed graph

I have a cyclic directed graph. 我有一个循环有向图。 Starting at the leaves, I wish to propagate data attached to each node downstream to all nodes that are reachable from that node. 从叶子开始,我希望将附加到每个节点下游的数据传播到可从该节点到达的所有节点。 In particular, I need to keep pushing data around any cycles that are reached until the cycles stabilise. 特别是,我需要在周期稳定的任何周期内继续推送数据。

I'm completely sure that this is a stock graph traversal problem. 我完全相信这是一个股票图遍历问题。 However, I'm having a fair bit of difficulty trying to find a suitable algorithm --- I think I'm missing a few crucial search keywords. 但是,我在寻找合适的算法时遇到了一些困难 - 我想我缺少一些关键的搜索关键字。

Before I attempt to write my own half-assed O(n^3) algorithm, can anyone point me at a proper solution? 在我尝试编写自己的半O(n ^ 3)算法之前,有人能指出我一个合适的解决方案吗? And what is this particular problem called? 所谓的这个特殊问题?

Since the graph is cyclic (ie can contain cycles), I would first break it down into strongly connected components. 由于图是循环的(即可以包含循环),我首先会将其分解为强连接组件。 A strongly connected component of a directed graph is a subgraph where each node is reachable from every other node in the same subgraph. 有向图的强连通分量是子图,其中每个节点可从同一子图中的每个其他节点到达。 This would yield a set of subgraphs. 这将产生一组子图。 Notice that a strongly connected component of more than one node is effectively a cycle. 请注意,多个节点的强连接组件实际上是一个循环。

Now, in each component, any information in one node will eventually end up in every other node of the graph (since they are all reachable). 现在,在每个组件中,一个节点中的任何信息最终都会在图的每个其他节点中结束(因为它们都可以访问)。 Thus for each subgraph we can simply take all the data from all the nodes in it and make every node have the same set of data. 因此,对于每个子图,我们可以简单地从其中的所有节点获取所有数据,并使每个节点具有相同的数据集。 No need to keep going through the cycles. 无需继续经历周期。 Also, at the end of this step, all nodes in the same component contains exactly the same data. 此外,在此步骤结束时,同一组件中的所有节点都包含完全相同的数据。

The next step would be to collapse each strongly connected component into a single node. 下一步是将每个强连接组件折叠为单个节点。 As the nodes within the same component all have the same data, and are therefore basically the same, this operation does not really change the graph. 由于同一组件中的节点都具有相同的数据,因此基本相同,因此该操作并未真正更改图形。 The newly created "super node" will inherit all the edges going out or coming into the component's nodes from nodes outside the component. 新创建的“超级节点”将继承从组件外部的节点出来或进入组件节点的所有边缘。

替代文字

Since we have collapsed all strongly connected components, there will be no cycles in the resultant graph (why? because had there been a cycle formed by the resultant nodes, they would all have been placed in the same component in the first place). 由于我们已经折叠了所有强连接组件,结果图中将没有循环(为什么?因为如果由结果节点形成了一个循环,它们将首先放在同一个组件中)。 The resultant graph is now a Directed Acyclic Graph . 结果图现在是有向无环图 There are no cycles, and a simple depth first traversal from all nodes with indegree=0 (ie nodes that have no incoming edges), propagating data from each node to its adjacent nodes (ie its "children"), should get the job done. 没有周期,并且从indegree = 0的所有节点(即没有传入边缘的节点)首先遍历一个简单的深度,将数据从每个节点传播到其相邻节点(即其“子节点”),应该完成工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM