-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBellmanFordGraphSSSP.java
More file actions
143 lines (118 loc) · 5.33 KB
/
BellmanFordGraphSSSP.java
File metadata and controls
143 lines (118 loc) · 5.33 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
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
import java.util.*;
// A weighted Graph Node
class GraphNode{
String name;
// list to store neighbours
ArrayList<GraphNode> neighbours;
// hashmap to store edge weight
HashMap<GraphNode,Integer> weightMap;
// shortest distance from source node
int distance;
//parent node
GraphNode parent;
// constructor
GraphNode(String name){
this.name = name;
this.neighbours = new ArrayList<>();
this.weightMap = new HashMap<>();
this.distance = Integer.MAX_VALUE/2;
this.parent = null;
}
}
public class BellmanFordGraphSSSP {
ArrayList<GraphNode> nodesList;
//constructor
BellmanFordGraphSSSP(){
this.nodesList = new ArrayList<>();
}
// method to find shortest distance from source node to all other nodes
// Time Complexity: O(V.E) Space Complexity: O(V)
public void bellmanFord(GraphNode source){
// setting distance of source node as zero
source.distance=0;
// run v-1 times
for(int i=1;i<this.nodesList.size();i++){
// for all node's neighbours
// so going for all nodes first (u)
for(GraphNode u : this.nodesList){
// now going for each edge of node (v)
for(GraphNode v : u.neighbours){
if(u.distance==Integer.MAX_VALUE && v.distance==Integer.MAX_VALUE){
continue;
}
else if((u.distance + u.weightMap.get(v))<v.distance){
v.distance = u.distance + u.weightMap.get(v);
v.parent = u;
}
}
}
}
// Vth iteration , here V is no. of vertices/nodes in the graph
// used to detect negative cycle
for(GraphNode u : this.nodesList){
for(GraphNode v : u.neighbours){
// if at any time the current value gets updated, it means we have a negative cycle
if((u.distance + u.weightMap.get(v)) < v.distance){
System.out.println("A Negative Cycle exists in the given graph, hence can't find SSSP");
return;
}
}
}
// printing shortest path for all nodes from the given source node
for(int i=0;i<this.nodesList.size();i++){
System.out.println("Printing Shortest path from source node: "+source.name+" to node : "+this.nodesList.get(i).name);
System.out.print(" Total distance: "+this.nodesList.get(i).distance+" Shortest path: ");
printPath(this.nodesList.get(i));
System.out.println();
System.out.println();
}
}
// A helper function to print the path from source node to the given node
private void printPath(GraphNode node){
if(node.parent!=null){
printPath(node.parent);
}
System.out.print(node.name+" ");
}
//a helper function to add directed weighted edge between source and dest node
private void addDirectedWeightedEdge(int i, int j, int weight){
GraphNode src = this.nodesList.get(i-1);
GraphNode dest = this.nodesList.get(j-1);
src.neighbours.add(dest);
src.weightMap.put(dest,weight);
}
public static void main(String[] args) throws Exception {
BellmanFordGraphSSSP graph = new BellmanFordGraphSSSP();
// adding 5 nodes V1-V5 to nodeList
for(int i=1;i<=5;i++){
graph.nodesList.add(new GraphNode("V"+i));
}
// Now adding directed weighted edges between nodes
// Diagram for the graph used is on the top of post: https://theheapspace.blogspot.com/2020/06/single-source-shortest-path-bellman.html
graph.addDirectedWeightedEdge(1,3,6); //Add V1 -> V3 , weight 6
graph.addDirectedWeightedEdge(1,4,6); //Add V1 -> V4 , weight 6
graph.addDirectedWeightedEdge(2,1,3); //Add V2 -> V1 , weight 3
graph.addDirectedWeightedEdge(3,4,2); //Add V3 -> V4 , weight 2
graph.addDirectedWeightedEdge(4,3,1); //Add V4 -> V3 , weight 1
graph.addDirectedWeightedEdge(4,2,1); //Add V4 -> V2 , weight 1
graph.addDirectedWeightedEdge(5,2,4); //Add V5 -> V2 , weight 4
graph.addDirectedWeightedEdge(5,4,2); //Add V5 -> V4 , weight 2
// Now we have added all nodes and directed weighted edges to it, we can now use Bellman Ford algo
System.out.println("Printing Shortest Path from source V5 to all other nodes using Bellman Ford: ");
System.out.println();
graph.bellmanFord(graph.nodesList.get(4));
}
}
/*=======================================OUTPUT==========================================
Printing Shortest Path from source V5 to all other nodes using Bellman Ford:
Printing Shortest path from source node: V5 to node : V1
Total distance: 6 Shortest path: V5 V4 V2 V1
Printing Shortest path from source node: V5 to node : V2
Total distance: 3 Shortest path: V5 V4 V2
Printing Shortest path from source node: V5 to node : V3
Total distance: 3 Shortest path: V5 V4 V3
Printing Shortest path from source node: V5 to node : V4
Total distance: 2 Shortest path: V5 V4
Printing Shortest path from source node: V5 to node : V5
Total distance: 0 Shortest path: V5
========================================================================================*/