mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-04-12 03:57:32 +00:00
feat: Added a hashset of visited nodes so bfs can be used on graphs
This commit is contained in:
parent
8c160c77c4
commit
7cc5a47d6a
1 changed files with 6 additions and 2 deletions
|
|
@ -163,7 +163,7 @@ namespace ICD.Common.Utils
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
/// <param name="root"></param>
|
/// <param name="root"></param>
|
||||||
|
|
@ -171,6 +171,7 @@ namespace ICD.Common.Utils
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private static IEnumerable<T> BreadthFirstSearchIterator<T>(T root, Func<T, IEnumerable<T>> getChildren)
|
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>();
|
Queue<T> process = new Queue<T>();
|
||||||
process.Enqueue(root);
|
process.Enqueue(root);
|
||||||
|
|
||||||
|
|
@ -179,8 +180,11 @@ namespace ICD.Common.Utils
|
||||||
{
|
{
|
||||||
yield return current;
|
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);
|
process.Enqueue(child);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue