0
7
0
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_ADAPTORS_H |
20 | 20 |
#define LEMON_ADAPTORS_H |
21 | 21 |
|
22 | 22 |
/// \ingroup graph_adaptors |
23 | 23 |
/// \file |
24 | 24 |
/// \brief Adaptor classes for digraphs and graphs |
25 | 25 |
/// |
26 | 26 |
/// This file contains several useful adaptors for digraphs and graphs. |
27 | 27 |
|
28 | 28 |
#include <lemon/core.h> |
29 | 29 |
#include <lemon/maps.h> |
30 | 30 |
#include <lemon/bits/variant.h> |
31 | 31 |
|
32 | 32 |
#include <lemon/bits/graph_adaptor_extender.h> |
33 |
#include <lemon/bits/map_extender.h> |
|
33 | 34 |
#include <lemon/tolerance.h> |
34 | 35 |
|
35 | 36 |
#include <algorithm> |
36 | 37 |
|
37 | 38 |
namespace lemon { |
38 | 39 |
|
39 | 40 |
#ifdef _MSC_VER |
40 | 41 |
#define LEMON_SCOPE_FIX(OUTER, NESTED) OUTER::NESTED |
41 | 42 |
#else |
42 | 43 |
#define LEMON_SCOPE_FIX(OUTER, NESTED) typename OUTER::template NESTED |
43 | 44 |
#endif |
44 | 45 |
|
45 | 46 |
template<typename DGR> |
46 | 47 |
class DigraphAdaptorBase { |
47 | 48 |
public: |
48 | 49 |
typedef DGR Digraph; |
49 | 50 |
typedef DigraphAdaptorBase Adaptor; |
50 | 51 |
|
51 | 52 |
protected: |
52 | 53 |
DGR* _digraph; |
53 | 54 |
DigraphAdaptorBase() : _digraph(0) { } |
54 | 55 |
void initialize(DGR& digraph) { _digraph = &digraph; } |
55 | 56 |
|
56 | 57 |
public: |
57 | 58 |
DigraphAdaptorBase(DGR& digraph) : _digraph(&digraph) { } |
58 | 59 |
|
59 | 60 |
typedef typename DGR::Node Node; |
60 | 61 |
typedef typename DGR::Arc Arc; |
61 | 62 |
|
62 | 63 |
void first(Node& i) const { _digraph->first(i); } |
63 | 64 |
void first(Arc& i) const { _digraph->first(i); } |
64 | 65 |
void firstIn(Arc& i, const Node& n) const { _digraph->firstIn(i, n); } |
65 | 66 |
void firstOut(Arc& i, const Node& n ) const { _digraph->firstOut(i, n); } |
66 | 67 |
|
67 | 68 |
void next(Node& i) const { _digraph->next(i); } |
68 | 69 |
void next(Arc& i) const { _digraph->next(i); } |
69 | 70 |
void nextIn(Arc& i) const { _digraph->nextIn(i); } |
70 | 71 |
void nextOut(Arc& i) const { _digraph->nextOut(i); } |
71 | 72 |
|
72 | 73 |
Node source(const Arc& a) const { return _digraph->source(a); } |
73 | 74 |
Node target(const Arc& a) const { return _digraph->target(a); } |
74 | 75 |
|
75 | 76 |
typedef NodeNumTagIndicator<DGR> NodeNumTag; |
76 | 77 |
int nodeNum() const { return _digraph->nodeNum(); } |
77 | 78 |
|
78 | 79 |
typedef ArcNumTagIndicator<DGR> ArcNumTag; |
79 | 80 |
int arcNum() const { return _digraph->arcNum(); } |
80 | 81 |
|
81 | 82 |
typedef FindArcTagIndicator<DGR> FindArcTag; |
82 | 83 |
Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) const { |
83 | 84 |
return _digraph->findArc(u, v, prev); |
84 | 85 |
} |
85 | 86 |
|
86 | 87 |
Node addNode() { return _digraph->addNode(); } |
87 | 88 |
Arc addArc(const Node& u, const Node& v) { return _digraph->addArc(u, v); } |
88 | 89 |
|
89 | 90 |
void erase(const Node& n) { _digraph->erase(n); } |
90 | 91 |
void erase(const Arc& a) { _digraph->erase(a); } |
91 | 92 |
|
92 | 93 |
void clear() { _digraph->clear(); } |
93 | 94 |
|
94 | 95 |
int id(const Node& n) const { return _digraph->id(n); } |
95 | 96 |
int id(const Arc& a) const { return _digraph->id(a); } |
96 | 97 |
|
97 | 98 |
Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); } |
98 | 99 |
Arc arcFromId(int ix) const { return _digraph->arcFromId(ix); } |
99 | 100 |
|
100 | 101 |
int maxNodeId() const { return _digraph->maxNodeId(); } |
101 | 102 |
int maxArcId() const { return _digraph->maxArcId(); } |
102 | 103 |
|
103 | 104 |
typedef typename ItemSetTraits<DGR, Node>::ItemNotifier NodeNotifier; |
104 | 105 |
NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); } |
105 | 106 |
|
106 | 107 |
typedef typename ItemSetTraits<DGR, Arc>::ItemNotifier ArcNotifier; |
107 | 108 |
ArcNotifier& notifier(Arc) const { return _digraph->notifier(Arc()); } |
108 | 109 |
|
109 | 110 |
template <typename V> |
110 | 111 |
class NodeMap : public DGR::template NodeMap<V> { |
111 | 112 |
public: |
112 | 113 |
|
113 | 114 |
typedef typename DGR::template NodeMap<V> Parent; |
114 | 115 |
|
115 | 116 |
explicit NodeMap(const Adaptor& adaptor) |
116 | 117 |
: Parent(*adaptor._digraph) {} |
117 | 118 |
|
118 | 119 |
NodeMap(const Adaptor& adaptor, const V& value) |
119 | 120 |
: Parent(*adaptor._digraph, value) { } |
120 | 121 |
|
121 | 122 |
private: |
122 | 123 |
NodeMap& operator=(const NodeMap& cmap) { |
123 | 124 |
return operator=<NodeMap>(cmap); |
124 | 125 |
} |
125 | 126 |
|
126 | 127 |
template <typename CMap> |
127 | 128 |
NodeMap& operator=(const CMap& cmap) { |
128 | 129 |
Parent::operator=(cmap); |
129 | 130 |
return *this; |
130 | 131 |
} |
131 | 132 |
|
132 | 133 |
}; |
133 | 134 |
|
134 | 135 |
template <typename V> |
135 | 136 |
class ArcMap : public DGR::template ArcMap<V> { |
136 | 137 |
public: |
137 | 138 |
|
138 | 139 |
typedef typename DGR::template ArcMap<V> Parent; |
139 | 140 |
|
140 | 141 |
explicit ArcMap(const DigraphAdaptorBase<DGR>& adaptor) |
141 | 142 |
: Parent(*adaptor._digraph) {} |
142 | 143 |
|
143 | 144 |
ArcMap(const DigraphAdaptorBase<DGR>& adaptor, const V& value) |
144 | 145 |
: Parent(*adaptor._digraph, value) {} |
145 | 146 |
|
146 | 147 |
private: |
147 | 148 |
ArcMap& operator=(const ArcMap& cmap) { |
148 | 149 |
return operator=<ArcMap>(cmap); |
149 | 150 |
} |
150 | 151 |
|
151 | 152 |
template <typename CMap> |
152 | 153 |
ArcMap& operator=(const CMap& cmap) { |
153 | 154 |
Parent::operator=(cmap); |
154 | 155 |
return *this; |
155 | 156 |
} |
156 | 157 |
|
157 | 158 |
}; |
158 | 159 |
|
159 | 160 |
}; |
160 | 161 |
1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BITS_EDGE_SET_EXTENDER_H |
20 | 20 |
#define LEMON_BITS_EDGE_SET_EXTENDER_H |
21 | 21 |
|
22 |
#include <lemon/core.h> |
|
22 | 23 |
#include <lemon/error.h> |
23 | 24 |
#include <lemon/bits/default_map.h> |
25 |
#include <lemon/bits/map_extender.h> |
|
24 | 26 |
|
25 | 27 |
///\ingroup digraphbits |
26 | 28 |
///\file |
27 | 29 |
///\brief Extenders for the arc set types |
28 | 30 |
namespace lemon { |
29 | 31 |
|
30 | 32 |
/// \ingroup digraphbits |
31 | 33 |
/// |
32 | 34 |
/// \brief Extender for the ArcSets |
33 | 35 |
template <typename Base> |
34 | 36 |
class ArcSetExtender : public Base { |
35 | 37 |
public: |
36 | 38 |
|
37 | 39 |
typedef Base Parent; |
38 | 40 |
typedef ArcSetExtender Digraph; |
39 | 41 |
|
40 | 42 |
// Base extensions |
41 | 43 |
|
42 | 44 |
typedef typename Parent::Node Node; |
43 | 45 |
typedef typename Parent::Arc Arc; |
44 | 46 |
|
45 | 47 |
int maxId(Node) const { |
46 | 48 |
return Parent::maxNodeId(); |
47 | 49 |
} |
48 | 50 |
|
49 | 51 |
int maxId(Arc) const { |
50 | 52 |
return Parent::maxArcId(); |
51 | 53 |
} |
52 | 54 |
|
53 | 55 |
Node fromId(int id, Node) const { |
54 | 56 |
return Parent::nodeFromId(id); |
55 | 57 |
} |
56 | 58 |
|
57 | 59 |
Arc fromId(int id, Arc) const { |
58 | 60 |
return Parent::arcFromId(id); |
59 | 61 |
} |
60 | 62 |
|
61 | 63 |
Node oppositeNode(const Node &n, const Arc &e) const { |
62 | 64 |
if (n == Parent::source(e)) |
63 | 65 |
return Parent::target(e); |
64 | 66 |
else if(n==Parent::target(e)) |
65 | 67 |
return Parent::source(e); |
66 | 68 |
else |
67 | 69 |
return INVALID; |
68 | 70 |
} |
69 | 71 |
|
70 | 72 |
|
71 | 73 |
// Alteration notifier extensions |
72 | 74 |
|
73 | 75 |
/// The arc observer registry. |
74 | 76 |
typedef AlterationNotifier<ArcSetExtender, Arc> ArcNotifier; |
75 | 77 |
|
76 | 78 |
protected: |
77 | 79 |
|
78 | 80 |
mutable ArcNotifier arc_notifier; |
79 | 81 |
|
80 | 82 |
public: |
81 | 83 |
|
82 | 84 |
using Parent::notifier; |
83 | 85 |
|
84 | 86 |
/// \brief Gives back the arc alteration notifier. |
85 | 87 |
/// |
86 | 88 |
/// Gives back the arc alteration notifier. |
87 | 89 |
ArcNotifier& notifier(Arc) const { |
88 | 90 |
return arc_notifier; |
89 | 91 |
} |
90 | 92 |
|
91 | 93 |
// Iterable extensions |
92 | 94 |
|
93 | 95 |
class NodeIt : public Node { |
94 | 96 |
const Digraph* digraph; |
95 | 97 |
public: |
96 | 98 |
|
97 | 99 |
NodeIt() {} |
98 | 100 |
|
99 | 101 |
NodeIt(Invalid i) : Node(i) { } |
100 | 102 |
|
101 | 103 |
explicit NodeIt(const Digraph& _graph) : digraph(&_graph) { |
102 | 104 |
_graph.first(static_cast<Node&>(*this)); |
103 | 105 |
} |
104 | 106 |
|
105 | 107 |
NodeIt(const Digraph& _graph, const Node& node) |
106 | 108 |
: Node(node), digraph(&_graph) {} |
107 | 109 |
|
108 | 110 |
NodeIt& operator++() { |
109 | 111 |
digraph->next(*this); |
110 | 112 |
return *this; |
111 | 113 |
} |
112 | 114 |
|
113 | 115 |
}; |
114 | 116 |
|
115 | 117 |
|
116 | 118 |
class ArcIt : public Arc { |
117 | 119 |
const Digraph* digraph; |
118 | 120 |
public: |
119 | 121 |
|
120 | 122 |
ArcIt() { } |
121 | 123 |
|
122 | 124 |
ArcIt(Invalid i) : Arc(i) { } |
123 | 125 |
|
124 | 126 |
explicit ArcIt(const Digraph& _graph) : digraph(&_graph) { |
125 | 127 |
_graph.first(static_cast<Arc&>(*this)); |
126 | 128 |
} |
127 | 129 |
|
128 | 130 |
ArcIt(const Digraph& _graph, const Arc& e) : |
129 | 131 |
Arc(e), digraph(&_graph) { } |
130 | 132 |
|
131 | 133 |
ArcIt& operator++() { |
132 | 134 |
digraph->next(*this); |
133 | 135 |
return *this; |
134 | 136 |
} |
135 | 137 |
|
136 | 138 |
}; |
137 | 139 |
|
138 | 140 |
|
139 | 141 |
class OutArcIt : public Arc { |
140 | 142 |
const Digraph* digraph; |
141 | 143 |
public: |
142 | 144 |
|
143 | 145 |
OutArcIt() { } |
144 | 146 |
|
145 | 147 |
OutArcIt(Invalid i) : Arc(i) { } |
146 | 148 |
|
147 | 149 |
OutArcIt(const Digraph& _graph, const Node& node) |
148 | 150 |
: digraph(&_graph) { |
149 | 151 |
_graph.firstOut(*this, node); |
150 | 152 |
} |
151 | 153 |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BITS_PRED_MAP_PATH_H |
20 | 20 |
#define LEMON_BITS_PRED_MAP_PATH_H |
21 | 21 |
|
22 |
#include <lemon/core.h> |
|
23 |
#include <lemon/concept_check.h> |
|
24 |
|
|
22 | 25 |
namespace lemon { |
23 | 26 |
|
24 | 27 |
template <typename _Digraph, typename _PredMap> |
25 | 28 |
class PredMapPath { |
26 | 29 |
public: |
27 | 30 |
typedef True RevPathTag; |
28 | 31 |
|
29 | 32 |
typedef _Digraph Digraph; |
30 | 33 |
typedef typename Digraph::Arc Arc; |
31 | 34 |
typedef _PredMap PredMap; |
32 | 35 |
|
33 | 36 |
PredMapPath(const Digraph& _digraph, const PredMap& _predMap, |
34 | 37 |
typename Digraph::Node _target) |
35 | 38 |
: digraph(_digraph), predMap(_predMap), target(_target) {} |
36 | 39 |
|
37 | 40 |
int length() const { |
38 | 41 |
int len = 0; |
39 | 42 |
typename Digraph::Node node = target; |
40 | 43 |
typename Digraph::Arc arc; |
41 | 44 |
while ((arc = predMap[node]) != INVALID) { |
42 | 45 |
node = digraph.source(arc); |
43 | 46 |
++len; |
44 | 47 |
} |
45 | 48 |
return len; |
46 | 49 |
} |
47 | 50 |
|
48 | 51 |
bool empty() const { |
49 | 52 |
return predMap[target] != INVALID; |
50 | 53 |
} |
51 | 54 |
|
52 | 55 |
class RevArcIt { |
53 | 56 |
public: |
54 | 57 |
RevArcIt() {} |
55 | 58 |
RevArcIt(Invalid) : path(0), current(INVALID) {} |
56 | 59 |
RevArcIt(const PredMapPath& _path) |
57 | 60 |
: path(&_path), current(_path.target) { |
58 | 61 |
if (path->predMap[current] == INVALID) current = INVALID; |
59 | 62 |
} |
60 | 63 |
|
61 | 64 |
operator const typename Digraph::Arc() const { |
62 | 65 |
return path->predMap[current]; |
63 | 66 |
} |
64 | 67 |
|
65 | 68 |
RevArcIt& operator++() { |
66 | 69 |
current = path->digraph.source(path->predMap[current]); |
67 | 70 |
if (path->predMap[current] == INVALID) current = INVALID; |
68 | 71 |
return *this; |
69 | 72 |
} |
70 | 73 |
|
71 | 74 |
bool operator==(const RevArcIt& e) const { |
72 | 75 |
return current == e.current; |
73 | 76 |
} |
74 | 77 |
|
75 | 78 |
bool operator!=(const RevArcIt& e) const { |
76 | 79 |
return current != e.current; |
77 | 80 |
} |
78 | 81 |
|
79 | 82 |
bool operator<(const RevArcIt& e) const { |
80 | 83 |
return current < e.current; |
81 | 84 |
} |
82 | 85 |
|
83 | 86 |
private: |
84 | 87 |
const PredMapPath* path; |
85 | 88 |
typename Digraph::Node current; |
86 | 89 |
}; |
87 | 90 |
|
88 | 91 |
private: |
89 | 92 |
const Digraph& digraph; |
90 | 93 |
const PredMap& predMap; |
91 | 94 |
typename Digraph::Node target; |
92 | 95 |
}; |
93 | 96 |
|
94 | 97 |
|
95 | 98 |
template <typename _Digraph, typename _PredMatrixMap> |
96 | 99 |
class PredMatrixMapPath { |
97 | 100 |
public: |
98 | 101 |
typedef True RevPathTag; |
99 | 102 |
|
100 | 103 |
typedef _Digraph Digraph; |
101 | 104 |
typedef typename Digraph::Arc Arc; |
102 | 105 |
typedef _PredMatrixMap PredMatrixMap; |
103 | 106 |
|
104 | 107 |
PredMatrixMapPath(const Digraph& _digraph, |
105 | 108 |
const PredMatrixMap& _predMatrixMap, |
106 | 109 |
typename Digraph::Node _source, |
107 | 110 |
typename Digraph::Node _target) |
108 | 111 |
: digraph(_digraph), predMatrixMap(_predMatrixMap), |
109 | 112 |
source(_source), target(_target) {} |
110 | 113 |
|
111 | 114 |
int length() const { |
112 | 115 |
int len = 0; |
113 | 116 |
typename Digraph::Node node = target; |
114 | 117 |
typename Digraph::Arc arc; |
115 | 118 |
while ((arc = predMatrixMap(source, node)) != INVALID) { |
116 | 119 |
node = digraph.source(arc); |
117 | 120 |
++len; |
118 | 121 |
} |
119 | 122 |
return len; |
120 | 123 |
} |
121 | 124 |
|
122 | 125 |
bool empty() const { |
123 | 126 |
return source != target; |
124 | 127 |
} |
125 | 128 |
|
126 | 129 |
class RevArcIt { |
127 | 130 |
public: |
128 | 131 |
RevArcIt() {} |
129 | 132 |
RevArcIt(Invalid) : path(0), current(INVALID) {} |
130 | 133 |
RevArcIt(const PredMatrixMapPath& _path) |
131 | 134 |
: path(&_path), current(_path.target) { |
132 | 135 |
if (path->predMatrixMap(path->source, current) == INVALID) |
133 | 136 |
current = INVALID; |
134 | 137 |
} |
135 | 138 |
|
136 | 139 |
operator const typename Digraph::Arc() const { |
137 | 140 |
return path->predMatrixMap(path->source, current); |
138 | 141 |
} |
139 | 142 |
|
140 | 143 |
RevArcIt& operator++() { |
141 | 144 |
current = |
142 | 145 |
path->digraph.source(path->predMatrixMap(path->source, current)); |
143 | 146 |
if (path->predMatrixMap(path->source, current) == INVALID) |
144 | 147 |
current = INVALID; |
145 | 148 |
return *this; |
146 | 149 |
} |
147 | 150 |
|
148 | 151 |
bool operator==(const RevArcIt& e) const { |
149 | 152 |
return current == e.current; |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BITS_SOLVER_BITS_H |
20 | 20 |
#define LEMON_BITS_SOLVER_BITS_H |
21 | 21 |
|
22 |
#include <vector> |
|
23 |
|
|
22 | 24 |
namespace lemon { |
23 | 25 |
|
24 | 26 |
namespace _solver_bits { |
25 | 27 |
|
26 | 28 |
class VarIndex { |
27 | 29 |
private: |
28 | 30 |
struct ItemT { |
29 | 31 |
int prev, next; |
30 | 32 |
int index; |
31 | 33 |
}; |
32 | 34 |
std::vector<ItemT> items; |
33 | 35 |
int first_item, last_item, first_free_item; |
34 | 36 |
|
35 | 37 |
std::vector<int> cross; |
36 | 38 |
|
37 | 39 |
public: |
38 | 40 |
|
39 | 41 |
VarIndex() |
40 | 42 |
: first_item(-1), last_item(-1), first_free_item(-1) { |
41 | 43 |
} |
42 | 44 |
|
43 | 45 |
void clear() { |
44 | 46 |
first_item = -1; |
45 | 47 |
first_free_item = -1; |
46 | 48 |
items.clear(); |
47 | 49 |
cross.clear(); |
48 | 50 |
} |
49 | 51 |
|
50 | 52 |
int addIndex(int idx) { |
51 | 53 |
int n; |
52 | 54 |
if (first_free_item == -1) { |
53 | 55 |
n = items.size(); |
54 | 56 |
items.push_back(ItemT()); |
55 | 57 |
} else { |
56 | 58 |
n = first_free_item; |
57 | 59 |
first_free_item = items[n].next; |
58 | 60 |
if (first_free_item != -1) { |
59 | 61 |
items[first_free_item].prev = -1; |
60 | 62 |
} |
61 | 63 |
} |
62 | 64 |
items[n].index = idx; |
63 | 65 |
if (static_cast<int>(cross.size()) <= idx) { |
64 | 66 |
cross.resize(idx + 1, -1); |
65 | 67 |
} |
66 | 68 |
cross[idx] = n; |
67 | 69 |
|
68 | 70 |
items[n].prev = last_item; |
69 | 71 |
items[n].next = -1; |
70 | 72 |
if (last_item != -1) { |
71 | 73 |
items[last_item].next = n; |
72 | 74 |
} else { |
73 | 75 |
first_item = n; |
74 | 76 |
} |
75 | 77 |
last_item = n; |
76 | 78 |
|
77 | 79 |
return n; |
78 | 80 |
} |
79 | 81 |
|
80 | 82 |
int addIndex(int idx, int n) { |
81 | 83 |
while (n >= static_cast<int>(items.size())) { |
82 | 84 |
items.push_back(ItemT()); |
83 | 85 |
items.back().prev = -1; |
84 | 86 |
items.back().next = first_free_item; |
85 | 87 |
if (first_free_item != -1) { |
86 | 88 |
items[first_free_item].prev = items.size() - 1; |
87 | 89 |
} |
88 | 90 |
first_free_item = items.size() - 1; |
89 | 91 |
} |
90 | 92 |
if (items[n].next != -1) { |
91 | 93 |
items[items[n].next].prev = items[n].prev; |
92 | 94 |
} |
93 | 95 |
if (items[n].prev != -1) { |
94 | 96 |
items[items[n].prev].next = items[n].next; |
95 | 97 |
} else { |
96 | 98 |
first_free_item = items[n].next; |
97 | 99 |
} |
98 | 100 |
|
99 | 101 |
items[n].index = idx; |
100 | 102 |
if (static_cast<int>(cross.size()) <= idx) { |
101 | 103 |
cross.resize(idx + 1, -1); |
102 | 104 |
} |
103 | 105 |
cross[idx] = n; |
104 | 106 |
|
105 | 107 |
items[n].prev = last_item; |
106 | 108 |
items[n].next = -1; |
107 | 109 |
if (last_item != -1) { |
108 | 110 |
items[last_item].next = n; |
109 | 111 |
} else { |
110 | 112 |
first_item = n; |
111 | 113 |
} |
112 | 114 |
last_item = n; |
113 | 115 |
|
114 | 116 |
return n; |
115 | 117 |
} |
116 | 118 |
|
117 | 119 |
void eraseIndex(int idx) { |
118 | 120 |
int n = cross[idx]; |
119 | 121 |
|
120 | 122 |
if (items[n].prev != -1) { |
121 | 123 |
items[items[n].prev].next = items[n].next; |
122 | 124 |
} else { |
123 | 125 |
first_item = items[n].next; |
124 | 126 |
} |
125 | 127 |
if (items[n].next != -1) { |
126 | 128 |
items[items[n].next].prev = items[n].prev; |
127 | 129 |
} else { |
128 | 130 |
last_item = items[n].prev; |
129 | 131 |
} |
130 | 132 |
|
131 | 133 |
if (first_free_item != -1) { |
132 | 134 |
items[first_free_item].prev = n; |
133 | 135 |
} |
134 | 136 |
items[n].next = first_free_item; |
135 | 137 |
items[n].prev = -1; |
136 | 138 |
first_free_item = n; |
137 | 139 |
|
138 | 140 |
while (!cross.empty() && cross.back() == -1) { |
139 | 141 |
cross.pop_back(); |
140 | 142 |
} |
141 | 143 |
} |
142 | 144 |
|
143 | 145 |
int maxIndex() const { |
144 | 146 |
return cross.size() - 1; |
145 | 147 |
} |
146 | 148 |
|
147 | 149 |
void shiftIndices(int idx) { |
148 | 150 |
for (int i = idx + 1; i < static_cast<int>(cross.size()); ++i) { |
149 | 151 |
cross[i - 1] = cross[i]; |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\ingroup concept |
20 | 20 |
///\file |
21 | 21 |
///\brief The concept of heaps. |
22 | 22 |
|
23 | 23 |
#ifndef LEMON_CONCEPT_HEAP_H |
24 | 24 |
#define LEMON_CONCEPT_HEAP_H |
25 | 25 |
|
26 | 26 |
#include <lemon/core.h> |
27 |
#include <lemon/concept_check.h> |
|
27 | 28 |
|
28 | 29 |
namespace lemon { |
29 | 30 |
|
30 | 31 |
namespace concepts { |
31 | 32 |
|
32 | 33 |
/// \addtogroup concept |
33 | 34 |
/// @{ |
34 | 35 |
|
35 | 36 |
/// \brief The heap concept. |
36 | 37 |
/// |
37 | 38 |
/// Concept class describing the main interface of heaps. |
38 | 39 |
template <typename Priority, typename ItemIntMap> |
39 | 40 |
class Heap { |
40 | 41 |
public: |
41 | 42 |
|
42 | 43 |
/// Type of the items stored in the heap. |
43 | 44 |
typedef typename ItemIntMap::Key Item; |
44 | 45 |
|
45 | 46 |
/// Type of the priorities. |
46 | 47 |
typedef Priority Prio; |
47 | 48 |
|
48 | 49 |
/// \brief Type to represent the states of the items. |
49 | 50 |
/// |
50 | 51 |
/// Each item has a state associated to it. It can be "in heap", |
51 | 52 |
/// "pre heap" or "post heap". The later two are indifferent |
52 | 53 |
/// from the point of view of the heap, but may be useful for |
53 | 54 |
/// the user. |
54 | 55 |
/// |
55 | 56 |
/// The \c ItemIntMap must be initialized in such a way, that it |
56 | 57 |
/// assigns \c PRE_HEAP (<tt>-1</tt>) to every item. |
57 | 58 |
enum State { |
58 | 59 |
IN_HEAP = 0, |
59 | 60 |
PRE_HEAP = -1, |
60 | 61 |
POST_HEAP = -2 |
61 | 62 |
}; |
62 | 63 |
|
63 | 64 |
/// \brief The constructor. |
64 | 65 |
/// |
65 | 66 |
/// The constructor. |
66 | 67 |
/// \param map A map that assigns \c int values to keys of type |
67 | 68 |
/// \c Item. It is used internally by the heap implementations to |
68 | 69 |
/// handle the cross references. The assigned value must be |
69 | 70 |
/// \c PRE_HEAP (<tt>-1</tt>) for every item. |
70 | 71 |
explicit Heap(ItemIntMap &map) {} |
71 | 72 |
|
72 | 73 |
/// \brief The number of items stored in the heap. |
73 | 74 |
/// |
74 | 75 |
/// Returns the number of items stored in the heap. |
75 | 76 |
int size() const { return 0; } |
76 | 77 |
|
77 | 78 |
/// \brief Checks if the heap is empty. |
78 | 79 |
/// |
79 | 80 |
/// Returns \c true if the heap is empty. |
80 | 81 |
bool empty() const { return false; } |
81 | 82 |
|
82 | 83 |
/// \brief Makes the heap empty. |
83 | 84 |
/// |
84 | 85 |
/// Makes the heap empty. |
85 | 86 |
void clear(); |
86 | 87 |
|
87 | 88 |
/// \brief Inserts an item into the heap with the given priority. |
88 | 89 |
/// |
89 | 90 |
/// Inserts the given item into the heap with the given priority. |
90 | 91 |
/// \param i The item to insert. |
91 | 92 |
/// \param p The priority of the item. |
92 | 93 |
void push(const Item &i, const Prio &p) {} |
93 | 94 |
|
94 | 95 |
/// \brief Returns the item having minimum priority. |
95 | 96 |
/// |
96 | 97 |
/// Returns the item having minimum priority. |
97 | 98 |
/// \pre The heap must be non-empty. |
98 | 99 |
Item top() const {} |
99 | 100 |
|
100 | 101 |
/// \brief The minimum priority. |
101 | 102 |
/// |
102 | 103 |
/// Returns the minimum priority. |
103 | 104 |
/// \pre The heap must be non-empty. |
104 | 105 |
Prio prio() const {} |
105 | 106 |
|
106 | 107 |
/// \brief Removes the item having minimum priority. |
107 | 108 |
/// |
108 | 109 |
/// Removes the item having minimum priority. |
109 | 110 |
/// \pre The heap must be non-empty. |
110 | 111 |
void pop() {} |
111 | 112 |
|
112 | 113 |
/// \brief Removes an item from the heap. |
113 | 114 |
/// |
114 | 115 |
/// Removes the given item from the heap if it is already stored. |
115 | 116 |
/// \param i The item to delete. |
116 | 117 |
void erase(const Item &i) {} |
117 | 118 |
|
118 | 119 |
/// \brief The priority of an item. |
119 | 120 |
/// |
120 | 121 |
/// Returns the priority of the given item. |
121 | 122 |
/// \pre \c i must be in the heap. |
122 | 123 |
/// \param i The item. |
123 | 124 |
Prio operator[](const Item &i) const {} |
124 | 125 |
|
125 | 126 |
/// \brief Sets the priority of an item or inserts it, if it is |
126 | 127 |
/// not stored in the heap. |
127 | 128 |
/// |
128 | 129 |
/// This method sets the priority of the given item if it is |
129 | 130 |
/// already stored in the heap. |
130 | 131 |
/// Otherwise it inserts the given item with the given priority. |
131 | 132 |
/// |
132 | 133 |
/// \param i The item. |
133 | 134 |
/// \param p The priority. |
134 | 135 |
void set(const Item &i, const Prio &p) {} |
135 | 136 |
|
136 | 137 |
/// \brief Decreases the priority of an item to the given value. |
137 | 138 |
/// |
138 | 139 |
/// Decreases the priority of an item to the given value. |
139 | 140 |
/// \pre \c i must be stored in the heap with priority at least \c p. |
140 | 141 |
/// \param i The item. |
141 | 142 |
/// \param p The priority. |
142 | 143 |
void decrease(const Item &i, const Prio &p) {} |
143 | 144 |
|
144 | 145 |
/// \brief Increases the priority of an item to the given value. |
145 | 146 |
/// |
146 | 147 |
/// Increases the priority of an item to the given value. |
147 | 148 |
/// \pre \c i must be stored in the heap with priority at most \c p. |
148 | 149 |
/// \param i The item. |
149 | 150 |
/// \param p The priority. |
150 | 151 |
void increase(const Item &i, const Prio &p) {} |
151 | 152 |
|
152 | 153 |
/// \brief Returns if an item is in, has already been in, or has |
153 | 154 |
/// never been in the heap. |
154 | 155 |
/// |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_ELEVATOR_H |
20 | 20 |
#define LEMON_ELEVATOR_H |
21 | 21 |
|
22 | 22 |
///\ingroup auxdat |
23 | 23 |
///\file |
24 | 24 |
///\brief Elevator class |
25 | 25 |
/// |
26 | 26 |
///Elevator class implements an efficient data structure |
27 | 27 |
///for labeling items in push-relabel type algorithms. |
28 | 28 |
/// |
29 | 29 |
|
30 |
#include <lemon/core.h> |
|
30 | 31 |
#include <lemon/bits/traits.h> |
31 | 32 |
|
32 | 33 |
namespace lemon { |
33 | 34 |
|
34 | 35 |
///Class for handling "labels" in push-relabel type algorithms. |
35 | 36 |
|
36 | 37 |
///A class for handling "labels" in push-relabel type algorithms. |
37 | 38 |
/// |
38 | 39 |
///\ingroup auxdat |
39 | 40 |
///Using this class you can assign "labels" (nonnegative integer numbers) |
40 | 41 |
///to the edges or nodes of a graph, manipulate and query them through |
41 | 42 |
///operations typically arising in "push-relabel" type algorithms. |
42 | 43 |
/// |
43 | 44 |
///Each item is either \em active or not, and you can also choose a |
44 | 45 |
///highest level active item. |
45 | 46 |
/// |
46 | 47 |
///\sa LinkedElevator |
47 | 48 |
/// |
48 | 49 |
///\param Graph Type of the underlying graph. |
49 | 50 |
///\param Item Type of the items the data is assigned to (Graph::Node, |
50 | 51 |
///Graph::Arc, Graph::Edge). |
51 | 52 |
template<class Graph, class Item> |
52 | 53 |
class Elevator |
53 | 54 |
{ |
54 | 55 |
public: |
55 | 56 |
|
56 | 57 |
typedef Item Key; |
57 | 58 |
typedef int Value; |
58 | 59 |
|
59 | 60 |
private: |
60 | 61 |
|
61 | 62 |
typedef Item *Vit; |
62 | 63 |
typedef typename ItemSetTraits<Graph,Item>::template Map<Vit>::Type VitMap; |
63 | 64 |
typedef typename ItemSetTraits<Graph,Item>::template Map<int>::Type IntMap; |
64 | 65 |
|
65 | 66 |
const Graph &_g; |
66 | 67 |
int _max_level; |
67 | 68 |
int _item_num; |
68 | 69 |
VitMap _where; |
69 | 70 |
IntMap _level; |
70 | 71 |
std::vector<Item> _items; |
71 | 72 |
std::vector<Vit> _first; |
72 | 73 |
std::vector<Vit> _last_active; |
73 | 74 |
|
74 | 75 |
int _highest_active; |
75 | 76 |
|
76 | 77 |
void copy(Item i, Vit p) |
77 | 78 |
{ |
78 | 79 |
_where.set(*p=i,p); |
79 | 80 |
} |
80 | 81 |
void copy(Vit s, Vit p) |
81 | 82 |
{ |
82 | 83 |
if(s!=p) |
83 | 84 |
{ |
84 | 85 |
Item i=*s; |
85 | 86 |
*p=i; |
86 | 87 |
_where.set(i,p); |
87 | 88 |
} |
88 | 89 |
} |
89 | 90 |
void swap(Vit i, Vit j) |
90 | 91 |
{ |
91 | 92 |
Item ti=*i; |
92 | 93 |
Vit ct = _where[ti]; |
93 | 94 |
_where.set(ti,_where[*i=*j]); |
94 | 95 |
_where.set(*j,ct); |
95 | 96 |
*j=ti; |
96 | 97 |
} |
97 | 98 |
|
98 | 99 |
public: |
99 | 100 |
|
100 | 101 |
///Constructor with given maximum level. |
101 | 102 |
|
102 | 103 |
///Constructor with given maximum level. |
103 | 104 |
/// |
104 | 105 |
///\param graph The underlying graph. |
105 | 106 |
///\param max_level The maximum allowed level. |
106 | 107 |
///Set the range of the possible labels to <tt>[0..max_level]</tt>. |
107 | 108 |
Elevator(const Graph &graph,int max_level) : |
108 | 109 |
_g(graph), |
109 | 110 |
_max_level(max_level), |
110 | 111 |
_item_num(_max_level), |
111 | 112 |
_where(graph), |
112 | 113 |
_level(graph,0), |
113 | 114 |
_items(_max_level), |
114 | 115 |
_first(_max_level+2), |
115 | 116 |
_last_active(_max_level+2), |
116 | 117 |
_highest_active(-1) {} |
117 | 118 |
///Constructor. |
118 | 119 |
|
119 | 120 |
///Constructor. |
120 | 121 |
/// |
121 | 122 |
///\param graph The underlying graph. |
122 | 123 |
///Set the range of the possible labels to <tt>[0..max_level]</tt>, |
123 | 124 |
///where \c max_level is equal to the number of labeled items in the graph. |
124 | 125 |
Elevator(const Graph &graph) : |
125 | 126 |
_g(graph), |
126 | 127 |
_max_level(countItems<Graph, Item>(graph)), |
127 | 128 |
_item_num(_max_level), |
128 | 129 |
_where(graph), |
129 | 130 |
_level(graph,0), |
130 | 131 |
_items(_max_level), |
131 | 132 |
_first(_max_level+2), |
132 | 133 |
_last_active(_max_level+2), |
133 | 134 |
_highest_active(-1) |
134 | 135 |
{ |
135 | 136 |
} |
136 | 137 |
|
137 | 138 |
///Activate item \c i. |
138 | 139 |
|
139 | 140 |
///Activate item \c i. |
140 | 141 |
///\pre Item \c i shouldn't be active before. |
141 | 142 |
void activate(Item i) |
142 | 143 |
{ |
143 | 144 |
const int l=_level[i]; |
144 | 145 |
swap(_where[i],++_last_active[l]); |
145 | 146 |
if(l>_highest_active) _highest_active=l; |
146 | 147 |
} |
147 | 148 |
|
148 | 149 |
///Deactivate item \c i. |
149 | 150 |
|
150 | 151 |
///Deactivate item \c i. |
151 | 152 |
///\pre Item \c i must be active before. |
152 | 153 |
void deactivate(Item i) |
153 | 154 |
{ |
154 | 155 |
swap(_where[i],_last_active[_level[i]]--); |
155 | 156 |
while(_highest_active>=0 && |
156 | 157 |
_last_active[_highest_active]<_first[_highest_active]) |
157 | 158 |
_highest_active--; |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_SUURBALLE_H |
20 | 20 |
#define LEMON_SUURBALLE_H |
21 | 21 |
|
22 | 22 |
///\ingroup shortest_path |
23 | 23 |
///\file |
24 | 24 |
///\brief An algorithm for finding arc-disjoint paths between two |
25 | 25 |
/// nodes having minimum total length. |
26 | 26 |
|
27 | 27 |
#include <vector> |
28 | 28 |
#include <lemon/bin_heap.h> |
29 | 29 |
#include <lemon/path.h> |
30 |
#include <lemon/list_graph.h> |
|
31 |
#include <lemon/maps.h> |
|
30 | 32 |
|
31 | 33 |
namespace lemon { |
32 | 34 |
|
33 | 35 |
/// \addtogroup shortest_path |
34 | 36 |
/// @{ |
35 | 37 |
|
36 | 38 |
/// \brief Algorithm for finding arc-disjoint paths between two nodes |
37 | 39 |
/// having minimum total length. |
38 | 40 |
/// |
39 | 41 |
/// \ref lemon::Suurballe "Suurballe" implements an algorithm for |
40 | 42 |
/// finding arc-disjoint paths having minimum total length (cost) |
41 | 43 |
/// from a given source node to a given target node in a digraph. |
42 | 44 |
/// |
43 | 45 |
/// In fact, this implementation is the specialization of the |
44 | 46 |
/// \ref CapacityScaling "successive shortest path" algorithm. |
45 | 47 |
/// |
46 | 48 |
/// \tparam Digraph The digraph type the algorithm runs on. |
47 | 49 |
/// The default value is \c ListDigraph. |
48 | 50 |
/// \tparam LengthMap The type of the length (cost) map. |
49 | 51 |
/// The default value is <tt>Digraph::ArcMap<int></tt>. |
50 | 52 |
/// |
51 | 53 |
/// \warning Length values should be \e non-negative \e integers. |
52 | 54 |
/// |
53 | 55 |
/// \note For finding node-disjoint paths this algorithm can be used |
54 | 56 |
/// with \ref SplitNodes. |
55 | 57 |
#ifdef DOXYGEN |
56 | 58 |
template <typename Digraph, typename LengthMap> |
57 | 59 |
#else |
58 | 60 |
template < typename Digraph = ListDigraph, |
59 | 61 |
typename LengthMap = typename Digraph::template ArcMap<int> > |
60 | 62 |
#endif |
61 | 63 |
class Suurballe |
62 | 64 |
{ |
63 | 65 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
64 | 66 |
|
65 | 67 |
typedef typename LengthMap::Value Length; |
66 | 68 |
typedef ConstMap<Arc, int> ConstArcMap; |
67 | 69 |
typedef typename Digraph::template NodeMap<Arc> PredMap; |
68 | 70 |
|
69 | 71 |
public: |
70 | 72 |
|
71 | 73 |
/// The type of the flow map. |
72 | 74 |
typedef typename Digraph::template ArcMap<int> FlowMap; |
73 | 75 |
/// The type of the potential map. |
74 | 76 |
typedef typename Digraph::template NodeMap<Length> PotentialMap; |
75 | 77 |
/// The type of the path structures. |
76 | 78 |
typedef SimplePath<Digraph> Path; |
77 | 79 |
|
78 | 80 |
private: |
79 | 81 |
|
80 | 82 |
/// \brief Special implementation of the Dijkstra algorithm |
81 | 83 |
/// for finding shortest paths in the residual network. |
82 | 84 |
/// |
83 | 85 |
/// \ref ResidualDijkstra is a special implementation of the |
84 | 86 |
/// \ref Dijkstra algorithm for finding shortest paths in the |
85 | 87 |
/// residual network of the digraph with respect to the reduced arc |
86 | 88 |
/// lengths and modifying the node potentials according to the |
87 | 89 |
/// distance of the nodes. |
88 | 90 |
class ResidualDijkstra |
89 | 91 |
{ |
90 | 92 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
91 | 93 |
typedef BinHeap<Length, HeapCrossRef> Heap; |
92 | 94 |
|
93 | 95 |
private: |
94 | 96 |
|
95 | 97 |
// The digraph the algorithm runs on |
96 | 98 |
const Digraph &_graph; |
97 | 99 |
|
98 | 100 |
// The main maps |
99 | 101 |
const FlowMap &_flow; |
100 | 102 |
const LengthMap &_length; |
101 | 103 |
PotentialMap &_potential; |
102 | 104 |
|
103 | 105 |
// The distance map |
104 | 106 |
PotentialMap _dist; |
105 | 107 |
// The pred arc map |
106 | 108 |
PredMap &_pred; |
107 | 109 |
// The processed (i.e. permanently labeled) nodes |
108 | 110 |
std::vector<Node> _proc_nodes; |
109 | 111 |
|
110 | 112 |
Node _s; |
111 | 113 |
Node _t; |
112 | 114 |
|
113 | 115 |
public: |
114 | 116 |
|
115 | 117 |
/// Constructor. |
116 | 118 |
ResidualDijkstra( const Digraph &digraph, |
117 | 119 |
const FlowMap &flow, |
118 | 120 |
const LengthMap &length, |
119 | 121 |
PotentialMap &potential, |
120 | 122 |
PredMap &pred, |
121 | 123 |
Node s, Node t ) : |
122 | 124 |
_graph(digraph), _flow(flow), _length(length), _potential(potential), |
123 | 125 |
_dist(digraph), _pred(pred), _s(s), _t(t) {} |
124 | 126 |
|
125 | 127 |
/// \brief Run the algorithm. It returns \c true if a path is found |
126 | 128 |
/// from the source node to the target node. |
127 | 129 |
bool run() { |
128 | 130 |
HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP); |
129 | 131 |
Heap heap(heap_cross_ref); |
130 | 132 |
heap.push(_s, 0); |
131 | 133 |
_pred[_s] = INVALID; |
132 | 134 |
_proc_nodes.clear(); |
133 | 135 |
|
134 | 136 |
// Process nodes |
135 | 137 |
while (!heap.empty() && heap.top() != _t) { |
136 | 138 |
Node u = heap.top(), v; |
137 | 139 |
Length d = heap.prio() + _potential[u], nd; |
138 | 140 |
_dist[u] = heap.prio(); |
139 | 141 |
heap.pop(); |
140 | 142 |
_proc_nodes.push_back(u); |
141 | 143 |
|
142 | 144 |
// Traverse outgoing arcs |
143 | 145 |
for (OutArcIt e(_graph, u); e != INVALID; ++e) { |
144 | 146 |
if (_flow[e] == 0) { |
145 | 147 |
v = _graph.target(e); |
146 | 148 |
switch(heap.state(v)) { |
147 | 149 |
case Heap::PRE_HEAP: |
148 | 150 |
heap.push(v, d + _length[e] - _potential[v]); |
149 | 151 |
_pred[v] = e; |
150 | 152 |
break; |
151 | 153 |
case Heap::IN_HEAP: |
152 | 154 |
nd = d + _length[e] - _potential[v]; |
153 | 155 |
if (nd < heap[v]) { |
154 | 156 |
heap.decrease(v, nd); |
155 | 157 |
_pred[v] = e; |
156 | 158 |
} |
157 | 159 |
break; |
0 comments (0 inline)