demo/steiner_demo.cc
author deba
Sun, 30 Sep 2007 19:19:33 +0000
changeset 2481 ddb851e1481a
parent 2400 b199ded24c19
child 2492 387f6ff851ef
permissions -rw-r--r--
Avoiding warnings
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2007
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     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.
    12  *
    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
    15  * purpose.
    16  *
    17  */
    18 
    19 #include <lemon/smart_graph.h>
    20 #include <lemon/kruskal.h>
    21 #include <lemon/graph_reader.h>
    22 #include <lemon/time_measure.h>
    23 #include <lemon/graph_to_eps.h>
    24 
    25 #include <lemon/steiner.h>
    26 
    27 #include <cstdlib>
    28 #include <cmath>
    29 
    30 using namespace lemon;
    31 using namespace lemon::dim2;
    32 
    33 using namespace std;
    34 
    35 int main(int argc, const char *argv[]) {
    36   std::string lgf = argc > 1 ? argv[1] : "steiner.lgf";
    37   std::string eps = argc > 2 ? argv[2] : "steiner.eps";
    38 
    39   SmartUGraph graph;
    40   SmartUGraph::NodeMap<bool> terminal(graph);
    41   SmartUGraph::NodeMap<int> label(graph);
    42   SmartUGraph::NodeMap<Point<double> > coord(graph);
    43   
    44   UGraphReader<SmartUGraph>(lgf, graph).
    45     readNodeMap("coordinates_x", xMap(coord)).
    46     readNodeMap("coordinates_y", yMap(coord)).
    47     readNodeMap("terminal", terminal).run();
    48 
    49   SmartUGraph::UEdgeMap<double> cost(graph);
    50   for (SmartUGraph::UEdgeIt it(graph); it != INVALID; ++it) {
    51     cost[it] = sqrt((coord[graph.target(it)] - 
    52                      coord[graph.source(it)]).normSquare());
    53   }
    54   
    55   SteinerTree<SmartUGraph> steiner(graph, cost);
    56   steiner.init();
    57 
    58   for (SmartUGraph::NodeIt it(graph); it != INVALID; ++it) {
    59     if (terminal[it]) {
    60       steiner.addTerminal(it);
    61     }
    62   }
    63   steiner.start();
    64 
    65 
    66   Palette nodepalette(0);
    67   nodepalette.add(Color(1.0, 1.0, 1.0));
    68   nodepalette.add(Color(0.0, 1.0, 0.0));
    69   nodepalette.add(Color(0.5, 0.5, 0.5));
    70   SmartUGraph::NodeMap<int> nodecolor(graph);
    71   for (SmartUGraph::NodeIt it(graph); it != INVALID; ++it) {
    72     if (steiner.terminal(it)) {
    73       nodecolor[it] = 1;
    74     } else if (steiner.steiner(it)) {
    75       nodecolor[it] = 2;
    76     } else {
    77       nodecolor[it] = 0;
    78     }
    79   }
    80 
    81 
    82   Palette edgepalette(0);
    83   edgepalette.add(Color(0.0, 0.0, 0.0));
    84   edgepalette.add(Color(1.0, 0.0, 0.0));
    85   SmartUGraph::UEdgeMap<int> edgecolor(graph);
    86   for (SmartUGraph::UEdgeIt it(graph); it != INVALID; ++it) {
    87     edgecolor[it] = steiner.tree(it) ? 1 : 0;
    88   }
    89 
    90 
    91   graphToEps(graph, eps).
    92     coords(coord).undirected().
    93     nodeScale(1.0).scaleToA4().
    94     nodeColors(composeMap(nodepalette, nodecolor)).
    95     edgeColors(composeMap(edgepalette, edgecolor)).
    96     nodeTexts(label).nodeTextSize(8).run();
    97 
    98   std::cout << "The tree constructed: " << eps << std::endl;
    99   std::cout << "The cost of the tree: " << steiner.treeValue() << std::endl;
   100 
   101   return 0;
   102 }