1+ import java .util .*;
2+ class BellmanFord
3+ /*Implementation of Bellman ford to detect negative cycles. Graph accepts inputs in form of edges which have
4+ start vertex, end vertes and weights. Vertices should be labelled with a number between 0 and total number of vertices-1,both inclusive*/
5+ {
6+ int vertex ,edge ;
7+ private Edge edges [];
8+ private int index =0 ;
9+ BellmanFord (int v ,int e )
10+ {
11+ vertex =v ;
12+ edge =e ;
13+ edges =new Edge [e ];
14+ }
15+ class Edge
16+ {
17+ int u ,v ;
18+ int w ;
19+ /**
20+ *@param u Source Vertex
21+ * @param v End vertex
22+ * @param c Weight
23+ */
24+ Edge (int a ,int b ,int c )
25+ {
26+ u =a ;
27+ v =b ;
28+ w =c ;
29+ }
30+ }
31+ /**
32+ * @param p[] Parent array which shows updates in edges
33+ * @param i Current vertex under consideration
34+ */
35+ void printPath (int p [],int i )
36+ {
37+ if (p [i ]==-1 )//Found the path back to parent
38+ return ;
39+ printPath (p ,p [i ]);
40+ System .out .print (i +" " );
41+ }
42+ public static void main (String args [])
43+ {
44+ BellmanFord obj =new BellmanFord (0 ,0 );//Dummy object to call nonstatic variables
45+ obj .go ();
46+ }
47+ public void go ()//Interactive run for understanding the class first time. Assumes source vertex is 0 and shows distaance to all vertices
48+ {
49+ Scanner sc =new Scanner (System .in );//Grab scanner object for user input
50+ int i ,v ,e ,u ,ve ,w ,j ,neg =0 ;
51+ System .out .println ("Enter no. of vertices and edges please" );
52+ v =sc .nextInt ();
53+ e =sc .nextInt ();
54+ Edge arr []=new Edge [e ];//Array of edges
55+ System .out .println ("Input edges" );
56+ for (i =0 ;i <e ;i ++)
57+ {
58+ u =sc .nextInt ();
59+ ve =sc .nextInt ();
60+ w =sc .nextInt ();
61+ arr [i ]=new Edge (u ,ve ,w );
62+ }
63+ int dist []=new int [v ];//Distance array for holding the finalized shortest path distance between source and all vertices
64+ int p []=new int [v ];//Parent array for holding the paths
65+ for (i =0 ;i <v ;i ++)
66+ dist [i ]=Integer .MAX_VALUE ;//Initializing distance values
67+ dist [0 ]=0 ;
68+ p [0 ]=-1 ;
69+ for (i =0 ;i <v -1 ;i ++)
70+ {
71+ for (j =0 ;j <e ;j ++)
72+ {
73+ if ((int )dist [arr [j ].u ]!=Integer .MAX_VALUE &&dist [arr [j ].v ]>dist [arr [j ].u ]+arr [j ].w )
74+ {
75+ dist [arr [j ].v ]=dist [arr [j ].u ]+arr [j ].w ;//Update
76+ p [arr [j ].v ]=arr [j ].u ;
77+ }
78+ }
79+ }
80+ //Final cycle for negative checking
81+ for (j =0 ;j <e ;j ++)
82+ if ((int )dist [arr [j ].u ]!=Integer .MAX_VALUE &&dist [arr [j ].v ]>dist [arr [j ].u ]+arr [j ].w )
83+ {
84+ neg =1 ;
85+ System .out .println ("Negative cycle" );
86+ break ;
87+ }
88+ if (neg ==0 )//Go ahead and show results of computaion
89+ {
90+ System .out .println ("Distances are: " );
91+ for (i =0 ;i <v ;i ++)
92+ System .out .println (i +" " +dist [i ]);
93+ System .out .println ("Path followed:" );
94+ for (i =0 ;i <v ;i ++)
95+ {
96+ System .out .print ("0 " );
97+ printPath (p ,i );
98+ System .out .println ();
99+ }
100+ }
101+ }
102+ /**
103+ * @param source Starting vertex
104+ * @param end Ending vertex
105+ * @param Edge Array of edges
106+ */
107+ public void show (int source ,int end , Edge arr [])//Just shows results of computation, if graph is passed to it. The graph should
108+ //be created by using addEdge() method and passed by calling getEdgeArray() method
109+ {
110+ int i ,j ,v =vertex ,e =edge ,neg =0 ;
111+ double dist []=new double [v ];//Distance array for holding the finalized shortest path distance between source and all vertices
112+ int p []=new int [v ];//Parent array for holding the paths
113+ for (i =0 ;i <v ;i ++)
114+ dist [i ]=Integer .MAX_VALUE ;//Initializing distance values
115+ dist [source ]=0 ;
116+ p [source ]=-1 ;
117+ for (i =0 ;i <v -1 ;i ++)
118+ {
119+ for (j =0 ;j <e ;j ++)
120+ {
121+ if ((int )dist [arr [j ].u ]!=Integer .MAX_VALUE &&dist [arr [j ].v ]>dist [arr [j ].u ]+arr [j ].w )
122+ {
123+ dist [arr [j ].v ]=dist [arr [j ].u ]+arr [j ].w ;//Update
124+ p [arr [j ].v ]=arr [j ].u ;
125+ }
126+ }
127+ }
128+ //Final cycle for negative checking
129+ for (j =0 ;j <e ;j ++)
130+ if ((int )dist [arr [j ].u ]!=Integer .MAX_VALUE &&dist [arr [j ].v ]>dist [arr [j ].u ]+arr [j ].w )
131+ {
132+ neg =1 ;
133+ System .out .println ("Negative cycle" );
134+ break ;
135+ }
136+ if (neg ==0 )//Go ahead and show results of computaion
137+ {
138+ System .out .println ("Distance is: " +dist [end ]);
139+ System .out .println ("Path followed:" );
140+ System .out .print (source +" " );
141+ printPath (p ,end );
142+ System .out .println ();
143+ }
144+ }
145+ /**
146+ *@param x Source Vertex
147+ * @param y End vertex
148+ * @param z Weight
149+ */
150+ public void addEdge (int x ,int y ,int z )//Adds unidirectionl Edge
151+ {
152+ edges [index ++]=new Edge (x ,y ,z );
153+ }
154+ public Edge [] getEdgeArray ()
155+ {
156+ return edges ;
157+ }
158+ }
0 commit comments