demo/steiner_demo.cc
changeset 2400 b199ded24c19
child 2472 fb60f631790b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/demo/steiner_demo.cc	Wed Mar 07 12:00:59 2007 +0000
     1.3 @@ -0,0 +1,84 @@
     1.4 +#include <lemon/smart_graph.h>
     1.5 +#include <lemon/kruskal.h>
     1.6 +#include <lemon/graph_reader.h>
     1.7 +#include <lemon/time_measure.h>
     1.8 +#include <lemon/graph_to_eps.h>
     1.9 +
    1.10 +#include <lemon/steiner.h>
    1.11 +
    1.12 +#include <cstdlib>
    1.13 +#include <cmath>
    1.14 +
    1.15 +using namespace lemon;
    1.16 +using namespace lemon::dim2;
    1.17 +
    1.18 +using namespace std;
    1.19 +
    1.20 +int main(int argc, const char *argv[]) {
    1.21 +  std::string lgf = argc > 1 ? argv[1] : "steiner.lgf";
    1.22 +  std::string eps = argc > 2 ? argv[2] : "steiner.eps";
    1.23 +
    1.24 +  SmartUGraph graph;
    1.25 +  SmartUGraph::NodeMap<bool> terminal(graph);
    1.26 +  SmartUGraph::NodeMap<int> label(graph);
    1.27 +  SmartUGraph::NodeMap<Point<double> > coord(graph);
    1.28 +  
    1.29 +  UGraphReader<SmartUGraph>(lgf, graph).
    1.30 +    readNodeMap("coordinates_x", xMap(coord)).
    1.31 +    readNodeMap("coordinates_y", yMap(coord)).
    1.32 +    readNodeMap("terminal", terminal).run();
    1.33 +
    1.34 +  SmartUGraph::UEdgeMap<double> cost(graph);
    1.35 +  for (SmartUGraph::UEdgeIt it(graph); it != INVALID; ++it) {
    1.36 +    cost[it] = sqrt((coord[graph.target(it)] - 
    1.37 +                     coord[graph.source(it)]).normSquare());
    1.38 +  }
    1.39 +  
    1.40 +  SteinerTree<SmartUGraph> steiner(graph, cost);
    1.41 +  steiner.init();
    1.42 +
    1.43 +  for (SmartUGraph::NodeIt it(graph); it != INVALID; ++it) {
    1.44 +    if (terminal[it]) {
    1.45 +      steiner.addTerminal(it);
    1.46 +    }
    1.47 +  }
    1.48 +  steiner.start();
    1.49 +
    1.50 +
    1.51 +  Palette nodepalette(0);
    1.52 +  nodepalette.add(Color(1.0, 1.0, 1.0));
    1.53 +  nodepalette.add(Color(0.0, 1.0, 0.0));
    1.54 +  nodepalette.add(Color(0.5, 0.5, 0.5));
    1.55 +  SmartUGraph::NodeMap<int> nodecolor(graph);
    1.56 +  for (SmartUGraph::NodeIt it(graph); it != INVALID; ++it) {
    1.57 +    if (steiner.terminal(it)) {
    1.58 +      nodecolor[it] = 1;
    1.59 +    } else if (steiner.steiner(it)) {
    1.60 +      nodecolor[it] = 2;
    1.61 +    } else {
    1.62 +      nodecolor[it] = 0;
    1.63 +    }
    1.64 +  }
    1.65 +
    1.66 +
    1.67 +  Palette edgepalette(0);
    1.68 +  edgepalette.add(Color(0.0, 0.0, 0.0));
    1.69 +  edgepalette.add(Color(1.0, 0.0, 0.0));
    1.70 +  SmartUGraph::UEdgeMap<int> edgecolor(graph);
    1.71 +  for (SmartUGraph::UEdgeIt it(graph); it != INVALID; ++it) {
    1.72 +    edgecolor[it] = steiner.tree(it) ? 1 : 0;
    1.73 +  }
    1.74 +
    1.75 +
    1.76 +  graphToEps(graph, eps).
    1.77 +    coords(coord).undirected().
    1.78 +    nodeScale(1.0).scaleToA4().
    1.79 +    nodeColors(composeMap(nodepalette, nodecolor)).
    1.80 +    edgeColors(composeMap(edgepalette, edgecolor)).
    1.81 +    nodeTexts(label).nodeTextSize(8).run();
    1.82 +
    1.83 +  std::cout << "The tree constructed: " << eps << std::endl;
    1.84 +  std::cout << "The cost of the tree: " << steiner.treeValue() << std::endl;
    1.85 +
    1.86 +  return 0;
    1.87 +}