lemon/steiner.h
author hegyi
Tue, 08 Apr 2008 11:39:40 +0000
changeset 2602 1c7790d9e025
parent 2510 bb523a4758f7
permissions -rw-r--r--
Rel.07 NEWS - 3. round
deba@2382
     1
/* -*- C++ -*-
deba@2382
     2
 *
deba@2382
     3
 * This file is a part of LEMON, a generic C++ optimization library
deba@2382
     4
 *
alpar@2553
     5
 * Copyright (C) 2003-2008
deba@2382
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@2382
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@2382
     8
 *
deba@2382
     9
 * Permission to use, modify and distribute this software is granted
deba@2382
    10
 * provided that this copyright notice appears in all copies. For
deba@2382
    11
 * precise terms see the accompanying LICENSE file.
deba@2382
    12
 *
deba@2382
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@2382
    14
 * express or implied, and with no claim as to its suitability for any
deba@2382
    15
 * purpose.
deba@2382
    16
 *
deba@2382
    17
 */
deba@2382
    18
deba@2382
    19
#ifndef LEMON_STEINER_H
deba@2382
    20
#define LEMON_STEINER_H
deba@2382
    21
deba@2382
    22
///\ingroup approx
deba@2382
    23
///\file
deba@2382
    24
///\brief Algorithm for the 2-approximation of Steiner Tree problem.
deba@2382
    25
///
deba@2382
    26
deba@2382
    27
#include <lemon/smart_graph.h>
deba@2382
    28
#include <lemon/graph_utils.h>
deba@2382
    29
#include <lemon/error.h>
deba@2382
    30
deba@2382
    31
#include <lemon/ugraph_adaptor.h>
deba@2382
    32
#include <lemon/maps.h>
deba@2382
    33
deba@2382
    34
#include <lemon/dijkstra.h>
deba@2382
    35
#include <lemon/prim.h>
deba@2382
    36
deba@2382
    37
deba@2382
    38
