public void Should_Throw_Add_Edge_With_Weight_If_It_Already_Exists() { //arrange var graph = new MyGraphAdj <int>(6, true); graph.AddVertex(0, 0); graph.AddVertex(1, 1); graph.AddEdge(0, 1, 1); //act Action act = () => graph.AddEdge(0, 1, 2); //assert act.ShouldThrow <ArgumentException>(); }
public void Should_Check_Negative_Cycle_Simple() { //arrange var graph = new MyGraphAdj <char>(2, true); graph.AddVertex(0, 'A'); graph.AddVertex(1, 'B'); graph.AddEdge(0, 1, -3); graph.AddEdge(1, 0, -5); //act Action act = () => _jonson.Compute(graph); //assert act.ShouldThrow <ArgumentException>(); }
public void Should_Throw_Add_Edge_If_No_vertexes() { //arrange var graph = new MyGraphAdj <int>(6); //act Action act = () => graph.AddEdge(0, 1); //assert act.ShouldThrow <InvalidOperationException>(); }
public void Should_Throw_Update_Edge_If_Index_Out_Of_Range() { //arrange var graph = new MyGraphAdj <int>(6, true); graph.AddVertex(0, 0); graph.AddVertex(1, 1); graph.AddEdge(0, 1, 1); //act Action actLowerFrom = () => graph.UpdateWeight(-1, 1, 2); Action actLowerTo = () => graph.AddEdge(0, -1, 2); Action actHigherFrom = () => graph.AddEdge(10, 1, 2); Action actHigherTo = () => graph.AddEdge(0, 10, 2); //assert actLowerFrom.ShouldThrow <ArgumentOutOfRangeException>(); actLowerTo.ShouldThrow <ArgumentOutOfRangeException>(); actHigherFrom.ShouldThrow <ArgumentOutOfRangeException>(); actHigherTo.ShouldThrow <ArgumentOutOfRangeException>(); }
public void Should_AddVertex() { //arrange var graph = new MyGraphAdj <int>(5); //act Action actLowerFrom = () => graph.AddEdge(-1, 1); Action actHigherFrom = () => graph.AddEdge(6, 1); Action actLowerTo = () => graph.AddEdge(1, -1); Action actHigherTo = () => graph.AddEdge(1, 6); //assert actLowerFrom.ShouldThrow <ArgumentOutOfRangeException>(); actHigherFrom.ShouldThrow <ArgumentOutOfRangeException>(); actLowerTo.ShouldThrow <ArgumentOutOfRangeException>(); actHigherTo.ShouldThrow <ArgumentOutOfRangeException>(); graph.Capacity.ShouldBeEquivalentTo(5); graph.Count.ShouldBeEquivalentTo(0); }
public void Should_Check_Remove_Count() { //arrange var graph = new MyGraphAdj <int>(4); graph.AddVertex(0, 0); graph.AddVertex(1, 1); graph.AddVertex(2, 2); graph.AddVertex(3, 3); graph.AddEdge(0, 2); graph.AddEdge(1, 2); graph.AddEdge(2, 0); graph.AddEdge(2, 1); graph.AddEdge(2, 3); graph.AddEdge(3, 2); //act graph.Remove(2); graph.Remove(7); //assert graph.Capacity.ShouldBeEquivalentTo(4); graph.Count.ShouldBeEquivalentTo(3); }
public void Should_Check_Example() { //arrange var graph = new MyGraphAdj <char>(5, true); graph.AddVertex(0, 'A'); graph.AddVertex(1, 'B'); graph.AddVertex(2, 'C'); graph.AddVertex(3, 'D'); graph.AddEdge(0, 1, -5); graph.AddEdge(0, 2, 2); graph.AddEdge(0, 3, 3); graph.AddEdge(1, 2, 4); graph.AddEdge(2, 3, 1); var distances = new int[, ] { { 0, -5, -1, 0 }, { Int32.MaxValue, 0, 4, 5 }, { Int32.MaxValue, Int32.MaxValue, 0, 1 }, { Int32.MaxValue, Int32.MaxValue, Int32.MaxValue, 0 } }; //act var result = _jonson.Compute(graph); //assert result.GetLength(0).ShouldBeEquivalentTo(distances.GetLength(0)); result.GetLength(1).ShouldBeEquivalentTo(distances.GetLength(1)); for (int i = 0; i < result.GetLength(0); i++) { for (int j = 0; j < result.GetLength(1); j++) { result[i, j].ShouldBeEquivalentTo(distances[i, j]); } } }
private MyGraphAdj <T> ExtendGraph(MyGraphAdj <T> graph) { var result = new MyGraphAdj <T>(graph.Count + 1, true); var vertexes = graph.GetAllVertexes().ToList(); for (int i = 0; i < graph.Count; i++) { result.AddVertex(i, vertexes[i].Key); } foreach (var item in graph.GetAllEdges().ToList()) { result.AddEdge(item[0], item[1], item[2]); } result.AddVertex(graph.Count, (T)((dynamic)vertexes.Max(x => x.Key) + 1)); foreach (var item in vertexes) { result.AddEdge(graph.Count, item.Value, 0); } return(result); }
public void Should_GetAllEdges_Weighted() { //arrange var graph = new MyGraphAdj <int>(6, true); graph.AddVertex(0, 0); graph.AddVertex(1, 1); graph.AddVertex(2, 2); graph.AddVertex(3, 3); graph.AddVertex(4, 4); graph.AddVertex(5, 5); graph.AddEdge(0, 5, 0); graph.AddEdge(0, 1, 1); graph.AddEdge(0, 4, 2); graph.AddEdge(1, 3, 3); graph.AddEdge(1, 4, 4); graph.AddEdge(2, 1, 5); graph.AddEdge(3, 2, 6); graph.AddEdge(3, 4, 7); var edges = new List <int[]> { new int[] { 0, 1, 1 }, new int[] { 0, 4, 2 }, new int[] { 0, 5, 0 }, new int[] { 1, 3, 3 }, new int[] { 1, 4, 4 }, new int[] { 2, 1, 5 }, new int[] { 3, 2, 6 }, new int[] { 3, 4, 7 } }; //act var result = graph.GetAllEdges().ToList(); //assert result.Count.ShouldBeEquivalentTo(edges.Count); for (int i = 0; i < edges.Count; i++) { edges[i].Length.ShouldBeEquivalentTo(result[i].Length); edges[i][0].ShouldBeEquivalentTo(result[i][0]); edges[i][1].ShouldBeEquivalentTo(result[i][1]); edges[i][2].ShouldBeEquivalentTo(result[i][2]); } }
public void Should_Throw_Add_Edge_With_Weight_If_Node_From_Equals_Node_To() { //arrange var graph = new MyGraphAdj <int>(6); graph.AddVertex(0, 0); graph.AddVertex(1, 1); //act Action act = () => graph.AddEdge(0, 0, 5); //assert act.ShouldThrow <ArgumentOutOfRangeException>(); }
public void Should_Throw_Add_Edge_With_Weight_If_Not_Weighted_Graph() { //arrange var graph = new MyGraphAdj <int>(6); graph.AddVertex(0, 0); graph.AddVertex(1, 1); //act Action act = () => graph.AddEdge(0, 1, 5); //assert act.ShouldThrow <InvalidOperationException>(); }
public void Should_GetAllNodes() { //arrange var graph = new MyGraphAdj <int>(6); graph.AddVertex(0, 0); graph.AddVertex(1, 1); graph.AddVertex(2, 2); graph.AddVertex(3, 3); graph.AddVertex(4, 4); graph.AddVertex(5, 5); graph.AddEdge(0, 5); graph.AddEdge(0, 1); graph.AddEdge(0, 4); graph.AddEdge(1, 3); graph.AddEdge(1, 4); graph.AddEdge(2, 1); graph.AddEdge(3, 2); graph.AddEdge(3, 4); var nodes = new List <int[]> { new [] { 0, 0 }, new [] { 1, 1 }, new [] { 2, 2 }, new [] { 3, 3 }, new [] { 4, 4 }, new [] { 5, 5 }, }; //act var result = graph.GetAllVertexes().ToList(); //assert result.Count.ShouldBeEquivalentTo(nodes.Count); for (int i = 0; i < result.Count; i++) { result[i].Key.ShouldBeEquivalentTo(nodes[i][0]); result[i].Value.ShouldBeEquivalentTo(nodes[i][1]); } }
public void Should_Update_Edge_Weight() { //arrange var graph = new MyGraphAdj <int>(6, true); graph.AddVertex(0, 0); graph.AddVertex(1, 1); graph.AddEdge(0, 1, 5); //act graph.UpdateWeight(0, 1, 7); var result = graph.GetAllEdges().ToList(); //assert result.Count.ShouldBeEquivalentTo(1); result[0][0].ShouldBeEquivalentTo(0); result[0][1].ShouldBeEquivalentTo(1); result[0][2].ShouldBeEquivalentTo(7); }
public void Should_RemoveEdge_With_Weight() { //arrange var graph = new MyGraphAdj <int>(6, true); graph.AddVertex(0, 0); graph.AddVertex(1, 1); graph.AddVertex(2, 2); graph.AddVertex(3, 3); graph.AddVertex(4, 4); graph.AddVertex(5, 5); graph.AddEdge(0, 5, 1); graph.AddEdge(0, 1, 2); graph.AddEdge(0, 4, 3); graph.AddEdge(1, 3, 4); graph.AddEdge(1, 4, 5); graph.AddEdge(2, 1, 6); graph.AddEdge(3, 2, 7); graph.AddEdge(3, 4, 8); var resBfs = new[] { 0, 4, 5 }; //act graph.RemoveEdge(0, 1); var result = graph.BreadthFirstSearch(0).ToArray(); //assert result.Length.ShouldBeEquivalentTo(resBfs.Length); for (int i = 0; i < result.Length; i++) { result[i].ShouldBeEquivalentTo(resBfs[i]); } graph.Capacity.ShouldBeEquivalentTo(6); graph.Count.ShouldBeEquivalentTo(6); }
public void Should_DepthFirstSearch() { //arrange var graph = new MyGraphAdj <int>(6); graph.AddVertex(0, 0); graph.AddVertex(1, 1); graph.AddVertex(2, 2); graph.AddVertex(3, 3); graph.AddVertex(4, 4); graph.AddVertex(5, 5); graph.AddEdge(0, 5); graph.AddEdge(0, 1); graph.AddEdge(0, 4); graph.AddEdge(1, 3); graph.AddEdge(1, 4); graph.AddEdge(2, 1); graph.AddEdge(3, 2); graph.AddEdge(3, 4); var resDfs = new[] { 0, 5, 4, 1, 3, 2 }; //act var result = graph.DepthFirstSearch(0).ToArray(); //assert result.Length.ShouldBeEquivalentTo(resDfs.Length); for (int i = 0; i < result.Length; i++) { result[i].ShouldBeEquivalentTo(resDfs[i]); } graph.Capacity.ShouldBeEquivalentTo(6); graph.Count.ShouldBeEquivalentTo(6); }
public void Should_RemoveVertex_With_Weighted_Edges() { //arrange var graph = new MyGraphAdj <int>(4, true); graph.AddVertex(0, 0); graph.AddVertex(1, 1); graph.AddVertex(2, 2); graph.AddVertex(3, 3); graph.AddEdge(0, 2, 1); graph.AddEdge(1, 2, 2); graph.AddEdge(2, 0, 3); graph.AddEdge(2, 1, 4); graph.AddEdge(2, 3, 5); graph.AddEdge(3, 2, 6); //act graph.Remove(2); var first = graph.BreadthFirstSearch(0).ToArray(); var second = graph.BreadthFirstSearch(1).ToArray(); var third = graph.BreadthFirstSearch(3).ToArray(); Action act = () => graph.BreadthFirstSearch(2).ToArray(); //assert first.Length.ShouldBeEquivalentTo(1); second.Length.ShouldBeEquivalentTo(1); third.Length.ShouldBeEquivalentTo(1); first[0].ShouldBeEquivalentTo(0); second[0].ShouldBeEquivalentTo(1); third[0].ShouldBeEquivalentTo(3); act.ShouldThrow <ArgumentException>(); graph.Capacity.ShouldBeEquivalentTo(4); graph.Count.ShouldBeEquivalentTo(3); }
public void Should_BidirectionalSearch_With_Weighted_Edges() { //arrange var graph = new MyGraphAdj <int>(15, true); graph.AddVertex(0, 0); graph.AddVertex(1, 1); graph.AddVertex(2, 2); graph.AddVertex(3, 3); graph.AddVertex(4, 4); graph.AddVertex(5, 5); graph.AddVertex(6, 6); graph.AddVertex(7, 7); graph.AddVertex(8, 8); graph.AddVertex(9, 9); graph.AddVertex(10, 10); graph.AddVertex(11, 11); graph.AddVertex(12, 12); graph.AddVertex(13, 13); graph.AddVertex(14, 14); graph.AddEdge(0, 4, 1); graph.AddEdge(1, 4, 2); graph.AddEdge(2, 5, 0); graph.AddEdge(3, 5, 4); graph.AddEdge(4, 0, 5); graph.AddEdge(4, 1, 6); graph.AddEdge(4, 6, 7); graph.AddEdge(5, 2, 8); graph.AddEdge(5, 3, 9); graph.AddEdge(5, 6, 0); graph.AddEdge(6, 4, 11); graph.AddEdge(6, 5, 12); graph.AddEdge(6, 7, -1); graph.AddEdge(7, 6, -2); graph.AddEdge(7, 8, -3); graph.AddEdge(8, 7, -4); graph.AddEdge(8, 9, 0); graph.AddEdge(8, 10, -6); graph.AddEdge(9, 8, -7); graph.AddEdge(9, 11, -8); graph.AddEdge(9, 12, -9); graph.AddEdge(10, 8, -10); graph.AddEdge(10, 13, -11); graph.AddEdge(10, 14, -12); graph.AddEdge(11, 9, 20); graph.AddEdge(12, 9, 21); graph.AddEdge(13, 10, -20); graph.AddEdge(14, 10, -21); var fromSearch = new[] { 1, 4, 0, 6, 5, 7 }; var toSearch = new[] { 14, 10, 8, 13, 7, 9 }; //act var result = graph.BidirectionalSearch(1, 14); //assert result[0].Count().ShouldBeEquivalentTo(fromSearch.Length); result[1].Count().ShouldBeEquivalentTo(toSearch.Length); for (int i = 0; i < fromSearch.Length; i++) { fromSearch[i].ShouldBeEquivalentTo(result[0].ElementAt(i)); } for (int i = 0; i < toSearch.Length; i++) { toSearch[i].ShouldBeEquivalentTo(result[1].ElementAt(i)); } graph.Capacity.ShouldBeEquivalentTo(15); graph.Count.ShouldBeEquivalentTo(15); }
public void Should_Check_Dijkstra_Negative_Edges() { //arrange var graph = new MyGraphAdj <char>(6, true); graph.AddVertex(0, 'A'); graph.AddVertex(1, 'B'); graph.AddVertex(2, 'C'); graph.AddVertex(3, 'D'); graph.AddVertex(4, 'E'); graph.AddVertex(5, 'F'); graph.AddEdge(0, 1, -7); graph.AddEdge(0, 3, 7); graph.AddEdge(0, 4, 5); graph.AddEdge(1, 0, 8); graph.AddEdge(1, 2, 1); graph.AddEdge(2, 1, 2); graph.AddEdge(2, 3, 3); graph.AddEdge(3, 2, -3); graph.AddEdge(3, 0, 5); graph.AddEdge(3, 5, 6); graph.AddEdge(5, 3, 4); graph.AddEdge(5, 4, 8); graph.AddEdge(4, 0, -3); graph.AddEdge(4, 5, -1); var distances = new int[, ] { { 0, -7, -6, -3, 5, 3 }, { 8, 0, 1, 4, 13, 10 }, { 8, 1, 0, 3, 13, 9 }, { 5, -2, -3, 0, 10, 6 }, { -3, -10, -9, -6, 0, -1 }, { 5, -2, -1, 2, 8, 0 } }; //act var result = _jonson.Compute(graph); //assert result.GetLength(0).ShouldBeEquivalentTo(distances.GetLength(0)); result.GetLength(1).ShouldBeEquivalentTo(distances.GetLength(1)); for (int i = 0; i < result.GetLength(0); i++) { for (int j = 0; j < result.GetLength(1); j++) { result[i, j].ShouldBeEquivalentTo(distances[i, j]); } } }
public void Should_BidirectionalSearch_False() { //arrange var graph = new MyGraphAdj <int>(15); graph.AddVertex(0, 0); graph.AddVertex(1, 1); graph.AddVertex(2, 2); graph.AddVertex(3, 3); graph.AddVertex(4, 4); graph.AddVertex(5, 5); graph.AddVertex(6, 6); graph.AddVertex(8, 8); graph.AddVertex(9, 9); graph.AddVertex(10, 10); graph.AddVertex(11, 11); graph.AddVertex(12, 12); graph.AddVertex(13, 13); graph.AddVertex(14, 14); graph.AddEdge(0, 4); graph.AddEdge(1, 4); graph.AddEdge(2, 5); graph.AddEdge(3, 5); graph.AddEdge(4, 0); graph.AddEdge(4, 1); graph.AddEdge(4, 6); graph.AddEdge(5, 2); graph.AddEdge(5, 3); graph.AddEdge(5, 6); graph.AddEdge(6, 4); graph.AddEdge(6, 5); graph.AddEdge(8, 9); graph.AddEdge(8, 10); graph.AddEdge(9, 8); graph.AddEdge(9, 11); graph.AddEdge(9, 12); graph.AddEdge(10, 8); graph.AddEdge(10, 13); graph.AddEdge(10, 14); graph.AddEdge(11, 9); graph.AddEdge(12, 9); graph.AddEdge(13, 10); graph.AddEdge(14, 10); //act var result = graph.BidirectionalSearch(1, 14); //assert result.ShouldBeEquivalentTo(null); graph.Capacity.ShouldBeEquivalentTo(15); graph.Count.ShouldBeEquivalentTo(14); }
public void Should_Check_Negative_Cycle_Complex() { //arrange var graph = new MyGraphAdj <char>(6, true); graph.AddVertex(0, 'A'); graph.AddVertex(1, 'B'); graph.AddVertex(2, 'C'); graph.AddVertex(3, 'D'); graph.AddVertex(4, 'E'); graph.AddVertex(5, 'F'); graph.AddEdge(0, 1, -7); graph.AddEdge(0, 3, 7); graph.AddEdge(0, 4, 5); graph.AddEdge(1, 0, 8); graph.AddEdge(1, 2, 1); graph.AddEdge(2, 1, 2); graph.AddEdge(2, 3, 3); graph.AddEdge(3, 2, -3); graph.AddEdge(3, 0, 5); graph.AddEdge(3, 5, -6); graph.AddEdge(5, 3, 4); graph.AddEdge(5, 4, 1); graph.AddEdge(4, 0, -3); graph.AddEdge(4, 5, -1); //act Action act = () => _jonson.Compute(graph); //assert act.ShouldThrow <ArgumentException>(); }