c# - 在列表中查找循环引用的最有效方法

标签 c# algorithm circular-reference cycle-detection

给定以下重定向列表

[
    {
        "old": "a",
        "target": "b"
    },
    {
        "old": "b",
        "target": "c"
    },
    {
        "old": "c",
        "target": "d"
    },
    {
        "old": "d",
        "target": "a"
    },
    {
        "old": "o",
        "target": "n"
    },
    {
        "old": "n",
        "target": "b"
    },
    {
        "old": "j",
        "target": "x"
    },
    {
        "old": "whatever",
        "target": "something"
    }
]

在这里我们可以看到第一个项目“a”应该重定向到“b”。 如果我们按照列表进行操作,我们可以看到以下模式:

a -> b
b -> c
c -> d
d -> a

所以我们最终会得到一个循环引用,因为“a”最终会指向“d”,而“d”会指向“a”。

查找循环引用的最有效方法是什么?

我在 C# 中提出了以下算法

var items = JsonConvert.DeserializeObject<IEnumerable<Item>>(json)
    .GroupBy(x => x.Old)
    .Select(x => x.First())
    .ToDictionary(x => x.Old, x => x.Target);
var circulars = new Dictionary<string, string>();
foreach (var item in items)
{
    var target = item.Value;
    while (items.ContainsKey(target))
    {
        target = items[target];

        if (target.Equals(item.Key))
        {
            circulars.Add(target, item.Value);
            break;
        }
    }
}

这将给我一个包含 4 个项目的列表,如下所示:

[
    {
        "old": "a",
        "target": "b"
    },
    {
        "old": "b",
        "target": "c"
    },
    {
        "old": "c",
        "target": "d"
    },
    {
        "old": "d",
        "target": "a"
    }
]

但我只对告诉用户类似的事情感兴趣

“嘿,你不能那样做,这将是一个循环引用,因为“a”指向“b”,“b”指向“c”,“c”指向“d”,“d”指向“a”

那么,你们有什么建议吗? 我确定存在其他一些(更好的)算法来执行此操作...:)

最佳答案

虽然通用图循环查找算法会起作用,但由于“旧的是唯一的,目标不是” 约束,您的情况有点特殊。它实际上意味着,每个节点只能有一个后继节点,因此它最多只能是一个循环的一部分。另外,DFS-Traversing节点时,不会有任何 fork ,因此迭代DFS实现变得非常容易。

给定任意起始节点,此函数可以找到从起始节点可达的环:

/// <summary>
/// Returns a node that is part of a cycle or null if no cycle is found
/// </summary>
static string FindCycleHelper(string start, Dictionary<string, string> successors, HashSet<string> stackVisited)
{
    string current = start;
    while (current != null)
    {
        if (stackVisited.Contains(current))
        {
            // this node is part of a cycle
            return current;
        }

        stackVisited.Add(current);

        successors.TryGetValue(current, out current);
    }

    return null;
}

为了保持效率,可以扩展到到达一个已经检查过的节点时提前返回(使用previouslyVisited):

/// <summary>
/// Returns a node that is part of a cycle or null if no cycle is found
/// </summary>
static string FindCycleHelper(string start, Dictionary<string, string> successors, HashSet<string> stackVisited, HashSet<string> previouslyVisited)
{
    string current = start;
    while (current != null)
    {
        if (previouslyVisited.Contains(current))
        {
            return null;
        }
        if (stackVisited.Contains(current))
        {
            // this node is part of a cycle
            return current;
        }

        stackVisited.Add(current);

        successors.TryGetValue(current, out current);
    }

    return null;
}

下面的函数用来维护访问集的一致性

static string FindCycle(string start, Dictionary<string, string> successors, HashSet<string> globalVisited)
{
    HashSet<string> stackVisited = new HashSet<string>();

    var result = FindCycleHelper(start, successors, stackVisited, globalVisited);

    // update collection of previously processed nodes
    globalVisited.UnionWith(stackVisited);

    return result;
}

为每个old 节点调用它以检查循环。当检测到循环起始节点时,可以单独创建循环信息:

// static testdata - can be obtained from JSON for real code
IEnumerable<Item> items = new Item[]
{
    new Item{ Old = "a", Target = "b" },
    new Item{ Old = "b", Target = "c" },
    new Item{ Old = "c", Target = "d" },
    new Item{ Old = "d", Target = "a" },
    new Item{ Old = "j", Target = "x" },
    new Item{ Old = "w", Target = "s" },
};
var successors = items.ToDictionary(x => x.Old, x => x.Target);

var visited = new HashSet<string>();

List<List<string>> cycles = new List<List<string>>();

foreach (var item in items)
{
    string cycleStart = FindCycle(item.Old, successors, visited);

    if (cycleStart != null)
    {
        // cycle found, get detail information about involved nodes
        List<string> cycle = GetCycleMembers(cycleStart, successors);
        cycles.Add(cycle);
    }
}

以您想要的任何方式输出您找到的循环。例如

foreach (var cycle in cycles)
{
    Console.WriteLine("Cycle:");
    Console.WriteLine(string.Join(" # ", cycle));
    Console.WriteLine();
}

GetCycleMembers 的实现非常简单 - 它取决于正确的起始节点:

/// <summary>
/// Returns the list of nodes that are involved in a cycle
/// </summary>
/// <param name="cycleStart">This is required to belong to a cycle, otherwise an exception will be thrown</param>
/// <param name="successors"></param>
/// <returns></returns>
private static List<string> GetCycleMembers(string cycleStart, Dictionary<string, string> successors)
{
    var visited = new HashSet<string>();
    var members = new List<string>();
    var current = cycleStart;
    while (!visited.Contains(current))
    {
        members.Add(current);
        visited.Add(current);
        current = successors[current];
    }
    return members;
}

关于c# - 在列表中查找循环引用的最有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46484725/

相关文章:

java - Android - 递归检查 map 是否可解的算法

php - MySQL - 创建一个 SQL 算法来确定随机 'popular' 内容

algorithm - 有没有一种有效的算法可以做到这一点?

c# - 运算符重载 ==, !=, Equals

c# - IoC/DI 框架与智能客户端 Winform 应用程序 : How should I approach this?

c# - 无法使用可选参数推断泛型类型

python - 在垃圾收集后保留循环引用

艾达:如何解决 "Circular Unit Dependency"?

.net - 解决实现 ISerializable 的对象的循环引用

c# - 没有定义故障契约时,如何访问 SOAP 故障中的 <detail> 部分?