forked from Shreyasheeetal20/HACKTOBERFEST22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bellman–Ford Algorithm.java
116 lines (100 loc) · 3.59 KB
/
Bellman–Ford Algorithm.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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
// A class to store a graph edge
class Edge
{
int source, dest, weight;
public Edge(int source, int dest, int weight)
{
this.source = source;
this.dest = dest;
this.weight = weight;
}
}
class Main
{
// Recursive function to print the path of a given vertex from source vertex
static void getPath(int parent[], int vertex, List<Integer> path)
{
if (vertex < 0) {
return;
}
getPath(parent, parent[vertex], path);
path.add(vertex);
}
// Function to run the Bellman–Ford algorithm from a given source
public static void bellmanFord(List<Edge> edges, int source, int n)
{
// distance[] and parent[] stores the shortest path
// (least cost/path) information
int distance[] = new int[n];
int parent[] = new int[n];
// initialize `distance[]` and `parent[]`. Initially, all vertices
// except source vertex weight INFINITY and no parent
Arrays.fill(distance, Integer.MAX_VALUE);
distance[source] = 0;
Arrays.fill(parent, -1);
// relaxation step (run V-1 times)
for (int i = 0; i < n - 1; i++)
{
for (Edge edge: edges)
{
// edge from `u` to `v` having weight `w`
int u = edge.source;
int v = edge.dest;
int w = edge.weight;
// if the distance to destination `v` can be
// shortened by taking edge (u, v)
if (distance[u] != Integer.MAX_VALUE && distance[u] + w < distance[v])
{
// update distance to the new lower value
distance[v] = distance[u] + w;
// set v's parent as `u`
parent[v] = u;
}
}
}
// run relaxation step once more for n'th time to
// check for negative-weight cycles
for (Edge edge: edges)
{
// edge from `u` to `v` having weight `w`
int u = edge.source;
int v = edge.dest;
int w = edge.weight;
// if the distance to destination `u` can be
// shortened by taking edge (u, v)
if (distance[u] != Integer.MAX_VALUE && distance[u] + w < distance[v])
{
System.out.println("Negative-weight cycle is found!!");
return;
}
}
for (int i = 0; i < n; i++)
{
if (i != source && distance[i] < Integer.MAX_VALUE) {
List<Integer> path = new ArrayList<>();
getPath(parent, i, path);
System.out.println("The distance of vertex " + i + " from vertex " +
source + " is " + distance[i] + ". Its path is " + path);
}
}
}
public static void main(String[] args)
{
// List of graph edges as per the above diagram
List<Edge> edges = Arrays.asList(
// (x, y, w) —> edge from `x` to `y` having weight `w`
new Edge(0, 1, -1), new Edge(0, 2, 4), new Edge(1, 2, 3),
new Edge(1, 3, 2), new Edge(1, 4, 2), new Edge(3, 2, 5),
new Edge(3, 1, 1), new Edge(4, 3, -3 )
);
// set the maximum number of nodes in the graph
int n = 5;
// run the Bellman–Ford algorithm from every node
for (int source = 0; source < n; source++) {
bellmanFord(edges, source, n);
}
}
}