@@ -44,7 +44,7 @@ public static void createGraph(ArrayList<Edge>[] graph) {
44
44
graph [4 ].add (new Edge (4 , 1 , -1 ));
45
45
}
46
46
47
- // Bellman Ford Algorithm (Shortest Path Src to All vertices) --------------------------------
47
+ // Bellman Ford Algorithm (Shortest Path Src to All vertices) -------------------------------- TC -> O(V*E)
48
48
public static void bellmanFord (ArrayList <Edge >[] graph , int src ) {
49
49
// Create a Array to store distances & initialize
50
50
int [] dist = new int [graph .length ];
@@ -55,8 +55,8 @@ public static void bellmanFord(ArrayList<Edge>[] graph, int src) {
55
55
}
56
56
int V = graph .length ;
57
57
58
- for (int i = 0 ; i < V - 1 ; i ++) { // Algorithm loop
59
- for (int j = 0 ; j < graph .length ; j ++) { // Vertices loop
58
+ for (int i = 0 ; i < V - 1 ; i ++) { // Algorithm loop - O(V)
59
+ for (int j = 0 ; j < graph .length ; j ++) { // Vertices loop - (E)
60
60
for (int k = 0 ; k < graph [j ].size (); k ++) { // Edge loop
61
61
Edge e = graph [j ].get (k );
62
62
int u = e .src ;
@@ -73,6 +73,60 @@ public static void bellmanFord(ArrayList<Edge>[] graph, int src) {
73
73
}
74
74
}
75
75
76
+ // 2nd Sorted Method with sort code (Bellman Ford) --------------------------------
77
+ // Create Graph ---
78
+ public static void createGraph2 (ArrayList <Edge > graph ) { // Edges graph
79
+ /*
80
+ * (-1)
81
+ * 1 ←—————————— 4
82
+ * ↗ | ↑ Edge = 6
83
+ * (2)/ | | Vertices = 5 (nodes)
84
+ * / | |
85
+ * 0 |(-4) |(4)
86
+ * \ | |
87
+ * (4)\ | |
88
+ * ↘ ↓ |
89
+ * 2 ——————————→ 3
90
+ * (2)
91
+ */
92
+ graph .add (new Edge (0 , 1 , 2 ));
93
+ graph .add (new Edge (0 , 2 , 4 ));
94
+
95
+ graph .add (new Edge (1 , 2 , -4 ));
96
+
97
+ graph .add (new Edge (2 , 3 , 2 ));
98
+
99
+ graph .add (new Edge (3 , 4 , 4 ));
100
+
101
+ graph .add (new Edge (4 , 1 , -1 ));
102
+ }
103
+
104
+ // Bellman Ford Algorithm (Shortest Path Src to All vertices) -------------------------------- TC -> O(V*E)
105
+ public static void bellmanFord2 (ArrayList <Edge > graph , int src , int V ) {
106
+ // Create a Array to store distances & initialize
107
+ int [] dist = new int [V ];
108
+ for (int i = 0 ; i < dist .length ; i ++) {
109
+ if (i != src ) {
110
+ dist [i ] = Integer .MAX_VALUE ;
111
+ }
112
+ }
113
+
114
+ for (int i = 0 ; i < V - 1 ; i ++) { // Algorithm loop - O(V)
115
+ for (int k = 0 ; k < graph .size (); k ++) { // Edge loop - O(E)
116
+ Edge e = graph .get (k );
117
+ int u = e .src ;
118
+ int v = e .dest ;
119
+ int wt = e .wt ;
120
+ if (dist [u ] != Integer .MAX_VALUE && dist [u ] + wt < dist [v ]) { // Relaxation
121
+ dist [v ] = dist [u ] + wt ;
122
+ }
123
+ }
124
+ }
125
+ for (int i = 0 ; i < dist .length ; i ++) {
126
+ System .out .println ("Source " + src + " to destination " + i + " Sorted path : " + dist [i ]);
127
+ }
128
+ }
129
+
76
130
public static void main (String [] args ) {
77
131
// Bellman Ford Algorithms --------------------------------
78
132
// Shortest paths from the source to all vertices (Weighted Graph)
@@ -82,6 +136,12 @@ public static void main(String[] args) {
82
136
createGraph (graph ); // call for create graph
83
137
int src = 0 ;
84
138
bellmanFord (graph , src );
139
+ System .out .println ();
140
+
141
+ // 2nd Sorted Method with sort code (Bellman Ford) --------------------------------
142
+ ArrayList <Edge > graph2 = new ArrayList <>();
143
+ createGraph2 (graph2 );
144
+ bellmanFord2 (graph2 , src , V );
85
145
}
86
146
87
147
}
0 commit comments