用于在图中查找闭合路径的伪代码
我有一个图的邻接矩阵,它通过在相应的 adjMat[i,j] = 1 中包含 1 来跟踪节点之间的边; 通过这个邻接矩阵,我希望找出图中存在的所有长度为 4 的闭合路径。 谁能给我提供一个伪代码。 感谢你
I have an adjaceny matrix for a graph which tracks the edges between the nodes by having a 1 in the corresponding adjMat[i,j] = 1;
Through this adjaceny matrix i wish to find out all the closed paths of length 4 which exists in the graph. Can anyone please provide me with a pseudo code. thank u
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这听起来像是家庭作业,所以我不会透露全部内容。 但这里有一个提示:由于您对查找长度为 4 的循环感兴趣,因此取邻接矩阵的 4 次方并沿对角线扫描。 如果任何条目 M[i,i] 不为零,则存在包含顶点 i 的循环。
This sounds like homework, so I won't give the whole thing away. But here's a hint: since you are interested in finding cycles of length 4, take the 4th power of the adjacency matrix and scan along the diagonal. If any entry M[i,i] is nonzero, there is a cycle containing vertex i.
也许这不是计算它的最佳方法(它是
O(n^4)
),但一个非常简单的方法是扫描所有顶点您可以检查然后检查以下每个周期:
您基本上是在检查每个有序顶点集(a、b、c、d),以了解它们形成循环的 3 种方式。
所以伪代码是:
Perhaps it is not the optimal way to compute it ( it's
O(n^4)
), but a very straightforward way is scan through the all the verticesYou can check then check for each of the following cycles:
You're basically checking every ordered set of vertices (a,b,c,d) for the 3 ways that they could form a cycle.
So the pseudo code would be:
对每个节点应用深度有限的深度优先搜索,并记录 DFS 找到起始节点的节点。
对于搜索,请参阅此处的伪代码: http://en.wikipedia.org/wiki/深度受限_搜索。 添加类似的内容即可。
您只需在循环的开头
Apply a depth-limited depth-first-search to every node and record nodes where the DFS finds the starting node.
For the search, see pseudo-code here: http://en.wikipedia.org/wiki/Depth-limited_search. You just need to add something like
to the beginning of the loop.