@@ -25,20 +25,16 @@ public class Bellman_Ford {
2525
2626 Problem (informal): Given a weighted graph G, source u and sink v, find minimally weighted u~v path in G
2727
28- Algorithm: BFS with priority queue; maintains distance to certain node and updates along with previous
28+ Algorithm: Iterates over all vertices and edges and relaxes edges
2929
3030 Complexity:
3131 * Time - O(|V|*|E|) since main loop iterates over all vertices and edges
3232 * Space - O(|V| + |E|) to store edges and vertices
3333
3434 Functions Defined:
35- * bellman_ford() - Main algorithm to return all pairs shortest path
35+ * bellman_ford() - Main algorithm to return shortest path or Integer.MIN_VALUE if negative cycle exists
3636 * Vertex() - Custom class to store weighted graphs
3737 * display() - Takes adjacency matrix input and displays GUI graph visualization (*Requires JUNG Library*)
38-
39-
40- Notes:
41- * IterativeBFS runs 3-4x faster than QueueBFS in practice (on average)
4238
4339 */
4440
@@ -53,29 +49,29 @@ public static void main(String[] args) {
5349 adjlist [4 ] = new ArrayList <Vertex >();
5450
5551 adjlist [1 ].add (new Vertex (2 , -1 )); adjlist [1 ].add (new Vertex (3 , 3 ));
56- adjlist [2 ].add (new Vertex (3 , 1 )); adjlist [2 ].add (new Vertex (4 , 2 )); adjlist [2 ].add (new Vertex (4 , 1 ));
52+ adjlist [2 ].add (new Vertex (3 , 1 )); adjlist [2 ].add (new Vertex (4 , 5 )); adjlist [2 ].add (new Vertex (4 , - 3 ));
5753 adjlist [3 ].add (new Vertex (4 , 2 ));
58- adjlist [4 ].add (new Vertex (1 , - 4 ));
54+ adjlist [4 ].add (new Vertex (1 , 10 ));
5955
6056 //Starting node
6157 int source = 1 ;
6258 //Ending node
6359 int dest = 4 ;
64- System .out .print (bellman_ford (adjlist , source ));
60+ System .out .print (bellman_ford (adjlist , source , dest ));
6561
6662 //Uncomment next line to visualize graph (need to convert to array first
6763 //display(adj, "Graph");
6864 }
6965
7066 //Queue based implementation (CLRS)
7167 //Returns value of shortest path from source to destination
72- public static boolean bellman_ford (ArrayList <Vertex >[] adjlist , int source ){
68+ public static int bellman_ford (ArrayList <Vertex >[] adjlist , int source , int dest ){
7369 boolean [] visited = new boolean [adjlist .length ];
7470 int [] distance = new int [adjlist .length ];
7571 int [] prev = new int [adjlist .length ];
7672 Arrays .fill (distance , Integer .MAX_VALUE );
77- distance [0 ] = 0 ;
78- for (int ver =1 ;ver <= adjlist .length ;ver ++){ //iterates over all vertices
73+ distance [1 ] = 0 ;
74+ for (int ver =1 ;ver <adjlist .length ;ver ++){ //iterates over all vertices
7975 for (int i =1 ;i <adjlist .length ;i ++){ //iterates over all edges
8076 for (Vertex neighbor :adjlist [i ]){
8177 if (distance [i ] + neighbor .weight < distance [neighbor .val ]){
@@ -88,11 +84,12 @@ public static boolean bellman_ford(ArrayList<Vertex>[] adjlist, int source){
8884
8985 for (int i =1 ;i <adjlist .length ;i ++){ //iterates over all edges
9086 for (Vertex neighbor :adjlist [i ]){
91- if (distance [neighbor .val ] > neighbor .edge + distance [i ])
92- return false ;
87+ if (distance [neighbor .val ] > neighbor .weight + distance [i ])
88+ return Integer . MIN_VALUE ; //Negative cycle exists
9389 }
9490 }
95- return true ;
91+
92+ return distance [dest ];
9693 }
9794
9895 //Uses JUNG to display 2D graph
0 commit comments