如何按依赖关系对依赖的对象进行排序

编程入门 行业动态 更新时间:2024-10-28 08:29:30
本文介绍了如何按依赖关系对依赖的对象进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个集合:

列表< VPair<项目,列表&项目>> dependencyHierarchy;

成对的第一个项目是一些对象(项目),第二个是相同的集合键入第一个依赖的对象。我想以依赖的顺序获得列表< Item> ,所以没有依赖于第一个元素的项目等等(没有循环依赖!)。 p>

输入:

项目4取决于Item3和Item5 Item3依赖于Item1 项目1不依赖于任何一个项目2取决于Item4 项目5不依赖于任何一个

结果:

项目1 项目5 项目3 项目4 项目2

谢谢。

解决方案:

拓扑排序(感谢LoïcFévrier为想法)

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

解决方案

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

en.wikipedia/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 :

en.wikipedia/wiki/Topological_sorting

It will give you exactly what you need.

更多推荐

如何按依赖关系对依赖的对象进行排序

本文发布于:2023-11-25 17:08:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630610.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   关系

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!