demo/steiner_demo.cc
author deba
Fri, 28 Sep 2007 12:14:18 +0000
changeset 2477 b5e1f017ff41
parent 2400 b199ded24c19
child 2492 387f6ff851ef
permissions -rw-r--r--
Bug fix in most significant digit calculation
kpeter@2472
     1
/* -*- C++ -*-
kpeter@2472
     2
 *
kpeter@2472
     3
 * This file is a part of LEMON, a generic C++ optimization library
kpeter@2472
     4
 *
kpeter@2472
     5
 * Copyright (C) 2003-2007
kpeter@2472
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
kpeter@2472
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
kpeter@2472
     8
 *
kpeter@2472
     9
 * Permission to use, modify and distribute this software is granted
kpeter@2472
    10
 * provided that this copyright notice appears in all copies. For
kpeter@2472
    11
 * precise terms see the accompanying LICENSE file.
kpeter@2472
    12
 *
kpeter@2472
    13
 * This software is provided "AS IS" with no warranty of any kind,
kpeter@2472
    14
 * express or implied, and with no claim as to its suitability for any
kpeter@2472
    15
 * purpose.
kpeter@2472
    16
 *
kpeter@2472
    17
 */
kpeter@2472
    18
deba@2400
    19
#include <lemon/smart_graph.h>
deba@2400
    20
#include <lemon/kruskal.h>
deba@2400
    21
#include <lemon/graph_reader.h>
deba@2400
    22
#include <lemon/time_measure.h>
deba@2400
    23
#include <lemon/graph_to_eps.h>
deba@2400
    24
deba@2400
    25
#include <lemon/steiner.h>
deba@2400
    26
deba@2400
    27
#include <cstdlib>
deba@2400
    28
#include <cmath>
deba@2400
    29
deba@2400
    30
using namespace lemon;
deba@2400
    31
using namespace lemon::dim2;
deba@2400
    32
deba@2400
    33
using namespace std;
deba@2400
    34
deba@2400
    35
int main(int argc, const char *argv[]) {
deba@2400
    36
  std::string lgf = argc > 1 ? argv[1] : "steiner.lgf";
deba@2400
    37
  std::string eps = argc > 2 ? argv[2] : "steiner.eps";
deba@2400
    38
deba@2400
    39
  SmartUGraph graph;
deba@2400
    40
  SmartUGraph::NodeMap<bool> terminal(graph);
deba@2400
    41
  SmartUGraph::NodeMap<int> label(graph);
deba@2400
    42
  SmartUGraph::NodeMap<Point<double> > coord(graph);
deba@2400
    43
  
deba@2400
    44
  UGraphReader<SmartUGraph>(lgf, graph).
deba@2400
    45
    readNodeMap("coordinates_x", xMap(coord)).
deba@2400
    46
    readNodeMap("coordinates_y", yMap(coord)).
deba@2400
    47
    readNodeMap("terminal", terminal).run();
deba@2400
    48
deba@2400
    49
  SmartUGraph::UEdgeMap<double> cost(graph);
deba@2400
    50
  for (SmartUGraph::UEdgeIt it(graph); it != INVALID; ++it) {
deba@2400
    51
    cost[it] = sqrt((coord[graph.target(it)] - 
deba@2400
    52
                     coord[graph.source(it)]).normSquare());
deba@2400
    53
  }
deba@2400
    54
  
deba@2400
    55
  SteinerTree<SmartUGraph> steiner(graph, cost);
deba@2400
    56
  steiner.init();
deba@2400
    57
deba@2400
    58
  for (SmartUGraph::NodeIt it(graph); it != INVALID; ++it) {
deba@2400
    59
    if (terminal[it]) {
deba@2400
    60
      steiner.addTerminal(it);
deba@2400
    61
    }
deba@2400
    62
  }
deba@2400
    63
  steiner.start();
deba@2400
    64
deba@2400
    65
deba@2400
    66
  Palette nodepalette(0);
deba@2400
    67
  nodepalette.add(Color(1.0, 1.0, 1.0));
deba@2400
    68
  nodepalette.add(Color(0.0, 1.0, 0.0));
deba@2400
    69
  nodepalette.add(Color(0.5, 0.5, 0.5));
deba@2400
    70
  SmartUGraph::NodeMap<int> nodecolor(graph);
deba@2400
    71
  for (SmartUGraph::NodeIt it(graph); it != INVALID; ++it) {
deba@2400
    72
    if (steiner.terminal(it)) {
deba@2400
    73
      nodecolor[it] = 1;
deba@2400
    74
    } else if (steiner.steiner(it)) {
deba@2400
    75
      nodecolor[it] = 2;
deba@2400
    76
    } else {
deba@2400
    77
      nodecolor[it] = 0;
deba@2400
    78
    }
deba@2400
    79
  }
deba@2400
    80
deba@2400
    81
deba@2400
    82
  Palette edgepalette(0);
deba@2400
    83
  edgepalette.add(Color(0.0, 0.0, 0.0));
deba@2400
    84
  edgepalette.add(Color(1.0, 0.0, 0.0));
deba@2400
    85
  SmartUGraph::UEdgeMap<int> edgecolor(graph);
deba@2400
    86
  for (SmartUGraph::UEdgeIt it(graph); it != INVALID; ++it) {
deba@2400
    87
    edgecolor[it] = steiner.tree(it) ? 1 : 0;
deba@2400
    88
  }
deba@2400
    89
deba@2400
    90
deba@2400
    91
  graphToEps(graph, eps).
deba@2400
    92
    coords(coord).undirected().
deba@2400
    93
    nodeScale(1.0).scaleToA4().
deba@2400
    94
    nodeColors(composeMap(nodepalette, nodecolor)).
deba@2400
    95
    edgeColors(composeMap(edgepalette, edgecolor)).
deba@2400
    96
    nodeTexts(label).nodeTextSize(8).run();
deba@2400
    97
deba@2400
    98
  std::cout << "The tree constructed: " << eps << std::endl;
deba@2400
    99
  std::cout << "The cost of the tree: " << steiner.treeValue() << std::endl;
deba@2400
   100
deba@2400
   101
  return 0;
deba@2400
   102
}