根据依赖关系获取订单的伪代码
好的,我的情况是这样的,我有一个项目列表,我需要根据它们所拥有的参考来获取这些项目的顺序。例如,假设我们有这些物品: A、B、C、D、E、F、
C 和 D 没有依赖关系,因此它们的顺序可以为 0。 B 是与 C、D 和 A 最多的那个。 A有C F 有 A 和 B
C D
| \ /
A /
/ | /
| B
\ |
F
在这种情况下, C、D = 0 A = 1 B=2 F = 3
我一直在浏览互联网,似乎我没有使用正确的科学术语。最有可能的是在某种程度上它是一套或一套包。我知道它不是一棵树,因为这种情况每个节点上都有两个以上的边。答案可以是编程语言,只是试图使其尽可能通用。
Ok, my situation is this I have a list of items and I need to get the order of these items based on the references they have. For example lets say we have these items:
A,B,C,D,E,F
C and D have no dependencies so their order can be 0.
B is the one that has the most with C, D and A.
A has C
and F has A and B
C D
| \ /
A /
/ | /
| B
\ |
F
In this case
C,D = 0
A = 1
B= 2
F = 3
I have been looking through the internet and it seems I am not using the correct scientific term for this. Most probably it is a Set or a Bag set in some way. I know it is not a tree as this situation has more than two edges on each node. The answer can be in a programming language, just trying to make it as general as possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个简单的算法如下。
迭代集合,寻找没有依赖关系的元素:将这些元素记为“0 级元素”。
再次迭代集合,查找可能依赖于“级别 0 元素”但不依赖于其他元素的元素:将这些元素记住为“级别 1 元素”。
再次迭代集合,查找可能依赖于“0 级元素”和/或“1 级元素”但不依赖于其他元素的元素:将这些元素记住为“2 级元素”。
等等。
当每个元素都有指定的级别时停止。
A simple algorithm is as follows.
Iterate the collection, looking for elements which have no dependencies: remember these elements as "the level 0 elements".
Iterate the collection again, looking for elements which may depend on "the level 0 elements" but not on other elements: remember these elements as "the level 1 elements".
Iterate the collection again, looking for elements which may depend on "the level 0 elements" and/or on "the level 1 elements", but not on other elements: remember these elements as "the level 2 elements".
Etc.
Stop when every element has an assigned level.
您可以创建图形并维护指针计数,也可以使用矩阵。
搜索并阅读一些图的基本概念(数学,而不是计算机图),你会发现它很容易。
You can create a graph and maintain the pointer counts, or you can use matrix.
Search and read some basic idea of graph(math, not computer graph), you can find it is pretty easy.