-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBellmanFordAlgorithm.java
117 lines (100 loc) · 3.14 KB
/
BellmanFordAlgorithm.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* @Bellman Ford Algorithm
* Shortest path from the sources to all vertices(negative edges)
*/
import java.util.ArrayList;
public class BellmanFordAlgorithm {
static class Edge{
int src;
int dest;
int wt;
Edge(int src, int dest, int wt){
this.src = src;
this.dest = dest;
this.wt = wt;
}
}
public static void createGraph(ArrayList<Edge>[]graph){
for (int i = 0; i < graph.length; i++) {
graph[i]= new ArrayList<>();
}
//0 Edge
graph[0].add(new Edge(0, 1, 2));
graph[0].add(new Edge(0, 2, 4));
//1 Edge
graph[1].add(new Edge(1, 2, -4));
//2 Edge
graph[2].add(new Edge(2, 3, 2));
//3 Edge
graph[3].add(new Edge(3, 4, 4));
//4 Edge
graph[4].add(new Edge(4, 1, -1));
}
public static void bellmanFord(ArrayList<Edge>graph[], int src){
int dist[] = new int[graph.length];
for (int i = 0; i < dist.length; i++) {
if (i!=src) {
dist[i] = Integer.MAX_VALUE;
}
}
int V = graph.length;
for (int i = 0; i < V-1; i++) {
for (int j = 0; j < graph.length; j++) {
for (int k = 0; k < graph[j].size(); k++) {
Edge e = graph[j].get(k);
int u = e.src;
int v = e.dest;
int wt = e.wt;
if (dist[u]!= Integer.MAX_VALUE &&dist[u]+wt < dist[v]) {
dist[v] = dist[u]+wt;
}
}
}
}
for (int i = 0; i < dist.length; i++) {
System.out.print(dist[i]+ " ");
}
}
public static void createGraph2(ArrayList<Edge>graph){
graph.add(new Edge(0, 1, 2));
graph.add(new Edge(0, 2, 4));
graph.add(new Edge(1, 2, -4));
graph.add(new Edge(2, 3, 2));
graph.add(new Edge(3, 4, 4));
graph.add(new Edge(4, 1, -1));
}
public static void bellmanFord2(ArrayList<Edge>graph, int src, int V){
int dist[] = new int[V];
for (int i = 0; i < dist.length; i++) {
if (i!=src) {
dist[i] = Integer.MAX_VALUE;
}
}
for (int i = 0; i < V-1; i++) {
for (int k = 0; k < graph.size(); k++) {
Edge e = graph.get(k);
int u = e.src;
int v = e.dest;
int wt = e.wt;
if (dist[u]!= Integer.MAX_VALUE &&dist[u]+wt < dist[v]) {
dist[v] = dist[u]+wt;
}
}
}
for (int i = 0; i < dist.length; i++) {
System.out.print(dist[i]+ " ");
}
}
public static void main(String[] args) {
int v=5;
// ArrayList<Edge> []graph = new ArrayList[v];
ArrayList<Edge> graph2 = new ArrayList<>();
// createGraph(graph);
createGraph2(graph2);
bellmanFord2(graph2, 0, v);
}
}
/*
* Output:
* 0 2 -2 0 4
*/