From 7cc5a47d6a48f845600ead1296dd0eda506ef520 Mon Sep 17 00:00:00 2001 From: Austin Noska Date: Thu, 27 Jun 2019 10:57:00 -0400 Subject: [PATCH] feat: Added a hashset of visited nodes so bfs can be used on graphs --- ICD.Common.Utils/RecursionUtils.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ICD.Common.Utils/RecursionUtils.cs b/ICD.Common.Utils/RecursionUtils.cs index 826f35f..7f3c853 100644 --- a/ICD.Common.Utils/RecursionUtils.cs +++ b/ICD.Common.Utils/RecursionUtils.cs @@ -163,7 +163,7 @@ namespace ICD.Common.Utils } /// - /// Returns all of the nodes in the tree via breadth-first search. + /// Returns all of the nodes in the graph via breadth-first search. /// /// /// @@ -171,6 +171,7 @@ namespace ICD.Common.Utils /// private static IEnumerable BreadthFirstSearchIterator(T root, Func> getChildren) { + IcdHashSet visited = new IcdHashSet {root}; Queue process = new Queue(); 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); + } } }