2
2
3
3
import java .util .*;
4
4
5
- class BellmanFord /*Implementation of Bellman ford to detect negative cycles. Graph accepts inputs
6
- in form of edges which have start vertex, end vertex and weights. Vertices should be labelled with a
7
- number between 0 and total number of vertices-1,both inclusive*/
5
+ class BellmanFord /*
6
+ * Implementation of Bellman ford to detect negative cycles. Graph accepts
7
+ * inputs
8
+ * in form of edges which have start vertex, end vertex and weights. Vertices
9
+ * should be labelled with a
10
+ * number between 0 and total number of vertices-1,both inclusive
11
+ */
8
12
{
9
13
10
14
int vertex , edge ;
@@ -36,7 +40,7 @@ public Edge(int a, int b, int c) {
36
40
37
41
/**
38
42
* @param p[] Parent array which shows updates in edges
39
- * @param i Current vertex under consideration
43
+ * @param i Current vertex under consideration
40
44
*/
41
45
void printPath (int [] p , int i ) {
42
46
if (p [i ] == -1 ) { // Found the path back to parent
@@ -52,64 +56,65 @@ public static void main(String[] args) {
52
56
}
53
57
54
58
public void go () { // shows distance to all vertices // Interactive run for understanding the
55
- // class first time. Assumes source vertex is 0 and
56
- Scanner sc = new Scanner (System .in ); // Grab scanner object for user input
57
- int i , v , e , u , ve , w , j , neg = 0 ;
58
- System .out .println ("Enter no. of vertices and edges please" );
59
- v = sc .nextInt ();
60
- e = sc .nextInt ();
61
- Edge [] arr = new Edge [e ]; // Array of edges
62
- System .out .println ("Input edges" );
63
- for (i = 0 ; i < e ; i ++) {
64
- u = sc .nextInt ();
65
- ve = sc .nextInt ();
66
- w = sc .nextInt ();
67
- arr [i ] = new Edge (u , ve , w );
68
- }
69
- int [] dist = new int [v ]; // Distance array for holding the finalized shortest path distance
70
- // between source
71
- // and all vertices
72
- int [] p = new int [v ]; // Parent array for holding the paths
73
- for (i = 0 ; i < v ; i ++) {
74
- dist [i ] = Integer .MAX_VALUE ; // Initializing distance values
75
- }
76
- dist [0 ] = 0 ;
77
- p [0 ] = -1 ;
78
- for (i = 0 ; i < v - 1 ; i ++) {
59
+ try ( // class first time. Assumes source vertex is 0 and
60
+ Scanner sc = new Scanner (System .in )) {
61
+ int i , v , e , u , ve , w , j , neg = 0 ;
62
+ System .out .println ("Enter no. of vertices and edges please" );
63
+ v = sc .nextInt ();
64
+ e = sc .nextInt ();
65
+ Edge [] arr = new Edge [e ]; // Array of edges
66
+ System .out .println ("Input edges" );
67
+ for (i = 0 ; i < e ; i ++) {
68
+ u = sc .nextInt ();
69
+ ve = sc .nextInt ();
70
+ w = sc .nextInt ();
71
+ arr [i ] = new Edge (u , ve , w );
72
+ }
73
+ int [] dist = new int [v ]; // Distance array for holding the finalized shortest path distance
74
+ // between source
75
+ // and all vertices
76
+ int [] p = new int [v ]; // Parent array for holding the paths
77
+ for (i = 0 ; i < v ; i ++) {
78
+ dist [i ] = Integer .MAX_VALUE ; // Initializing distance values
79
+ }
80
+ dist [0 ] = 0 ;
81
+ p [0 ] = -1 ;
82
+ for (i = 0 ; i < v - 1 ; i ++) {
83
+ for (j = 0 ; j < e ; j ++) {
84
+ if (dist [arr [j ].u ] != Integer .MAX_VALUE && dist [arr [j ].v ] > dist [arr [j ].u ] + arr [j ].w ) {
85
+ dist [arr [j ].v ] = dist [arr [j ].u ] + arr [j ].w ; // Update
86
+ p [arr [j ].v ] = arr [j ].u ;
87
+ }
88
+ }
89
+ }
90
+ // Final cycle for negative checking
79
91
for (j = 0 ; j < e ; j ++) {
80
92
if (dist [arr [j ].u ] != Integer .MAX_VALUE && dist [arr [j ].v ] > dist [arr [j ].u ] + arr [j ].w ) {
81
- dist [arr [j ].v ] = dist [arr [j ].u ] + arr [j ].w ; // Update
82
- p [arr [j ].v ] = arr [j ].u ;
93
+ neg = 1 ;
94
+ System .out .println ("Negative cycle" );
95
+ break ;
83
96
}
84
97
}
85
- }
86
- // Final cycle for negative checking
87
- for (j = 0 ; j < e ; j ++) {
88
- if (dist [arr [j ].u ] != Integer .MAX_VALUE && dist [arr [j ].v ] > dist [arr [j ].u ] + arr [j ].w ) {
89
- neg = 1 ;
90
- System .out .println ("Negative cycle" );
91
- break ;
92
- }
93
- }
94
- if (neg == 0 ) { // Go ahead and show results of computation
95
- System .out .println ("Distances are: " );
96
- for (i = 0 ; i < v ; i ++) {
97
- System .out .println (i + " " + dist [i ]);
98
- }
99
- System .out .println ("Path followed:" );
100
- for (i = 0 ; i < v ; i ++) {
101
- System .out .print ("0 " );
102
- printPath (p , i );
103
- System .out .println ();
98
+ if (neg == 0 ) { // Go ahead and show results of computation
99
+ System .out .println ("Distances are: " );
100
+ for (i = 0 ; i < v ; i ++) {
101
+ System .out .println (i + " " + dist [i ]);
102
+ }
103
+ System .out .println ("Path followed:" );
104
+ for (i = 0 ; i < v ; i ++) {
105
+ System .out .print ("0 " );
106
+ printPath (p , i );
107
+ System .out .println ();
108
+ }
104
109
}
110
+ sc .close ();
105
111
}
106
- sc .close ();
107
112
}
108
113
109
114
/**
110
115
* @param source Starting vertex
111
- * @param end Ending vertex
112
- * @param Edge Array of edges
116
+ * @param end Ending vertex
117
+ * @param Edge Array of edges
113
118
*/
114
119
public void show (int source , int end ,
115
120
Edge [] arr ) { // be created by using addEdge() method and passed by calling getEdgeArray()
0 commit comments