demo/disjoint_paths_demo.cc
author deba
Thu, 19 Apr 2007 15:14:34 +0000
changeset 2426 6e1027a05d73
parent 2391 14a343be7a5a
child 2514 57143c09dc20
permissions -rw-r--r--
Allowing 'string' type quoting
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
/// \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) {
alpar@2174
    44
  return b ? RED : BLACK;
deba@2084
    45
}
deba@2084
    46
deba@2084
    47
int main() {
deba@2392
    48
  cout << "This program calculates the number " 
deba@2392
    49
    "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
alpar@2207
    55
  Graph::NodeMap<dim2::Point<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@2392
    73
    title("edge disjoint paths").scaleToA4().
deba@2392
    74
    copyright("(C) 2003-2007 LEMON Project").drawArrows().
deba@2084
    75
    edgeColors(composeMap(functorMap(color), flow)).
deba@2392
    76
    coords(coords).run();
deba@2084
    77
deba@2084
    78
deba@2084
    79
  cout << "The paths are written into edge_disjoint_paths.eps" << endl;
deba@2084
    80
deba@2084
    81
  typedef SplitGraphAdaptor<SmartGraph> SGraph;
deba@2084
    82
deba@2084
    83
  SGraph sgraph(graph);
deba@2084
    84
deba@2084
    85
  typedef ConstMap<SGraph::Edge, int> SCapacity;
deba@2084
    86
  SCapacity scapacity(1);
deba@2084
    87
deba@2084
    88
  SGraph::EdgeMap<int> sflow(sgraph);
deba@2084
    89
deba@2084
    90
  Preflow<SGraph, int, SCapacity> spreflow(sgraph, SGraph::outNode(source), 
deba@2084
    91
                                           SGraph::inNode(target), 
deba@2084
    92
                                           scapacity, sflow);
deba@2084
    93
deba@2084
    94
  spreflow.run();
deba@2084
    95
deba@2084
    96
  cout << "Number of node disjoint paths: " << spreflow.flowValue() << endl;
deba@2084
    97
deba@2084
    98
deba@2084
    99
  graphToEps(sgraph, "node_disjoint_paths.eps").
deba@2392
   100
    title("node disjoint paths").scaleToA4().
deba@2392
   101
    copyright("(C) 2003-2007 LEMON Project").drawArrows().
deba@2084
   102
    edgeColors(composeMap(functorMap(color), sflow)).
alpar@2207
   103
    coords(SGraph::combinedNodeMap(coords,
alpar@2207
   104
				   shiftMap(coords,
alpar@2207
   105
					    dim2::Point<double>(5, 0)))).
deba@2392
   106
    run();
deba@2084
   107
deba@2084
   108
  cout << "The paths are written into node_disjoint_paths.eps" << endl;
deba@2084
   109
  
deba@2084
   110
  return 0;
deba@2084
   111
}