forked from SoumyadeepMukherjee/Advanced-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbellmanford.java
148 lines (148 loc) · 2.95 KB
/
bellmanford.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import java.util.ArrayList;
import java.util.List;
class Graph
{
//vertices of the graph
private int V;
//edges in the graph
private List<Edge> edges;
//creating a constructor of the Graph class and generating getters and setters
public Graph(int v)
{
V = v;
edges = new ArrayList<Edge>();
}
public int getV()
{
return V;
}
public void setV(int v)
{
V = v;
}
public List<Edge> getEdges()
{
return edges;
}
public void setEdges(List<Edge> edges)
{
this.edges = edges;
}
public void addEdge(int u, int v, int w)
{
Edge e = new Edge(u, v, w);
edges.add(e);
}
}
class Edge
{
//it is the source vertex
private int u;
//it is the destination vertex
private int v;
//it denotes the weight on edge
private int w;
//generating getters and setters
public int getU()
{
return u;
}
public void setU(int u)
{
this.u = u;
}
public int getV()
{
return v;
}
public void setV(int v)
{
this.v = v;
}
public int getW()
{
return w;
}
public void setW(int w)
{
this.w = w;
}
//creating constructor of the Edge class
public Edge(int u, int v, int w)
{
this.u = u;
this.v = v;
this.w = w;
}
}
public class BellmanFordImplementation
{
public static void main(String args[])
{
Graph g = createGraph();
int distance[] = new int[g.getV()];
boolean hasNegativeCycle = getShortestPaths(g, 1, distance);
if(!hasNegativeCycle)
{
System.out.println("Vertex \t: Distance");
for(int i = 1; i < distance.length; i++)
System.out.println("\t"+i + " " + "\t\t"+(distance[i] == Integer.MAX_VALUE ? "-" : distance[i]));
}
else
{
System.out.println("Negative cycle exists in the graph, no solution found!!!");
}
}
private static Graph createGraph()
{
int v = 7;
//creating a graph having 7 vertices
Graph g = new Graph(v);
//adding edges to the graph
g.addEdge(1, 2, 4);
g.addEdge(1, 4, 9);
g.addEdge(2, 3, -1);
g.addEdge(3, 6, 3);
g.addEdge(4, 3, 2);
g.addEdge(4, 5, -5);
g.addEdge(5, 6, 0);
//returns graph
return g;
}
//Bellman-Ford logic
public static boolean getShortestPaths(Graph g, int source, int[] distance)
{
int V = g.getV();
//initializing distances from source to other vertices
for(int i = 1; i < V; i++)
{
distance[i] = Integer.MAX_VALUE;
}
//source vertex initialize to 0
distance[source] = 0;
//relaxing edges
for(int i = 1; i < V; i++)
{
//iterate over edges
for(Edge e: g.getEdges())
{
int u = e.getU(), v = e.getV(), w = e.getW();
if(distance[u] != Integer.MAX_VALUE && distance[v] > distance[u] + w)
{
//calculates distance
distance[v] = distance[u] + w;
}
}
}
//checks if there exist negative cycles in graph G
for(Edge e: g.getEdges())
{
int u = e.getU(), v = e.getV(), w = e.getW();
if(distance[u] != Integer.MAX_VALUE && distance[v] > distance[u] + w)
{
return true;
}
}
return false;
}
}