-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBellmanFordAlgorithm.java
85 lines (71 loc) · 2.61 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
import java.util.*;
class BellmanFordAlgorithm{ //Shortest distance from every node to all other vertices
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;
}
}
static void initializeGraph(ArrayList<ArrayList<Edge>> graph, int V){
for(int i = 0; i<V; i++){
graph.add(new ArrayList<Edge>());
}
}
static void createAdjList(ArrayList<ArrayList<Edge>> graph, Edge e){
if((0<=e.src) && (e.src<graph.size())){
graph.get(e.src).add(e);
System.out.println("src : " + e.src + " dest: " +e.dest + " weight: "+e.wt);
}
else{
System.out.println("Node out of graph bounds, enter a valid source.");
}
}
static void shortestPath(ArrayList<ArrayList<Edge>> graph, int V, int src){
int[] dist = new int[V];
Arrays.fill(dist, Integer.MAX_VALUE);
dist[src] = 0;
for(int k = 0; k<V-1; k++){
for(int i = 0; i<V; i++){
for(Edge neighbor : graph.get(i)){
int u = neighbor.src;
int v = neighbor.dest;
int w = neighbor.wt;
if(dist[u] != Integer.MAX_VALUE && dist[u] + w < dist[v]){
dist[v] = dist[u] + w;
}
}
}
}
//Negative weight cycles
for(int i = 0; i<V; i++){
for(Edge neighbor : graph.get(i)){
int u = neighbor.src;
int v = neighbor.dest;
int w = neighbor.wt;
if(dist[u] != Integer.MAX_VALUE && dist[u] + w < dist[v]){
System.out.println("Negative weight cycle exists.");
break;
}
}
}
System.out.println("Distances: " + Arrays.toString(dist));
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter the no.of vertices: ");
int V = input.nextInt();
System.out.println("Enter the no.of edges: ");
int e = input.nextInt();
ArrayList<ArrayList<Edge>> graph = new ArrayList<ArrayList<Edge>>();
initializeGraph(graph, V);
for(int i = 0; i<e; i++){
System.out.println("Enter the edge (src, dest, weight/cost): ");
createAdjList(graph, new Edge(input.nextInt(), input.nextInt(), input.nextInt()));
}
shortestPath(graph, V, 0);
}
}