mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-04-12 03:57:32 +00:00
Merge branch 'feat/GetCliqueOptimization' of Common/Utils into ConnectPro_v1.3
This commit is contained in:
commit
1f6915a825
3 changed files with 17 additions and 8 deletions
|
|
@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||
|
||||
### Added
|
||||
- Added RecursionUtils method to get a single clique given a starting node
|
||||
- Breadth First Search can now search graphs in addition to trees
|
||||
|
||||
### Changed
|
||||
- Fixed bug in IcdUriBuilder where Query property behaved differently to UriBuilder
|
||||
|
|
|
|||
|
|
@ -130,7 +130,7 @@ namespace ICD.Common.Utils.Tests
|
|||
[Test]
|
||||
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.IsTrue(clique.Contains(1));
|
||||
|
|
@ -138,7 +138,7 @@ namespace ICD.Common.Utils.Tests
|
|||
Assert.IsTrue(clique.Contains(3));
|
||||
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.IsTrue(clique.Contains(5));
|
||||
|
|
|
|||
|
|
@ -36,14 +36,18 @@ namespace ICD.Common.Utils
|
|||
/// Gets the clique containing the given node.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="nodes"></param>
|
||||
/// <param name="node"></param>
|
||||
/// <param name="getAdjacent"></param>
|
||||
/// <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);
|
||||
return GetClique(map, node);
|
||||
if (node == null)
|
||||
throw new ArgumentNullException("node");
|
||||
|
||||
if (getAdjacent == null)
|
||||
throw new ArgumentNullException("getAdjacent");
|
||||
|
||||
return BreadthFirstSearch(node, getAdjacent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -163,7 +167,7 @@ namespace ICD.Common.Utils
|
|||
}
|
||||
|
||||
/// <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>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="root"></param>
|
||||
|
|
@ -171,6 +175,7 @@ namespace ICD.Common.Utils
|
|||
/// <returns></returns>
|
||||
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>();
|
||||
process.Enqueue(root);
|
||||
|
||||
|
|
@ -179,8 +184,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue