public void Test1() { var gn0 = new GraphNode <int>(0); var gn1 = new GraphNode <int>(1); var gn2 = new GraphNode <int>(2); var gn3 = new GraphNode <int>(3); var gn4 = new GraphNode <int>(4); gn0.Add(gn1); gn0.Add(gn2); gn1.Add(gn1); gn1.Add(gn2); gn2.Add(gn0); gn2.Add(gn1); gn3.Add(gn4); gn4.Add(gn3); var graph = new Graph <int>(); graph.Add(gn0, gn1, gn2, gn3, gn4); var sut = new CountUnivistedNodes(); var count = sut.Count(graph, gn0); Assert.That(count, Is.EqualTo(2)); }
public void Test3() { var a = new GraphNode <char>('A'); var b = new GraphNode <char>('B'); var e = new GraphNode <char>('E'); var f = new GraphNode <char>('F'); var d = new GraphNode <char>('D'); a.Add(b); b.Add(e); e.Add(f); f.Add(d); d.Add(a); // another branch var c = new GraphNode <char>('C'); var g = new GraphNode <char>('G'); a.Add(c); c.Add(g); // another branch var x = new GraphNode <char>('X'); var w = new GraphNode <char>('W'); var k = new GraphNode <char>('K'); c.Add(x); x.Add(w); w.Add(k); var detect = new DetectCycleInAGraph(); var hasCycle = detect.HasCycle(a); Assert.IsTrue(hasCycle); }
public void TestBFS() { GraphNode <int> graphRoot = new GraphNode <int>(1); graphRoot.Add(2).Add(4); GraphNode <int> expected = graphRoot.Add(3).Add(5).Add(6); Assert.AreEqual(expected, BFS.Search(graphRoot, 6)); }
public void Three_Nodes_In_Tree_Graph_Returns_Valid_Path_With_Length_Of_Two() { var graphRoot = new GraphNode<int> {Value = 1}; var secondNode = new GraphNode<int> {Value = 2}; var thirdNode = new GraphNode<int> {Value = 3}; graphRoot.Add(secondNode); graphRoot.Add(thirdNode); var walker = new GraphWalker<int>(graphRoot); var path = walker.TraverseTo(3); Assert.AreEqual(true, path.IsValid); Assert.AreEqual(2, path.Count(), "Path length should be two (nodes) for three node tree graph"); }
public void Test2() { GraphNode <int> one = new GraphNode <int>(1); GraphNode <int> two = new GraphNode <int>(2); one.Add(two); GraphNode <int> three = new GraphNode <int>(3); GraphNode <int> four = new GraphNode <int>(4); three.Add(four); bool result = impl.HasCycle(new List <GraphNode <int> >() { one, two, three, four }); Assert.That(result, Is.False); Assert.That(impl.Set.Count(), Is.EqualTo(4), "Invalid set count"); Assert.That(impl.Set[0], Is.EqualTo(-2)); Assert.That(impl.Set[1], Is.EqualTo(0)); Assert.That(impl.Set[2], Is.EqualTo(-2)); Assert.That(impl.Set[3], Is.EqualTo(2)); }
public void Test1() { GraphNode <int> warsaw = new GraphNode <int>(0); GraphNode <int> ostrow = new GraphNode <int>(1); GraphNode <int> kalisz = new GraphNode <int>(2); GraphNode <int> wroclaw = new GraphNode <int>(3); warsaw.Add(ostrow); warsaw.Add(kalisz); kalisz.Add(wroclaw); var result = impl.Bfs(warsaw); Console.WriteLine(String.Join(",", result)); Assert.That(result.SequenceEqual(new int[] { 0, 1, 2, 3 }), Is.True); }
public void Test1() { var a = new GraphNode <int>(0); var b = new GraphNode <int>(5); var c = new GraphNode <int>(10); var d = new GraphNode <int>(15); a.Add(b); b.Add(c); c.Add(d); var e = new GraphNode <int>(20); var f = new GraphNode <int>(30); a.Add(e); e.Add(f); //var result = dijkstra.ShortestPath(a, d); //Console.WriteLine(String.Join(",", result)); }
public void Test2() { var start = new GraphNode <int>(10); var twenty = new GraphNode <int>(20); var thirty = new GraphNode <int>(30); start.Add(twenty); twenty.Add(thirty); var detect = new DetectCycleInAGraph(); var hasCycle = detect.HasCycle(start); Assert.IsFalse(hasCycle); }
public void Test1() { var warsaw = new GraphNode <int>(0); var ostrow = new GraphNode <int>(1); var wroclaw = new GraphNode <int>(2); var kalisz = new GraphNode <int>(3); var gdansk = new GraphNode <int>(4); var gdynia = new GraphNode <int>(5); warsaw.Add(ostrow); ostrow.Add(wroclaw); wroclaw.Add(kalisz); warsaw.Add(gdansk); gdansk.Add(gdynia); var result = new List <int>(); impl.Dfs(warsaw, result); Assert.That(result.SequenceEqual(new int[] { 0, 1, 2, 3, 4, 5 }), Is.True); }
public void Five_Nodes_In_A_Graph_Returns_Valid_Path_With_Length_Of_Three() { var graphRoot = new GraphNode<int> {Value = 1}; var secondNode = new GraphNode<int> {Value = 2}; var thirdNode = new GraphNode<int> {Value = 3}; var fourthNode = new GraphNode<int> {Value = 4}; var fifthNode = new GraphNode<int> {Value = 5}; //first level graphRoot.Add(secondNode); graphRoot.Add(thirdNode); //second level thirdNode.Add(fourthNode); secondNode.Add(fifthNode); fourthNode.Add(graphRoot); var walker = new GraphWalker<int>(graphRoot); var path = walker.TraverseTo(5); Assert.AreEqual(true, path.IsValid); Assert.AreEqual(3, path.Count(), "Path length should be three (nodes) for six node graph"); }
public void Test5() { GraphNode <int> one = new GraphNode <int>(1); GraphNode <int> two = new GraphNode <int>(2); one.Add(two); GraphNode <int> three = new GraphNode <int>(3); GraphNode <int> four = new GraphNode <int>(4); three.Add(four); GraphNode <int> five = new GraphNode <int>(5); GraphNode <int> six = new GraphNode <int>(6); five.Add(six); var seven = new GraphNode <int>(7); var eight = new GraphNode <int>(8); seven.Add(eight); var two2 = new GraphNode <int>(2); var four2 = new GraphNode <int>(4); two2.Add(four2); bool result = impl.HasCycle(new List <GraphNode <int> >() { one, two, three, four, five, six, seven, eight, two2, four2 }); Assert.That(result, Is.False); Assert.That(impl.Set.Count(), Is.EqualTo(8), "Invalid set count"); Assert.That(impl.Set[0], Is.EqualTo(-4)); Assert.That(impl.Set[1], Is.EqualTo(0)); Assert.That(impl.Set[2], Is.EqualTo(1)); Assert.That(impl.Set[3], Is.EqualTo(2)); Assert.That(impl.Set[4], Is.EqualTo(-2)); Assert.That(impl.Set[5], Is.EqualTo(4)); Assert.That(impl.Set[6], Is.EqualTo(-2)); Assert.That(impl.Set[7], Is.EqualTo(6)); }
public void Test1() { GraphNode <int> one = new GraphNode <int>(1); GraphNode <int> two = new GraphNode <int>(2); one.Add(two); bool result = impl.HasCycle(new List <GraphNode <int> >() { one, two }); Assert.That(result, Is.False); Assert.That(impl.Set.Count(), Is.EqualTo(2), "Invalid set count"); Assert.That(impl.Set[0], Is.EqualTo(-2)); Assert.That(impl.Set[1], Is.EqualTo(0)); }
public void Two_Paths_To_Target_Returns_Shortest_Valid_Path() { var graphRoot = new GraphNode<int> {Value = 1}; var secondNode = new GraphNode<int> {Value = 2}; var thirdNode = new GraphNode<int> {Value = 3}; var fourthNode = new GraphNode<int> {Value = 4}; var fifthNode = new GraphNode<int> {Value = 5}; graphRoot.Add(secondNode); graphRoot.Add(thirdNode); secondNode.Add(fourthNode); thirdNode.Add(fifthNode); fourthNode.Add(fifthNode); var walker = new GraphWalker<int>(graphRoot); var path = walker.TraverseTo(5); Assert.AreEqual(true, path.IsValid); Assert.AreEqual(3, path.Count(), "Path length should be three (nodes) for five node graph"); }
public void Value_Not_In_Graph_Throws_Arg_Out_Of_Range() { var graphRoot = new GraphNode<int> {Value = 1}; var secondNode = new GraphNode<int> {Value = 2}; var thirdNode = new GraphNode<int> {Value = 3}; graphRoot.Add(secondNode); graphRoot.Add(thirdNode); var walker = new GraphWalker<int>(graphRoot); var exceptional = false; try { walker.TraverseTo(5); } catch (ArgumentOutOfRangeException ex) { exceptional = true; Assert.AreEqual(5, ex.ActualValue); Assert.AreEqual("value", ex.ParamName); } Assert.IsTrue(exceptional, "Expected to catch an argument out of range exception"); }