From 9dca46add5a0169e59416f6d85e4fb2c1b6bec53 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Thu, 24 May 2018 10:59:51 -0400 Subject: [PATCH] feat: Pathfinding methods for determining if a path exists from a root to a destination --- ICD.Common.Utils/RecursionUtils.cs | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/ICD.Common.Utils/RecursionUtils.cs b/ICD.Common.Utils/RecursionUtils.cs index fe2a4fd..554053d 100644 --- a/ICD.Common.Utils/RecursionUtils.cs +++ b/ICD.Common.Utils/RecursionUtils.cs @@ -87,6 +87,42 @@ namespace ICD.Common.Utils yield return item; } + /// + /// Returns true if there is a path from the given root to the given child node. + /// + /// + /// + /// + /// + /// + public static bool BreadthFirstSearch(T root, T child, Func> getChildren) + { + if (getChildren == null) + throw new ArgumentNullException("getChildren"); + + return BreadthFirstSearchPath(root, child, getChildren) != null; + } + + /// + /// Returns true if there is a path from the given root to the given child node. + /// + /// + /// + /// + /// + /// + /// + public static bool BreadthFirstSearch(T root, T destination, Func> getChildren, IEqualityComparer comparer) + { + if (getChildren == null) + throw new ArgumentNullException("getChildren"); + + if (comparer == null) + throw new ArgumentNullException("comparer"); + + return BreadthFirstSearchPath(root, destination, getChildren, comparer) != null; + } + /// /// Returns all of the nodes in the tree via breadth-first search. ///