Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cbcbd91

Browse files
Merge pull request fnplus#351 from shanky199912/bellman
added bellman ford algo in java
2 parents 45ce78e + ab0612e commit cbcbd91

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import java.util.*;
2+
import java.lang.*;
3+
import java.io.*;
4+
5+
public class bellman_ford_algo {
6+
7+
class Edge {
8+
int src, dest, weight;
9+
10+
Edge() {
11+
src = dest = weight = 0;
12+
}
13+
}
14+
15+
private int V, E;
16+
private Edge[] edge;
17+
18+
// Creates a graph with V vertices and E edges
19+
private bellman_ford_algo(int v, int e) {
20+
V = v;
21+
E = e;
22+
edge = new Edge[e];
23+
for (int i = 0; i < e; ++i)
24+
edge[i] = new Edge();
25+
}
26+
27+
public static void main(String[] args) {
28+
int V = 5;
29+
int E = 8;
30+
31+
bellman_ford_algo graph = new bellman_ford_algo(V, E);
32+
33+
graph.edge[0].src = 0;
34+
graph.edge[0].dest = 1;
35+
graph.edge[0].weight = -1;
36+
37+
graph.edge[1].src = 0;
38+
graph.edge[1].dest = 2;
39+
graph.edge[1].weight = 4;
40+
41+
graph.edge[2].src = 1;
42+
graph.edge[2].dest = 2;
43+
graph.edge[2].weight = 3;
44+
45+
graph.edge[3].src = 1;
46+
graph.edge[3].dest = 3;
47+
graph.edge[3].weight = 2;
48+
49+
graph.edge[4].src = 1;
50+
graph.edge[4].dest = 4;
51+
graph.edge[4].weight = 2;
52+
53+
graph.edge[5].src = 3;
54+
graph.edge[5].dest = 2;
55+
graph.edge[5].weight = 5;
56+
57+
graph.edge[6].src = 3;
58+
graph.edge[6].dest = 1;
59+
graph.edge[6].weight = 1;
60+
61+
graph.edge[7].src = 4;
62+
graph.edge[7].dest = 3;
63+
graph.edge[7].weight = -3;
64+
65+
bellman_ford_algo.BellmanFord(graph, 0);
66+
}
67+
68+
private static void BellmanFord(bellman_ford_algo graph, int src) {
69+
int V = graph.V, E = graph.E;
70+
int dist[] = new int[V];
71+
72+
for (int i = 0; i < V; ++i)
73+
dist[i] = Integer.MAX_VALUE;
74+
dist[src] = 0;
75+
76+
for (int i = 1; i < V; ++i) {
77+
for (int j = 0; j < E; ++j) {
78+
int u = graph.edge[j].src;
79+
int v = graph.edge[j].dest;
80+
int weight = graph.edge[j].weight;
81+
if (dist[u] != Integer.MAX_VALUE && dist[u] + weight < dist[v])
82+
dist[v] = dist[u] + weight;
83+
}
84+
}
85+
86+
for (int j = 0; j < E; ++j) {
87+
int u = graph.edge[j].src;
88+
int v = graph.edge[j].dest;
89+
int weight = graph.edge[j].weight;
90+
if (dist[u] != Integer.MAX_VALUE && dist[u] + weight < dist[v]) {
91+
System.out.println("Graph contains negative weight cycle");
92+
return;
93+
}
94+
}
95+
printArr(dist, V);
96+
}
97+
98+
private static void printArr(int[] dist, int V) {
99+
System.out.println("Vertex Distance from Source");
100+
for (int i = 0; i < V; ++i)
101+
System.out.println(i + "\t\t" + dist[i]);
102+
}
103+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /