public void BellmanFordExecuteTest() { Func <IGraphEdge, Double> weightFunction = edge => Convert.ToDouble(edge["Weight"]); IDictionary <OperationParameter, Object> parameters = new Dictionary <OperationParameter, Object>(); parameters[GraphOperationParameters.SourceVertex] = _sourceVertex; parameters[GraphOperationParameters.WeightMetric] = weightFunction; BellmanFordAlgorithm operation = new BellmanFordAlgorithm(_sourceGraph, parameters); operation.Execute(); Assert.AreEqual(_resultGraph.VertexCount, operation.Result.VertexCount); Assert.AreEqual(_resultGraph.EdgeCount, operation.Result.EdgeCount); foreach (IGraphVertex resultVertex in operation.Result.Vertices) { IGraphVertex vertex = _resultGraph.GetVertex(resultVertex.Coordinate); Assert.IsNotNull(vertex); Assert.AreEqual(vertex["Distance"], resultVertex["Distance"]); Assert.IsTrue(operation.Result.OutEdges(resultVertex).All(edge => _resultGraph.GetAllEdges(edge.Source.Coordinate, edge.Target.Coordinate).Count == 1)); } }
public void ExecuteTest() { IGraph <string, string> graph = new AdjacencyMapGraph <string, string>(); IVertex <string> A = graph.addVertex("A"); IVertex <string> B = graph.addVertex("B"); IVertex <string> C = graph.addVertex("C"); IVertex <string> D = graph.addVertex("D"); IVertex <string> E = graph.addVertex("E"); IVertex <string> F = graph.addVertex("F"); IVertex <string> G = graph.addVertex("G"); IVertex <string> H = graph.addVertex("H"); IVertex <string> I = graph.addVertex("I"); IVertex <string> J = graph.addVertex("J"); IVertex <string> K = graph.addVertex("K"); IVertex <string> L = graph.addVertex("L"); IVertex <string> M = graph.addVertex("M"); IVertex <string> N = graph.addVertex("N"); graph.addEdge(A, B, "1", 3); graph.addEdge(C, B, "2", 3); graph.addEdge(A, E, "3", 2); graph.addEdge(A, I, "4", 5); graph.addEdge(I, E, "5", 3); graph.addEdge(E, D, "6", 4); graph.addEdge(D, C, "7", 2); graph.addEdge(C, G, "8", 5); graph.addEdge(D, F, "9", 3); graph.addEdge(F, G, "10", 3); graph.addEdge(G, H, "11", 4); graph.addEdge(H, F, "12", 2); graph.addEdge(E, M, "13", 1); graph.addEdge(E, J, "14", 5); graph.addEdge(M, N, "15", 5); graph.addEdge(M, J, "16", 2); graph.addEdge(M, F, "17", 3); graph.addEdge(M, K, "18", 2); graph.addEdge(J, K, "19", 3); graph.addEdge(K, L, "20", 1); BellmanFordAlgorithm <string, string> d = new BellmanFordAlgorithm <string, string>(graph); d.Execute(A); StringBuilder sb = new StringBuilder(); foreach (IVertex <string> v in d.getPredecessors().Keys) { if (d.getPredecessors()[v] != null) { sb.Append(v.ToString() + " from " + d.getPredecessors()[v].ToString() + Environment.NewLine); } else { sb.Append(v.ToString() + " from <null> (start)" + Environment.NewLine); } } var tree = d.getSpanningForest()[0]; var pos = tree.PositionContaining(K); // MessageBox.Show(pos.ToString()); List <IVertex <string> > path = new List <IVertex <string> >(); foreach (var p in tree.PathToRoot(pos)) { path.Add(p.Element()); } // MessageBox.Show(path.Count.ToString()); string res = ""; foreach (var vert in path) { res += vert.ToString() + " -> "; } /* * MessageBox.Show(res.ToString()); * MessageBox.Show(sb.ToString()); * MessageBox.Show(d.getSpanningForest()[0].ToString()); */ }
private static void P5DijkstraBellman() { Console.WriteLine("-------------------------------"); Console.WriteLine("Neuer Graph"); var hFileName = GraphFileRessources.P4GraphWege1; var hGraph = AdjacentListGraphImporter.ImportAdjacentList(hFileName, EdgeKind.DirectedWeighted); var hDijkstraAlgorithm = new Dijkstra(hGraph); hDijkstraAlgorithm.Execute(2, 0); var hBellmanFordAlgorithm = new BellmanFordAlgorithm(hGraph); hBellmanFordAlgorithm.Execute(2, 0); Console.WriteLine("-------------------------------"); Console.WriteLine("Neuer Graph"); Console.WriteLine("Gerichtet"); hFileName = GraphFileRessources.P4GraphWege2; hGraph = AdjacentListGraphImporter.ImportAdjacentList(hFileName, EdgeKind.DirectedWeighted); hDijkstraAlgorithm = new Dijkstra(hGraph); hDijkstraAlgorithm.Execute(2, 0); hBellmanFordAlgorithm = new BellmanFordAlgorithm(hGraph); hBellmanFordAlgorithm.Execute(2, 0); Console.WriteLine("-------------------------------"); Console.WriteLine("Neuer Graph"); Console.WriteLine("Gerichtet"); hFileName = GraphFileRessources.P4GraphWege3; hGraph = AdjacentListGraphImporter.ImportAdjacentList(hFileName, EdgeKind.DirectedWeighted); hDijkstraAlgorithm = new Dijkstra(hGraph); hDijkstraAlgorithm.Execute(2, 0); hBellmanFordAlgorithm = new BellmanFordAlgorithm(hGraph); hBellmanFordAlgorithm.Execute(2, 0); Console.WriteLine("-------------------------------"); Console.WriteLine("Neuer Graph"); Console.WriteLine("Gerichtet"); hFileName = GraphFileRessources.P4GraphG12; hGraph = AdjacentListGraphImporter.ImportAdjacentList(hFileName, EdgeKind.DirectedWeighted); hDijkstraAlgorithm = new Dijkstra(hGraph); hDijkstraAlgorithm.Execute(0, 1); hBellmanFordAlgorithm = new BellmanFordAlgorithm(hGraph); hBellmanFordAlgorithm.Execute(0, 1); Console.WriteLine("-------------------------------"); Console.WriteLine("Neuer Graph"); Console.WriteLine("Ungerichtet"); hFileName = GraphFileRessources.P4GraphG12; hGraph = AdjacentListGraphImporter.ImportAdjacentList(hFileName, EdgeKind.UndirectedWeighted); hDijkstraAlgorithm = new Dijkstra(hGraph); hDijkstraAlgorithm.Execute(0, 1); hBellmanFordAlgorithm = new BellmanFordAlgorithm(hGraph); hBellmanFordAlgorithm.Execute(0, 1); }