-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBellmanFord.java
More file actions
56 lines (56 loc) · 1.83 KB
/
BellmanFord.java
File metadata and controls
56 lines (56 loc) · 1.83 KB
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
import java.util.*;
public class BellmanFord {
static final int INF=99;
void printSolution(int dist[],int n) {
int i;
System.out.println("Vertex\tDistance from Source");
for(i=0;i<n;++i) {
if(dist[i]==INF)
System.out.println(i + "\t\tINF");
else
System.out.println(i + "\t\t" +dist[i]);
}
}
//Function to run the Bellman-Ford algorithm
void bellmanFord(int[][] graph, int V,int src) {
int dist[] = new int[V];
int i,u,v;
for(i=0;i<V;i++)
dist[i]=INF;
dist[src]=0;
for(i=1;i<=V-1;i++) {
for(u=0;u<V;u++) {
for(v=0;v<V;v++) {
if(graph[u][v]!=INF && dist[u]+graph[u][v]<dist[v])
dist[v] = dist[u] +graph[u][v];
}
}
}
for(u=0;u<V;u++) {
for(v=0;v<V;v++) {
if(graph[u][v]!=INF && dist[u]+graph[u][v]<dist[v]) {
System.out.println("Graph contains negative weight cycle");
return;
}
}
}
printSolution(dist,V);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int i,j,V,src;
System.out.print("Enter the number of vertices: ");
V=sc.nextInt();
int graph[][] = new int[V][V];
System.out.println("Enter the adjacncy matrix(use " + INF + " for INF ): ");
for(i=0;i<V;i++) {
for(j=0;j<V;j++)
graph[i][j]=sc.nextInt();
}
System.out.print("Enter the source vertex: ");
src=sc.nextInt();
BellmanFord ob=new BellmanFord();
ob.bellmanFord(graph, V, src);
sc.close();
}
}