mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-04-12 12:07:05 +00:00
feat: Pathfinding methods for determining if a path exists from a root to a destination
This commit is contained in:
parent
35593bfa47
commit
9dca46add5
1 changed files with 36 additions and 0 deletions
|
|
@ -87,6 +87,42 @@ namespace ICD.Common.Utils
|
||||||
yield return item;
|
yield return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns true if there is a path from the given root to the given child node.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="root"></param>
|
||||||
|
/// <param name="child"></param>
|
||||||
|
/// <param name="getChildren"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool BreadthFirstSearch<T>(T root, T child, Func<T, IEnumerable<T>> getChildren)
|
||||||
|
{
|
||||||
|
if (getChildren == null)
|
||||||
|
throw new ArgumentNullException("getChildren");
|
||||||
|
|
||||||
|
return BreadthFirstSearchPath(root, child, getChildren) != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns true if there is a path from the given root to the given child node.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="root"></param>
|
||||||
|
/// <param name="destination"></param>
|
||||||
|
/// <param name="getChildren"></param>
|
||||||
|
/// <param name="comparer"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool BreadthFirstSearch<T>(T root, T destination, Func<T, IEnumerable<T>> getChildren, IEqualityComparer<T> comparer)
|
||||||
|
{
|
||||||
|
if (getChildren == null)
|
||||||
|
throw new ArgumentNullException("getChildren");
|
||||||
|
|
||||||
|
if (comparer == null)
|
||||||
|
throw new ArgumentNullException("comparer");
|
||||||
|
|
||||||
|
return BreadthFirstSearchPath(root, destination, getChildren, comparer) != null;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns all of the nodes in the tree via breadth-first search.
|
/// Returns all of the nodes in the tree via breadth-first search.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue