-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeightedGraph.java
99 lines (90 loc) · 3.7 KB
/
WeightedGraph.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
import java.util.*;
public class WeightedGraph{
ArrayList<WeightedGraphNode> nodeList = new ArrayList<>();
public WeightedGraph(ArrayList<WeightedGraphNode> nodeList){
this.nodeList = nodeList;
}
public void addWeightedDirectedEdge(int i, int j, int weight){
nodeList.get(i).neighbors.add(nodeList.get(j));
nodeList.get(i).weightMap.put(nodeList.get(j), weight);
}
public static void pathPrint(WeightedGraphNode curr){
if(curr.parent != null)
pathPrint(curr.parent);
System.out.print(curr+" ");
}
void dijkstra(WeightedGraphNode node){
PriorityQueue<WeightedGraphNode> queue = new PriorityQueue<>();
node.distance = 0;
queue.addAll(nodeList);
while(!queue.isEmpty()){
WeightedGraphNode curr = queue.remove();
for(WeightedGraphNode neighbor: curr.neighbors){
if(queue.contains(neighbor)){
if(neighbor.distance > curr.distance + curr.weightMap.get(neighbor)){
neighbor.distance = curr.distance + curr.weightMap.get(neighbor);
neighbor.parent = curr;
queue.remove(neighbor);
queue.add(neighbor);
}
}
}
}
for(WeightedGraphNode curr: nodeList){
System.out.print("Node: "+curr+", distance: "+curr.distance+" Path:");
pathPrint(curr);
System.out.println();
curr.distance = Integer.MAX_VALUE;
}
}
public void bellmanFord(WeightedGraphNode node){
node.distance = 0;
for(int i=0; i<nodeList.size();i++){
for(WeightedGraphNode curr: nodeList){
for(WeightedGraphNode neighbor: curr.neighbors){
if(neighbor.distance > curr.distance + curr.weightMap.get(neighbor)){
neighbor.parent = curr;
neighbor.distance = curr.distance + curr.weightMap.get(neighbor);
}
}
}
}
System.out.println("Checking for negative cycle");
for(WeightedGraphNode curr: nodeList){
for(WeightedGraphNode neighbor: curr.neighbors){
if(neighbor.distance > curr.distance + curr.weightMap.get(neighbor)){
System.out.println("Negative Cycle Found!!\n");
return;
}
}
}
System.out.println("Negative Cycle not found");
for(WeightedGraphNode curr: nodeList){
System.out.print("Node: "+curr+", distance: "+curr.distance+" Path:");
pathPrint(curr);
System.out.println();
curr.distance = Integer.MAX_VALUE;
}
}
// Floyd Warshall Algo for All pair Shortest path problem
public void floydWarshall(){
int size = this.nodeList.size();
int[][] V = new int[size][size];
for(int i=0; i<size; i++){
for(int j=0; j<size; j++){
if(i==j) V[i][j] = 0;
else if (nodeList.get(i).weightMap.containsKey(nodeList.get(j)))
V[i][j] = nodeList.get(i).weightMap.get(nodeList.get(j));
else V[i][j] = Integer.MAX_VALUE / 10;
}
}
for(int k=0; k<size; k++) for(int i=0; i<size; i++) for(int j=0; j<size; j++)
if( V[i][j] > V[i][k] + V[k][j] ) V[i][j] = V[i][k] + V[k][j];
for(int i=0; i<size; i++){
System.out.print("Printing distance list for the node : "+nodeList.get(i)+" :");
for(int j=0;j<size;j++)
System.out.print(" "+V[i][j]);
System.out.println();
}
}
}