//http://www.geeksforgeeks.org/dynamic-programming-set-23-bellman-ford-algorithm/ //https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm public static int[] BellmanFord(BellmanGraph graph, int src) { //declare variables int Vertices = graph.Vertex, Edges = graph.Edges; int[] dist = new int[Vertices]; int[] predecessor = new int[Vertices]; // Step 1: Initialize distances from src to all other // vertices as INFINITE for (int i = 0; i < Vertices; i++) { dist[i] = int.MaxValue; predecessor[i] = int.MaxValue; } dist[src] = 0; // Step 2: Relax all edges |V| - 1 times. A simple // shortest path from src to any other vertex can // have at-most |V| - 1 edges for (int i = 1; i < Vertices; i++) { for (int j = 0; j < Edges; j++) { //int u = graph.edge[j].src; //int v = graph.edge[j].dest; int weight = graph.edge[j].weight; if (dist[graph.edge[j].src] != int.MaxValue && dist[graph.edge[j].src] + weight < dist[graph.edge[j].dest]) { dist[graph.edge[j].dest] = dist[graph.edge[j].src] + weight; predecessor[graph.edge[j].dest] = graph.edge[j].src; } } } // Step 3: check for negative-weight cycles. The above // step guarantees shortest distances if graph doesn't // contain negative weight cycle. If we get a shorter // path, then there is a cycle. for (int j = 0; j < Edges; j++) { int u = graph.edge[j].src; int v = graph.edge[j].dest; int weight = graph.edge[j].weight; if (dist[u] != int.MaxValue && dist[u] + weight < dist[v]) { break; //System.out.println("Graph contains negative weight cycle"); } } return dist; }
private void RunApplication() { //Answer for the assignment char finish = 'A'; int doubledges = 0; //Different graphs DijkstrasGraph dijkstrasgraph = new DijkstrasGraph(); BellmanGraph bellmanGraph; foreach (var unique in FromDropdownlist) { //Dictionary for handling all routes from unique to new nodes Dictionary<char, int> routes = new Dictionary<char, int>(); //Get all possible routes both from the route and from dest to route foreach (var route in collection.Where(s => s.From == unique)) { routes.Add(route.Dest, route.Cost); doubledges++; } //All paths is linked A-B then B->A if (IsDoubleLinked) { foreach (var route in collection.Where(s => s.Dest == unique)) { routes.Add(route.From, route.Cost); doubledges++; } } //Add the routes to the graph dijkstrasgraph.AddNewVertices(unique, routes); } int i = 0; //Checks if bellman is double linked if (IsDoubleLinked) { bellmanGraph = new BellmanGraph(dijkstrasgraph.Vertex, doubledges); //Go through all edges foreach (var edge in collection) { //Compare bellman graphs bellmanGraph.edge[i] = new BellmanNode(edge.From, DennisAlphabet[edge.From], DennisAlphabet[edge.Dest], edge.Cost); i++; bellmanGraph.edge[i] = new BellmanNode(edge.Dest, DennisAlphabet[edge.Dest], DennisAlphabet[edge.From], edge.Cost); i++; } } else { //Vertices = 23, collection 35 bellmanGraph = new BellmanGraph(FromDropdownlist.Count, collection.Count); //Go through all edges foreach (var edge in collection) { //Compare bellman graphs bellmanGraph.edge[i] = new BellmanNode(edge.From, DennisAlphabet[edge.From], DennisAlphabet[edge.Dest], edge.Cost); i++; } } //************************************************************** // Dijkstras & Bellman Ford //************************************************************** List<Path> answersDij = new List<Path>(); List<int[]> bga = new List<int[]>(); stopwatch.Start(); foreach (var start in FromDropdownlist.Where(s => s != finish)) { answersDij.Add(ShortestPath.PathShortest(dijkstrasgraph.Graph, start, finish)); } stopwatch.Stop(); DjikstrasTime = stopwatch.ElapsedMilliseconds; bga.Add(ShortestPath.BellmanFord(bellmanGraph, DennisAlphabet['E'])); //STOPWATCH FOR Alphabet BellmanWatch.Start(); foreach (var start in FromDropdownlist.Where(s => s != finish)) { bga.Add(ShortestPath.BellmanFord(bellmanGraph, DennisAlphabet[start])); } //STOP BellmanWatch.Stop(); BellmanFordTime = BellmanWatch.ElapsedMilliseconds; //**************************************************************** // Creates the answer sheet //**************************************************************** string spliter = "-------------------------"; for (int x = 0; x < answersDij.Count; x++) { OutputText.Add(spliter); //Return string string textway = string.Format("{0} -> ", answersDij[x].Start); //Checks if there is any paths if (answersDij[x].PathChars != null) { foreach (var pathway in answersDij[x].PathChars) { textway = string.Concat(textway, (answersDij[x].PathChars.Last() != pathway) ? (string.Format(" {0} -> ", pathway)) : (string.Format("{0}", pathway))); } OutputText.Add(string.Format("[From: {0} to {1}] Dijkstras [{2}] Cost:{3} ", answersDij[x].Start, finish, textway, answersDij[x].Cost)); if (IsBellman) { OutputText.Add(string.Format("Bellman using arrays: Cost: {0}", bga[x][5])); } } else //No path is possible { OutputText.Add(string.Format("[From: {0} to {1}] No path is found. ", answersDij[x].Start, finish)); } OutputText.Add(spliter); OutputText.Add(string.Empty); } }