/* -*- C++ -*- * * This file is a part of LEMON, a generic C++ optimization library * * Copyright (C) 2003-2008 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport * (Egervary Research Group on Combinatorial Optimization, EGRES). * * Permission to use, modify and distribute this software is granted * provided that this copyright notice appears in all copies. For * precise terms see the accompanying LICENSE file. * * This software is provided "AS IS" with no warranty of any kind, * express or implied, and with no claim as to its suitability for any * purpose. * */ ///\ingroup demos ///\file ///\brief Calculating an approximate Steiner-tree. /// /// Calculating an approximate Steiner-tree. /// /// \include steiner_demo.cc #include #include #include #include #include #include #include #include using namespace lemon; using namespace lemon::dim2; using namespace std; int main(int argc, const char *argv[]) { std::string lgf = argc > 1 ? argv[1] : "steiner.lgf"; std::string eps = argc > 2 ? argv[2] : "steiner.eps"; SmartUGraph graph; SmartUGraph::NodeMap terminal(graph); SmartUGraph::NodeMap label(graph); SmartUGraph::NodeMap > coord(graph); UGraphReader(lgf, graph). readNodeMap("coordinates_x", xMap(coord)). readNodeMap("coordinates_y", yMap(coord)). readNodeMap("terminal", terminal).run(); SmartUGraph::UEdgeMap cost(graph); for (SmartUGraph::UEdgeIt it(graph); it != INVALID; ++it) { cost[it] = sqrt((coord[graph.target(it)] - coord[graph.source(it)]).normSquare()); } SteinerTree steiner(graph, cost); steiner.init(); for (SmartUGraph::NodeIt it(graph); it != INVALID; ++it) { if (terminal[it]) { steiner.addTerminal(it); } } steiner.start(); Palette nodepalette(0); nodepalette.add(Color(1.0, 1.0, 1.0)); nodepalette.add(Color(0.0, 1.0, 0.0)); nodepalette.add(Color(0.5, 0.5, 0.5)); SmartUGraph::NodeMap nodecolor(graph); for (SmartUGraph::NodeIt it(graph); it != INVALID; ++it) { if (steiner.terminal(it)) { nodecolor[it] = 1; } else if (steiner.steiner(it)) { nodecolor[it] = 2; } else { nodecolor[it] = 0; } } Palette edgepalette(0); edgepalette.add(Color(0.0, 0.0, 0.0)); edgepalette.add(Color(1.0, 0.0, 0.0)); SmartUGraph::UEdgeMap edgecolor(graph); for (SmartUGraph::UEdgeIt it(graph); it != INVALID; ++it) { edgecolor[it] = steiner.tree(it) ? 1 : 0; } graphToEps(graph, eps). coords(coord).undirected(). nodeScale(1.0).scaleToA4(). nodeColors(composeMap(nodepalette, nodecolor)). edgeColors(composeMap(edgepalette, edgecolor)). nodeTexts(label).nodeTextSize(8).run(); std::cout << "The tree constructed: " << eps << std::endl; std::cout << "The cost of the tree: " << steiner.treeValue() << std::endl; return 0; }