在圖中查找週期的簡單方法是使用遞歸深度優先圖着色算法,其中節點標記爲「訪問」或「訪問」。如果在訪問一個節點時發現它已經處於「訪問」狀態,那麼就有一個循環。標記爲「已訪問」的節點可以跳過。例如:
public class DependencyExtensions
{
enum VisitState
{
NotVisited,
Visiting,
Visited
};
public static TValue ValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
{
TValue value;
if (dictionary.TryGetValue(key, out value))
return value;
return defaultValue;
}
static void DepthFirstSearch<T>(T node, Func<T, IEnumerable<T>> lookup, List<T> parents, Dictionary<T, VisitState> visited, List<List<T>> cycles)
{
var state = visited.ValueOrDefault(node, VisitState.NotVisited);
if (state == VisitState.Visited)
return;
else if (state == VisitState.Visiting)
{
// Do not report nodes not included in the cycle.
cycles.Add(parents.Concat(new[] { node }).SkipWhile(parent => !EqualityComparer<T>.Default.Equals(parent, node)).ToList());
}
else
{
visited[node] = VisitState.Visiting;
parents.Add(node);
foreach (var child in lookup(node))
DepthFirstSearch(child, lookup, parents, visited, cycles);
parents.RemoveAt(parents.Count - 1);
visited[node] = VisitState.Visited;
}
}
public static List<List<T>> FindCycles<T>(this IEnumerable<T> nodes, Func<T, IEnumerable<T>> edges)
{
var cycles = new List<List<T>>();
var visited = new Dictionary<T, VisitState>();
foreach (var node in nodes)
DepthFirstSearch(node, edges, new List<T>(), visited, cycles);
return cycles;
}
public static List<List<T>> FindCycles<T, TValueList>(this IDictionary<T, TValueList> listDictionary)
where TValueList : class, IEnumerable<T>
{
return listDictionary.Keys.FindCycles(key => listDictionary.ValueOrDefault(key, null) ?? Enumerable.Empty<T>());
}
}
然後,你可以使用它像:
var serviceDependence = new Dictionary<string, List<string>>
{
{ "A", new List<string> { "A" }},
{ "B", new List<string> { "C", "D" }},
{ "D", new List<string> { "E" }},
{ "E", new List<string> { "F", "Q" }},
{ "F", new List<string> { "D" }},
};
var cycles = serviceDependence.FindCycles();
Debug.WriteLine(JsonConvert.SerializeObject(cycles, Formatting.Indented));
foreach (var cycle in cycles)
{
serviceDependence[cycle[cycle.Count - 2]].Remove(cycle[cycle.Count - 1]);
}
Debug.Assert(serviceDependence.FindCycles().Count == 0);
更新
您的問題已更新請求「最有效的算法」尋找循環依賴。原始答案中的代碼是遞歸的,因此對於數千個級別的依賴關係鏈,可能有StackOverflowException
。這裏有一個非遞歸版本明確的堆棧變量:
public static class DependencyExtensions
{
enum VisitState
{
NotVisited,
Visiting,
Visited
};
public static TValue ValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
{
TValue value;
if (dictionary.TryGetValue(key, out value))
return value;
return defaultValue;
}
private static void TryPush<T>(T node, Func<T, IEnumerable<T>> lookup, Stack<KeyValuePair<T, IEnumerator<T>>> stack, Dictionary<T, VisitState> visited, List<List<T>> cycles)
{
var state = visited.ValueOrDefault(node, VisitState.NotVisited);
if (state == VisitState.Visited)
return;
else if (state == VisitState.Visiting)
{
Debug.Assert(stack.Count > 0);
var list = stack.Select(pair => pair.Key).TakeWhile(parent => !EqualityComparer<T>.Default.Equals(parent, node)).ToList();
list.Add(node);
list.Reverse();
list.Add(node);
cycles.Add(list);
}
else
{
visited[node] = VisitState.Visiting;
stack.Push(new KeyValuePair<T, IEnumerator<T>>(node, lookup(node).GetEnumerator()));
}
}
static List<List<T>> FindCycles<T>(T root, Func<T, IEnumerable<T>> lookup, Dictionary<T, VisitState> visited)
{
var stack = new Stack<KeyValuePair<T, IEnumerator<T>>>();
var cycles = new List<List<T>>();
TryPush(root, lookup, stack, visited, cycles);
while (stack.Count > 0)
{
var pair = stack.Peek();
if (!pair.Value.MoveNext())
{
stack.Pop();
visited[pair.Key] = VisitState.Visited;
pair.Value.Dispose();
}
else
{
TryPush(pair.Value.Current, lookup, stack, visited, cycles);
}
}
return cycles;
}
public static List<List<T>> FindCycles<T>(this IEnumerable<T> nodes, Func<T, IEnumerable<T>> edges)
{
var cycles = new List<List<T>>();
var visited = new Dictionary<T, VisitState>();
foreach (var node in nodes)
cycles.AddRange(FindCycles(node, edges, visited));
return cycles;
}
public static List<List<T>> FindCycles<T, TValueList>(this IDictionary<T, TValueList> listDictionary)
where TValueList : class, IEnumerable<T>
{
return listDictionary.Keys.FindCycles(key => listDictionary.ValueOrDefault(key, null) ?? Enumerable.Empty<T>());
}
}
這應該是合理有效的N*log(N) + E
其中N
是節點的數量和E
是邊數。 Log(N)
來自構建visited
哈希表,可以通過使每個節點記住它的來消除。這似乎是合理的表現;在下面的測試工具,時間找到平均長度4393的17897次循環與125603只總依賴10000個節點是圍繞10.2秒:
public class TestClass
{
public static void TestBig()
{
var elapsed = TestBig(10000);
Debug.WriteLine(elapsed.ToString());
}
static string GetName(int i)
{
return "ServiceDependence" + i.ToString();
}
public static TimeSpan TestBig(int count)
{
var serviceDependence = new Dictionary<string, List<string>>();
for (int iItem = 0; iItem < count; iItem++)
{
var name = GetName(iItem);
// Add several forward references.
for (int iRef = iItem - 1; iRef > 0; iRef = iRef/2)
serviceDependence.Add(name, GetName(iRef));
// Add some backwards references.
if (iItem > 0 && (iItem % 5 == 0))
serviceDependence.Add(name, GetName(iItem + 5));
}
// Add one backwards reference that will create some extremely long cycles.
serviceDependence.Add(GetName(1), GetName(count - 1));
List<List<string>> cycles;
var stopwatch = new Stopwatch();
stopwatch.Start();
try
{
cycles = serviceDependence.FindCycles();
}
finally
{
stopwatch.Stop();
}
var elapsed = stopwatch.Elapsed;
var averageLength = cycles.Average(l => (double)l.Count);
var total = serviceDependence.Values.Sum(l => l.Count);
foreach (var cycle in cycles)
{
serviceDependence[cycle[cycle.Count - 2]].Remove(cycle[cycle.Count - 1]);
}
Debug.Assert(serviceDependence.FindCycles().Count == 0);
Console.WriteLine(string.Format("Time to find {0} cycles of average length {1} in {2} nodes with {3} total dependencies: {4}", cycles.Count, averageLength, count, total, elapsed));
Console.ReadLine();
System.Environment.Exit(0);
return elapsed;
}
}
你有什麼試過?爲什麼你的算法不工作?它有什麼問題?我們不是在這裏爲你寫代碼。 –
@ThomasWeller我更新了我的代碼。但它運作緩慢 – Anatoly
拓撲排序可以幫助http://en.wikipedia.org/wiki/Topological_sorting –