我目前正在开展一个项目,以图解方式解释 Hopcroft-Karp 算法。
我正在使用维基百科文章中的伪代码。
我也看到过这个算法在 Stack Overflow Python 中实现
如果我不这样做的话,效果会非常好不必完全理解该算法即可使用它。
我的问题如下:伪代码中的Dist[]数组是什么意思,广度优先搜索中图的分层是如何完成的。我已经掌握了 DFS 的运作方式。
提前致谢。
I am currently working on a project to pictorially explain the Hopcroft-Karp algorithm.
I am using the pseudo-code from the Wikipedia article.
I have also seen this algorithm implemented on Stack Overflow in Python
This would do fantastically if only I didn't have to understand the algorithm completely to use it.
My questions are as follows: What is the meaning of the Dist[] array in the pseudo-code, and how is the layering of the graph done in the Breadth-First-Search. I have a grasp on the workings of the DFS.
Thanks in advance.
发布评论
评论(2)
标准 BFS 创建层,使得连续层中的节点之间的距离恰好为 1 (即连续层的节点之间存在长度为1的路径)。
因此,该代码初始化 BFS 树的第一层,设置所有“空闲节点"
v
(即Pair[v] == NIL
)距离为 0。此代码继续构建 BFS逐层树,对于作为
v
邻居的节点u
(距离恰好为 1)。Dist[]
只是一种管理从节点到 BFS 初始层的距离的方法The standard BFS creates layers such that nodes in successive layers are at a distance of exactly 1 apart (i.e. there is a path of length 1 between the nodes of successive layers).
So that code initializes the first layer of the BFS tree, setting all "free nodes"
v
(i.e.Pair[v] == NIL
) to be at distance 0.and this code continues on building the BFS tree layer by layer, for nodes
u
that are neighbors ofv
(at distance exactly one).Dist[]
is just a way to manage the distances from nodes to the initial layer of the BFS这是我编写的 Hopcroft Karp 算法的完整注释代码。下面带注释的代码希望有助于解释这个美丽算法的几乎每个角落。它是用 C# 编写的,但代码可以轻松转换为 C++/Java。您还可以在我的 Github 存储库上找到测试用例。
注意:我还在 维基百科文章。
Here's fully annotated code for Hopcroft Karp algorithm that I'd wrote. Below annotated code hopefully helps explain pretty much every nook and cranny of this beautiful algorithm. It's in C# but code can be easily translated to C++/Java. You can also find the test cases on my Github repo.
Note: I've also added the summarized version of this explanation and below diagram in Wikipedia article.