-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijkstra.cpp
62 lines (49 loc) · 1.85 KB
/
dijkstra.cpp
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
// summary for shortest path
// Dijkstra's algorithm: compute the shortest weighted paths to all the other nodes from a source
typedef pair<int, int> entry;
class Graph {
public:
Graph() {}
void addNode(int node);
void addEdge(int fromNode, int toNode, int weight);
vector<int> dijkstra(int origin); // compute the shortest path starting from the origin node
private:
unordered_set<int> nodes;
unordered_map<int, vector<entry> > adjacent;
}
void Graph::addNode(int node) {
nodes.insert(node);
}
void Graph::addEdge(int fromNode, int toNode, int weight) {
adjacent[fromNode].push_back(make_pair(toNode, weight));
addNode(fromNode);
addNode(toNode);
}
// Dijkstra's algorithm to compute the shortest paths to other nodes from a source
vector<int> Graph::dijkstra(int origin) {
// initialize distance
vector<int> dist(nodes.size(), INT_MAX);
dist[origin] = 0;
// use a min_heap to maintain a connection list
auto compare = [] (entry &u, entry &v) {
return (u.first < v.first);
}
priority_queue<entry, vector<entry>, decltype(compare)> minHeap(compare);
minHeap.push(make_pair(0, origin));
// main loop: keep retrieving a shortest-connect node and update the distance to its neighbors
while (!minHeap.empty()) {
entry curr = minHeap.top();
minHeap.pop();
int fromNode = curr.second;
int size = adjacent[fromNode].size();
for (int i = 0; i < size; ++i) {
int toNode = adjacent[fromNode][i].first;
int weight = adjacent[fromNode][i].second;
if (1L * dist[toNode] - dist[fromNode] - weight > 0) { // if shorter when taking a detour
dist[toNode] = dist[fromNode] + weight;
minHeap.push(make_pair(dist[toNode], toNode));
}
}
}
return dist;
}