Skip to content

Commit 029de8c

Browse files
committed
2nd Method Bellman Ford
1 parent 1762c6a commit 029de8c

File tree

1 file changed

+63
-3
lines changed

1 file changed

+63
-3
lines changed

36-Graphs_(Part-IV)/GraphsP4_BellmanFord.java

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static void createGraph(ArrayList<Edge>[] graph) {
4444
graph[4].add(new Edge(4, 1, -1));
4545
}
4646

47-
// Bellman Ford Algorithm (Shortest Path Src to All vertices) --------------------------------
47+
// Bellman Ford Algorithm (Shortest Path Src to All vertices) -------------------------------- TC -> O(V*E)
4848
public static void bellmanFord(ArrayList<Edge>[] graph, int src) {
4949
// Create a Array to store distances & initialize
5050
int[] dist = new int[graph.length];
@@ -55,8 +55,8 @@ public static void bellmanFord(ArrayList<Edge>[] graph, int src) {
5555
}
5656
int V = graph.length;
5757

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)
6060
for (int k = 0; k < graph[j].size(); k++) { // Edge loop
6161
Edge e = graph[j].get(k);
6262
int u = e.src;
@@ -73,6 +73,60 @@ public static void bellmanFord(ArrayList<Edge>[] graph, int src) {
7373
}
7474
}
7575

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+
76130
public static void main(String[] args) {
77131
// Bellman Ford Algorithms --------------------------------
78132
// Shortest paths from the source to all vertices (Weighted Graph)
@@ -82,6 +136,12 @@ public static void main(String[] args) {
82136
createGraph(graph); // call for create graph
83137
int src = 0;
84138
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);
85145
}
86146

87147
}

0 commit comments

Comments
 (0)