public void Test() { // orientovaný ohodnocený. var graph1 = new DirectedWeightedGraph(false); for (int i = 'a'; i < 'h'; i++) { graph1.AddVertex(Convert.ToString(Convert.ToChar(i))); } graph1.AddEdge("a", "b", 10); graph1.AddEdge("b", "c", 20); graph1.AddEdge("c", "d", 3); graph1.AddEdge("d", "e", 4); graph1.AddEdge("f", "c", -20); graph1.AddEdge("b", "f", 3); graph1.AddEdge("e", "b", 10); BellmanFordShortestPath <DirectedWeightedGraph> .Search(graph1, "a"); Assert.AreEqual(0, graph1.GetVertex("a").distance); Assert.AreEqual(10, graph1.GetVertex("b").distance); Assert.AreEqual(-7, graph1.GetVertex("c").distance); Assert.AreEqual(-4, graph1.GetVertex("d").distance); Assert.AreEqual(0, graph1.GetVertex("e").distance); Assert.AreEqual(13, graph1.GetVertex("f").distance); Assert.AreEqual(long.MaxValue, graph1.GetVertex("g").distance); Stack <Vertex> stack = new Stack <Vertex>(); stack.Push(graph1.GetVertex("e")); stack.Push(graph1.GetVertex("d")); stack.Push(graph1.GetVertex("c")); stack.Push(graph1.GetVertex("f")); stack.Push(graph1.GetVertex("b")); stack.Push(graph1.GetVertex("a")); var stackTest = BellmanFordShortestPath <DirectedWeightedGraph> .GetShortestPath(graph1, "e"); while (stack.Count > 0) { Assert.AreEqual(stack.Pop(), stackTest.Pop()); } }
public void BellmanFord_Smoke_Test() { var graph = new WeightedDiGraph <char, int>(); graph.AddVertex('S'); graph.AddVertex('A'); graph.AddVertex('B'); graph.AddVertex('C'); graph.AddVertex('D'); graph.AddVertex('T'); graph.AddEdge('S', 'A', -10); graph.AddEdge('S', 'C', -5); graph.AddEdge('A', 'B', 4); graph.AddEdge('A', 'C', 2); graph.AddEdge('A', 'D', 8); graph.AddEdge('B', 'T', 10); graph.AddEdge('C', 'D', 9); graph.AddEdge('D', 'B', 6); graph.AddEdge('D', 'T', 10); var algo = new BellmanFordShortestPath <char, int>(new BellmanFordShortestPathOperators()); var result = algo.GetShortestPath(graph, 'S', 'T'); Assert.AreEqual(4, result.Length); var expectedPath = new char[] { 'S', 'A', 'B', 'T' }; for (int i = 0; i < expectedPath.Length; i++) { Assert.AreEqual(expectedPath[i], result.Path[i]); } }
static int minimumMovesDij(string[] grid, int startX, int startY, int goalX, int goalY) { int from = -1; int to = -1; int n = grid.Length; int[][] matrix = new int[n][]; int tracker = 0; for (int i = 0; i < n; i++) { matrix[i] = new int[n]; for (int j = 0; j < n; j++) { if (grid[i][j] != 'X') { matrix[i][j] = tracker; } else { matrix[i][j] = -1; } tracker++; if (startX == i && startY == j) { from = matrix[i][j]; } if (goalX == i && goalY == j) { to = matrix[i][j]; } } } EdgeWeightedDiGraph ewdg = new EdgeWeightedDiGraph(tracker); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i < n - 1 && matrix[i][j] != -1 && matrix[i + 1][j] != -1) { ewdg.AddEdge(new DirectedEdgeAPI(matrix[i][j], matrix[i + 1][j], 1), "V"); ewdg.AddEdge(new DirectedEdgeAPI(matrix[i + 1][j], matrix[i][j], 1), "V"); } if (j < n - 1 && matrix[i][j] != -1 && matrix[i][j + 1] != -1) { ewdg.AddEdge(new DirectedEdgeAPI(matrix[i][j], matrix[i][j + 1], 1), "H"); ewdg.AddEdge(new DirectedEdgeAPI(matrix[i][j + 1], matrix[i][j], 1), "H"); } } } BellmanFordShortestPath djkForward = new BellmanFordShortestPath(ewdg, from); //DijkstraShortestPath djkBackward = new DijkstraShortestPath(ewdg, from); //var count = djkForward.DistTo[to] > djkBackward.DistTo[from] ? djkBackward.DistTo[from] : djkForward.DistTo[to]; //List<int> list = djk.PathTo(from, to).ToList(); //string previousAllign = string.Empty; //int count = 0; //for (int i = 0; i < list.Count() - 1; i++) //{ // string key = list[i] < list[i + 1] ? list[i] + "|" + list[i + 1] : list[i + 1] + "|" + list[i]; // var item = ewdg.dict[key]; // if (previousAllign == string.Empty || previousAllign != item) // { // count++; // previousAllign = item; // } //} return((int)0); }
GetAllPairShortestPaths(WeightedDiGraph <T, W> graph) { var workGraph = graph.Clone(); //add an extra vertex with zero weight edge to all nodes var randomVetex = operators.RandomVertex(); if (workGraph.Vertices.ContainsKey(randomVetex)) { throw new Exception("Random Vertex is not unique for given graph."); } workGraph.AddVertex(randomVetex); foreach (var vertex in workGraph.Vertices) { workGraph.AddEdge(randomVetex, vertex.Key, operators.DefaultValue); } //now compute shortest path from random vertex to all other vertices var bellmanFordSP = new BellmanFordShortestPath <T, W>(operators); var bellFordResult = new Dictionary <T, W>(); foreach (var vertex in workGraph.Vertices) { var result = bellmanFordSP.GetShortestPath(workGraph, randomVetex, vertex.Key); bellFordResult.Add(vertex.Key, result.Length); } //adjust edges so that all edge values are now +ive foreach (var vertex in workGraph.Vertices) { foreach (var edge in vertex.Value.OutEdges.ToList()) { vertex.Value.OutEdges[edge.Key] = operators.Substract( operators.Sum(bellFordResult[vertex.Key], edge.Value), bellFordResult[edge.Key.Value]); } } workGraph.RemoveVertex(randomVetex); //now run dijikstra for all pairs of vertices //trace path var dijikstras = new DijikstraShortestPath <T, W>(operators); var finalResult = new List <AllPairShortestPathResult <T, W> >(); foreach (var vertexA in workGraph.Vertices) { foreach (var vertexB in workGraph.Vertices) { var source = vertexA.Key; var dest = vertexB.Key; var sp = dijikstras.GetShortestPath(workGraph, source, dest); //no path exists if (sp.Length.Equals(operators.MaxValue)) { continue; } var distance = sp.Length; var path = sp.Path; finalResult.Add(new AllPairShortestPathResult <T, W>(source, dest, distance, path)); } } return(finalResult); }