forked from leo000leo/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweighted_digraph_algorithm.h
More file actions
238 lines (229 loc) · 8.46 KB
/
Copy pathweighted_digraph_algorithm.h
File metadata and controls
238 lines (229 loc) · 8.46 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//
// weighted_digraph_algorithm
// Algorithm
//
// Created by dtysky on 2017/3/11.
// Copyright © 2017 [email protected]. All rights reserved.
//
#ifndef ALGORITHM_WEIGHTED_DIGRAPH_ALGORITHM_H
#define ALGORITHM_WEIGHTED_DIGRAPH_ALGORITHM_H
#include <cstdio>
#include "weighted_digraph.h"
#include "priority_queue.h"
#include "union_find.h"
namespace my_algorithm {
using data_structures::WeightedDigraph;
using data_structures::WeightedGraphEdge;
using data_structures::WeightedGraphAdjSet;
using data_structures::TreeElement;
using data_structures::RBTreeNode;
using data_structures::SymbolTable;
using data_structures::PriorityQueue;
// only works on connected graph and the weight must be positive
template <typename T, typename Weight>
class WeightedDigraphSPDijkstra {
typedef RBTreeNode<TreeElement<T, WeightedGraphAdjSet<T, Weight>>> Node;
typedef WeightedGraphEdge<Node, Weight> Edge;
struct SPElement {
public:
T vertex;
Weight weight;
SPElement() {}
SPElement(T vertex, const Weight& weight) {
this->vertex = vertex;
this->weight = weight;
}
SPElement(const SPElement& element) {
vertex = element.vertex;
weight = element.weight;
}
SPElement& operator=(const SPElement& element) {
vertex = element.vertex;
weight = element.weight;
return *this;
}
bool operator==(const SPElement& element) const {
return vertex == element.vertex;
}
bool operator!=(const SPElement& element) const {
return vertex != element.vertex;
}
bool operator>(const SPElement& element) const {
return weight > element.weight;
}
bool operator<(const SPElement& element) const {
return weight < element.weight;
}
bool operator>=(const SPElement& element) const {
return weight >= element.weight;
}
bool operator<=(const SPElement& element) const {
return weight <= element.weight;
}
};
protected:
T _start;
SymbolTable<T, Edge> _edge_to;
SymbolTable<T, Weight> _weight_to;
PriorityQueue<SPElement> _queue;
void _relax(WeightedDigraph<T, Weight> &graph, const SPElement& element) {
auto vertex = element.vertex;
auto weight = element.weight;
auto node = graph.getNode(vertex);
auto edges = node->element.value.adjEdges();
for (auto &edge: edges) {
auto to_vertex = edge.to->element.key;
auto new_weight = weight + edge.weight;
if (!_weight_to.has(to_vertex)) {
_weight_to.set(to_vertex, new_weight);
_edge_to.set(to_vertex, edge);
_queue.enqueue(SPElement(to_vertex, new_weight));
continue;
}
if (_weight_to.get(to_vertex) > new_weight) {
_weight_to.set(to_vertex, new_weight);
_edge_to.set(to_vertex, edge);
// todo: create a new priority queue which supports method `change`
auto size = _queue.size();
for (size_t i = 1; i <= size; i++) {
if (_queue.minN(i).vertex == to_vertex) {
_queue.dequeueMinN(i);
break;
}
}
_queue.enqueue(SPElement(to_vertex, new_weight));
}
}
}
public:
WeightedDigraphSPDijkstra(WeightedDigraph<T, Weight> &graph, T vertex) {
_start = vertex;
_edge_to.clear();
_weight_to.clear();
_queue.clear();
auto start = graph.getNode(vertex);
_edge_to.set(vertex, Edge(start, start, Weight()));
_weight_to.set(vertex, Weight());
_queue.enqueue(SPElement(_start, Weight()));
while (!_queue.isEmpty()) {
_relax(graph, _queue.dequeueMinN(1));
}
}
virtual ~WeightedDigraphSPDijkstra() {}
Weight weightTo(T vertex) {
if (!hasPathTo(vertex)) {
throw std::out_of_range("No path!");
}
return _weight_to.get(vertex);
}
bool hasPathTo(T vertex) {
return _weight_to.has(vertex);
}
std::vector<Edge>pathTo(T vertex) {
if (!hasPathTo(vertex)) {
throw std::out_of_range("No path!");
}
std::vector<Edge> path;
auto edge = _edge_to.get(vertex);
while (true) {
path.insert(path.begin(), edge);
if (edge.from->element.key == _start) {
break;
}
edge = _edge_to.get(edge.from->element.key);
}
return path;
}
};
// only works on DAG
template <typename T, typename Weight>
class WeightedDigraphSPAcyclic {
typedef RBTreeNode<TreeElement<T, WeightedGraphAdjSet<T, Weight>>> Node;
typedef WeightedGraphEdge<Node, Weight> Edge;
protected:
T _start;
SymbolTable<T, Edge> _edge_to;
SymbolTable<T, Weight> _weight_to;
std::vector<Node*> _marked_nodes;
std::vector<Node*> _reserve_post_nodes;
void _dfs(Node* vertex) {
auto edges = vertex->element.value.adjEdges();
_marked_nodes.push_back(vertex);
for (auto &edge: edges) {
if (isInVector<Node*>(_marked_nodes, edge.to)) {
continue;
}
_dfs(edge.to);
}
_reserve_post_nodes.insert(_reserve_post_nodes.begin(), vertex);
}
void _relax(WeightedDigraph<T, Weight> &graph, Node* node) {
auto vertex = node->element.key;
auto weight = _weight_to.get(vertex);
auto edges = node->element.value.adjEdges();
for (auto &edge: edges) {
auto to_vertex = edge.to->element.key;
auto new_weight = weight + edge.weight;
if (!_weight_to.has(to_vertex)) {
_weight_to.set(to_vertex, new_weight);
_edge_to.set(to_vertex, edge);
continue;
}
if (_weight_to.get(to_vertex) > new_weight) {
_weight_to.set(to_vertex, new_weight);
_edge_to.set(to_vertex, edge);
}
}
}
public:
WeightedDigraphSPAcyclic(WeightedDigraph<T, Weight> &graph) {
_marked_nodes.clear();
_reserve_post_nodes.clear();
auto nodes = graph.getAllNodes();
// Topological sorting
for (auto node: nodes) {
if (!isInVector<Node*>(_marked_nodes, node)) {
_dfs(node);
}
}
auto start_node = _reserve_post_nodes[0];
_start = start_node->element.key;
_edge_to.clear();
_weight_to.clear();
_edge_to.set(_start, Edge(start_node, start_node, Weight()));
_weight_to.set(_start, Weight());
for (auto node: _reserve_post_nodes) {
_relax(graph, node);
}
}
virtual ~WeightedDigraphSPAcyclic() {}
T start() {
return _start;
}
Weight weightTo(T vertex) {
if (!hasPathTo(vertex)) {
throw std::out_of_range("No path!");
}
return _weight_to.get(vertex);
}
bool hasPathTo(T vertex) {
return _weight_to.has(vertex);
}
std::vector<Edge>pathTo(T vertex) {
if (!hasPathTo(vertex)) {
throw std::out_of_range("No path!");
}
std::vector<Edge> path;
auto edge = _edge_to.get(vertex);
while (true) {
path.insert(path.begin(), edge);
if (edge.from->element.key == _start) {
break;
}
edge = _edge_to.get(edge.from->element.key);
}
return path;
}
};
}
#endif //ALGORITHM_WEIGHTED_DIGRAPH_ALGORITHM_H