基于依赖获取订单的伪代码 [英] Pseudocode for getting order based on Dependency

查看:38
本文介绍了基于依赖获取订单的伪代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我的情况是我有一个项目列表,我需要根据它们拥有的引用来获取这些项目的顺序.例如,假设我们有这些项目:A、B、C、D、E、F

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 和 D 没有依赖关系,所以它们的顺序可以是 0.B是与C,D和A最多的一个.A 有 C而 F 有 A 和 B

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

在这种情况下C,D = 0一 = 1乙= 2F = 3

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.

推荐答案

一个简单的算法如下.

迭代集合,寻找没有依赖关系的元素:记住这些元素为0级元素".

Iterate the collection, looking for elements which have no dependencies: remember these elements as "the level 0 elements".

再次迭代集合,寻找可能依赖于0 级元素"但不依赖于其他元素的元素:记住这些元素为1 级元素".

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".

再次迭代集合,寻找可能依赖于0 级元素"和/或1 级元素"但不依赖于其他元素的元素:记住这些元素为2 级元素".

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".

当每个元素都有指定的级别时停止.

Stop when every element has an assigned level.

这篇关于基于依赖获取订单的伪代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