3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
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;
131 /// \brief Constructor
135 SteinerTree(const UGraph &graph, const CostMap &cost)
136 : _graph(graph), _cost(cost), _dijkstra(graph, _cost),
137 _pred(0), _comp(0), _forked(0), _filter(0), _tree(0) {}
139 /// \brief Initializes the internal data structures.
141 /// Initializes the internal data structures.
143 if (!_pred) _pred = new PredMap(_graph);
144 if (!_comp) _comp = new CompMap(_graph);
145 if (!_forked) _forked = new ForkedMap(*_pred, *_comp);
146 if (!_filter) _filter = new FilterMap(_graph);
147 if (!_tree) _tree = new TreeMap(_graph);
148 _dijkstra.predMap(*_forked);
151 for (NodeIt it(_graph); it != INVALID; ++it) {
152 _filter->set(it, false);
156 /// \brief Adds a new terminal node.
158 /// Adds a new terminal node to the Steiner-tree problem.
159 void addTerminal(const Node& node) {
160 if (!_dijkstra.reached(node)) {
161 _dijkstra.addSource(node);
162 _comp->comp(node, _terminal_num);
167 /// \brief Executes the algorithm.
169 /// Executes the algorithm.
171 /// \pre init() must be called and at least some nodes should be
172 /// added with addTerminal() before using this function.
174 /// This method constructs an approximation of the Steiner-Tree.
178 std::vector<External> externals;
179 for (UEdgeIt it(_graph); it != INVALID; ++it) {
180 Node s = _graph.source(it);
181 Node t = _graph.target(it);
182 if (_comp->comp(s) == _comp->comp(t)) continue;
184 Value cost = _dijkstra.dist(s) + _dijkstra.dist(t) + _cost[it];
186 if (_comp->comp(s) < _comp->comp(t)) {
187 externals.push_back(External(_comp->comp(s), _comp->comp(t),
190 externals.push_back(External(_comp->comp(t), _comp->comp(s),
194 std::sort(externals.begin(), externals.end(), ExternalLess());
196 SmartUGraph aux_graph;
197 std::vector<SmartUGraph::Node> aux_nodes;
199 for (int i = 0; i < _terminal_num; ++i) {
200 aux_nodes.push_back(aux_graph.addNode());
203 SmartUGraph::UEdgeMap<Value> aux_cost(aux_graph);
204 SmartUGraph::UEdgeMap<UEdge> cross(aux_graph);
207 while (i < (int)externals.size()) {
208 int sn = externals[i].source;
209 int tn = externals[i].target;
210 Value ev = externals[i].value;
211 UEdge ee = externals[i].uedge;
213 while (i < (int)externals.size() &&
214 sn == externals[i].source && tn == externals[i].target) {
215 if (externals[i].value < ev) {
216 ev = externals[i].value;
217 ee = externals[i].uedge;
221 SmartUGraph::UEdge ne =
222 aux_graph.addEdge(aux_nodes[sn], aux_nodes[tn]);
223 aux_cost.set(ne, ev);
228 std::vector<SmartUGraph::UEdge> aux_tree_edges;
229 BackInserterBoolMap<std::vector<SmartUGraph::UEdge> >
230 aux_tree_map(aux_tree_edges);
231 prim(aux_graph, aux_cost, aux_tree_map);
233 for (std::vector<SmartUGraph::UEdge>::iterator
234 it = aux_tree_edges.begin(); it != aux_tree_edges.end(); ++it) {
236 node = _graph.source(cross[*it]);
237 while (node != INVALID && !(*_filter)[node]) {
238 _filter->set(node, true);
239 node = (*_pred)[node] != INVALID ?
240 _graph.source((*_pred)[node]) : INVALID;
242 node = _graph.target(cross[*it]);
243 while (node != INVALID && !(*_filter)[node]) {
244 _filter->set(node, true);
245 node = (*_pred)[node] != INVALID ?
246 _graph.source((*_pred)[node]) : INVALID;
250 prim(nodeSubUGraphAdaptor(_graph, *_filter), _cost, *_tree);
254 /// \brief Checks if an edge is in the Steiner-tree or not.
256 /// Checks if an edge is in the Steiner-tree or not.
257 /// \param e is the edge that will be checked
258 /// \return \c true if e is in the Steiner-tree, \c false otherwise
263 /// \brief Checks if the node is in the Steiner-tree or not.
265 /// Checks if a node is in the Steiner-tree or not.
266 /// \param n is the node that will be checked
267 /// \return \c true if n is in the Steiner-tree, \c false otherwise
269 return (*_filter)[n];
275 } //END OF NAMESPACE LEMON