虽然通用图形循环查找算法将起作用,但由于“旧是唯一的,目标不是”约束,您的情况有点特殊。这实际上意味着,每个节点只能有一个后继节点,因此它最多只能是一个周期的一部分。此外,当 DFS 遍历节点时,不会有任何分叉,因此迭代 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;
}