gpt4 book ai didi

java - 依赖排序算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:37:35 25 4
gpt4 key购买 nike

我有一个这样的节点列表

{ a, b , c }

它们之间的依赖定义为

  • a => { b, c }
  • b => { c }
  • c => { }

该算法应该返回一个排序列表,这样任何给定的节点都应该出现在它的任何依赖项之前例如,一个有效的解决方案是:{c, b, a}

到目前为止我有这个类:

public static class DependencySorter
{
public static ICollection<T> SortDependencies<T>(this IEnumerable<T> nodes) where T : IDependency<T>
{
var set = new HashSet<T>();

foreach (var node in nodes)
{
foreach (var dependency in node.Resolve())
{
set.Add(dependency);
}
}

return set.ToList();
}

public static ICollection<T> Resolve<T>(this T node) where T : IDependency<T>
{
var resolved = new Collection<T>();
ResolveDependenciesRecursive(node, resolved, new List<T>());
return resolved;
}

private static void ResolveDependenciesRecursive<T>(T node, ICollection<T> resolved, ICollection<T> notResolved) where T : IDependency<T>
{
notResolved.Add(node);
foreach (var edge in node.Dependencies.Where(edge => !resolved.Contains(edge)))
{
if (notResolved.Contains(edge))
{
throw new InvalidOperationException($"Circular dependency detected {node} => {edge}");
}

ResolveDependenciesRecursive(edge, resolved, notResolved);
}

resolved.Add(node);
notResolved.Remove(node);
}
}

public interface IDependency<out T>
{
IEnumerable<T> Dependencies { get; }
}

我确信它的性能和复杂性真的很差。

最佳答案

这称为“拓扑排序”。有一些有效且相对简单的算法可用(维基百科列出了一些),通常在 O(|V|+|E|) 时间内。我最喜欢的是基于深度优先搜索的那个:

L ← Empty list that will contain the sorted nodes
while there are unmarked nodes do
select an unmarked node n
visit(n)

function visit(node n)
if n has a temporary mark then stop (not a DAG)
if n is not marked (i.e. has not been visited yet) then
mark n temporarily
for each node m with an edge from n to m do
visit(m)
mark n permanently
unmark n temporarily
add n to head of L

(这是来自 https://en.wikipedia.org/wiki/Topological_sorting 的复制和粘贴)

关于java - 依赖排序算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32750282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com