private void ExtractSpecialNodeData(SyncNode[] syncNodeBlocks) { //loops loopPairByStart = new Dictionary <int, LoopStart>(); loopPairByEnd = new Dictionary <int, LoopPair>(); List <LoopPair> loops = new LoopDetector(dependencyGraph).FindLoops(); foreach (LoopPair loopPair in loops) { if (!loopPairByStart.ContainsKey(loopPair.Start.NodeId)) { loopPairByStart.Add(loopPair.Start.NodeId, loopPair.Start); } loopPairByEnd.Add(loopPair.End.NodeId, loopPair); } //sync syncById = new Dictionary <int, SyncNode>(); foreach (SyncNode sync in syncNodeBlocks) { syncById.Add(sync.NodeId, sync); } }
public void SimpleExtendedLoop() { // S -> LoopStart -> LoopEnd -> Process -> End List <DependentNode> nodes = new List <DependentNode>(); DependentNode start = new DependentNode(0, "start"), loopStart = new DependentNode(1, LoopStart.TypeName), loopEnd = new DependentNode(2, LoopEnd.TypeName), process = new DependentNode(3, "pro"), end = new DependentNode(4, "end"); nodes.Add(start); nodes.Add(loopStart); nodes.Add(loopEnd); nodes.Add(process); nodes.Add(end); TestHelpers.MatchSlots(start, loopStart, 0, 0); TestHelpers.MatchSlots(loopStart, loopEnd, 0, 0); TestHelpers.MatchSlots(loopStart, loopEnd, 1, 2); TestHelpers.MatchSlots(loopEnd, process, 0, 0); TestHelpers.MatchSlots(process, end, 0, 0); List <LoopPair> loops = new LoopDetector(TestHelpers.ConvertToDictionary(nodes)).FindLoops(); Assert.AreEqual(1, loops.Count, "incorrect amount of loops detected"); Assert.AreEqual(0, loops[0].Depth); }
public void KataTests() { LDNode n; n = LoopDetector.createChain(1, 3); _pobj.getLoopSize(n).ShouldBe(3); n = LoopDetector.createChain(3, 30); _pobj.getLoopSize(n).ShouldBe(30); n = LoopDetector.createChain(3904, 1087); _pobj.getLoopSize(n).ShouldBe(1087); }
public void FourNodesWithLoopSize3() { var n1 = new LoopDetector.Node(); var n2 = new LoopDetector.Node(); var n3 = new LoopDetector.Node(); var n4 = new LoopDetector.Node(); n1.next = n2; n2.next = n3; n3.next = n4; n4.next = n2; Assert.AreEqual(3, LoopDetector.getLoopSize(n1)); }
public void MiddleExitLoops() { // End // ᴧ // S -> LoopStart -> LoopEnd -> End List <DependentNode> nodes = new List <DependentNode>(); { // start node DependentNode dep = new DependentNode(0, "start"); dep.AddDependent(1, 0, 0); nodes.Add(dep); } { // loop start DependentNode dep = new DependentNode(1, LoopStart.TypeName); dep.AddDependency(0, 0, 0); dep.AddDependent(2, 0, 0); dep.AddDependent(2, 2, 1); dep.AddDependent(4, 1, 0); nodes.Add(dep); } { // condition for end loop DependentNode dep = new DependentNode(5, "condition"); dep.AddDependent(2, 1, 0); nodes.Add(dep); } { // middle end DependentNode dep = new DependentNode(4, "end"); dep.AddDependency(1, 1, 0); nodes.Add(dep); } { // loop end DependentNode dep = new DependentNode(2, LoopEnd.TypeName); dep.AddDependency(1, 0, 0); dep.AddDependency(5, 0, 1); dep.AddDependency(1, 1, 2); dep.AddDependent(3, 0, 0); nodes.Add(dep); } { // end node DependentNode dep = new DependentNode(3, "end"); dep.AddDependency(2, 0, 0); nodes.Add(dep); } List <LoopPair> loops = new LoopDetector(TestHelpers.ConvertToDictionary(nodes)).FindLoops(); Assert.AreEqual(1, loops.Count, "incorrect amount of loops detected"); Assert.AreEqual(0, loops[0].Depth); }
private void TestSingleChain(int taillength, int looplength) { LDNode n, n2; n = LoopDetector.createChain(taillength, looplength); for (int i = 0; i < taillength; i++) { //Console.WriteLine($"tail {i}: {n.ID}"); n = n.next; } n2 = n; for (int i = 0; i < looplength - 1; i++) { //Console.WriteLine($"loop {i}: {n2.next.ID}"); n2 = n2.next; } //n2.next.ID.ShouldBe(n.ID); n2.ShouldBe(n); }
public void NoLoop() { // S -> End List <DependentNode> nodes = new List <DependentNode>(); { // start node DependentNode dep = new DependentNode(0, "start"); dep.AddDependent(3, 0, 0); nodes.Add(dep); } { // end node DependentNode dep = new DependentNode(3, "end"); dep.AddDependency(0, 0, 0); nodes.Add(dep); } List <LoopPair> loops = new LoopDetector(TestHelpers.ConvertToDictionary(nodes)).FindLoops(); Assert.AreEqual(0, loops.Count, "incorrect amount of loops detected"); }
public static long getLoopSize(LoopDetector.Node initialNode) { //Been a while since I wrote imperative code this ugly... ISet<LoopDetector.Node> visitedNotes = new HashSet<LoopDetector.Node>(); var currentNode = initialNode; while (!visitedNotes.Contains(currentNode)) { visitedNotes.Add(currentNode); currentNode = currentNode.next; } var endOfCycle = currentNode; currentNode = currentNode.next; var loopLength = 1; while (endOfCycle != currentNode) { loopLength++; currentNode = currentNode.next; } return loopLength; }
public void NestedLoop2() { // S -> LoopStart -> LoopStart -> Process -> LoopEnd -> LoopEnd -> End List <DependentNode> nodes = new List <DependentNode>(); DependentNode start = new DependentNode(0, "start"), outerLoopStart = new DependentNode(1, LoopStart.TypeName), innerLoopStart = new DependentNode(2, LoopStart.TypeName), process = new DependentNode(3, ""), innerLoopEnd = new DependentNode(4, LoopEnd.TypeName), outerLoopEnd = new DependentNode(5, LoopEnd.TypeName), end = new DependentNode(6, "end"); nodes.Add(start); nodes.Add(outerLoopEnd); nodes.Add(innerLoopEnd); nodes.Add(innerLoopStart); nodes.Add(outerLoopStart); nodes.Add(process); nodes.Add(end); TestHelpers.MatchSlots(start, outerLoopStart, 0, 0); TestHelpers.MatchSlots(outerLoopStart, outerLoopEnd, 0, 0); TestHelpers.MatchSlots(outerLoopStart, innerLoopStart, 2, 2); TestHelpers.MatchSlots(innerLoopStart, innerLoopEnd, 0, 0); TestHelpers.MatchSlots(innerLoopStart, process, 2, 0); TestHelpers.MatchSlots(process, innerLoopEnd, 0, 2); TestHelpers.MatchSlots(innerLoopEnd, outerLoopEnd, 0, 2); TestHelpers.MatchSlots(outerLoopEnd, end, 0, 0); List <LoopPair> loops = new LoopDetector(TestHelpers.ConvertToDictionary(nodes)).FindLoops(); Assert.AreEqual(2, loops.Count, "incorrect amount of loops detected"); Assert.AreEqual(1, loops[0].Depth); Assert.AreEqual(0, loops[1].Depth); Assert.AreNotSame(loops[0].Id, loops[1].Id); Assert.AreNotSame(loops[0].End.NodeId, loops[1].End.NodeId); }
public void GetLoopSizeTest(int n, int m) { var startNode = LoopDetector.GenerateLinkedNodes(n, m); Assert.AreEqual(m, LoopDetector.GetLoopSizeClever(startNode)); }
static void Main(string[] args) { var path = @"..\..\..\Data\web-baidu-baike-related.edges"; var graphLoader = new AdjacencyListGraphLoader(); var(graph, map) = graphLoader.Load(path); var similarNodeProcessor = new SimilarNodeProcessor(); var loopDetector = new LoopDetector(); var graphCompressor = new GraphCompressor(similarNodeProcessor, loopDetector); var compressedGraph = graphCompressor.Compress(graph); #region Compress ration test var originalGraph = (UndirectedGraph)graph; var originalGraphSize = originalGraph.RawGraphStructure.Count + Enumerable.Sum(originalGraph.RawGraphStructure.Select(x => x.Value.Count)) * 64; var compressedGraphSize = 0.0; foreach (var x in compressedGraph.GraphStructure) { compressedGraphSize += 64; //ReferenceToObject compressedGraphSize += 64; //Id if (x.ReferenceId != null) { compressedGraphSize += 64; //ReferenceId } if (x.ReferenceList != null) { compressedGraphSize += 64; //Reference to ReferenceList compressedGraphSize += x.ReferenceList.Count; } if (x.ExtraNodes != null) { compressedGraphSize += 64; //Reference to ExtraNodeList compressedGraphSize += x.ExtraNodes.Count * 64; } } var kkk = (compressedGraphSize / originalGraphSize) * 100; //43.76 = MAX, 43.68 = 200, 43.57 = 10, 43.55 = 1 | 94 vs 84 var pp = compressedGraph.GraphStructure.Where(x => x.ReferenceId != null).ToList(); #endregion #region Test same nodes var randomGenerator = new Random(); var visitedNodes = new HashSet <int>(); while (true) { var nodeId = randomGenerator.Next(0, compressedGraph.GraphStructure.Count - 1); while (visitedNodes.Contains(nodeId)) { nodeId = randomGenerator.Next(0, compressedGraph.GraphStructure.Count - 1); } visitedNodes.Add(nodeId); var node = compressedGraph.GetNodeById(nodeId); var originalGraphNeighbors = originalGraph.RawGraphStructure[nodeId]; if (originalGraphNeighbors.Count != node.Neighbors.Count) { //BAD Console.WriteLine(); } foreach (var neighbor in originalGraph.RawGraphStructure[nodeId]) { if (!node.Neighbors.Contains(neighbor)) { //BAD Console.WriteLine(); } } } #endregion }
public void RandomLongChainNodesWithLoopSize103387() { var n1 = LoopDetector.createChain(3903004, 2103387); Assert.AreEqual(2103387, Kata.getLoopSize(n1)); }
public void RandomChainNodesWithLoopSize30() { var n1 = LoopDetector.createChain(3, 30); Assert.AreEqual(30, Kata.getLoopSize(n1)); }