demo/strongly_connected_orientation.cc
author deba
Wed, 24 Oct 2007 16:31:49 +0000
changeset 2502 9c23c3762bc5
parent 2207 75a29ac69c19
child 2510 bb523a4758f7
permissions -rw-r--r--
BpUGraphReader and Writer
deba@2084
     1
/* -*- C++ -*-
deba@2084
     2
 *
deba@2084
     3
 * This file is a part of LEMON, a generic C++ optimization library
deba@2084
     4
 *
alpar@2391
     5
 * Copyright (C) 2003-2007
deba@2084
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@2084
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@2084
     8
 *
deba@2084
     9
 * Permission to use, modify and distribute this software is granted
deba@2084
    10
 * provided that this copyright notice appears in all copies. For
deba@2084
    11
 * precise terms see the accompanying LICENSE file.
deba@2084
    12
 *
deba@2084
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@2084
    14
 * express or implied, and with no claim as to its suitability for any
deba@2084
    15
 * purpose.
deba@2084
    16
 *
deba@2084
    17
 */
deba@2084
    18
deba@2084
    19
#include <iostream>
deba@2084
    20
deba@2116
    21
#include <lemon/smart_graph.h>
deba@2084
    22
#include <lemon/graph_reader.h>
deba@2084
    23
#include <lemon/ugraph_adaptor.h>
deba@2084
    24
#include <lemon/graph_to_eps.h>
deba@2084
    25
#include <lemon/dfs.h>
deba@2084
    26
#include <lemon/topology.h>
deba@2084
    27
deba@2084
    28
deba@2084
    29
/// \ingroup demos
deba@2084
    30
/// \file
deba@2084
    31
/// \brief Strongly connected orientation
deba@2084
    32
///
deba@2084
    33
/// This example demonstrates the usage of the DirUGraphAdaptor,
deba@2084
    34
/// the DfsVisitor and some topology functions. The program reads
deba@2084
    35
/// an input undirected graph and with a DfsVisit it orients each edge
deba@2084
    36
/// to minimize the strongly connected components in the graph. At least
deba@2084
    37
/// it checks the result of the orientation with to topology functions.
deba@2084
    38
///
deba@2084
    39
/// \include strongly_connected_orientation.cc
deba@2084
    40
deba@2084
    41
using namespace lemon;
deba@2084
    42
using namespace std;
deba@2084
    43
deba@2084
    44
typedef SmartUGraph UGraph;
deba@2084
    45
UGRAPH_TYPEDEFS(UGraph)
deba@2084
    46
deba@2084
    47
Color color(bool c) {
alpar@2174
    48
  return c ? BLACK : RED;
deba@2084
    49
}
deba@2084
    50
deba@2084
    51
template <typename DirMap>
deba@2084
    52
class OrientVisitor : public DfsVisitor<UGraph> {
deba@2084
    53
public:
deba@2084
    54
deba@2084
    55
  OrientVisitor(const UGraph& ugraph, DirMap& dirMap)
deba@2084
    56
    : _ugraph(ugraph), _dirMap(dirMap), _processed(ugraph, false) {}
deba@2084
    57
deba@2084
    58
  void discover(const Edge& edge) {
deba@2084
    59
    _processed.set(edge, true);
deba@2084
    60
    _dirMap.set(edge, _ugraph.direction(edge));
deba@2084
    61
  }
deba@2084
    62
deba@2084
    63
  void examine(const Edge& edge) {
deba@2084
    64
    if (_processed[edge]) return;
deba@2084
    65
    _processed.set(edge, true);
deba@2084
    66
    _dirMap.set(edge, _ugraph.direction(edge));
deba@2084
    67
  }  
deba@2084
    68
deba@2084
    69
private:
deba@2084
    70
  const UGraph& _ugraph;  
deba@2084
    71
  DirMap& _dirMap;
deba@2084
    72
  UGraph::UEdgeMap<bool> _processed;
deba@2084
    73
};
deba@2084
    74
deba@2084
    75
alpar@2173
    76
int main() {
deba@2084
    77
  cout << "Orientation of the strongly_connected_orientation.lgf "
deba@2084
    78
       << "to be strongly connected" << endl;
deba@2084
    79
deba@2084
    80
  UGraph ugraph;
alpar@2207
    81
  UGraph::NodeMap<dim2::Point<double> > coords(ugraph);
deba@2084
    82
  UGraphReader<UGraph>("strongly_connected_orientation.lgf", ugraph).
deba@2084
    83
    readNodeMap("coords", coords).run();
deba@2084
    84
deba@2084
    85
  UGraph::UEdgeMap<bool> dmap(ugraph);
deba@2084
    86
deba@2084
    87
  typedef OrientVisitor<UGraph::UEdgeMap<bool> > Visitor;
deba@2084
    88
  Visitor visitor(ugraph, dmap);
deba@2084
    89
deba@2084
    90
  DfsVisit<UGraph, Visitor> dfs(ugraph, visitor);
deba@2084
    91
deba@2084
    92
  dfs.run();
deba@2084
    93
deba@2084
    94
  typedef DirUGraphAdaptor<UGraph> DGraph;
deba@2084
    95
  DGraph dgraph(ugraph, dmap);
deba@2084
    96
deba@2084
    97
  cout << "The result written into the " 
deba@2084
    98
       << "strongly_connected_orientation.eps file" << endl;
deba@2084
    99
deba@2084
   100
  graphToEps(dgraph, "strongly_connected_orientation.eps").
deba@2084
   101
    drawArrows().coords(coords).scaleToA4().enableParallel().
deba@2084
   102
    autoNodeScale().autoEdgeWidthScale().run();
deba@2084
   103
deba@2084
   104
  int num_scc = countStronglyConnectedComponents(dgraph);
deba@2084
   105
  int num_becc = countBiEdgeConnectedComponents(ugraph);
deba@2084
   106
deba@2161
   107
  if (num_scc != num_becc) {
deba@2161
   108
    std::cerr << "Wrong Orientation" << std::endl;
deba@2161
   109
  }
deba@2084
   110
  
deba@2084
   111
  return 0;
deba@2084
   112
}