| ... | ... |
@@ -72,192 +72,209 @@ |
| 72 | 72 |
#else |
| 73 | 73 |
template <typename GR, |
| 74 | 74 |
typename CAP = typename GR::template ArcMap<int>, |
| 75 | 75 |
typename TOL = Tolerance<typename CAP::Value> > |
| 76 | 76 |
#endif |
| 77 | 77 |
class HaoOrlin {
|
| 78 | 78 |
public: |
| 79 | 79 |
|
| 80 | 80 |
/// The digraph type of the algorithm |
| 81 | 81 |
typedef GR Digraph; |
| 82 | 82 |
/// The capacity map type of the algorithm |
| 83 | 83 |
typedef CAP CapacityMap; |
| 84 | 84 |
/// The tolerance type of the algorithm |
| 85 | 85 |
typedef TOL Tolerance; |
| 86 | 86 |
|
| 87 | 87 |
private: |
| 88 | 88 |
|
| 89 | 89 |
typedef typename CapacityMap::Value Value; |
| 90 | 90 |
|
| 91 | 91 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 92 | 92 |
|
| 93 | 93 |
const Digraph& _graph; |
| 94 | 94 |
const CapacityMap* _capacity; |
| 95 | 95 |
|
| 96 | 96 |
typedef typename Digraph::template ArcMap<Value> FlowMap; |
| 97 | 97 |
FlowMap* _flow; |
| 98 | 98 |
|
| 99 | 99 |
Node _source; |
| 100 | 100 |
|
| 101 | 101 |
int _node_num; |
| 102 | 102 |
|
| 103 | 103 |
// Bucketing structure |
| 104 | 104 |
std::vector<Node> _first, _last; |
| 105 | 105 |
typename Digraph::template NodeMap<Node>* _next; |
| 106 | 106 |
typename Digraph::template NodeMap<Node>* _prev; |
| 107 | 107 |
typename Digraph::template NodeMap<bool>* _active; |
| 108 | 108 |
typename Digraph::template NodeMap<int>* _bucket; |
| 109 | 109 |
|
| 110 | 110 |
std::vector<bool> _dormant; |
| 111 | 111 |
|
| 112 | 112 |
std::list<std::list<int> > _sets; |
| 113 | 113 |
std::list<int>::iterator _highest; |
| 114 | 114 |
|
| 115 | 115 |
typedef typename Digraph::template NodeMap<Value> ExcessMap; |
| 116 | 116 |
ExcessMap* _excess; |
| 117 | 117 |
|
| 118 | 118 |
typedef typename Digraph::template NodeMap<bool> SourceSetMap; |
| 119 | 119 |
SourceSetMap* _source_set; |
| 120 | 120 |
|
| 121 | 121 |
Value _min_cut; |
| 122 | 122 |
|
| 123 | 123 |
typedef typename Digraph::template NodeMap<bool> MinCutMap; |
| 124 | 124 |
MinCutMap* _min_cut_map; |
| 125 | 125 |
|
| 126 | 126 |
Tolerance _tolerance; |
| 127 | 127 |
|
| 128 | 128 |
public: |
| 129 | 129 |
|
| 130 | 130 |
/// \brief Constructor |
| 131 | 131 |
/// |
| 132 | 132 |
/// Constructor of the algorithm class. |
| 133 | 133 |
HaoOrlin(const Digraph& graph, const CapacityMap& capacity, |
| 134 | 134 |
const Tolerance& tolerance = Tolerance()) : |
| 135 | 135 |
_graph(graph), _capacity(&capacity), _flow(0), _source(), |
| 136 | 136 |
_node_num(), _first(), _last(), _next(0), _prev(0), |
| 137 | 137 |
_active(0), _bucket(0), _dormant(), _sets(), _highest(), |
| 138 | 138 |
_excess(0), _source_set(0), _min_cut(), _min_cut_map(0), |
| 139 | 139 |
_tolerance(tolerance) {}
|
| 140 | 140 |
|
| 141 | 141 |
~HaoOrlin() {
|
| 142 | 142 |
if (_min_cut_map) {
|
| 143 | 143 |
delete _min_cut_map; |
| 144 | 144 |
} |
| 145 | 145 |
if (_source_set) {
|
| 146 | 146 |
delete _source_set; |
| 147 | 147 |
} |
| 148 | 148 |
if (_excess) {
|
| 149 | 149 |
delete _excess; |
| 150 | 150 |
} |
| 151 | 151 |
if (_next) {
|
| 152 | 152 |
delete _next; |
| 153 | 153 |
} |
| 154 | 154 |
if (_prev) {
|
| 155 | 155 |
delete _prev; |
| 156 | 156 |
} |
| 157 | 157 |
if (_active) {
|
| 158 | 158 |
delete _active; |
| 159 | 159 |
} |
| 160 | 160 |
if (_bucket) {
|
| 161 | 161 |
delete _bucket; |
| 162 | 162 |
} |
| 163 | 163 |
if (_flow) {
|
| 164 | 164 |
delete _flow; |
| 165 | 165 |
} |
| 166 | 166 |
} |
| 167 | 167 |
|
| 168 |
/// \brief Set the tolerance used by the algorithm. |
|
| 169 |
/// |
|
| 170 |
/// This function sets the tolerance object used by the algorithm. |
|
| 171 |
/// \return <tt>(*this)</tt> |
|
| 172 |
HaoOrlin& tolerance(const Tolerance& tolerance) {
|
|
| 173 |
_tolerance = tolerance; |
|
| 174 |
return *this; |
|
| 175 |
} |
|
| 176 |
|
|
| 177 |
/// \brief Returns a const reference to the tolerance. |
|
| 178 |
/// |
|
| 179 |
/// This function returns a const reference to the tolerance object |
|
| 180 |
/// used by the algorithm. |
|
| 181 |
const Tolerance& tolerance() const {
|
|
| 182 |
return _tolerance; |
|
| 183 |
} |
|
| 184 |
|
|
| 168 | 185 |
private: |
| 169 | 186 |
|
| 170 | 187 |
void activate(const Node& i) {
|
| 171 | 188 |
(*_active)[i] = true; |
| 172 | 189 |
|
| 173 | 190 |
int bucket = (*_bucket)[i]; |
| 174 | 191 |
|
| 175 | 192 |
if ((*_prev)[i] == INVALID || (*_active)[(*_prev)[i]]) return; |
| 176 | 193 |
//unlace |
| 177 | 194 |
(*_next)[(*_prev)[i]] = (*_next)[i]; |
| 178 | 195 |
if ((*_next)[i] != INVALID) {
|
| 179 | 196 |
(*_prev)[(*_next)[i]] = (*_prev)[i]; |
| 180 | 197 |
} else {
|
| 181 | 198 |
_last[bucket] = (*_prev)[i]; |
| 182 | 199 |
} |
| 183 | 200 |
//lace |
| 184 | 201 |
(*_next)[i] = _first[bucket]; |
| 185 | 202 |
(*_prev)[_first[bucket]] = i; |
| 186 | 203 |
(*_prev)[i] = INVALID; |
| 187 | 204 |
_first[bucket] = i; |
| 188 | 205 |
} |
| 189 | 206 |
|
| 190 | 207 |
void deactivate(const Node& i) {
|
| 191 | 208 |
(*_active)[i] = false; |
| 192 | 209 |
int bucket = (*_bucket)[i]; |
| 193 | 210 |
|
| 194 | 211 |
if ((*_next)[i] == INVALID || !(*_active)[(*_next)[i]]) return; |
| 195 | 212 |
|
| 196 | 213 |
//unlace |
| 197 | 214 |
(*_prev)[(*_next)[i]] = (*_prev)[i]; |
| 198 | 215 |
if ((*_prev)[i] != INVALID) {
|
| 199 | 216 |
(*_next)[(*_prev)[i]] = (*_next)[i]; |
| 200 | 217 |
} else {
|
| 201 | 218 |
_first[bucket] = (*_next)[i]; |
| 202 | 219 |
} |
| 203 | 220 |
//lace |
| 204 | 221 |
(*_prev)[i] = _last[bucket]; |
| 205 | 222 |
(*_next)[_last[bucket]] = i; |
| 206 | 223 |
(*_next)[i] = INVALID; |
| 207 | 224 |
_last[bucket] = i; |
| 208 | 225 |
} |
| 209 | 226 |
|
| 210 | 227 |
void addItem(const Node& i, int bucket) {
|
| 211 | 228 |
(*_bucket)[i] = bucket; |
| 212 | 229 |
if (_last[bucket] != INVALID) {
|
| 213 | 230 |
(*_prev)[i] = _last[bucket]; |
| 214 | 231 |
(*_next)[_last[bucket]] = i; |
| 215 | 232 |
(*_next)[i] = INVALID; |
| 216 | 233 |
_last[bucket] = i; |
| 217 | 234 |
} else {
|
| 218 | 235 |
(*_prev)[i] = INVALID; |
| 219 | 236 |
_first[bucket] = i; |
| 220 | 237 |
(*_next)[i] = INVALID; |
| 221 | 238 |
_last[bucket] = i; |
| 222 | 239 |
} |
| 223 | 240 |
} |
| 224 | 241 |
|
| 225 | 242 |
void findMinCutOut() {
|
| 226 | 243 |
|
| 227 | 244 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 228 | 245 |
(*_excess)[n] = 0; |
| 229 | 246 |
(*_source_set)[n] = false; |
| 230 | 247 |
} |
| 231 | 248 |
|
| 232 | 249 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 233 | 250 |
(*_flow)[a] = 0; |
| 234 | 251 |
} |
| 235 | 252 |
|
| 236 | 253 |
int bucket_num = 0; |
| 237 | 254 |
std::vector<Node> queue(_node_num); |
| 238 | 255 |
int qfirst = 0, qlast = 0, qsep = 0; |
| 239 | 256 |
|
| 240 | 257 |
{
|
| 241 | 258 |
typename Digraph::template NodeMap<bool> reached(_graph, false); |
| 242 | 259 |
|
| 243 | 260 |
reached[_source] = true; |
| 244 | 261 |
bool first_set = true; |
| 245 | 262 |
|
| 246 | 263 |
for (NodeIt t(_graph); t != INVALID; ++t) {
|
| 247 | 264 |
if (reached[t]) continue; |
| 248 | 265 |
_sets.push_front(std::list<int>()); |
| 249 | 266 |
|
| 250 | 267 |
queue[qlast++] = t; |
| 251 | 268 |
reached[t] = true; |
| 252 | 269 |
|
| 253 | 270 |
while (qfirst != qlast) {
|
| 254 | 271 |
if (qsep == qfirst) {
|
| 255 | 272 |
++bucket_num; |
| 256 | 273 |
_sets.front().push_front(bucket_num); |
| 257 | 274 |
_dormant[bucket_num] = !first_set; |
| 258 | 275 |
_first[bucket_num] = _last[bucket_num] = INVALID; |
| 259 | 276 |
qsep = qlast; |
| 260 | 277 |
} |
| 261 | 278 |
|
| 262 | 279 |
Node n = queue[qfirst++]; |
| 263 | 280 |
addItem(n, bucket_num); |
0 comments (0 inline)