demo/disjoint_paths_demo.cc
author deba
Mon, 19 Jun 2006 13:44:06 +0000
changeset 2101 439b7f21ccc4
child 2174 f9e43b5cc617
permissions -rw-r--r--
Improvement:

The item sets are written in the order sorted by the labels.

It solves the problem if we read a graph from a file and
then write it back then the nodes will be reversed.
It can be switched off with the LemonWriter interface.
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
 *
deba@2084
     5
 * Copyright (C) 2003-2006
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
/// \ingroup demos
deba@2084
    20
/// \file
deba@2084
    21
/// \brief Node and edge disjoint paths in directed graph.
deba@2084
    22
///
deba@2084
    23
/// This demo program calculates how many edge disjoint and node disjoint
deba@2084
    24
/// paths are in a directed graph between a source and a target node.
deba@2084
    25
/// The edge disjoint paths can be computed with a flow algorithm,
deba@2084
    26
/// in this example we use the Preflow algorithm class. To get the node
deba@2084
    27
/// disjoint paths we should first adapt the graph with the SplitGraphAdaptor
deba@2084
    28
/// and just then calculate the flow.  
deba@2084
    29
///
deba@2084
    30
/// \include disjoint_paths_demo.cc
deba@2084
    31
deba@2084
    32
#include <iostream>
deba@2084
    33
deba@2084
    34
#include <lemon/smart_graph.h>
deba@2084
    35
#include <lemon/graph_adaptor.h>
deba@2084
    36
#include <lemon/graph_reader.h>
deba@2084
    37
#include <lemon/preflow.h>
deba@2084
    38
#include <lemon/graph_to_eps.h>
deba@2084
    39
deba@2084
    40
using namespace lemon;
deba@2084
    41
using namespace std;
deba@2084
    42
deba@2084
    43
Color color(bool b) {
deba@2084
    44
  return b ? Color(1.0, 0.0, 0.0) : Color(0.0, 0.0, 0.0);
deba@2084
    45
}
deba@2084
    46
deba@2084
    47
int main() {
deba@2084
    48
  cout << "This program calculates the number " <<
deba@2084
    49
    "of disjoint paths in a graph" << endl;
deba@2084
    50
  cout << "The graph is read from the disjoint_paths_demo.lgf file" << endl;
deba@2084
    51
  typedef SmartGraph Graph;
deba@2084
    52
deba@2084
    53
  Graph graph;
deba@2084
    54
deba@2084
    55
  Graph::NodeMap<xy<double> > coords(graph);
deba@2084
    56
  Graph::Node source, target;
deba@2084
    57
  GraphReader<Graph>("disjoint_paths_demo.lgf", graph).
deba@2084
    58
    readNodeMap("coords", coords).
deba@2084
    59
    readNode("source", source).readNode("target", target).run();
deba@2084
    60
deba@2084
    61
  typedef ConstMap<Graph::Edge, int> Capacity;
deba@2084
    62
  Capacity capacity(1);
deba@2084
    63
deba@2084
    64
  Graph::EdgeMap<int> flow(graph);
deba@2084
    65
deba@2084
    66
  Preflow<Graph, int, Capacity> preflow(graph, source, target, capacity, flow); 
deba@2084
    67
  
deba@2084
    68
  preflow.run();
deba@2084
    69
deba@2084
    70
  cout << "Number of edge disjoint paths: " << preflow.flowValue() << endl;
deba@2084
    71
deba@2084
    72
  graphToEps(graph, "edge_disjoint_paths.eps").
deba@2084
    73
    title("edge disjoint path").copyright("(C) 2006 LEMON Project").drawArrows().
deba@2084
    74
    edgeColors(composeMap(functorMap(color), flow)).
deba@2084
    75
    coords(coords).autoNodeScale().run();
deba@2084
    76
deba@2084
    77
deba@2084
    78
  cout << "The paths are written into edge_disjoint_paths.eps" << endl;
deba@2084
    79
deba@2084
    80
  typedef SplitGraphAdaptor<SmartGraph> SGraph;
deba@2084
    81
deba@2084
    82
  SGraph sgraph(graph);
deba@2084
    83
deba@2084
    84
  typedef ConstMap<SGraph::Edge, int> SCapacity;
deba@2084
    85
  SCapacity scapacity(1);
deba@2084
    86
deba@2084
    87
  SGraph::EdgeMap<int> sflow(sgraph);
deba@2084
    88
deba@2084
    89
  Preflow<SGraph, int, SCapacity> spreflow(sgraph, SGraph::outNode(source), 
deba@2084
    90
                                           SGraph::inNode(target), 
deba@2084
    91
                                           scapacity, sflow);
deba@2084
    92
deba@2084
    93
  spreflow.run();
deba@2084
    94
deba@2084
    95
  cout << "Number of node disjoint paths: " << spreflow.flowValue() << endl;
deba@2084
    96
deba@2084
    97
deba@2084
    98
  graphToEps(sgraph, "node_disjoint_paths.eps").
deba@2084
    99
    title("node disjoint path").copyright("(C) 2006 LEMON Project").drawArrows().
deba@2084
   100
    edgeColors(composeMap(functorMap(color), sflow)).
deba@2084
   101
    coords(SGraph::combinedNodeMap(coords, shiftMap(coords, xy<double>(5, 0)))).
deba@2084
   102
    autoNodeScale().run();
deba@2084
   103
deba@2084
   104
  cout << "The paths are written into node_disjoint_paths.eps" << endl;
deba@2084
   105
  
deba@2084
   106
  return 0;
deba@2084
   107
}