mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-12 19:25:00 +00:00
feat: Added a hashset of visited nodes so bfs can be used on graphs
This commit is contained in:
@@ -163,7 +163,7 @@ namespace ICD.Common.Utils
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns all of the nodes in the tree via breadth-first search.
|
||||
/// Returns all of the nodes in the graph via breadth-first search.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="root"></param>
|
||||
@@ -171,6 +171,7 @@ namespace ICD.Common.Utils
|
||||
/// <returns></returns>
|
||||
private static IEnumerable<T> BreadthFirstSearchIterator<T>(T root, Func<T, IEnumerable<T>> getChildren)
|
||||
{
|
||||
IcdHashSet<T> visited = new IcdHashSet<T> {root};
|
||||
Queue<T> process = new Queue<T>();
|
||||
process.Enqueue(root);
|
||||
|
||||
@@ -179,8 +180,11 @@ namespace ICD.Common.Utils
|
||||
{
|
||||
yield return current;
|
||||
|
||||
foreach (T child in getChildren(current))
|
||||
foreach (T child in getChildren(current).Where(c => !visited.Contains(c)))
|
||||
{
|
||||
visited.Add(child);
|
||||
process.Enqueue(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user