demo/topology_demo.cc
author ladanyi
Thu, 02 Feb 2006 15:44:45 +0000
changeset 1943 7349b09373a0
parent 1909 2d806130e700
child 1956 a055123339d5
permissions -rw-r--r--
Fix crash when an arrow is clicked with the delete tool.
deba@1802
     1
/* -*- C++ -*-
deba@1802
     2
 * demo/min_route.cc - Part of LEMON, a generic C++ optimization library
deba@1802
     3
 *
alpar@1875
     4
 * Copyright (C) 2006 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@1802
     5
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1802
     6
 *
deba@1802
     7
 * Permission to use, modify and distribute this software is granted
deba@1802
     8
 * provided that this copyright notice appears in all copies. For
deba@1802
     9
 * precise terms see the accompanying LICENSE file.
deba@1802
    10
 *
deba@1802
    11
 * This software is provided "AS IS" with no warranty of any kind,
deba@1802
    12
 * express or implied, and with no claim as to its suitability for any
deba@1802
    13
 * purpose.
deba@1802
    14
 *
deba@1802
    15
 */
deba@1802
    16
deba@1802
    17
#include <lemon/list_graph.h>
deba@1802
    18
#include <lemon/topology.h>
deba@1802
    19
#include <lemon/graph_to_eps.h>
deba@1802
    20
#include <lemon/graph_reader.h>
deba@1802
    21
#include <lemon/xy.h>
deba@1802
    22
deba@1802
    23
#include <iostream>
deba@1802
    24
deba@1802
    25
#include <cstdlib>
deba@1802
    26
#include <ctime>
deba@1802
    27
deba@1802
    28
/// \ingroup demos
deba@1802
    29
/// \file
deba@1802
    30
/// \brief Demo what shows the result of some topology functions.
deba@1802
    31
///
deba@1802
    32
///  Demo what shows the result of some topology functions.
deba@1802
    33
///
deba@1802
    34
/// \include topology_demo.cc
deba@1802
    35
deba@1802
    36
using namespace lemon;
deba@1802
    37
using namespace std;
deba@1802
    38
deba@1802
    39
deba@1802
    40
Color color(bool val) {
deba@1802
    41
  return val ? Color(1.0, 0.0, 0.0) : Color(0.0, 0.0, 1.0);
deba@1802
    42
}
deba@1802
    43
deba@1802
    44
deba@1802
    45
void drawConnectedComponents() {
klao@1909
    46
  typedef ListUGraph Graph;
deba@1802
    47
  typedef Graph::Node Node;
deba@1802
    48
deba@1802
    49
  Graph graph;
deba@1802
    50
  Graph::NodeMap<xy<double> > coords(graph);
deba@1802
    51
klao@1909
    52
  UGraphReader<Graph>("u_components.lgf", graph).
deba@1802
    53
    readNodeMap("coordinates_x", xMap(coords)).
deba@1802
    54
    readNodeMap("coordinates_y", yMap(coords)).
deba@1802
    55
    run();
deba@1802
    56
  
deba@1802
    57
  ColorSet colorSet;
deba@1802
    58
deba@1802
    59
  Graph::NodeMap<int> compMap(graph);
deba@1802
    60
  connectedComponents(graph, compMap);
deba@1802
    61
deba@1910
    62
  graphToEps(graph, "connected_components.eps").undirected().
deba@1802
    63
    coords(coords).scaleToA4().enableParallel().
deba@1802
    64
    parEdgeDist(20.0).edgeWidthScale(2.0).nodeScale(20.0).
deba@1802
    65
    nodeColors(composeMap(colorSet, compMap)).run();
deba@1802
    66
deba@1802
    67
  std::cout << "Result: connected_components.eps" << std::endl;
deba@1802
    68
}
deba@1802
    69
deba@1802
    70
void drawStronglyConnectedComponents() {
deba@1802
    71
  typedef ListGraph Graph;
deba@1802
    72
  typedef Graph::Node Node;
deba@1802
    73
deba@1802
    74
  Graph graph;
deba@1802
    75
  Graph::NodeMap<xy<double> > coords(graph);
deba@1802
    76
deba@1802
    77
  GraphReader<Graph>("dir_components.lgf", graph).
deba@1802
    78
    readNodeMap("coordinates_x", xMap(coords)).
deba@1802
    79
    readNodeMap("coordinates_y", yMap(coords)).
deba@1802
    80
    run();
deba@1802
    81
  
deba@1802
    82
  ColorSet colorSet;
deba@1802
    83
deba@1802
    84
  Graph::NodeMap<int> compMap(graph);
deba@1802
    85
  Graph::EdgeMap<bool> cutMap(graph);
deba@1802
    86
  stronglyConnectedComponents(graph, compMap);
deba@1802
    87
  stronglyConnectedCutEdges(graph, cutMap);
deba@1802
    88
deba@1802
    89
  graphToEps(graph, "strongly_connected_components.eps").
deba@1802
    90
    coords(coords).scaleToA4().enableParallel().
deba@1802
    91
    drawArrows().arrowWidth(10.0).arrowLength(10.0).
deba@1802
    92
    parEdgeDist(20.0).edgeWidthScale(2.0).nodeScale(20.0).
deba@1802
    93
    nodeColors(composeMap(colorSet, compMap)).
deba@1802
    94
    edgeColors(composeMap(functorMap(&color), cutMap)).run();
deba@1802
    95
deba@1802
    96
  std::cout << "Result: strongly_connected_components.eps" << std::endl;
deba@1802
    97
}
deba@1802
    98
deba@1802
    99
