C# 我应该如何解决“a”中的逻辑依赖关系;“平坦”;数据结构?

C# 我应该如何解决“a”中的逻辑依赖关系;“平坦”;数据结构?,c#,dependencies,C#,Dependencies,我目前有一个字典,您只需要编写一些递归算法。看这里: internal static class Program { private static Dictionary<string, List<string>> _dependencies; static void Main(string[] args) { _dependencies = new Dictionary<string, List<string>&

我目前有一个
字典,您只需要编写一些递归算法。看这里:

internal static class Program
{
    private static Dictionary<string, List<string>> _dependencies;

    static void Main(string[] args)
    {
        _dependencies = new Dictionary<string, List<string>>
        {
            {"a", new List<string>{"b","d"}},
            {"b", new List<string>()},
            {"c", new List<string>{"a"}},
            {"d", new List<string>{"b","e","f"}},
            {"e", new List<string>()},
            {"f", new List<string>()}
        };
        Console.WriteLine(string.Join(",", DeepWalkDependencies("a", new HashSet<string>())));
        Console.ReadLine();
    }

    static IEnumerable<string> DeepWalkDependencies(string key, HashSet<string> alreadyVisitedKeys)
    {
        foreach (var d in _dependencies[key])
        {
            if (alreadyVisitedKeys.Add(d))
            {
                yield return d;
                foreach (var dd in DeepWalkDependencies(d, alreadyVisitedKeys))
                {
                    yield return dd;
                }
            }
        }
    }
}
内部静态类程序
{
私有静态字典依赖关系;
静态void Main(字符串[]参数)
{
_依赖项=新字典
{
{“a”,新列表{“b”,“d”},
{“b”,新列表(),
{“c”,新列表{“a”},
{“d”,新列表{“b”,“e”,“f”},
{“e”,新列表(),
{“f”,新列表()}
};
WriteLine(string.Join(“,”,DeepWalkDependencies(“a”),newhashset());
Console.ReadLine();
}
静态IEnumerable DeepWalkDependencies(字符串键、HashSet-alreadyVisitedKeys)
{
foreach(依赖项[key]中的变量d)
{
如果(已准备好查看键。添加(d))
{
收益率d;
foreach(DeepWalkDependencies中的var dd(d,alreadyVisitedKeys))
{
收益率;
}
}
}
}
}

检测循环依赖关系也可以类似地实现。您需要的不是
HashSet-alreadyVisitedKeys
,而是
List-currentPath
,它将在每次调用递归函数之前重新创建。实现这个算法是一个很好的练习。享受吧

谢谢你的回答。我已经开始实现一些更高效的东西,因为我需要更好的性能,但是你可以用最少的代码编写代码。
a -> b,d
b -> nothing
c -> a
d -> b,e,f
e -> nothing
f -> nothing
a -> b,d,e,f (d has a dependency to e and f).
b -> nothing
c -> a,b,d (a has a dependency to b and d).
d -> b,e,f
e -> nothing
f -> nothing
a -> b,d,e,f
b -> nothing
c -> a,b,d,e,f
d -> b,e,f
e -> nothing
f -> nothing
internal static class Program
{
    private static Dictionary<string, List<string>> _dependencies;

    static void Main(string[] args)
    {
        _dependencies = new Dictionary<string, List<string>>
        {
            {"a", new List<string>{"b","d"}},
            {"b", new List<string>()},
            {"c", new List<string>{"a"}},
            {"d", new List<string>{"b","e","f"}},
            {"e", new List<string>()},
            {"f", new List<string>()}
        };
        Console.WriteLine(string.Join(",", DeepWalkDependencies("a", new HashSet<string>())));
        Console.ReadLine();
    }

    static IEnumerable<string> DeepWalkDependencies(string key, HashSet<string> alreadyVisitedKeys)
    {
        foreach (var d in _dependencies[key])
        {
            if (alreadyVisitedKeys.Add(d))
            {
                yield return d;
                foreach (var dd in DeepWalkDependencies(d, alreadyVisitedKeys))
                {
                    yield return dd;
                }
            }
        }
    }
}