demo/steiner_demo.cc
author deba
Thu, 19 Apr 2007 15:14:34 +0000
changeset 2426 6e1027a05d73
child 2472 fb60f631790b
permissions -rw-r--r--
Allowing 'string' type quoting
deba@2400
     1
#include <lemon/smart_graph.h>
deba@2400
     2
#include <lemon/kruskal.h>
deba@2400
     3
#include <lemon/graph_reader.h>
deba@2400
     4
#include <lemon/time_measure.h>
deba@2400
     5
#include <lemon/graph_to_eps.h>
deba@2400
     6
deba@2400
     7
#include <lemon/steiner.h>
deba@2400
     8
deba@2400
     9
#include <cstdlib>
deba@2400
    10
#include <cmath>
deba@2400
    11
deba@2400
    12
using namespace lemon;
deba@2400
    13
using namespace lemon::dim2;
deba@2400
    14
deba@2400
    15
using namespace std;
deba@2400
    16
deba@2400
    17
int main(int argc, const char *argv[]) {
deba@2400
    18
  std::string lgf = argc > 1 ? argv[1] : "steiner.lgf";
deba@2400
    19
  std::string eps = argc > 2 ? argv[2] : "steiner.eps";
deba@2400
    20
deba@2400
    21
  SmartUGraph graph;
deba@2400
    22
  SmartUGraph::NodeMap<bool> terminal(graph);
deba@2400
    23
  SmartUGraph::NodeMap<int> label(graph);
deba@2400
    24
  SmartUGraph::NodeMap<Point<double> > coord(graph);
deba@2400
    25
  
deba@2400
    26
  UGraphReader<SmartUGraph>(lgf, graph).
deba@2400
    27
    readNodeMap("coordinates_x", xMap(coord)).
deba@2400
    28
    readNodeMap("coordinates_y", yMap(coord)).
deba@2400
    29
    readNodeMap("terminal", terminal).run();
deba@2400
    30
deba@2400
    31
  SmartUGraph::UEdgeMap<double> cost(graph);
deba@2400
    32
  for (SmartUGraph::UEdgeIt it(graph); it != INVALID; ++it) {
deba@2400
    33
    cost[it] = sqrt((coord[graph.target(it)] - 
deba@2400
    34
                     coord[graph.source(it)]).normSquare());
deba@2400
    35
  }
deba@2400
    36
  
deba@2400
    37
  SteinerTree<SmartUGraph> steiner(graph, cost);
deba@2400
    38
  steiner.init();
deba@2400
    39
deba@2400
    40
  for (SmartUGraph::NodeIt it(graph); it != INVALID; ++it) {
deba@2400
    41
    if (terminal[it]) {
deba@2400
    42
      steiner.addTerminal(it);
deba@2400
    43
    }
deba@2400
    44
  }
deba@2400
    45
  steiner.start();
deba@2400
    46
deba@2400
    47
deba@2400
    48
  Palette nodepalette(0);
deba@2400
    49
  nodepalette.add(Color(1.0, 1.0, 1.0));
deba@2400
    50
  nodepalette.add(Color(0.0, 1.0, 0.0));
deba@2400
    51
  nodepalette.add(Color(0.5, 0.5, 0.5));
deba@2400
    52
  SmartUGraph::NodeMap<int> nodecolor(graph);
deba@2400
    53
  for (SmartUGraph::NodeIt it(graph); it != INVALID; ++it) {
deba@2400
    54
    if (steiner.terminal(it)) {
deba@2400
    55
      nodecolor[it] = 1;
deba@2400
    56
    } else if (steiner.steiner(it)) {
deba@2400
    57
      nodecolor[it] = 2;
deba@2400
    58
    } else {
deba@2400
    59
      nodecolor[it] = 0;
deba@2400
    60
    }
deba@2400
    61
  }
deba@2400
    62
deba@2400
    63
deba@2400
    64
  Palette edgepalette(0);
deba@2400
    65
  edgepalette.add(Color(0.0, 0.0, 0.0));
deba@2400
    66
  edgepalette.add(Color(1.0, 0.0, 0.0));
deba@2400
    67
  SmartUGraph::UEdgeMap<int> edgecolor(graph);
deba@2400
    68
  for (SmartUGraph::UEdgeIt it(graph); it != INVALID; ++it) {
deba@2400
    69
    edgecolor[it] = steiner.tree(it) ? 1 : 0;
deba@2400
    70
  }
deba@2400
    71
deba@2400
    72
deba@2400
    73
  graphToEps(graph, eps).
deba@2400
    74
    coords(coord).undirected().
deba@2400
    75
    nodeScale(1.0).scaleToA4().
deba@2400
    76
    nodeColors(composeMap(nodepalette, nodecolor)).
deba@2400
    77
    edgeColors(composeMap(edgepalette, edgecolor)).
deba@2400
    78
    nodeTexts(label).nodeTextSize(8).run();
deba@2400
    79
deba@2400
    80
  std::cout << "The tree constructed: " << eps << std::endl;
deba@2400
    81
  std::cout << "The cost of the tree: " << steiner.treeValue() << std::endl;
deba@2400
    82
deba@2400
    83
  return 0;
deba@2400
    84
}