namespace lemon {
deba@2382
    39
deba@2382
    40
  /// \ingroup approx
deba@2382
    41
  
deba@2382
    42
  /// \brief Algorithm for the 2-approximation of Steiner Tree problem
deba@2382
    43
  ///
deba@2382
    44
  /// The Steiner-tree problem is the next: Given a connected
deba@2382
    45
  /// undirected graph, a cost function on the edges and a subset of
deba@2382
    46
  /// the nodes. Construct a tree with minimum cost which covers the
deba@2382
    47
  /// given subset of the nodes. The problem is NP-hard moreover
deba@2382
    48
  /// it is APX-complete too.
deba@2382
    49
  ///
deba@2382
    50
  /// Mehlhorn's approximation algorithm is implemented in this class,
deba@2382
    51
  /// which gives a 2-approximation for the Steiner-tree problem. The
deba@2382
    52
  /// algorithm's time complexity is O(nlog(n)+e).
deba@2382
    53
  template <typename UGraph,
deba@2382
    54
            typename CostMap = typename UGraph:: template UEdgeMap<double> >
deba@2382
    55
  class SteinerTree {
deba@2382
    56
  public:
deba@2382
    57
    
deba@2510
    58
    UGRAPH_TYPEDEFS(typename UGraph);
deba@2382
    59
deba@2382
    60
    typedef typename CostMap::Value Value;
deba@2382
    61
    
deba@2382
    62
  private:
deba@2382
    63
deba@2382
    64
    class CompMap {
deba@2382
    65
    public:
deba@2382
    66
      typedef Node Key;
deba@2382
    67
      typedef Edge Value;
deba@2382
    68
deba@2382
    69
    private:
deba@2382
    70
      const UGraph& _graph;
deba@2382
    71
      typename UGraph::template NodeMap<int> _comp;
deba@2382
    72
deba@2382
    73
    public:
deba@2382
    74
      CompMap(const UGraph& graph) : _graph(graph), _comp(graph) {}
deba@2382
    75
deba@2382
    76
      void set(const Node& node, const Edge& edge) {
deba@2382
    77
        if (edge != INVALID) {
deba@2382
    78
          _comp.set(node, _comp[_graph.source(edge)]);
deba@2382
    79
        } else {
deba@2382
    80
          _comp.set(node, -1);
deba@2382
    81
        }
deba@2382
    82
      }
deba@2382
    83
deba@2382
    84
      int comp(const Node& node) const { return _comp[node]; }
deba@2382
    85
      void comp(const Node& node, int value) { _comp.set(node, value); }
deba@2382
    86
    };
deba@2382
    87
deba@2382
    88
    typedef typename UGraph::template NodeMap<Edge> PredMap;
deba@2382
    89
deba@2382
    90
    typedef ForkWriteMap<PredMap, CompMap> ForkedMap;
deba@2382
    91
deba@2382
    92
deba@2382
    93
    struct External {
deba@2382
    94
      int source, target;
deba@2382
    95
      UEdge uedge;
deba@2382
    96
      Value value;
deba@2382
    97
deba@2382
    98
      External(int s, int t, const UEdge& e, const Value& v)
deba@2382
    99
        : source(s), target(t), uedge(e), value(v) {}
deba@2382
   100
    };
deba@2382
   101
deba@2382
   102
    struct ExternalLess {
deba@2382
   103
      bool operator()(const External& left, const External& right) const {
deba@2382
   104
        return (left.source < right.source) || 
deba@2382
   105
          (left.source == right.source && left.target < right.target);
deba@2382
   106
      }
deba@2382
   107
    };
deba@2382
   108
deba@2382
   109
deba@2382
   110
    typedef typename UGraph::template NodeMap<bool> FilterMap;
deba@2382
   111
deba@2382
   112
    typedef typename UGraph::template UEdgeMap<bool> TreeMap;
deba@2382
   113
deba@2382
   114
    const UGraph& _graph;
deba@2382
   115
    const CostMap& _cost;
deba@2382
   116
deba@2382
   117
    typename Dijkstra<UGraph, CostMap>::
deba@2382
   118
    template DefPredMap<ForkedMap>::Create _dijkstra;
deba@2382
   119
deba@2382
   120
    PredMap* _pred;
deba@2382
   121
    CompMap* _comp;
deba@2382
   122
    ForkedMap* _forked;
deba@2382
   123
deba@2382
   124
    int _terminal_num;
deba@2382
   125
deba@2382
   126
    FilterMap *_filter;
deba@2382
   127
    TreeMap *_tree;
deba@2382
   128
deba@2399
   129
    Value _value;
deba@2399
   130
deba@2382
   131
  public:
deba@2382
   132
deba@2382
   133
    /// \brief Constructor
deba@2382
   134
    
deba@2382
   135
    /// Constructor
deba@2382
   136
    ///
deba@2382
   137
    SteinerTree(const UGraph &graph, const CostMap &cost)
deba@2382
   138
      : _graph(graph), _cost(cost), _dijkstra(graph, _cost), 
deba@2382
   139
        _pred(0), _comp(0), _forked(0), _filter(0), _tree(0) {}
deba@2382
   140
deba@2382
   141
    /// \brief Initializes the internal data structures.
deba@2382
   142
    ///
deba@2382
   143
    /// Initializes the internal data structures.
deba@2382
   144
    void init() {
deba@2382
   145
      if (!_pred) _pred = new PredMap(_graph);
deba@2382
   146
      if (!_comp) _comp = new CompMap(_graph);
deba@2382
   147
      if (!_forked) _forked = new ForkedMap(*_pred, *_comp);
deba@2382
   148
      if (!_filter) _filter = new FilterMap(_graph);
deba@2382
   149
      if (!_tree) _tree = new TreeMap(_graph);
deba@2382
   150
      _dijkstra.predMap(*_forked);
deba@2382
   151
      _dijkstra.init();
deba@2382
   152
      _terminal_num = 0;
deba@2382
   153
      for (NodeIt it(_graph); it != INVALID; ++it) {
deba@2382
   154
        _filter->set(it, false);
deba@2382
   155
      }
deba@2382
   156
    }
deba@2382
   157
deba@2382
   158
    /// \brief Adds a new terminal node.
deba@2382
   159
    ///
deba@2382
   160
    /// Adds a new terminal node to the Steiner-tree problem.
deba@2382
   161
    void addTerminal(const Node& node) {
deba@2382
   162
      if (!_dijkstra.reached(node)) {
deba@2382
   163
        _dijkstra.addSource(node);
deba@2382
   164
        _comp->comp(node, _terminal_num);
deba@2382
   165
        ++_terminal_num;
deba@2382
   166
      }
deba@2382
   167
    }
deba@2382
   168
    
deba@2382
   169
    /// \brief Executes the algorithm.
deba@2382
   170
    ///
deba@2382
   171
    /// Executes the algorithm.
deba@2382
   172
    ///
deba@2382
   173
    /// \pre init() must be called and at least some nodes should be
deba@2382
   174
    /// added with addTerminal() before using this function.
deba@2382
   175
    ///
deba@2382
   176
    /// This method constructs an approximation of the Steiner-Tree.
deba@2382
   177
    void start() {
deba@2382
   178
      _dijkstra.start();
deba@2382
   179
      
deba@2382
   180
      std::vector<External> externals;
deba@2382
   181
      for (UEdgeIt it(_graph); it != INVALID; ++it) {
deba@2382
   182
        Node s = _graph.source(it);
deba@2382
   183
        Node t = _graph.target(it);
deba@2382
   184
        if (_comp->comp(s) == _comp->comp(t)) continue;
deba@2382
   185
deba@2382
   186
        Value cost = _dijkstra.dist(s) + _dijkstra.dist(t) + _cost[it];
deba@2382
   187
deba@2382
   188
        if (_comp->comp(s) < _comp->comp(t)) {
deba@2382
   189
          externals.push_back(External(_comp->comp(s), _comp->comp(t), 
deba@2382
   190
                                       it, cost));
deba@2382
   191
        } else {
deba@2382
   192
          externals.push_back(External(_comp->comp(t), _comp->comp(s), 
deba@2382
   193
                                       it, cost));
deba@2382
   194
        }
deba@2382
   195
      }
deba@2382
   196
      std::sort(externals.begin(), externals.end(), ExternalLess());
deba@2382
   197
deba@2382
   198
      SmartUGraph aux_graph;
deba@2382
   199
      std::vector<SmartUGraph::Node> aux_nodes;
deba@2382
   200
deba@2382
   201
      for (int i = 0; i < _terminal_num; ++i) {
deba@2382
   202
        aux_nodes.push_back(aux_graph.addNode());
deba@2382
   203
      }
deba@2382
   204
deba@2382
   205
      SmartUGraph::UEdgeMap<Value> aux_cost(aux_graph);
deba@2382
   206
      SmartUGraph::UEdgeMap<UEdge> cross(aux_graph);
deba@2382
   207
      {
deba@2382
   208
        int i = 0;
deba@2386
   209
        while (i < int(externals.size())) {
deba@2382
   210
          int sn = externals[i].source;
deba@2382
   211
          int tn = externals[i].target;
deba@2382
   212
          Value ev = externals[i].value;
deba@2382
   213
          UEdge ee = externals[i].uedge;
deba@2382
   214
          ++i;
deba@2386
   215
          while (i < int(externals.size()) && 
deba@2382
   216
                 sn == externals[i].source && tn == externals[i].target) {
deba@2382
   217
            if (externals[i].value < ev) {
deba@2382
   218
              ev = externals[i].value;
deba@2382
   219
              ee = externals[i].uedge;
deba@2382
   220
            }
deba@2382
   221
            ++i;
deba@2382
   222
          }
deba@2382
   223
          SmartUGraph::UEdge ne = 
deba@2382
   224
            aux_graph.addEdge(aux_nodes[sn], aux_nodes[tn]);
deba@2382
   225
          aux_cost.set(ne, ev);
deba@2382
   226
          cross.set(ne, ee);
deba@2382
   227
        }
deba@2382
   228
      }
deba@2382
   229
deba@2382
   230
      std::vector<SmartUGraph::UEdge> aux_tree_edges;
deba@2382
   231
      BackInserterBoolMap<std::vector<SmartUGraph::UEdge> >
deba@2382
   232
        aux_tree_map(aux_tree_edges);
deba@2382
   233
      prim(aux_graph, aux_cost, aux_tree_map);
deba@2382
   234
deba@2382
   235
      for (std::vector<SmartUGraph::UEdge>::iterator 
deba@2382
   236
             it = aux_tree_edges.begin(); it != aux_tree_edges.end(); ++it) {
deba@2382
   237
        Node node;
deba@2382
   238
        node = _graph.source(cross[*it]);
deba@2382
   239
        while (node != INVALID && !(*_filter)[node]) {
deba@2382
   240
          _filter->set(node, true);
deba@2382
   241
          node = (*_pred)[node] != INVALID ? 
deba@2382
   242
            _graph.source((*_pred)[node]) : INVALID;
deba@2382
   243
        }
deba@2382
   244
        node = _graph.target(cross[*it]);
deba@2382
   245
        while (node != INVALID && !(*_filter)[node]) {
deba@2382
   246
          _filter->set(node, true);
deba@2382
   247
          node = (*_pred)[node] != INVALID ? 
deba@2382
   248
            _graph.source((*_pred)[node]) : INVALID;
deba@2382
   249
        }
deba@2382
   250
      }
deba@2382
   251
deba@2399
   252
      _value = prim(nodeSubUGraphAdaptor(_graph, *_filter), _cost, *_tree);
deba@2382
   253
            
deba@2382
   254
    }
deba@2382
   255
deba@2382
   256
    /// \brief Checks if an edge is in the Steiner-tree or not.
deba@2382
   257
    ///
deba@2382
   258
    /// Checks if an edge is in the Steiner-tree or not.
deba@2382
   259
    /// \param e is the edge that will be checked
deba@2382
   260
    /// \return \c true if e is in the Steiner-tree, \c false otherwise
deba@2382
   261
    bool tree(UEdge e){
deba@2382
   262
      return (*_tree)[e];
deba@2382
   263
    }
deba@2382
   264
deba@2382
   265
    /// \brief Checks if the node is in the Steiner-tree or not.
deba@2382
   266
    ///
deba@2382
   267
    /// Checks if a node is in the Steiner-tree or not.
deba@2382
   268
    /// \param n is the node that will be checked
deba@2382
   269
    /// \return \c true if n is in the Steiner-tree, \c false otherwise
deba@2382
   270
    bool tree(Node n){
deba@2382
   271
      return (*_filter)[n];
deba@2382
   272
    }
deba@2399
   273
deba@2399
   274
    /// \brief Checks if the node is a Steiner-node.
deba@2399
   275
    ///
deba@2399
   276
    /// Checks if the node is a Steiner-node (i.e. a tree node but
deba@2399
   277
    /// not terminal).
deba@2399
   278
    /// \param n is the node that will be checked
deba@2399
   279
    /// \return \c true if n is a Steiner-node, \c false otherwise
deba@2399
   280
    bool steiner(Node n){
deba@2399
   281
      return (*_filter)[n] && (*_pred)[n] != INVALID;
deba@2399
   282
    }
deba@2399
   283
deba@2399
   284
    /// \brief Checks if the node is a terminal.
deba@2399
   285
    ///
deba@2399
   286
    /// Checks if the node is a terminal.
deba@2399
   287
    /// \param n is the node that will be checked
deba@2399
   288
    /// \return \c true if n is a terminal, \c false otherwise
deba@2399
   289
    bool terminal(Node n){
deba@2399
   290
      return _dijkstra.reached(n) && (*_pred)[n] == INVALID;
deba@2399
   291
    }
deba@2382
   292
    
deba@2399
   293
    /// \brief The total cost of the tree
deba@2399
   294
    ///
deba@2399
   295
    /// The total cost of the constructed tree. The calculated value does
deba@2399
   296
    /// not exceed the double of the optimal value.
deba@2399
   297
    Value treeValue() const {
deba@2399
   298
      return _value;
deba@2399
   299
    }
deba@2399
   300
    
deba@2382
   301
  };
deba@2382
   302
deba@2382
   303
} //END OF NAMESPACE LEMON
deba@2382
   304
deba@2382
   305
#endif