void drawNodeBiconnectedComponents() {
klao@1909
   100
  typedef ListUGraph Graph;
deba@1802
   101
  typedef Graph::Node Node;
klao@1909
   102
  typedef Graph::UEdge UEdge;
deba@1802
   103
deba@1802
   104
  Graph graph;
deba@1802
   105
  Graph::NodeMap<xy<double> > coords(graph);
deba@1802
   106
klao@1909
   107
  UGraphReader<Graph>("u_components.lgf", graph).
deba@1802
   108
    readNodeMap("coordinates_x", xMap(coords)).
deba@1802
   109
    readNodeMap("coordinates_y", yMap(coords)).
deba@1802
   110
    run();
deba@1802
   111
deba@1802
   112
  ColorSet colorSet;
deba@1802
   113
klao@1909
   114
  Graph::UEdgeMap<int> compMap(graph);
deba@1802
   115
  Graph::NodeMap<bool> cutMap(graph);
deba@1802
   116
  biNodeConnectedComponents(graph, compMap);
deba@1802
   117
  biNodeConnectedCutNodes(graph, cutMap);
deba@1910
   118
deba@1910
   119
  graphToEps(graph, "bi_node_connected_components.eps").undirected().
deba@1802
   120
    coords(coords).scaleToA4().enableParallel().
deba@1802
   121
    parEdgeDist(20.0).edgeWidthScale(5.0).nodeScale(20.0).
deba@1802
   122
    edgeColors(composeMap(colorSet, compMap)).
deba@1802
   123
    nodeColors(composeMap(functorMap(&color), cutMap)).
deba@1802
   124
    run();
deba@1802
   125
deba@1802
   126
  std::cout << "Result: bi_node_connected_components.eps" << std::endl;
deba@1802
   127
}
deba@1802
   128
deba@1802
   129
void drawEdgeBiconnectedComponents() {
klao@1909
   130
  typedef ListUGraph Graph;
deba@1802
   131
  typedef Graph::Node Node;
klao@1909
   132
  typedef Graph::UEdge UEdge;
deba@1802
   133
deba@1802
   134
  Graph graph;
deba@1802
   135
  Graph::NodeMap<xy<double> > coords(graph);
deba@1802
   136
klao@1909
   137
  UGraphReader<Graph>("u_components.lgf", graph).
deba@1802
   138
    readNodeMap("coordinates_x", xMap(coords)).
deba@1802
   139
    readNodeMap("coordinates_y", yMap(coords)).
deba@1802
   140
    run();
deba@1802
   141
  
deba@1802
   142
  ColorSet colorSet;
deba@1802
   143
deba@1802
   144
  Graph::NodeMap<int> compMap(graph);
klao@1909
   145
  Graph::UEdgeMap<bool> cutMap(graph);
deba@1802
   146
  biEdgeConnectedComponents(graph, compMap);
deba@1802
   147
  biEdgeConnectedCutEdges(graph, cutMap);
deba@1802
   148
deba@1910
   149
  graphToEps(graph, "bi_edge_connected_components.eps").undirected().
deba@1802
   150
    coords(coords).scaleToA4().enableParallel().
deba@1802
   151
    parEdgeDist(20.0).edgeWidthScale(2.0).nodeScale(20.0).
deba@1802
   152
    nodeColors(composeMap(colorSet, compMap)).
deba@1802
   153
    edgeColors(composeMap(functorMap(&color), cutMap)).run();
deba@1802
   154
  
deba@1802
   155
  std::cout << "Result: bi_edge_connected_components.eps" << std::endl;
deba@1802
   156
}
deba@1802
   157
deba@1802
   158
void drawBipartitePartitions() {
klao@1909
   159
  typedef ListUGraph Graph;
deba@1802
   160
  typedef Graph::Node Node;
klao@1909
   161
  typedef Graph::UEdge UEdge;
deba@1802
   162
deba@1802
   163
  Graph graph;
deba@1802
   164
  Graph::NodeMap<xy<double> > coords(graph);
deba@1802
   165
klao@1909
   166
  UGraphReader<Graph>("partitions.lgf", graph).
deba@1802
   167
    readNodeMap("coordinates_x", xMap(coords)).
deba@1802
   168
    readNodeMap("coordinates_y", yMap(coords)).
deba@1802
   169
    run();
deba@1802
   170
  
deba@1802
   171
  ColorSet colorSet;
deba@1802
   172
deba@1802
   173
  Graph::NodeMap<bool> partMap(graph);
deba@1802
   174
  bipartitePartitions(graph, partMap);
deba@1802
   175
deba@1910
   176
  graphToEps(graph, "bipartite_partitions.eps").undirected().
deba@1802
   177
    coords(coords).scaleToA4().enableParallel().
deba@1802
   178
    parEdgeDist(20.0).edgeWidthScale(2.0).nodeScale(20.0).
deba@1802
   179
    nodeColors(composeMap(functorMap(&color), partMap)).run();
deba@1802
   180
  
deba@1802
   181
  std::cout << "Result: bipartite_partitions.eps" << std::endl;
deba@1802
   182
} 
deba@1802
   183
deba@1802
   184
int main() {
deba@1802
   185
  srand(time(0));
deba@1802
   186
deba@1802
   187
  drawConnectedComponents();
deba@1802
   188
  drawStronglyConnectedComponents();
deba@1802
   189
  drawNodeBiconnectedComponents();
deba@1802
   190
  drawEdgeBiconnectedComponents();
deba@1802
   191
  drawBipartitePartitions();
deba@1802
   192
  return 0;
deba@1802
   193
}