Merge branch 'feat/GetCliqueOptimization' of Common/Utils into ConnectPro_v1.3

This commit is contained in:
Chris Cameron
2019-06-27 15:02:00 +00:00
committed by Gogs
3 changed files with 17 additions and 8 deletions

View File

@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added ### Added
- Added RecursionUtils method to get a single clique given a starting node - Added RecursionUtils method to get a single clique given a starting node
- Breadth First Search can now search graphs in addition to trees
### Changed ### Changed
- Fixed bug in IcdUriBuilder where Query property behaved differently to UriBuilder - Fixed bug in IcdUriBuilder where Query property behaved differently to UriBuilder

View File

@@ -130,7 +130,7 @@ namespace ICD.Common.Utils.Tests
[Test] [Test]
public void GetCliqueSingleNodeTest() public void GetCliqueSingleNodeTest()
{ {
int[] clique = RecursionUtils.GetClique(s_CliqueGraph.Keys, 1, n => s_CliqueGraph[n]).ToArray(); int[] clique = RecursionUtils.GetClique(1, n => s_CliqueGraph[n]).ToArray();
Assert.AreEqual(4, clique.Length); Assert.AreEqual(4, clique.Length);
Assert.IsTrue(clique.Contains(1)); Assert.IsTrue(clique.Contains(1));
@@ -138,7 +138,7 @@ namespace ICD.Common.Utils.Tests
Assert.IsTrue(clique.Contains(3)); Assert.IsTrue(clique.Contains(3));
Assert.IsTrue(clique.Contains(4)); Assert.IsTrue(clique.Contains(4));
clique = RecursionUtils.GetClique(s_CliqueGraph.Keys, 5, n => s_CliqueGraph[n]).ToArray(); clique = RecursionUtils.GetClique(5, n => s_CliqueGraph[n]).ToArray();
Assert.AreEqual(2, clique.Length); Assert.AreEqual(2, clique.Length);
Assert.IsTrue(clique.Contains(5)); Assert.IsTrue(clique.Contains(5));

View File

@@ -36,14 +36,18 @@ namespace ICD.Common.Utils
/// Gets the clique containing the given node. /// Gets the clique containing the given node.
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
/// <param name="nodes"></param>
/// <param name="node"></param> /// <param name="node"></param>
/// <param name="getAdjacent"></param> /// <param name="getAdjacent"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<T> GetClique<T>(IEnumerable<T> nodes, T node, Func<T, IEnumerable<T>> getAdjacent) public static IEnumerable<T> GetClique<T>(T node, Func<T, IEnumerable<T>> getAdjacent)
{ {
Dictionary<T, IEnumerable<T>> map = nodes.ToDictionary(n => n, getAdjacent); if (node == null)
return GetClique(map, node); throw new ArgumentNullException("node");
if (getAdjacent == null)
throw new ArgumentNullException("getAdjacent");
return BreadthFirstSearch(node, getAdjacent);
} }
/// <summary> /// <summary>
@@ -163,7 +167,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 +175,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 +184,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);
}
} }
} }