如何按依赖项对依赖对象进行排序 [英] How to sort depended objects by dependency

查看:32
本文介绍了如何按依赖项对依赖对象进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个收藏:

List>依赖层次结构;

成对中的第一项是某个对象(项目),第二项是第一个所依赖的相同类型对象的集合.我想按依赖顺序获取 List,因此没有依赖于第一个元素等的项目(没有循环依赖!).

输入:

<前>Item4 依赖于 Item3 和 Item5Item3 依赖于 Item1Item1 不依赖于任何一个Item2 依赖于 Item4Item5 不依赖于任何一个

结果:

<前>项目 1第5项项目 3项目 4项目 2

谢谢.

解决方案:

拓扑排序(感谢 Loïc Février 的想法)

C# 示例Java 示例(感谢 xcud 很好的例子)

解决方案

使用拓扑排序的完美示例:

http://en.wikipedia.org/wiki/Topological_sorting

它会给你你所需要的.

I have a collection:

List<VPair<Item, List<Item>> dependencyHierarchy;

The first item in pair is some object (item) and the second one is a collection of the same type objects that the first one depends on. I want to get a List<Item> in order of dependency, so there are not items that depend on the first element and so on (no cycled dependency!).

Input:

Item4 depends on Item3 and Item5
Item3 depends on Item1
Item1 does not depend on any one
Item2 depends on Item4 
Item5 does not depend on any one 

Result:

Item1
Item5
Item3
Item4
Item2

Thank you.

SOLUTION:

Topological Sorting (thanks to Loïc Février for idea)

and

example on C#, example on Java (thanks to xcud for great examples)

解决方案

Perfect example to use a topological sort :

http://en.wikipedia.org/wiki/Topological_sorting

It will give you exactly what you need.

这篇关于如何按依赖项对依赖对象进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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