Various improvements in NetworkSimplex.
- Faster variant of "Altering Candidate List" pivot rule using make_heap
instead of partial_sort.
- Doc improvements.
- Removing unecessary inline keywords.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2008
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_STEINER_H
20 #define LEMON_STEINER_H
24 ///\brief Algorithm for the 2-approximation of Steiner Tree problem.
27 #include <lemon/smart_graph.h>
28 #include <lemon/graph_utils.h>
29 #include <lemon/error.h>
31 #include <lemon/ugraph_adaptor.h>
32 #include <lemon/maps.h>
34 #include <lemon/dijkstra.h>
35 #include <lemon/prim.h>
42 /// \brief Algorithm for the 2-approximation of Steiner Tree problem
44 /// The Steiner-tree problem is the next: Given a connected
45 /// undirected graph, a cost function on the edges and a subset of
46 /// the nodes. Construct a tree with minimum cost which covers the
47 /// given subset of the nodes. The problem is NP-hard moreover
48 /// it is APX-complete too.
50 /// Mehlhorn's approximation algorithm is implemented in this class,
51 /// which gives a 2-approximation for the Steiner-tree problem. The
52 /// algorithm's time complexity is O(nlog(n)+e).
53 template <typename UGraph,
54 typename CostMap = typename UGraph:: template UEdgeMap<double> >
58 UGRAPH_TYPEDEFS(typename UGraph);
60 typedef typename CostMap::Value Value;
71 typename UGraph::template NodeMap<int> _comp;
74 CompMap(const UGraph& graph) : _graph(graph), _comp(graph) {}
76 void set(const Node& node, const Edge& edge) {
77 if (edge != INVALID) {
78 _comp.set(node, _comp[_graph.source(edge)]);
84 int comp(const Node& node) const { return _comp[node]; }
85 void comp(const Node& node, int value) { _comp.set(node, value); }
88 typedef typename UGraph::template NodeMap<Edge> PredMap;
90 typedef ForkWriteMap<PredMap, CompMap> ForkedMap;
98 External(int s, int t, const UEdge& e, const Value& v)
99 : source(s), target(t), uedge(e), value(v) {}
102 struct ExternalLess {
103 bool operator()(const External& left, const External& right) const {
104 return (left.source < right.source) ||
105 (left.source == right.source && left.target < right.target);
110 typedef typename UGraph::template NodeMap<bool> FilterMap;
112 typedef typename UGraph::template UEdgeMap<bool> TreeMap;
114 const UGraph& _graph;
115 const CostMap& _cost;
117 typename Dijkstra<UGraph, CostMap>::
118 template DefPredMap<ForkedMap>::Create _dijkstra;
133 /// \brief Constructor
137 SteinerTree(const UGraph &graph, const CostMap &cost)
138 : _graph(graph), _cost(cost), _dijkstra(graph, _cost),
139 _pred(0), _comp(0), _forked(0), _filter(0), _tree(0) {}
141 /// \brief Initializes the internal data structures.
143 /// Initializes the internal data structures.
145 if (!_pred) _pred = new PredMap(_graph);
146 if (!_comp) _comp = new CompMap(_graph);
147 if (!_forked) _forked = new ForkedMap(*_pred, *_comp);
148 if (!_filter) _filter = new FilterMap(_graph);
149 if (!_tree) _tree = new TreeMap(_graph);
150 _dijkstra.predMap(*_forked);
153 for (NodeIt it(_graph); it != INVALID; ++it) {
154 _filter->set(it, false);
158 /// \brief Adds a new terminal node.
160 /// Adds a new terminal node to the Steiner-tree problem.
161 void addTerminal(const Node& node) {
162 if (!_dijkstra.reached(node)) {
163 _dijkstra.addSource(node);
164 _comp->comp(node, _terminal_num);
169 /// \brief Executes the algorithm.
171 /// Executes the algorithm.
173 /// \pre init() must be called and at least some nodes should be
174 /// added with addTerminal() before using this function.
176 /// This method constructs an approximation of the Steiner-Tree.
180 std::vector<External> externals;
181 for (UEdgeIt it(_graph); it != INVALID; ++it) {
182 Node s = _graph.source(it);
183 Node t = _graph.target(it);
184 if (_comp->comp(s) == _comp->comp(t)) continue;
186 Value cost = _dijkstra.dist(s) + _dijkstra.dist(t) + _cost[it];
188 if (_comp->comp(s) < _comp->comp(t)) {
189 externals.push_back(External(_comp->comp(s), _comp->comp(t),
192 externals.push_back(External(_comp->comp(t), _comp->comp(s),
196 std::sort(externals.begin(), externals.end(), ExternalLess());
198 SmartUGraph aux_graph;
199 std::vector<SmartUGraph::Node> aux_nodes;
201 for (int i = 0; i < _terminal_num; ++i) {
202 aux_nodes.push_back(aux_graph.addNode());
205 SmartUGraph::UEdgeMap<Value> aux_cost(aux_graph);
206 SmartUGraph::UEdgeMap<UEdge> cross(aux_graph);
209 while (i < int(externals.size())) {
210 int sn = externals[i].source;
211 int tn = externals[i].target;
212 Value ev = externals[i].value;
213 UEdge ee = externals[i].uedge;
215 while (i < int(externals.size()) &&
216 sn == externals[i].source && tn == externals[i].target) {
217 if (externals[i].value < ev) {
218 ev = externals[i].value;
219 ee = externals[i].uedge;
223 SmartUGraph::UEdge ne =
224 aux_graph.addEdge(aux_nodes[sn], aux_nodes[tn]);
225 aux_cost.set(ne, ev);
230 std::vector<SmartUGraph::UEdge> aux_tree_edges;
231 BackInserterBoolMap<std::vector<SmartUGraph::UEdge> >
232 aux_tree_map(aux_tree_edges);
233 prim(aux_graph, aux_cost, aux_tree_map);
235 for (std::vector<SmartUGraph::UEdge>::iterator
236 it = aux_tree_edges.begin(); it != aux_tree_edges.end(); ++it) {
238 node = _graph.source(cross[*it]);
239 while (node != INVALID && !(*_filter)[node]) {
240 _filter->set(node, true);
241 node = (*_pred)[node] != INVALID ?
242 _graph.source((*_pred)[node]) : INVALID;
244 node = _graph.target(cross[*it]);
245 while (node != INVALID && !(*_filter)[node]) {
246 _filter->set(node, true);
247 node = (*_pred)[node] != INVALID ?
248 _graph.source((*_pred)[node]) : INVALID;
252 _value = prim(nodeSubUGraphAdaptor(_graph, *_filter), _cost, *_tree);
256 /// \brief Checks if an edge is in the Steiner-tree or not.
258 /// Checks if an edge is in the Steiner-tree or not.
259 /// \param e is the edge that will be checked
260 /// \return \c true if e is in the Steiner-tree, \c false otherwise
265 /// \brief Checks if the node is in the Steiner-tree or not.
267 /// Checks if a node is in the Steiner-tree or not.
268 /// \param n is the node that will be checked
269 /// \return \c true if n is in the Steiner-tree, \c false otherwise
271 return (*_filter)[n];
274 /// \brief Checks if the node is a Steiner-node.
276 /// Checks if the node is a Steiner-node (i.e. a tree node but
278 /// \param n is the node that will be checked
279 /// \return \c true if n is a Steiner-node, \c false otherwise
280 bool steiner(Node n){
281 return (*_filter)[n] && (*_pred)[n] != INVALID;
284 /// \brief Checks if the node is a terminal.
286 /// Checks if the node is a terminal.
287 /// \param n is the node that will be checked
288 /// \return \c true if n is a terminal, \c false otherwise
289 bool terminal(Node n){
290 return _dijkstra.reached(n) && (*_pred)[n] == INVALID;
293 /// \brief The total cost of the tree
295 /// The total cost of the constructed tree. The calculated value does
296 /// not exceed the double of the optimal value.
297 Value treeValue() const {
303 } //END OF NAMESPACE LEMON