public void TwoEmptyGraphSameTest() { Graph g1 = new Graph(); Graph g2 = new Graph(); Assert.IsTrue(g1.Equals(g2)); }
public void TwoGraphWithSameIdSameTest() { Graph g1 = new Graph(1); Graph g2 = new Graph(1); Assert.IsTrue(g1.Equals(g2)); }
public void TwoGraphWithDifferentIdSameTest() { Graph g1 = new Graph(1); Graph g2 = new Graph(2); Assert.IsFalse(g1.Equals(g2)); }
public void TwoGraphWithSameNodeWithoutEdges() { Graph g1 = new Graph(1); Graph g2 = new Graph(2); Node n = new Node("n"); g1.AddNode(n); g2.AddNode(n); Assert.IsFalse(g1.Equals(g2)); }
public void AddGraph(Graph graph) { try { Graphs.Add(graph.Identifier, graph); } catch (ArgumentException) { Graphs.Remove(graph.Identifier); Graphs.Add(graph.Identifier, graph); } }
public void TwoGraphWithEqualNodesWithoutEdges() { Graph g1 = new Graph(1); Graph g2 = new Graph(2); Node n1 = new Node("n"); Node n2 = new Node("n"); g1.AddNode(n1); g2.AddNode(n2); Assert.IsFalse(g1.Equals(g2)); }
public void TwoGraphWithTwoEqualNodesWithoutEdges() { Graph g1 = new Graph(1); Graph g2 = new Graph(2); Node n1 = new Node("n1"); Node n2 = new Node("n2"); Node n3 = new Node("n3"); Node n4 = new Node("n4"); g1.AddNode(n1); g1.AddNode(n2); g2.AddNode(n3); g2.AddNode(n4); Assert.IsFalse(g1.Equals(g2)); }
public void ShortestPathTwoNodesNoPaths() { Graph g = new Graph(1); Node a = new Node("a"); Node b = new Node("b"); g.AddNode(a); g.AddNode(b); Path actual = g.ShortestPath(a, b); Assert.IsNull(actual); }
public void RemoveNodeWithEdgesInGraphFromGraph() { Graph actual = new Graph(1); Graph expected = new Graph(1); Node a = new Node("a"); Node b = new Node("b"); Edge ab = new Edge(2); actual.AddNode(a); actual.AddNode(b); actual.AddUndirectedEdge(new Tuple<Node, int>(a, 0), new Tuple<Node, int>(b, 0), ab); actual.RemoveNode(b); expected.AddNode(a); Assert.IsTrue(actual.Equals(expected)); }
public void RemoveNodeInGraphFromGraph() { Graph actual = new Graph(1); Graph expected = new Graph(1); Node a = new Node("a"); Node b = new Node("b"); actual.AddNode(a); actual.AddNode(b); actual.RemoveNode(b); expected.AddNode(a); Assert.IsTrue(actual.Equals(expected)); }
// Populates the database with the graph private static void PopulateDatabaseWithGraph() { // Check if there is a graph else create a graph if (Database.Instance.Data.Graphs.Count <= 0) { // Instanciate graph Graph warehouse = new Graph(1); // Instanciate all nodes Node A = new Node("A"); Node B = new Node("B"); Node C = new Node("C"); Node D = new Node("D"); Node E = new Node("E"); Node F = new Node("F"); Node G = new Node("G"); Node H = new Node("H"); Node I = new Node("I"); Node J = new Node("J"); Node K = new Node("K"); Node L = new Node("L"); Node M = new Node("M"); Node N = new Node("N"); Node O = new Node("O"); Node P = new Node("P"); Node Q = new Node("Q"); // Add the nodes to the graph warehouse.AddNode(A); warehouse.AddNode(B); warehouse.AddNode(C); warehouse.AddNode(D); warehouse.AddNode(E); warehouse.AddNode(F); warehouse.AddNode(G); warehouse.AddNode(H); warehouse.AddNode(I); warehouse.AddNode(J); warehouse.AddNode(K); warehouse.AddNode(L); warehouse.AddNode(M); warehouse.AddNode(N); warehouse.AddNode(O); warehouse.AddNode(P); warehouse.AddNode(Q); warehouse.AddUndirectedEdge(new Tuple<Node, int>(A, 1), new Tuple<Node, int>(B, 3), 4); warehouse.AddUndirectedEdge(new Tuple<Node, int>(A, 3), new Tuple<Node, int>(P, 0), 2); warehouse.AddUndirectedEdge(new Tuple<Node, int>(A, 2), new Tuple<Node, int>(N, 0), 1); warehouse.AddUndirectedEdge(new Tuple<Node, int>(B, 1), new Tuple<Node, int>(C, 0), 2); warehouse.AddUndirectedEdge(new Tuple<Node, int>(B, 2), new Tuple<Node, int>(H, 0), 1); warehouse.AddUndirectedEdge(new Tuple<Node, int>(C, 1), new Tuple<Node, int>(D, 3), 1); warehouse.AddUndirectedEdge(new Tuple<Node, int>(C, 2), new Tuple<Node, int>(F, 0), 1); warehouse.AddUndirectedEdge(new Tuple<Node, int>(E, 3), new Tuple<Node, int>(F, 1), 1); warehouse.AddUndirectedEdge(new Tuple<Node, int>(F, 2), new Tuple<Node, int>(G, 1), 2); warehouse.AddUndirectedEdge(new Tuple<Node, int>(G, 0), new Tuple<Node, int>(H, 2), 1); warehouse.AddUndirectedEdge(new Tuple<Node, int>(G, 3), new Tuple<Node, int>(Q, 1), 1); warehouse.AddUndirectedEdge(new Tuple<Node, int>(H, 3), new Tuple<Node, int>(J, 1), 1); warehouse.AddUndirectedEdge(new Tuple<Node, int>(I, 2), new Tuple<Node, int>(J, 0), 1); warehouse.AddUndirectedEdge(new Tuple<Node, int>(J, 3), new Tuple<Node, int>(L, 1), 1); warehouse.AddUndirectedEdge(new Tuple<Node, int>(J, 2), new Tuple<Node, int>(Q, 0), 1); warehouse.AddUndirectedEdge(new Tuple<Node, int>(K, 2), new Tuple<Node, int>(L, 0), 1); warehouse.AddUndirectedEdge(new Tuple<Node, int>(L, 2), new Tuple<Node, int>(M, 0), 1); warehouse.AddUndirectedEdge(new Tuple<Node, int>(L, 3), new Tuple<Node, int>(N, 1), 1); warehouse.AddUndirectedEdge(new Tuple<Node, int>(N, 2), new Tuple<Node, int>(O, 0), 1); warehouse.AddUndirectedEdge(new Tuple<Node, int>(N, 3), new Tuple<Node, int>(P, 1), 1); warehouse.AddUndirectedEdge(new Tuple<Node, int>(P, 2), new Tuple<Node, int>(Q, 3), 4); // Save it to the database Database.Instance.Data.AddGraph(warehouse); } }
public void ShortestPathToBlockedEdge() { Graph graph = new Graph(1); Node a = new Node("a"); Node b = new Node("b"); Node c = new Node("c"); Node d = new Node("d"); Node e = new Node("e"); graph.AddNode(a); graph.AddNode(b); graph.AddNode(c); graph.AddNode(d); graph.AddNode(e); graph.AddUndirectedEdge(new Tuple<Node, int>(a, 1), new Tuple<Node, int>(b, 3), 1); graph.AddUndirectedEdge(new Tuple<Node, int>(b, 2), new Tuple<Node, int>(c, 0), 2); graph.AddUndirectedEdge(new Tuple<Node, int>(c, 3), new Tuple<Node, int>(d, 1), 3); //block this graph.AddUndirectedEdge(new Tuple<Node, int>(d, 0), new Tuple<Node, int>(e, 2), 200); graph.AddUndirectedEdge(new Tuple<Node, int>(e, 1), new Tuple<Node, int>(a, 3), 100); graph.BlockEdge(c, d); Path expectedPath = new Path() { Identifier = 1, Nodes = new List<Node>(){a, b, c} }; Path actualPath = graph.PathToBlockedEdge(a, graph.BlockedEdges.FirstOrDefault()); actualPath.Identifier = 1; Assert.IsTrue(expectedPath.Equals(actualPath)); }
public void RemoveNodeNotInGraphFromGraph() { Graph graph = new Graph(1); Node a = new Node("a"); Node b = new Node("b"); graph.AddNode(a); Assert.IsFalse(graph.RemoveNode(b)); }
public void ShortestPathThreeNodesTwoPaths() { Graph g = new Graph(1); Node a = new Node("a"); Node b = new Node("b"); Node c = new Node("c"); Edge ab = new Edge(20); Edge ac = new Edge(10); Edge cb = new Edge(5); g.AddNode(a); g.AddNode(b); g.AddNode(c); g.AddUndirectedEdge(new Tuple<Node, int>(a, 0), new Tuple<Node, int>(b, 0), ab); g.AddUndirectedEdge(new Tuple<Node, int>(a, 1), new Tuple<Node, int>(c, 0), ac); g.AddUndirectedEdge(new Tuple<Node, int>(c, 1), new Tuple<Node, int>(b, 1), cb); Path actual = g.ShortestPath(a, b); Path expected = new Path(); expected.Nodes.Add(a); expected.Nodes.Add(c); expected.Nodes.Add(b); Assert.IsTrue(expected.Equals(actual)); }
public void TwoGraphWithThreeNodeWithSingleEdgeOneRemoved() { Graph g1 = new Graph(1); Graph g2 = new Graph(2); Node a = new Node("a"); Node b = new Node("b"); Node c = new Node("c"); Edge ab = new Edge(1); Edge bc = new Edge(2); g1.AddNode(a); g1.AddNode(b); g1.AddNode(c); g1.AddUndirectedEdge(new Tuple<Node, int>(a, 0), new Tuple<Node, int>(b, 0), ab); g1.AddUndirectedEdge(new Tuple<Node, int>(b, 1), new Tuple<Node, int>(c, 0), bc); g1.RemoveUndirectedEdge(b, c); g2.AddNode(a); g2.AddNode(b); g2.AddNode(c); g1.AddUndirectedEdge(new Tuple<Node, int>(a, 0), new Tuple<Node, int>(b, 0), ab); g1.AddUndirectedEdge(new Tuple<Node, int>(b, 1), new Tuple<Node, int>(c, 0), bc); g2.RemoveUndirectedEdge(b, c); Assert.IsFalse(g1.Equals(g2)); }
public void ShortestPathTwoNodes() { Graph g = new Graph(1); Node a = new Node("a"); Node b = new Node("b"); Edge ab = new Edge(10); g.AddNode(a); g.AddNode(b); g.AddUndirectedEdge(new Tuple<Node, int>(a, 0), new Tuple<Node, int>(b, 0), ab); Path actual = g.ShortestPath(a, b); Path expected = new Path(); expected.Nodes.Add(a); expected.Nodes.Add(b); Assert.IsTrue(actual.Equals(expected)); }
public void TwoGraphWithTwoNodeOneRemoved() { Graph g1 = new Graph(1); Graph g2 = new Graph(2); Node a = new Node("a"); Node b = new Node("b"); Node c = new Node("b"); g1.AddNode(a); g1.AddNode(b); g1.AddNode(c); g1.RemoveNode(c); g2.AddNode(a); g2.AddNode(b); g2.AddNode(c); g2.RemoveNode(c); Assert.IsFalse(g1.Equals(g2)); }
public void UnblockEdgeInGraph() { Graph graph = new Graph(1); Node a = new Node("a"); Node b = new Node("b"); graph.AddNode(a); graph.AddNode(b); var tupleOne = new Tuple<Node, int>(a, 1); var tupleTwo = new Tuple<Node, int>(b, 3); graph.AddUndirectedEdge(tupleOne, tupleTwo, 5); graph.BlockEdge(a, b); graph.UnblockEdge(a, b); Assert.IsFalse(graph.BlockedEdges.Exists(x => x.Item1.Equals(a) && x.Item2.Equals(b))); Assert.IsFalse(a.BlockedNeighbours.Exists(x => x.Key != null && x.Key.Equals(b))); Assert.IsFalse(b.BlockedNeighbours.Exists(x => x.Key != null && x.Key.Equals(a))); Assert.IsTrue(a.Neighbours.Exists(x => x.Key != null && x.Key.Equals(b))); Assert.IsTrue(b.Neighbours.Exists(x => x.Key != null && x.Key.Equals(a))); }
public void FindNodeInGraphByNameBothUppercase() { Graph g = new Graph(); Node expected = new Node("A"); g.AddNode(expected); Node actual = g.getNode("A"); Assert.IsTrue(expected.Equals(actual)); }
public void TestEdgeWeightChangeAfterVisit() { Graph g = new Graph(1); Node a = new Node("a"); Node b = new Node("b"); Node c = new Node("c"); Edge ab = new Edge(2) { Visited = 99, Blocked = 50 }; Edge bc = new Edge(2) { Visited = 99, Blocked = 50 }; g.AddNode(a); g.AddNode(b); g.AddNode(c); g.AddUndirectedEdge(new Tuple<Node, int>(a, 1), new Tuple<Node, int>(b, 3), ab); g.AddUndirectedEdge(new Tuple<Node, int>(b, 1), new Tuple<Node, int>(c, 3), bc); ab.Visited += 1; bc.Visited += 1; const double expected = 4.0; double actualab = ab.Weight; double actualbc = bc.Weight; Assert.IsTrue(Math.Abs(expected - actualab) < 1); Assert.IsTrue(Math.Abs(expected - actualbc) < 1); }
public void FindNodeInGraphByNameDifferentLettercaseTwo() { Graph g = new Graph(); Node expected = new Node("A"); g.AddNode(expected); Node actual = g.getNode("a"); Assert.IsTrue(expected.Equals(actual)); }
public void TestGraphSmallerWeightsUponDecay() { Graph g = new Graph(1); Node a = new Node("a"); Node b = new Node("b"); Edge ab = new Edge(2) { Visited = 20, Blocked = 1 }; g.AddNode(a); g.AddNode(b); g.AddUndirectedEdge(new Tuple<Node, int>(a, 1), new Tuple<Node, int>(b, 3), ab); g.Decay(0.5); double actual = ab.Weight; const double expected = 2.05; Assert.IsTrue(Math.Abs(actual - expected) < 0.01); }
public void TryFindNotExistingNodeInGraph() { Graph g = new Graph(); Node searchNode = g.getNode("a"); }
public void TwoGraphWithTwoNodeWithSingleEdge() { Graph g1 = new Graph(1); Graph g2 = new Graph(2); Node a = new Node("a"); Node b = new Node("b"); Edge ab = new Edge(1); g1.AddNode(a); g1.AddNode(b); g1.AddUndirectedEdge(new Tuple<Node, int>(a, 0), new Tuple<Node, int>(b, 0), ab); g2.AddNode(a); g2.AddNode(b); g2.AddUndirectedEdge(new Tuple<Node, int>(a, 0), new Tuple<Node, int>(b, 0), ab); Assert.IsFalse(g1.Equals(g2)); }
public void TryFindNodeInGraphWithMultipleSameNode() { Graph g = new Graph(); Node sameNodeOne = new Node("a"); Node sameNodeTwo = new Node("A"); g.AddNode(sameNodeOne); g.AddNode(sameNodeTwo); Node searchNode = g.getNode("a"); }
public bool RemoveGraph(Graph graph) { bool result = Graphs.Remove(graph.Identifier); return result; }
// Populates the database with the graph private static void PopulateDatabaseWithGraph() { // Check if there is a graph else create a graph if (Database.Instance.Data.Graphs.Count <= 0) { // Instanciate graph Graph warehouse = new Graph(1); // Instanciate all nodes Node A = new Node("A"); Node B = new Node("B"); Node C = new Node("C"); Node D = new Node("D", true); Node G = new Node("G"); Node H = new Node("H"); Node I = new Node("I", true); Node J = new Node("J"); Node N = new Node("N"); Node O = new Node("O", true); Node P = new Node("P"); Node Q = new Node("Q"); // Add the nodes to the graph warehouse.AddNode(A); warehouse.AddNode(B); warehouse.AddNode(C); warehouse.AddNode(D); warehouse.AddNode(G); warehouse.AddNode(H); warehouse.AddNode(I); warehouse.AddNode(J); warehouse.AddNode(N); warehouse.AddNode(O); warehouse.AddNode(P); warehouse.AddNode(Q); warehouse.AddUndirectedEdge(new Tuple<Node, int>(A, 1), new Tuple<Node, int>(B, 3), 113); warehouse.AddUndirectedEdge(new Tuple<Node, int>(A, 3), new Tuple<Node, int>(P, 0), 100); warehouse.AddUndirectedEdge(new Tuple<Node, int>(A, 2), new Tuple<Node, int>(N, 0), 51); warehouse.AddUndirectedEdge(new Tuple<Node, int>(B, 1), new Tuple<Node, int>(C, 0), 90); warehouse.AddUndirectedEdge(new Tuple<Node, int>(B, 2), new Tuple<Node, int>(H, 0), 51); warehouse.AddUndirectedEdge(new Tuple<Node, int>(C, 1), new Tuple<Node, int>(D, 3), 43); warehouse.AddUndirectedEdge(new Tuple<Node, int>(C, 2), new Tuple<Node, int>(G, 1), 90); warehouse.AddUndirectedEdge(new Tuple<Node, int>(G, 0), new Tuple<Node, int>(H, 2), 50); warehouse.AddUndirectedEdge(new Tuple<Node, int>(G, 3), new Tuple<Node, int>(Q, 1), 60); warehouse.AddUndirectedEdge(new Tuple<Node, int>(H, 3), new Tuple<Node, int>(J, 1), 60); warehouse.AddUndirectedEdge(new Tuple<Node, int>(I, 2), new Tuple<Node, int>(J, 0), 41); warehouse.AddUndirectedEdge(new Tuple<Node, int>(J, 3), new Tuple<Node, int>(N, 1), 52); warehouse.AddUndirectedEdge(new Tuple<Node, int>(J, 2), new Tuple<Node, int>(Q, 0), 50); warehouse.AddUndirectedEdge(new Tuple<Node, int>(N, 2), new Tuple<Node, int>(O, 0), 41); warehouse.AddUndirectedEdge(new Tuple<Node, int>(N, 3), new Tuple<Node, int>(P, 1), 51); warehouse.AddUndirectedEdge(new Tuple<Node, int>(P, 2), new Tuple<Node, int>(Q, 3), 146); // Save it to the database Database.Instance.Data.AddGraph(warehouse); } }