demo/topology_demo.cc
changeset 1802 fdfa3aa18607
child 1875 98698b69a902
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/demo/topology_demo.cc	Wed Nov 16 09:15:41 2005 +0000
     1.3 @@ -0,0 +1,192 @@
     1.4 +/* -*- C++ -*-
     1.5 + * demo/min_route.cc - Part of LEMON, a generic C++ optimization library
     1.6 + *
     1.7 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     1.8 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
     1.9 + *
    1.10 + * Permission to use, modify and distribute this software is granted
    1.11 + * provided that this copyright notice appears in all copies. For
    1.12 + * precise terms see the accompanying LICENSE file.
    1.13 + *
    1.14 + * This software is provided "AS IS" with no warranty of any kind,
    1.15 + * express or implied, and with no claim as to its suitability for any
    1.16 + * purpose.
    1.17 + *
    1.18 + */
    1.19 +
    1.20 +#include <lemon/list_graph.h>
    1.21 +#include <lemon/topology.h>
    1.22 +#include <lemon/graph_to_eps.h>
    1.23 +#include <lemon/graph_reader.h>
    1.24 +#include <lemon/xy.h>
    1.25 +
    1.26 +#include <iostream>
    1.27 +
    1.28 +#include <cstdlib>
    1.29 +#include <ctime>
    1.30 +
    1.31 +/// \ingroup demos
    1.32 +/// \file
    1.33 +/// \brief Demo what shows the result of some topology functions.
    1.34 +///
    1.35 +///  Demo what shows the result of some topology functions.
    1.36 +///
    1.37 +/// \include topology_demo.cc
    1.38 +
    1.39 +using namespace lemon;
    1.40 +using namespace std;
    1.41 +
    1.42 +
    1.43 +Color color(bool val) {
    1.44 +  return val ? Color(1.0, 0.0, 0.0) : Color(0.0, 0.0, 1.0);
    1.45 +}
    1.46 +
    1.47 +
    1.48 +void drawConnectedComponents() {
    1.49 +  typedef UndirListGraph Graph;
    1.50 +  typedef Graph::Node Node;
    1.51 +
    1.52 +  Graph graph;
    1.53 +  Graph::NodeMap<xy<double> > coords(graph);
    1.54 +
    1.55 +  UndirGraphReader<Graph>("undir_components.lgf", graph).
    1.56 +    readNodeMap("coordinates_x", xMap(coords)).
    1.57 +    readNodeMap("coordinates_y", yMap(coords)).
    1.58 +    run();
    1.59 +  
    1.60 +  ColorSet colorSet;
    1.61 +
    1.62 +  Graph::NodeMap<int> compMap(graph);
    1.63 +  connectedComponents(graph, compMap);
    1.64 +
    1.65 +  graphToEps(graph, "connected_components.eps").undir().
    1.66 +    coords(coords).scaleToA4().enableParallel().
    1.67 +    parEdgeDist(20.0).edgeWidthScale(2.0).nodeScale(20.0).
    1.68 +    nodeColors(composeMap(colorSet, compMap)).run();
    1.69 +
    1.70 +  std::cout << "Result: connected_components.eps" << std::endl;
    1.71 +}
    1.72 +
    1.73 +void drawStronglyConnectedComponents() {
    1.74 +  typedef ListGraph Graph;
    1.75 +  typedef Graph::Node Node;
    1.76 +
    1.77 +  Graph graph;
    1.78 +  Graph::NodeMap<xy<double> > coords(graph);
    1.79 +
    1.80 +  GraphReader<Graph>("dir_components.lgf", graph).
    1.81 +    readNodeMap("coordinates_x", xMap(coords)).
    1.82 +    readNodeMap("coordinates_y", yMap(coords)).
    1.83 +    run();
    1.84 +  
    1.85 +  ColorSet colorSet;
    1.86 +
    1.87 +  Graph::NodeMap<int> compMap(graph);
    1.88 +  Graph::EdgeMap<bool> cutMap(graph);
    1.89 +  stronglyConnectedComponents(graph, compMap);
    1.90 +  stronglyConnectedCutEdges(graph, cutMap);
    1.91 +
    1.92 +  graphToEps(graph, "strongly_connected_components.eps").
    1.93 +    coords(coords).scaleToA4().enableParallel().
    1.94 +    drawArrows().arrowWidth(10.0).arrowLength(10.0).
    1.95 +    parEdgeDist(20.0).edgeWidthScale(2.0).nodeScale(20.0).
    1.96 +    nodeColors(composeMap(colorSet, compMap)).
    1.97 +    edgeColors(composeMap(functorMap(&color), cutMap)).run();
    1.98 +
    1.99 +  std::cout << "Result: strongly_connected_components.eps" << std::endl;
   1.100 +}
   1.101 +
   1.102 +void drawNodeBiconnectedComponents() {
   1.103 +  typedef UndirListGraph Graph;
   1.104 +  typedef Graph::Node Node;
   1.105 +  typedef Graph::UndirEdge UndirEdge;
   1.106 +
   1.107 +  Graph graph;
   1.108 +  Graph::NodeMap<xy<double> > coords(graph);
   1.109 +
   1.110 +  UndirGraphReader<Graph>("undir_components.lgf", graph).
   1.111 +    readNodeMap("coordinates_x", xMap(coords)).
   1.112 +    readNodeMap("coordinates_y", yMap(coords)).
   1.113 +    run();
   1.114 +
   1.115 +  ColorSet colorSet;
   1.116 +
   1.117 +  Graph::UndirEdgeMap<int> compMap(graph);
   1.118 +  Graph::NodeMap<bool> cutMap(graph);
   1.119 +  biNodeConnectedComponents(graph, compMap);
   1.120 +  biNodeConnectedCutNodes(graph, cutMap);
   1.121 +  graphToEps(graph, "bi_node_connected_components.eps").undir().
   1.122 +    coords(coords).scaleToA4().enableParallel().
   1.123 +    parEdgeDist(20.0).edgeWidthScale(5.0).nodeScale(20.0).
   1.124 +    edgeColors(composeMap(colorSet, compMap)).
   1.125 +    nodeColors(composeMap(functorMap(&color), cutMap)).
   1.126 +    run();
   1.127 +
   1.128 +  std::cout << "Result: bi_node_connected_components.eps" << std::endl;
   1.129 +}
   1.130 +
   1.131 +void drawEdgeBiconnectedComponents() {
   1.132 +  typedef UndirListGraph Graph;
   1.133 +  typedef Graph::Node Node;
   1.134 +  typedef Graph::UndirEdge UndirEdge;
   1.135 +
   1.136 +  Graph graph;
   1.137 +  Graph::NodeMap<xy<double> > coords(graph);
   1.138 +
   1.139 +  UndirGraphReader<Graph>("undir_components.lgf", graph).
   1.140 +    readNodeMap("coordinates_x", xMap(coords)).
   1.141 +    readNodeMap("coordinates_y", yMap(coords)).
   1.142 +    run();
   1.143 +  
   1.144 +  ColorSet colorSet;
   1.145 +
   1.146 +  Graph::NodeMap<int> compMap(graph);
   1.147 +  Graph::UndirEdgeMap<bool> cutMap(graph);
   1.148 +  biEdgeConnectedComponents(graph, compMap);
   1.149 +  biEdgeConnectedCutEdges(graph, cutMap);
   1.150 +
   1.151 +  graphToEps(graph, "bi_edge_connected_components.eps").undir().
   1.152 +    coords(coords).scaleToA4().enableParallel().
   1.153 +    parEdgeDist(20.0).edgeWidthScale(2.0).nodeScale(20.0).
   1.154 +    nodeColors(composeMap(colorSet, compMap)).
   1.155 +    edgeColors(composeMap(functorMap(&color), cutMap)).run();
   1.156 +  
   1.157 +  std::cout << "Result: bi_edge_connected_components.eps" << std::endl;
   1.158 +}
   1.159 +
   1.160 +void drawBipartitePartitions() {
   1.161 +  typedef UndirListGraph Graph;
   1.162 +  typedef Graph::Node Node;
   1.163 +  typedef Graph::UndirEdge UndirEdge;
   1.164 +
   1.165 +  Graph graph;
   1.166 +  Graph::NodeMap<xy<double> > coords(graph);
   1.167 +
   1.168 +  UndirGraphReader<Graph>("partitions.lgf", graph).
   1.169 +    readNodeMap("coordinates_x", xMap(coords)).
   1.170 +    readNodeMap("coordinates_y", yMap(coords)).
   1.171 +    run();
   1.172 +  
   1.173 +  ColorSet colorSet;
   1.174 +
   1.175 +  Graph::NodeMap<bool> partMap(graph);
   1.176 +  bipartitePartitions(graph, partMap);
   1.177 +
   1.178 +  graphToEps(graph, "bipartite_partitions.eps").undir().
   1.179 +    coords(coords).scaleToA4().enableParallel().
   1.180 +    parEdgeDist(20.0).edgeWidthScale(2.0).nodeScale(20.0).
   1.181 +    nodeColors(composeMap(functorMap(&color), partMap)).run();
   1.182 +  
   1.183 +  std::cout << "Result: bipartite_partitions.eps" << std::endl;
   1.184 +} 
   1.185 +
   1.186 +int main() {
   1.187 +  srand(time(0));
   1.188 +
   1.189 +  drawConnectedComponents();
   1.190 +  drawStronglyConnectedComponents();
   1.191 +  drawNodeBiconnectedComponents();
   1.192 +  drawEdgeBiconnectedComponents();
   1.193 +  drawBipartitePartitions();
   1.194 +  return 0;
   1.195 +}