public void TestPrint() { var graph = new DirectedGraph <string>(); var v1 = "One"; var v2 = "Two"; var v3 = "Three"; var v4 = "Four"; var v5 = "Five"; graph.AddVertex(v1); graph.AddVertex(v2); graph.AddVertex(v3); graph.AddVertex(v4); graph.AddVertex(v5); graph.AddEdge(v1, v2); graph.AddEdge(v2, v4); graph.AddEdge(v3, v2); graph.AddEdge(v3, v4); graph.AddEdge(v3, v5); graph.AddEdge(v4, v1); graph.AddEdge(v5, v3); Console.WriteLine(graph.Print()); }
public void TestPriorityCalculation1NoWitnesses() { var graph = new DirectedGraph<CHEdgeData>(); var vertex1 = graph.AddVertex(1, 0); var vertex2 = graph.AddVertex(2, 0); var vertex3 = graph.AddVertex(3, 0); graph.AddEdge(vertex1, vertex3, new CHEdgeData(1, true, true, true, 10)); graph.AddEdge(vertex3, vertex1, new CHEdgeData(1, false, true, true, 10)); graph.AddEdge(vertex2, vertex3, new CHEdgeData(1, true, true, true, 10)); graph.AddEdge(vertex3, vertex2, new CHEdgeData(1, false, true, true, 10)); var witnessCalculator = new DykstraWitnessCalculator(int.MaxValue); var priorityCalculator = new EdgeDifferenceContractedSearchSpace(graph, witnessCalculator); int newEdges, removedEdges, contracted, depth; priorityCalculator.Calculate(vertex1, out newEdges, out removedEdges, out depth, out contracted); Assert.AreEqual(0, newEdges); Assert.AreEqual(1, removedEdges); Assert.AreEqual(0, depth); Assert.AreEqual(0, contracted); priorityCalculator.Calculate(vertex2, out newEdges, out removedEdges, out depth, out contracted); Assert.AreEqual(0, newEdges); Assert.AreEqual(1, removedEdges); Assert.AreEqual(0, depth); Assert.AreEqual(0, contracted); priorityCalculator.Calculate(vertex3, out newEdges, out removedEdges, out depth, out contracted); Assert.AreEqual(2, newEdges); Assert.AreEqual(2, removedEdges); Assert.AreEqual(0, depth); Assert.AreEqual(0, contracted); }
public void TestDFSFail() { var graph = new DirectedGraph <string>(); var v1 = "One"; var v2 = "Two"; var v3 = "Three"; var v4 = "Four"; var v5 = "Five"; graph.AddVertex(v1); graph.AddVertex(v2); graph.AddVertex(v3); graph.AddVertex(v4); graph.AddVertex(v5); graph.AddEdge(v1, v2); graph.AddEdge(v2, v4); graph.AddEdge(v3, v5); var expected = new LinkedList <string>(); var actual = DepthFirstSearch <string> .Search(graph, v1, v5); Assert.AreEqual(expected, actual); }
public void TestPriorityCalculation1NoWitnesses() { var graph = new DirectedGraph <CHEdgeData>(); var vertex1 = graph.AddVertex(1, 0); var vertex2 = graph.AddVertex(2, 0); var vertex3 = graph.AddVertex(3, 0); graph.AddEdge(vertex1, vertex3, new CHEdgeData(1, true, true, true, 10)); graph.AddEdge(vertex3, vertex1, new CHEdgeData(1, false, true, true, 10)); graph.AddEdge(vertex2, vertex3, new CHEdgeData(1, true, true, true, 10)); graph.AddEdge(vertex3, vertex2, new CHEdgeData(1, false, true, true, 10)); var witnessCalculator = new DykstraWitnessCalculator(int.MaxValue); var priorityCalculator = new EdgeDifferenceContractedSearchSpace(graph, witnessCalculator); int newEdges, removedEdges, contracted, depth; priorityCalculator.Calculate(vertex1, out newEdges, out removedEdges, out depth, out contracted); Assert.AreEqual(0, newEdges); Assert.AreEqual(1, removedEdges); Assert.AreEqual(0, depth); Assert.AreEqual(0, contracted); priorityCalculator.Calculate(vertex2, out newEdges, out removedEdges, out depth, out contracted); Assert.AreEqual(0, newEdges); Assert.AreEqual(1, removedEdges); Assert.AreEqual(0, depth); Assert.AreEqual(0, contracted); priorityCalculator.Calculate(vertex3, out newEdges, out removedEdges, out depth, out contracted); Assert.AreEqual(2, newEdges); Assert.AreEqual(2, removedEdges); Assert.AreEqual(0, depth); Assert.AreEqual(0, contracted); }
public void FindAllEdges_ToItself_Indirectly() { DirectedGraph <string, string> graph = new DirectedGraph <string, string>(); graph.AddVertex("A"); graph.AddVertex("O"); graph.AddEdge("A", "O", "Pattern1"); graph.AddEdge("O", "A", "Pattern1"); graph.AddEdge("O", "A", "Pattern2"); // Get all paths from O to O. List <IReadOnlyList <DirectedEdge <string, string> > > result = graph.FindAllEdges("O", "O").ToList(); Assert.AreEqual(2, result.Count); Assert.AreEqual(2, result[0].Count); Assert.AreEqual("O", result[0][0].From); Assert.AreEqual("A", result[0][0].To); Assert.AreEqual("Pattern1", result[0][0].Value); Assert.AreEqual("A", result[0][1].From); Assert.AreEqual("O", result[0][1].To); Assert.AreEqual("Pattern1", result[0][1].Value); Assert.AreEqual("O", result[1][0].From); Assert.AreEqual("A", result[1][0].To); Assert.AreEqual("Pattern2", result[1][0].Value); Assert.AreEqual("A", result[1][1].From); Assert.AreEqual("O", result[1][1].To); Assert.AreEqual("Pattern1", result[1][1].Value); }
public void CreateEmptyDirectedGraphAndAddVertices() { var graph = new DirectedGraph(); Assert.AreEqual(0, graph.Vertices()); Assert.AreEqual(0, graph.Edges()); int vertex1id = graph.AddVertex(); int vertex2id = graph.AddVertex(); Assert.AreEqual(0, vertex1id); Assert.AreEqual(1, vertex2id); graph.AddEdge(0, 1); Assert.AreEqual(2, graph.Vertices()); Assert.AreEqual(1, graph.Edges()); Assert.IsTrue(graph.AdjacentVertices(0).Contains(1)); Assert.IsFalse(graph.AdjacentVertices(1).Contains(0)); // add the same edge again graph.AddEdge(1, 0); Assert.AreEqual(2, graph.Vertices()); Assert.AreEqual(2, graph.Edges()); Assert.IsTrue(graph.AdjacentVertices(0).Contains(1)); Assert.IsTrue(graph.AdjacentVertices(1).Contains(0)); }
public void CloneTest() { var graph = new DirectedGraph(); var newVertex1 = new Vertex("test-1"); var newVertex2 = new Vertex("test-2"); var newVertex3 = new Vertex("test-3"); graph.AddVertex(newVertex1); graph.AddVertex(newVertex2); graph.AddVertex(newVertex3); var newEdge1 = new DirectedEdge(newVertex1, newVertex2); var newEdge2 = new DirectedEdge(newVertex1, newVertex3); graph.AddEdge(newEdge1); graph.AddEdge(newEdge2); var clonedGraph = graph.Clone() as IGraph; Assert.IsTrue(clonedGraph is DirectedGraph); Assert.AreEqual(graph.VerticesCount, clonedGraph.VerticesCount); foreach (var vertex in graph.Vertices) { var clonedVertex = clonedGraph.Vertices.Single(v => v.Equals(vertex)); Assert.AreNotSame(clonedVertex, vertex); } Assert.AreEqual(graph.EdgesCount, clonedGraph.EdgesCount); foreach (var clonedEdge in clonedGraph.Edges) { Assert.IsTrue(clonedEdge is DirectedEdge); var edge = graph.Edges.Single(e => e.Equals(clonedEdge)); Assert.AreNotSame(edge, clonedEdge); } }
public void TestVerifiedContraction4ReplacePrevious() { var graph = new DirectedGraph <CHEdgeData>(); var vertex1 = graph.AddVertex(1, 0); var vertex2 = graph.AddVertex(2, 0); var vertex3 = graph.AddVertex(3, 0); var vertex4 = graph.AddVertex(4, 0); graph.AddEdge(vertex1, vertex4, new CHEdgeData(1, true, true, true, 15)); graph.AddEdge(vertex4, vertex1, new CHEdgeData(1, false, true, true, 15)); graph.AddEdge(vertex2, vertex4, new CHEdgeData(1, true, true, true, 15)); graph.AddEdge(vertex4, vertex2, new CHEdgeData(1, false, true, true, 15)); var witnessCalculator = new DykstraWitnessCalculator(); var preProcessor = new CHPreProcessor(graph, new EdgeDifferenceContractedSearchSpace(graph, witnessCalculator), witnessCalculator); preProcessor.Contract(4); Assert.IsTrue(graph.ContainsEdge(vertex1, vertex2, new CHEdgeData(4, true, true, 30))); Assert.IsTrue(graph.ContainsEdge(vertex2, vertex1, new CHEdgeData(4, true, true, 30))); // add edges later to prevent witnesses from being found! graph.AddEdge(vertex1, vertex3, new CHEdgeData(1, true, true, true, 10)); graph.AddEdge(vertex3, vertex1, new CHEdgeData(1, false, true, true, 10)); graph.AddEdge(vertex2, vertex3, new CHEdgeData(1, true, true, true, 10)); graph.AddEdge(vertex3, vertex2, new CHEdgeData(1, false, true, true, 10)); preProcessor.Contract(3); Assert.IsTrue(graph.ContainsEdge(vertex1, vertex2, new CHEdgeData(3, true, true, 20))); Assert.IsTrue(graph.ContainsEdge(vertex2, vertex1, new CHEdgeData(3, true, true, 20))); }
public void TestVerifiedContraction6DuplicateForward() { var graph = new DirectedGraph <CHEdgeData>(); var witnessCalculator = new DykstraWitnessCalculator(); var preProcessor = new CHPreProcessor(graph, new EdgeDifferenceContractedSearchSpace(graph, witnessCalculator), witnessCalculator); var vertex1 = graph.AddVertex(1, 0); var vertex2 = graph.AddVertex(2, 0); var vertex3 = graph.AddVertex(3, 0); var vertex4 = graph.AddVertex(4, 0); graph.AddEdge(vertex1, vertex3, new CHEdgeData(1, true, false, true, 10)); graph.AddEdge(vertex3, vertex1, new CHEdgeData(1, false, true, false, 10)); graph.AddEdge(vertex2, vertex3, new CHEdgeData(1, true, true, false, 10)); graph.AddEdge(vertex3, vertex2, new CHEdgeData(1, false, false, true, 10)); graph.AddEdge(vertex1, vertex4, new CHEdgeData(1, true, true, false, 15)); graph.AddEdge(vertex4, vertex1, new CHEdgeData(1, false, false, true, 15)); graph.AddEdge(vertex2, vertex4, new CHEdgeData(1, true, false, true, 15)); graph.AddEdge(vertex4, vertex2, new CHEdgeData(1, false, true, false, 15)); preProcessor.Contract(3); Assert.IsTrue(graph.ContainsEdge(vertex1, vertex2, new CHEdgeData(3, false, true, 20))); Assert.IsTrue(graph.ContainsEdge(vertex2, vertex1, new CHEdgeData(3, true, false, 20))); preProcessor.Contract(4); Assert.IsTrue(graph.ContainsEdge(vertex1, vertex2, new CHEdgeData(4, true, false, 30))); Assert.IsTrue(graph.ContainsEdge(vertex2, vertex1, new CHEdgeData(4, false, true, 30))); Assert.IsTrue(graph.ContainsEdge(vertex1, vertex2, new CHEdgeData(3, false, true, 20))); Assert.IsTrue(graph.ContainsEdge(vertex2, vertex1, new CHEdgeData(3, true, false, 20))); }
public void TestVerifiedContraction1NoWitnesses() { var graph = new DirectedGraph <CHEdgeData>(); var vertex1 = graph.AddVertex(1, 0); var vertex2 = graph.AddVertex(2, 0); var vertex3 = graph.AddVertex(3, 0); graph.AddEdge(vertex1, vertex3, new CHEdgeData(1, true, true, true, 10)); graph.AddEdge(vertex3, vertex1, new CHEdgeData(1, false, true, true, 10)); graph.AddEdge(vertex2, vertex3, new CHEdgeData(1, true, true, true, 10)); graph.AddEdge(vertex3, vertex2, new CHEdgeData(1, false, true, true, 10)); var witnessCalculator = new DykstraWitnessCalculator(); var preProcessor = new CHPreProcessor(graph, new EdgeDifferenceContractedSearchSpace(graph, witnessCalculator), witnessCalculator); preProcessor.Contract(3); // there should be no edge from 1->3 and from 2->3. Assert.IsFalse(graph.ContainsEdges(1, 3)); Assert.IsFalse(graph.ContainsEdges(2, 3)); var router = new CHRouter(); // expected: (1)-10s-(3)-10s-(2) (20s in total). var path = router.Calculate(graph, 1, 2); Assert.IsNotNull(path); Assert.AreEqual(20, path.Weight); var pathArray = path.ToArrayWithWeight(); Assert.AreEqual(3, pathArray.Length); float latitude, longitude; Assert.AreEqual(0, pathArray[0].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[0].Item1, out latitude, out longitude)); Assert.AreEqual(1, latitude); Assert.AreEqual(10, pathArray[1].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[1].Item1, out latitude, out longitude)); Assert.AreEqual(3, latitude); Assert.AreEqual(20, pathArray[2].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[2].Item1, out latitude, out longitude)); Assert.AreEqual(2, latitude); // expected: (2)-10s-(3)-10s-(1) (20s in total). path = router.Calculate(graph, 2, 1); Assert.IsNotNull(path); Assert.AreEqual(20, path.Weight); pathArray = path.ToArrayWithWeight(); Assert.AreEqual(3, pathArray.Length); Assert.AreEqual(0, pathArray[0].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[0].Item1, out latitude, out longitude)); Assert.AreEqual(2, latitude); Assert.AreEqual(10, pathArray[1].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[1].Item1, out latitude, out longitude)); Assert.AreEqual(3, latitude); Assert.AreEqual(20, pathArray[2].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[2].Item1, out latitude, out longitude)); Assert.AreEqual(1, latitude); }
public void ComputeShortestPath_NotReachable_Throws() { var g = new DirectedGraph <Empty, int>(); var start = g.AddVertex(Empty.Default); var target = g.AddVertex(Empty.Default); var a = new AStarShortestPathAlgorithm <DirectedGraph <Empty, int>, VertexIdx, EdgeIdx, int>(g, new IntCalculator(), (_, _) => 0); Assert.That(() => a.ComputeShortestPath(start, target, out _), Throws.ArgumentException); }
public void WithTwoDisconnectedVertices_ShouldReturnVerticesInDiscoveredOrder() { var graph = new DirectedGraph <string>(); graph.AddVertex("A"); graph.AddVertex("B"); ExecuteTest(graph, "A", "B"); }
public void Count() { DirectedGraph <string, int> graph = new DirectedGraph <string, int>(); graph.AddVertex("A"); graph.AddVertex("B"); Assert.AreEqual(2, graph.Count); }
public void Should_add_vertex() { Graph graph2 = new DirectedGraph(); graph2.AddVertex(v1); graph2.AddVertex(v2); Assert.AreEqual("Custom House", graph2.MyGraph[1].VertexID); }
public void Vertex_Enumerator() { DirectedGraph <string, int> graph = new DirectedGraph <string, int>(); graph.AddVertex("A"); graph.AddVertex("B"); Assert.IsTrue(graph.Any(x => x == "A")); Assert.IsTrue(graph.Any(x => x == "B")); }
public void ContainsVertex() { DirectedGraph <string, int> graph = new DirectedGraph <string, int>(); graph.AddVertex("A"); graph.AddVertex("B"); Assert.IsTrue(graph.ContainsVertex("A")); Assert.IsFalse(graph.ContainsVertex("bla")); }
public void AddVertex_AlreadyExists() { DirectedGraph <string, int> graph = new DirectedGraph <string, int>(); graph.AddVertex("A"); graph.AddVertex("A"); Assert.AreEqual(1, graph.Count); Assert.IsTrue(graph.Contains("A")); }
public void TestAddExistingVertex() { const int Vertex = 0; var sut = new DirectedGraph <int>(); sut.AddVertex(Vertex); Check.That(sut.AddVertex(Vertex)).IsFalse(); }
public void TestVerifiedContraction1NoWitnesses() { var graph = new DirectedGraph<CHEdgeData>(); var vertex1 = graph.AddVertex(1, 0); var vertex2 = graph.AddVertex(2, 0); var vertex3 = graph.AddVertex(3, 0); graph.AddEdge(vertex1, vertex3, new CHEdgeData(1, true, true, true, 10)); graph.AddEdge(vertex3, vertex1, new CHEdgeData(1, false, true, true, 10)); graph.AddEdge(vertex2, vertex3, new CHEdgeData(1, true, true, true, 10)); graph.AddEdge(vertex3, vertex2, new CHEdgeData(1, false, true, true, 10)); var witnessCalculator = new DykstraWitnessCalculator(); var preProcessor = new CHPreprocessor(graph, new EdgeDifferenceContractedSearchSpace(graph, witnessCalculator), witnessCalculator); preProcessor.Contract(3); // there should be no edge from 1->3 and from 2->3. Assert.IsFalse(graph.ContainsEdges(1, 3)); Assert.IsFalse(graph.ContainsEdges(2, 3)); var router = new CHRouter(); // expected: (1)-10s-(3)-10s-(2) (20s in total). var path = router.Calculate(graph, 1, 2); Assert.IsNotNull(path); Assert.AreEqual(20, path.Weight); var pathArray = path.ToArrayWithWeight(); Assert.AreEqual(3, pathArray.Length); float latitude, longitude; Assert.AreEqual(0, pathArray[0].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[0].Item1, out latitude, out longitude)); Assert.AreEqual(1, latitude); Assert.AreEqual(10, pathArray[1].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[1].Item1, out latitude, out longitude)); Assert.AreEqual(3, latitude); Assert.AreEqual(20, pathArray[2].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[2].Item1, out latitude, out longitude)); Assert.AreEqual(2, latitude); // expected: (2)-10s-(3)-10s-(1) (20s in total). path = router.Calculate(graph, 2, 1); Assert.IsNotNull(path); Assert.AreEqual(20, path.Weight); pathArray = path.ToArrayWithWeight(); Assert.AreEqual(3, pathArray.Length); Assert.AreEqual(0, pathArray[0].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[0].Item1, out latitude, out longitude)); Assert.AreEqual(2, latitude); Assert.AreEqual(10, pathArray[1].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[1].Item1, out latitude, out longitude)); Assert.AreEqual(3, latitude); Assert.AreEqual(20, pathArray[2].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[2].Item1, out latitude, out longitude)); Assert.AreEqual(1, latitude); }
public void AddVertexTest() { var graph = new DirectedGraph(); var newVertex = new Vertex("test"); Assert.DoesNotThrow(() => graph.AddVertex(newVertex)); Assert.AreEqual(graph.VerticesCount, 1); Assert.AreEqual(graph.Vertices.Count, 1); Assert.AreEqual(graph.Vertices.First(), newVertex); Assert.Throws <InvalidOperationException>(() => graph.AddVertex(newVertex)); Assert.Throws <ArgumentNullException>(() => graph.AddVertex(null)); }
public void TestAddExistingEdge() { const int Vertex1 = 0; const int Vertex2 = 1; var sut = new DirectedGraph <int>(); sut.AddVertex(Vertex1); sut.AddVertex(Vertex2); sut.AddEdge(Vertex1, Vertex2); Check.That(sut.AddEdge(Vertex1, Vertex2)).IsFalse(); }
public void InitGraph() { _graph = new DirectedGraph<char>(); _graph.AddVertex('A'); _graph.AddVertex('B'); _graph.AddVertex('D'); _graph.AddVertex('E'); _graph.AddEdge('A', 'B'); _graph.AddEdge('A', 'D'); _graph.AddEdge('D', 'B'); _graph.AddEdge('D', 'E'); _graph.AddEdge('E', 'B'); }
public void InitGraph() { _graph = new DirectedGraph <char>(); _graph.AddVertex('A'); _graph.AddVertex('B'); _graph.AddVertex('D'); _graph.AddVertex('E'); _graph.AddEdge('A', 'B'); _graph.AddEdge('A', 'D'); _graph.AddEdge('D', 'B'); _graph.AddEdge('D', 'E'); _graph.AddEdge('E', 'B'); }
public void RemoveVertex() { DirectedGraph <string, int> graph = new DirectedGraph <string, int>(); graph.AddVertex("A"); graph.AddVertex("B"); Assert.IsTrue(graph.RemoveVertex("B")); Assert.AreEqual(1, graph.Count); Assert.IsTrue(graph.Contains("A")); Assert.IsFalse(graph.RemoveVertex("bla")); }
public void DG_AddRemoveVertices() { var g = new DirectedGraph <int>(); g.AddVertex(1); g.AddVertex(5); g.AddVertex(3); g.AddVertex(7); g.AddVertex(3); g.RemoveVertex(5); Assert.AreEqual(3, g.VerticesCount); Assert.IsTrue(g.VertexExists(7)); Assert.IsFalse(g.VertexExists(5)); Assert.IsFalse(g.VertexExists(9)); }
public void ComputeShortestPath_MultiplePaths_ReturnsShortest() { var g = new DirectedGraph <Empty, int>(); var start = g.AddVertex(Empty.Default); var middle = g.AddVertex(Empty.Default); var target = g.AddVertex(Empty.Default); g.AddEdge(start, middle, 1); g.AddEdge(start, middle, 2); g.AddEdge(middle, target, 3); var a = new AStarShortestPathAlgorithm <DirectedGraph <Empty, int>, VertexIdx, EdgeIdx, int>(g, new IntCalculator(), (_, _) => 0); a.ComputeShortestPath(start, target, out var distance); Assert.That(distance, Is.EqualTo(4)); }
public ParserBuilder(IEnumerable <T> alphabet) { _accepted = new HashSet <Vertex <DirectedGraph <string, IEnumerable <T>?>, string, IEnumerable <T>?> >(); _graph = new DirectedGraph <string, IEnumerable <T>?>(); _alphabet = alphabet?.Distinct()?.ToArray() ?? Array.Empty <T>(); _start = _graph.AddVertex("[start]"); }
public void ContainsVertex_ShouldUseCustomComparer() { var graph = new DirectedGraph <string>(StringComparer.OrdinalIgnoreCase); graph.AddVertex("A"); graph.ContainsVertex("a").Should().BeTrue(); }
public void FindAllPaths_SelfReference_LastElement() { DirectedGraph <string, string> graph = new DirectedGraph <string, string>(); graph.AddVertex("A"); graph.AddVertex("O"); graph.AddEdge("A", "O", "AO"); graph.AddEdge("O", "O", "OO"); // to itself // Get all paths from A to O. var result = graph.FindAllPaths("A", "O").Select(x => string.Join("", x)).ToList(); Assert.AreEqual(2, result.Count); Assert.AreEqual("AO", result[0]); Assert.AreEqual("AOO", result[1]); }
public void TraversingFromSameSourceAndTarget_ShouldReturnSingleVertex() { var graph = new DirectedGraph <string>(); graph.AddVertex("A"); RunSuccessfulTest(graph, "A", "A", "A"); }
public void Test() { var g = new DirectedGraph(4); g.AddVertex(0, 1); g.AddVertex(1, 2); g.AddVertex(1, 3); g.AddVertex(3, 1); Console.WriteLine(g.IsCyclic()); Assert.IsTrue(g.IsCyclic()); g = new DirectedGraph(4); g.AddVertex(0, 1); g.AddVertex(1, 2); g.AddVertex(2, 3); Console.WriteLine(g.IsCyclic()); Assert.IsFalse(g.IsCyclic()); }
public void RemoveVertexFromNonEmptyGraphTest() { var graph = new DirectedGraph(); var newVertex1 = new Vertex("test-1"); var newVertex2 = new Vertex("test-2"); graph.AddVertex(newVertex1); graph.AddVertex(newVertex2); var edge = new DirectedEdge(newVertex1, newVertex2); graph.AddEdge(edge); Assert.DoesNotThrow(() => graph.RemoveVertex(newVertex1)); Assert.AreEqual(graph.VerticesCount, 1); Assert.AreEqual(graph.EdgesCount, 0); Assert.IsTrue(graph.Vertices.Contains(newVertex2)); }
public void Dijkstra() { //Airpot string[] lines = File.ReadAllLines("ALEXISBAD.txt"); var Graph = new DirectedGraph <string>(); int path = int.Parse(lines[0]); var CorrectPathes = new string[path][]; for (int i = 0; i < path; i++) { CorrectPathes[i] = lines[i + 1].Split(','); } for (int i = path + 1; i < lines.Length; i++) { string[] temp = lines[i].Split(','); if (Graph.search(temp[0]) == null) { Graph.AddVertex(new Vertex <string>(temp[0])); } //do samething for temp 1 if (Graph.search(temp[1]) == null) { Graph.AddVertex(new Vertex <string>(temp[1])); } Graph.AddEdge(Graph.search(temp[0]), Graph.search(temp[1]), int.Parse(temp[2])); } for (int i = 1; i < path + 1; i++) { for (int j = 0; j < path; j++) { string[] temp = lines[i].Split(','); string[] myPath = Graph.Dijkstra(Graph.search(temp[0]), Graph.search(temp[temp.Length - 1])).ToArray().Select(x => x.Value).ToArray(); Assert.AreEqual(myPath[j], CorrectPathes[j]); } } }
public void TestVerifiedContraction3TinyOneWay() { var graph = new DirectedGraph<CHEdgeData>(); var vertex1 = graph.AddVertex(1, 0); var vertex2 = graph.AddVertex(2, 0); var vertex3 = graph.AddVertex(3, 0); var vertex4 = graph.AddVertex(4, 0); var vertex5 = graph.AddVertex(5, 0); graph.AddEdge(vertex1, vertex4, new CHEdgeData(1, true, true, true, 10)); graph.AddEdge(vertex4, vertex1, new CHEdgeData(1, false, true, true, 10)); graph.AddEdge(vertex1, vertex2, new CHEdgeData(1, true, true, false, 10)); // oneway forward graph.AddEdge(vertex2, vertex1, new CHEdgeData(1, false, false, true, 10)); // oneway backward. graph.AddEdge(vertex1, vertex3, new CHEdgeData(1, true, true, true, 10)); graph.AddEdge(vertex3, vertex1, new CHEdgeData(1, false, true, true, 10)); graph.AddEdge(vertex2, vertex3, new CHEdgeData(1, true, true, true, 10)); graph.AddEdge(vertex3, vertex2, new CHEdgeData(1, false, true, true, 10)); graph.AddEdge(vertex2, vertex5, new CHEdgeData(1, true, true, true, 10)); graph.AddEdge(vertex5, vertex2, new CHEdgeData(1, false, true, true, 10)); Assert.IsFalse(graph.ContainsEdges(vertex1, vertex1)); Assert.IsTrue(graph.ContainsEdges(vertex2, vertex1)); Assert.IsTrue(graph.ContainsEdges(vertex3, vertex1)); Assert.IsTrue(graph.ContainsEdges(vertex4, vertex1)); Assert.IsFalse(graph.ContainsEdges(vertex5, vertex1)); Assert.IsTrue(graph.ContainsEdges(vertex1, vertex2)); Assert.IsFalse(graph.ContainsEdges(vertex2, vertex2)); Assert.IsTrue(graph.ContainsEdges(vertex3, vertex2)); Assert.IsFalse(graph.ContainsEdges(vertex4, vertex2)); Assert.IsTrue(graph.ContainsEdges(vertex5, vertex2)); Assert.IsTrue(graph.ContainsEdges(vertex1, vertex3)); Assert.IsTrue(graph.ContainsEdges(vertex2, vertex3)); Assert.IsFalse(graph.ContainsEdges(vertex3, vertex3)); Assert.IsFalse(graph.ContainsEdges(vertex4, vertex3)); Assert.IsFalse(graph.ContainsEdges(vertex5, vertex3)); Assert.IsTrue(graph.ContainsEdges(vertex1, vertex4)); Assert.IsFalse(graph.ContainsEdges(vertex2, vertex4)); Assert.IsFalse(graph.ContainsEdges(vertex3, vertex4)); Assert.IsFalse(graph.ContainsEdges(vertex4, vertex4)); Assert.IsFalse(graph.ContainsEdges(vertex5, vertex4)); Assert.IsFalse(graph.ContainsEdges(vertex1, vertex5)); Assert.IsTrue(graph.ContainsEdges(vertex2, vertex5)); Assert.IsFalse(graph.ContainsEdges(vertex3, vertex5)); Assert.IsFalse(graph.ContainsEdges(vertex4, vertex5)); Assert.IsFalse(graph.ContainsEdges(vertex5, vertex5)); var witnessCalculator = new DykstraWitnessCalculator(); var preProcessor = new CHPreprocessor(graph, new EdgeDifferenceContractedSearchSpace(graph, witnessCalculator), witnessCalculator); preProcessor.Start(); var router = new CHRouter(); // expected: (4)-10s-(1)-10s-(2)-10s-(3) (30s in total). var path = router.Calculate(graph, 4, 5); Assert.IsNotNull(path); Assert.AreEqual(30, path.Weight); var pathArray = path.ToArrayWithWeight(); Assert.AreEqual(4, pathArray.Length); float latitude, longitude; Assert.AreEqual(0, pathArray[0].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[0].Item1, out latitude, out longitude)); Assert.AreEqual(4, latitude); Assert.AreEqual(10, pathArray[1].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[1].Item1, out latitude, out longitude)); Assert.AreEqual(1, latitude); Assert.AreEqual(20, pathArray[2].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[2].Item1, out latitude, out longitude)); Assert.AreEqual(2, latitude); Assert.AreEqual(30, pathArray[3].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[3].Item1, out latitude, out longitude)); Assert.AreEqual(5, latitude); // expected: (5)-10s-(2)-10s-(3)-10s-(1)-10s-(4) (40s in total). path = router.Calculate(graph, 5, 4); Assert.IsNotNull(path); Assert.AreEqual(40, path.Weight); pathArray = path.ToArrayWithWeight(); Assert.AreEqual(5, pathArray.Length); Assert.AreEqual(0, pathArray[0].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[0].Item1, out latitude, out longitude)); Assert.AreEqual(5, latitude); Assert.AreEqual(10, pathArray[1].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[1].Item1, out latitude, out longitude)); Assert.AreEqual(2, latitude); Assert.AreEqual(20, pathArray[2].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[2].Item1, out latitude, out longitude)); Assert.AreEqual(3, latitude); Assert.AreEqual(30, pathArray[3].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[3].Item1, out latitude, out longitude)); Assert.AreEqual(1, latitude); Assert.AreEqual(40, pathArray[4].Item2); Assert.IsTrue(graph.GetVertex((uint)pathArray[4].Item1, out latitude, out longitude)); Assert.AreEqual(4, latitude); }
public void SortDirectedHilbertTestSteps4() { var n = 4; // build locations. var locations = new List<GeoCoordinate>(); locations.Add(new GeoCoordinate(-90, -180)); locations.Add(new GeoCoordinate(-90, -60)); locations.Add(new GeoCoordinate(-90, 60)); locations.Add(new GeoCoordinate(-90, 180)); locations.Add(new GeoCoordinate(-30, -180)); locations.Add(new GeoCoordinate(-30, -60)); locations.Add(new GeoCoordinate(-30, 60)); locations.Add(new GeoCoordinate(-30, 180)); locations.Add(new GeoCoordinate(30, -180)); locations.Add(new GeoCoordinate(30, -60)); locations.Add(new GeoCoordinate(30, 60)); locations.Add(new GeoCoordinate(30, 180)); locations.Add(new GeoCoordinate(90, -180)); locations.Add(new GeoCoordinate(90, -60)); locations.Add(new GeoCoordinate(90, 60)); locations.Add(new GeoCoordinate(90, 180)); // build graph. var graph = new DirectedGraph<Edge>(); for (var idx = 0; idx < locations.Count; idx++) { graph.AddVertex((float)locations[idx].Latitude, (float)locations[idx].Longitude); } // build a sorted version. graph.SortHilbert(n); // test if sorted. for (uint vertex = 1; vertex < graph.VertexCount; vertex++) { Assert.IsTrue( GraphExtensions.HilbertDistance(graph, n, vertex) <= GraphExtensions.HilbertDistance(graph, n, vertex + 1)); } // sort locations. locations.Sort((x, y) => { return HilbertCurve.HilbertDistance((float)x.Latitude, (float)x.Longitude, n).CompareTo( HilbertCurve.HilbertDistance((float)y.Latitude, (float)y.Longitude, n)); }); // confirm sort. for (uint vertex = 1; vertex <= graph.VertexCount; vertex++) { float latitude, longitude; graph.GetVertex(vertex, out latitude, out longitude); Assert.AreEqual(latitude, locations[(int)(vertex - 1)].Latitude); Assert.AreEqual(longitude, locations[(int)(vertex - 1)].Longitude); } }
public void SortDirectedHilbertTestDefaultStepsWithEdges() { var n = GraphExtensions.DefaultHilbertSteps; // build locations. var locations = new List<Tuple<GeoCoordinate, uint>>(); locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(-90, -180), 1)); locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(-90, -60), 2)); locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(-90, 60), 3)); locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(-90, 180), 4)); locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(-30, -180), 5)); locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(-30, -60), 6)); locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(-30, 60), 7)); locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(-30, 180), 8)); locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(30, -180), 9)); locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(30, -60), 10)); locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(30, 60), 11)); locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(30, 180), 12)); locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(90, -180), 13)); locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(90, -60), 14)); locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(90, 60), 15)); locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(90, 180), 16)); // build graph. var graph = new DirectedGraph<Edge>(); for (var idx = 0; idx < locations.Count; idx++) { graph.AddVertex((float)locations[idx].Item1.Latitude, (float)locations[idx].Item1.Longitude); } // add edges. graph.AddEdge(1, 2, new Edge() { Tags = 1 }); graph.AddEdge(2, 3, new Edge() { Tags = 2 }); graph.AddEdge(3, 4, new Edge() { Tags = 3 }); graph.AddEdge(4, 5, new Edge() { Tags = 4 }); graph.AddEdge(5, 6, new Edge() { Tags = 5 }); graph.AddEdge(6, 7, new Edge() { Tags = 6 }); graph.AddEdge(7, 8, new Edge() { Tags = 7 }); graph.AddEdge(8, 9, new Edge() { Tags = 8 }); graph.AddEdge(9, 10, new Edge() { Tags = 9 }); graph.AddEdge(10, 11, new Edge() { Tags = 10 }); graph.AddEdge(11, 12, new Edge() { Tags = 11 }); graph.AddEdge(12, 13, new Edge() { Tags = 12 }); graph.AddEdge(13, 14, new Edge() { Tags = 13 }); graph.AddEdge(14, 15, new Edge() { Tags = 14 }); // build a sorted version. graph.SortHilbert(n); // sort locations. locations.Sort((x, y) => { return HilbertCurve.HilbertDistance((float)x.Item1.Latitude, (float)x.Item1.Longitude, n).CompareTo( HilbertCurve.HilbertDistance((float)y.Item1.Latitude, (float)y.Item1.Longitude, n)); }); // confirm sort. float latitude, longitude; var newToOld = new Dictionary<uint, uint>(); for (uint vertex = 1; vertex <= graph.VertexCount; vertex++) { graph.GetVertex(vertex, out latitude, out longitude); Assert.AreEqual(latitude, locations[(int)(vertex - 1)].Item1.Latitude); Assert.AreEqual(longitude, locations[(int)(vertex - 1)].Item1.Longitude); newToOld.Add(vertex, locations[(int)(vertex - 1)].Item2); } for (uint vertex = 1; vertex <= graph.VertexCount; vertex++) { var edges = graph.GetEdges(vertex); var originalVertex = newToOld[vertex]; foreach (var edge in edges) { var originalNeighbour = newToOld[edges.Neighbour]; Assert.IsTrue(originalVertex - 1 == originalNeighbour || originalVertex + 1 == originalNeighbour); } } }
public void TestGraphSerialize1() { var graph = new DirectedGraph<Edge>(); var vertex1 = graph.AddVertex(51, 1); var vertex2 = graph.AddVertex(51, 2); graph.AddEdge(vertex1, vertex2, new Edge() { Forward = true, Tags = 1 }, null); // serialize. using (var stream = new MemoryStream()) { graph.Serialize(stream, Edge.SizeUints, Edge.MapFromDelegate, Edge.MapToDelegate); // deserialize. stream.Seek(0, SeekOrigin.Begin); var graphDeserialized = DirectedGraph<Edge>.Deserialize(stream, Edge.SizeUints, Edge.MapFromDelegate, Edge.MapToDelegate, false); // compare. Assert.AreEqual(graph.VertexCount, graphDeserialized.VertexCount); for (uint vertex = 1; vertex <= graph.VertexCount; vertex++) { float latitude1, longitude1, latitude2, longitude2; if (graph.GetVertex(vertex, out latitude1, out longitude1) && graphDeserialized.GetVertex(vertex, out latitude2, out longitude2)) { Assert.AreEqual(latitude1, latitude2, 0.000001); Assert.AreEqual(longitude1, longitude2, 0.000001); } } var edges = graphDeserialized.GetEdges(vertex1, vertex2).ToKeyValuePairs(); Assert.AreEqual(1, edges.Length); Assert.AreEqual(1, edges[0].Value.Tags); } }
public void TestVerifiedContraction6DuplicateForward() { var graph = new DirectedGraph<CHEdgeData>(); var witnessCalculator = new DykstraWitnessCalculator(); var preProcessor = new CHPreprocessor(graph, new EdgeDifferenceContractedSearchSpace(graph, witnessCalculator), witnessCalculator); var vertex1 = graph.AddVertex(1, 0); var vertex2 = graph.AddVertex(2, 0); var vertex3 = graph.AddVertex(3, 0); var vertex4 = graph.AddVertex(4, 0); graph.AddEdge(vertex1, vertex3, new CHEdgeData(1, true, false, true, 10)); graph.AddEdge(vertex3, vertex1, new CHEdgeData(1, false, true, false, 10)); graph.AddEdge(vertex2, vertex3, new CHEdgeData(1, true, true, false, 10)); graph.AddEdge(vertex3, vertex2, new CHEdgeData(1, false, false, true, 10)); graph.AddEdge(vertex1, vertex4, new CHEdgeData(1, true, true, false, 15)); graph.AddEdge(vertex4, vertex1, new CHEdgeData(1, false, false, true, 15)); graph.AddEdge(vertex2, vertex4, new CHEdgeData(1, true, false, true, 15)); graph.AddEdge(vertex4, vertex2, new CHEdgeData(1, false, true, false, 15)); preProcessor.Contract(3); Assert.IsTrue(graph.ContainsEdge(vertex1, vertex2, new CHEdgeData(3, false, true, 20))); Assert.IsTrue(graph.ContainsEdge(vertex2, vertex1, new CHEdgeData(3, true, false, 20))); preProcessor.Contract(4); Assert.IsTrue(graph.ContainsEdge(vertex1, vertex2, new CHEdgeData(4, true, false, 30))); Assert.IsTrue(graph.ContainsEdge(vertex2, vertex1, new CHEdgeData(4, false, true, 30))); Assert.IsTrue(graph.ContainsEdge(vertex1, vertex2, new CHEdgeData(3, false, true, 20))); Assert.IsTrue(graph.ContainsEdge(vertex2, vertex1, new CHEdgeData(3, true, false, 20))); }
public void TestVerifiedContraction4ReplacePrevious() { var graph = new DirectedGraph<CHEdgeData>(); var vertex1 = graph.AddVertex(1, 0); var vertex2 = graph.AddVertex(2, 0); var vertex3 = graph.AddVertex(3, 0); var vertex4 = graph.AddVertex(4, 0); graph.AddEdge(vertex1, vertex4, new CHEdgeData(1, true, true, true, 15)); graph.AddEdge(vertex4, vertex1, new CHEdgeData(1, false, true, true, 15)); graph.AddEdge(vertex2, vertex4, new CHEdgeData(1, true, true, true, 15)); graph.AddEdge(vertex4, vertex2, new CHEdgeData(1, false, true, true, 15)); var witnessCalculator = new DykstraWitnessCalculator(); var preProcessor = new CHPreprocessor(graph, new EdgeDifferenceContractedSearchSpace(graph, witnessCalculator), witnessCalculator); preProcessor.Contract(4); Assert.IsTrue(graph.ContainsEdge(vertex1, vertex2, new CHEdgeData(4, true, true, 30))); Assert.IsTrue(graph.ContainsEdge(vertex2, vertex1, new CHEdgeData(4, true, true, 30))); // add edges later to prevent witnesses from being found! graph.AddEdge(vertex1, vertex3, new CHEdgeData(1, true, true, true, 10)); graph.AddEdge(vertex3, vertex1, new CHEdgeData(1, false, true, true, 10)); graph.AddEdge(vertex2, vertex3, new CHEdgeData(1, true, true, true, 10)); graph.AddEdge(vertex3, vertex2, new CHEdgeData(1, false, true, true, 10)); preProcessor.Contract(3); Assert.IsTrue(graph.ContainsEdge(vertex1, vertex2, new CHEdgeData(3, true, true, 20))); Assert.IsTrue(graph.ContainsEdge(vertex2, vertex1, new CHEdgeData(3, true, true, 20))); }
public void TestDirectedGraphSwitchTwiceThreeVerticesTwoEdges() { float latitude, longitude; var graph = new DirectedGraph<Edge>(); var vertex1 = graph.AddVertex(1, 1); var vertex2 = graph.AddVertex(2, 2); var vertex3 = graph.AddVertex(3, 3); graph.AddEdge(1, 2, new Edge() { Tags = 1, Forward = true }); graph.AddEdge(3, 1, new Edge() { Tags = 2, Forward = true }); var reverse = new Dictionary<uint, List<uint>>(); reverse.Add(2, new List<uint>(new uint[] { 1 })); reverse.Add(1, new List<uint>(new uint[] { 3 })); graph.Switch(vertex1, vertex2, reverse); graph.Switch(vertex2, vertex3, reverse); graph.GetVertex(1, out latitude, out longitude); Assert.AreEqual(2, latitude); Assert.AreEqual(2, longitude); graph.GetVertex(2, out latitude, out longitude); Assert.AreEqual(3, latitude); Assert.AreEqual(3, longitude); graph.GetVertex(3, out latitude, out longitude); Assert.AreEqual(1, latitude); Assert.AreEqual(1, longitude); var edges = graph.GetEdges(1); Assert.AreEqual(0, edges.Count); edges = graph.GetEdges(2); Assert.AreEqual(1, edges.Count); foreach (var edge in edges) { Assert.AreEqual(3, edge.Neighbour); Assert.AreEqual(2, edge.EdgeData.Tags); Assert.IsTrue(edge.EdgeData.Forward); } edges = graph.GetEdges(3); Assert.AreEqual(1, edges.Count); foreach (var edge in edges) { Assert.AreEqual(1, edge.Neighbour); Assert.AreEqual(1, edge.EdgeData.Tags); Assert.IsTrue(edge.EdgeData.Forward); } }
public void TestDirectedGraphSwitchTwoVertices() { // two vertices var graph = new DirectedGraph<Edge>(); var vertex1 = graph.AddVertex(1, 1); var vertex2 = graph.AddVertex(2, 2); var reverse = new Dictionary<uint, List<uint>>(); graph.Switch(vertex1, vertex2, reverse); float latitude, longitude; graph.GetVertex(1, out latitude, out longitude); Assert.AreEqual(2, latitude); Assert.AreEqual(2, longitude); graph.GetVertex(2, out latitude, out longitude); Assert.AreEqual(1, latitude); Assert.AreEqual(1, longitude); }
public void TestDirectedGraphSwitchThreeVerticesTwoEdges() { float latitude, longitude; var graph = new DirectedGraph<Edge>(); var vertex1 = graph.AddVertex(1, 1); var vertex2 = graph.AddVertex(2, 2); var vertex3 = graph.AddVertex(3, 3); graph.AddEdge(1, 2, new Edge() { Tags = 1, Forward = true }); graph.AddEdge(1, 3, new Edge() { Tags = 2, Forward = true }); var reverse = new Dictionary<uint, List<uint>>(); reverse.Add(2, new List<uint>(new uint[] { 1 })); reverse.Add(3, new List<uint>(new uint[] { 1 })); graph.Switch(vertex1, vertex2, reverse); graph.GetVertex(1, out latitude, out longitude); Assert.AreEqual(2, latitude); Assert.AreEqual(2, longitude); graph.GetVertex(2, out latitude, out longitude); Assert.AreEqual(1, latitude); Assert.AreEqual(1, longitude); var edges = graph.GetEdges(1); Assert.AreEqual(0, edges.Count); edges = graph.GetEdges(2); Assert.AreEqual(2, edges.Count); foreach (var edge in edges) { if (edge.Neighbour == 1) { Assert.AreEqual(1, edge.EdgeData.Tags); Assert.IsTrue(edge.EdgeData.Forward); } else if (edge.Neighbour == 3) { Assert.AreEqual(2, edge.EdgeData.Tags); Assert.IsTrue(edge.EdgeData.Forward); } else { Assert.Fail("The only edge should be an edge with neighbour equal to 2."); } } }