forked from profpiyush/Hacktberfest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDijkstraAlgo.java
148 lines (123 loc) · 4.03 KB
/
DijkstraAlgo.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.*;
// A class to store a graph edge
class Edge
{
int source, dest, weight;
public Edge(int source, int dest, int weight)
{
this.source = source;
this.dest = dest;
this.weight = weight;
}
}
// A class to store a heap node
class Node
{
int vertex, weight;
public Node(int vertex, int weight)
{
this.vertex = vertex;
this.weight = weight;
}
}
// A class to represent a graph object
class Graph
{
// A list of lists to represent an adjacency list
List<List<Edge>> adjList = null;
// Constructor
Graph(List<Edge> edges, int n)
{
adjList = new ArrayList<>();
for (int i = 0; i < n; i++) {
adjList.add(new ArrayList<>());
}
// add edges to the directed graph
for (Edge edge: edges) {
adjList.get(edge.source).add(edge);
}
}
}
class Main
{
private static void getRoute(int[] prev, int i, List<Integer> route)
{
if (i >= 0)
{
getRoute(prev, prev[i], route);
route.add(i);
}
}
// Run Dijkstra’s algorithm on a given graph
public static void findShortestPaths(Graph graph, int source, int n)
{
// create a min-heap and push source node having distance 0
PriorityQueue<Node> minHeap;
minHeap = new PriorityQueue<>(Comparator.comparingInt(node -> node.weight));
minHeap.add(new Node(source, 0));
// set initial distance from the source to `v` as infinity
List<Integer> dist;
dist = new ArrayList<>(Collections.nCopies(n, Integer.MAX_VALUE));
// distance from the source to itself is zero
dist.set(source, 0);
// boolean array to track vertices for which minimum
// cost is already found
boolean[] done = new boolean[n];
done[source] = true;
// stores predecessor of a vertex (to a print path)
int[] prev = new int[n];
prev[source] = -1;
// run till min-heap is empty
while (!minHeap.isEmpty())
{
// Remove and return the best vertex
Node node = minHeap.poll();
// get the vertex number
int u = node.vertex;
// do for each neighbor `v` of `u`
for (Edge edge: graph.adjList.get(u))
{
int v = edge.dest;
int weight = edge.weight;
// Relaxation step
if (!done[v] && (dist.get(u) + weight) < dist.get(v))
{
dist.set(v, dist.get(u) + weight);
prev[v] = u;
minHeap.add(new Node(v, dist.get(v)));
}
}
// mark vertex `u` as done so it will not get picked up again
done[u] = true;
}
List<Integer> route = new ArrayList<>();
for (int i = 0; i < n; i++)
{
if (i != source && dist.get(i) != Integer.MAX_VALUE)
{
getRoute(prev, i, route);
System.out.printf("Path (%d —> %d): Minimum cost = %d, Route = %s\n",
source, i, dist.get(i), route);
route.clear();
}
}
}
public static void main(String[] args)
{
// initialize edges as per the above diagram
// (u, v, w) represent edge from vertex `u` to vertex `v` having weight `w`
List<Edge> edges = Arrays.asList(
new Edge(0, 1, 10), new Edge(0, 4, 3), new Edge(1, 2, 2),
new Edge(1, 4, 4), new Edge(2, 3, 9), new Edge(3, 2, 7),
new Edge(4, 1, 1), new Edge(4, 2, 8), new Edge(4, 3, 2)
);
// Total number of nodes in the graph (labelled from 0 to 4)
int n = 5;
// Construct graph
Graph graph = new Graph(edges, n);
// run the Dijkstra’s algorithm from every node
for (int source = 0; source < n; source++) {
findShortestPaths(graph, source, n);
}
}
}