在 Ada 中实现 Kruskal 算法,不知道从哪里开始
参考Ada中的Kruskal算法,我不知道从哪里开始。
在实际编写程序之前,我试图仔细考虑所有内容,但对于应该使用哪些数据结构以及如何表示所有内容感到非常困惑。
我最初的想法是在邻接列表中表示完整的树,但是阅读维基百科,算法指出创建一个森林 F(一组树),其中图中的每个顶点都是一个单独的树并且我不知道如何实现这一点而又不会很快变得混乱。
它说要做的下一件事是创建一个包含图中所有边的集合 S,但我再次不确定执行此操作的最佳方法是什么。我正在考虑一组记录,其中包含 to
、from
和 weight
,但我迷失在森林< /代码>。
最后,我试图弄清楚如何知道一条边是否连接两棵树,但再次不确定完成所有这一切的最佳方法是什么。
With reference to Kruskal's algorithm in Ada, I'm not sure where to start.
I'm trying to think through everything before I actually write the program, but am pretty lost as to what data structures I should be using and how to represent everything.
My original thought is to represent the full tree in an adjacency list, but reading Wikipedia the algorithm states to create a forest F (a set of trees), where each vertex in the graph is a separate tree
and I'm not sure how to implement this without getting really messy quickly.
The next thing it says to do is create a set S containing all the edges in the graph
, but once again I'm not sure what the best way to do this would be. I was thinking of an array of records, with a to
, from
and weight
, but I'm lost on the forest
.
Lastly, I'm trying to figure out how I would know if an edge connects two trees, but again am not sure what the best way to do all of this is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可以看到他们的算法描述会让你对如何开始感到困惑。它给我留下了同样的方式。
我建议您阅读后面的示例部分。这使得如何继续进行变得非常清楚,并且您可能可以从中想出执行此操作所需的数据结构。
看起来基本思想如下:
I can see where their algorithm description would leave you confused as how to start. It left me the same way.
I'd suggest reading over the later Example section instead. That makes it pretty clear how to proceed, and you can probably come up with the data structures you would need to do it just from that.
It looks like the basic idea is the following:
“创建森林部分”的真正含义是:从页面不相交集数据结构。如果您可以阅读 C++,那么我有一个非常简单的实现这里。 (这个实现是有效的,我自己用它来实现 Kruskal 算法:)
The "create a forest part" really means: implement the pseudocode from the page Disjoint-set data structure. If you can read C++, then I have a pretty straightforward implementation here. (That implementation works, I've used it to implement Kruskal's algo myself :)