Optimize multiroute

This commit is contained in:
Jack Kanarish
2017-12-20 20:28:58 -05:00
parent 222365f210
commit 02cae250a6

View File

@@ -3,127 +3,203 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using ICD.Common.Properties; using ICD.Common.Properties;
using ICD.Common.Utils.Collections; using ICD.Common.Utils.Collections;
using ICD.Common.Utils.Extensions;
namespace ICD.Common.Utils namespace ICD.Common.Utils
{ {
public static class RecursionUtils public static class RecursionUtils
{ {
/// <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>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
/// <param name="root"></param> /// <param name="root"></param>
/// <param name="getChildren"></param> /// <param name="getChildren"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<T> BreadthFirstSearch<T>(T root, Func<T, IEnumerable<T>> getChildren) public static IEnumerable<T> BreadthFirstSearch<T>(T root, Func<T, IEnumerable<T>> getChildren)
{ {
if (getChildren == null) if (getChildren == null)
throw new ArgumentNullException("getChildren"); throw new ArgumentNullException("getChildren");
Queue<T> process = new Queue<T>(); Queue<T> process = new Queue<T>();
process.Enqueue(root); process.Enqueue(root);
while (process.Count > 0) while (process.Count > 0)
{ {
T current = process.Dequeue(); T current = process.Dequeue();
yield return current; yield return current;
foreach (T child in getChildren(current)) foreach (T child in getChildren(current))
process.Enqueue(child); process.Enqueue(child);
} }
} }
/// <summary> /// <summary>
/// Returns the shortest path from root to destination via breadth-first search. /// Returns the shortest path from root to destination via breadth-first search.
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
/// <param name="root"></param> /// <param name="root"></param>
/// <param name="destination"></param> /// <param name="destination"></param>
/// <param name="getChildren"></param> /// <param name="getChildren"></param>
/// <returns></returns> /// <returns></returns>
[CanBeNull] [CanBeNull]
public static IEnumerable<T> BreadthFirstSearchPath<T>(T root, T destination, Func<T, IEnumerable<T>> getChildren) public static IEnumerable<T> BreadthFirstSearchPath<T>(T root, T destination, Func<T, IEnumerable<T>> getChildren)
{ {
if (getChildren == null) if (getChildren == null)
throw new ArgumentNullException("getChildren"); throw new ArgumentNullException("getChildren");
return BreadthFirstSearchPath(root, destination, getChildren, EqualityComparer<T>.Default); return BreadthFirstSearchPath(root, destination, getChildren, EqualityComparer<T>.Default);
} }
/// <summary> [NotNull]
/// Returns the shortest path from root to destination via breadth-first search. public static Dictionary<T2, IEnumerable<T>> BreadthFirstSearchManyDestinations<T, T2>(T root, Dictionary<T2, T> destinations, Func<T, IEnumerable<T>> getChildren)
/// </summary> {
/// <typeparam name="T"></typeparam> if (getChildren == null)
/// <param name="root"></param> throw new ArgumentNullException("getChildren");
/// <param name="destination"></param>
/// <param name="getChildren"></param>
/// <param name="comparer"></param>
/// <returns></returns>
[CanBeNull]
public static IEnumerable<T> BreadthFirstSearchPath<T>(T root, T destination, Func<T, IEnumerable<T>> getChildren,
IEqualityComparer<T> comparer)
{
if (getChildren == null)
throw new ArgumentNullException("getChildren");
if (comparer == null) return BreadthFirstSearchPathManyDestinations(root, destinations, getChildren, EqualityComparer<T>.Default);
throw new ArgumentNullException("comparer"); }
// Edge case - root and destination are the same /// <summary>
if (comparer.Equals(root, destination)) /// Returns the shortest path from root to destination via breadth-first search.
return new[] {root}; /// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="root"></param>
/// <param name="destination"></param>
/// <param name="getChildren"></param>
/// <param name="comparer"></param>
/// <returns></returns>
[CanBeNull]
public static IEnumerable<T> BreadthFirstSearchPath<T>(T root, T destination, Func<T, IEnumerable<T>> getChildren,
IEqualityComparer<T> comparer)
{
if (getChildren == null)
throw new ArgumentNullException("getChildren");
Queue<T> queue = new Queue<T>(); if (comparer == null)
queue.Enqueue(root); throw new ArgumentNullException("comparer");
Dictionary<T, T> nodeParents = new Dictionary<T, T>(); // Edge case - root and destination are the same
if (comparer.Equals(root, destination))
return new[] { root };
while (queue.Count > 0) Queue<T> queue = new Queue<T>();
{ queue.Enqueue(root);
T current = queue.Dequeue();
foreach (T node in getChildren(current)) Dictionary<T, T> nodeParents = new Dictionary<T, T>();
{
if (nodeParents.ContainsKey(node))
continue;
queue.Enqueue(node); while (queue.Count > 0)
nodeParents.Add(node, current); {
T current = queue.Dequeue();
// Found a path to the destination foreach (T node in getChildren(current))
if (comparer.Equals(node, destination)) {
return GetPath(destination, nodeParents).Reverse(); if (nodeParents.ContainsKey(node))
} continue;
}
return null; queue.Enqueue(node);
} nodeParents.Add(node, current);
/// <summary> // Found a path to the destination
/// Walks through a map of nodes from the starting point. if (comparer.Equals(node, destination))
/// </summary> return GetPath(destination, nodeParents).Reverse();
/// <typeparam name="T"></typeparam> }
/// <param name="start"></param> }
/// <param name="nodeParents"></param>
/// <returns></returns>
private static IEnumerable<T> GetPath<T>(T start, IDictionary<T, T> nodeParents)
{
IcdHashSet<T> visited = new IcdHashSet<T>();
while (true) return null;
{ }
yield return start;
visited.Add(start);
T next; [NotNull]
if (!nodeParents.TryGetValue(start, out next)) public static Dictionary<T2, IEnumerable<T>> BreadthFirstSearchPathManyDestinations<T, T2>(T root, Dictionary<T2, T> destinations,
break; Func<T, IEnumerable<T>> getChildren,
IEqualityComparer<T> comparer)
{
if (getChildren == null)
throw new ArgumentNullException("getChildren");
if (visited.Contains(next)) if (comparer == null)
break; throw new ArgumentNullException("comparer");
start = next; Dictionary<T2, T> destinationsToBeProcessed = new Dictionary<T2, T>(destinations);
} List<T> destinationsProcessed = new List<T>();
} Dictionary<T2, IEnumerable<T>> pathsToReturn = new Dictionary<T2, IEnumerable<T>>();
}
//Edge case, root is the destination
foreach (var destination in destinationsToBeProcessed.Where(destination => comparer.Equals(root, destination.Value)))
{
destinationsProcessed.Add(destination.Value);
pathsToReturn.Add(destination.Key, new[] { root });
}
foreach (var destination in destinationsProcessed)
{
destinationsToBeProcessed.RemoveValue(destination);
}
destinationsProcessed.Clear();
if (destinationsToBeProcessed.Count == 0)
{
return pathsToReturn;
}
Queue<T> queue = new Queue<T>();
queue.Enqueue(root);
Dictionary<T, T> nodeParents = new Dictionary<T, T>();
while (queue.Count > 0)
{
T current = queue.Dequeue();
foreach (T node in getChildren(current).Where(node => !nodeParents.ContainsKey(node)))
{
queue.Enqueue(node);
nodeParents.Add(node, current);
foreach (var destination in destinationsToBeProcessed.Where(destination => comparer.Equals(node, destination.Value)))
{
destinationsProcessed.Add(destination.Value);
pathsToReturn.Add(destination.Key, GetPath(destination.Value, nodeParents).Reverse());
}
foreach (var destination in destinationsProcessed)
{
destinationsToBeProcessed.RemoveValue(destination);
}
destinationsProcessed.Clear();
if (destinationsToBeProcessed.Count == 0)
{
return pathsToReturn;
}
}
}
return pathsToReturn;
}
/// <summary>
/// Walks through a map of nodes from the starting point.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="start"></param>
/// <param name="nodeParents"></param>
/// <returns></returns>
private static IEnumerable<T> GetPath<T>(T start, IDictionary<T, T> nodeParents)
{
IcdHashSet<T> visited = new IcdHashSet<T>();
while (true)
{
yield return start;
visited.Add(start);
T next;
if (!nodeParents.TryGetValue(start, out next))
break;
if (visited.Contains(next))
break;
start = next;
}
}
}
} }