demo/disjoint_paths_demo.cc
author deba
Fri, 20 Apr 2007 14:47:19 +0000
changeset 2428 c06e86364234
parent 2391 14a343be7a5a
child 2514 57143c09dc20
permissions -rw-r--r--
Doc change
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2007
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 /// \ingroup demos
    20 /// \file
    21 /// \brief Node and edge disjoint paths in directed graph.
    22 ///
    23 /// This demo program calculates how many edge disjoint and node disjoint
    24 /// paths are in a directed graph between a source and a target node.
    25 /// The edge disjoint paths can be computed with a flow algorithm,
    26 /// in this example we use the Preflow algorithm class. To get the node
    27 /// disjoint paths we should first adapt the graph with the SplitGraphAdaptor
    28 /// and just then calculate the flow.  
    29 ///
    30 /// \include disjoint_paths_demo.cc
    31 
    32 #include <iostream>
    33 
    34 #include <lemon/smart_graph.h>
    35 #include <lemon/graph_adaptor.h>
    36 #include <lemon/graph_reader.h>
    37 #include <lemon/preflow.h>
    38 #include <lemon/graph_to_eps.h>
    39 
    40 using namespace lemon;
    41 using namespace std;
    42 
    43 Color color(bool b) {
    44   return b ? RED : BLACK;
    45 }
    46 
    47 int main() {
    48   cout << "This program calculates the number " 
    49     "disjoint paths in a graph" << endl;
    50   cout << "The graph is read from the disjoint_paths_demo.lgf file" << endl;
    51   typedef SmartGraph Graph;
    52 
    53   Graph graph;
    54 
    55   Graph::NodeMap<dim2::Point<double> > coords(graph);
    56   Graph::Node source, target;
    57   GraphReader<Graph>("disjoint_paths_demo.lgf", graph).
    58     readNodeMap("coords", coords).
    59     readNode("source", source).readNode("target", target).run();
    60 
    61   typedef ConstMap<Graph::Edge, int> Capacity;
    62   Capacity capacity(1);
    63 
    64   Graph::EdgeMap<int> flow(graph);
    65 
    66   Preflow<Graph, int, Capacity> preflow(graph, source, target, capacity, flow); 
    67   
    68   preflow.run();
    69 
    70   cout << "Number of edge disjoint paths: " << preflow.flowValue() << endl;
    71 
    72   graphToEps(graph, "edge_disjoint_paths.eps").
    73     title("edge disjoint paths").scaleToA4().
    74     copyright("(C) 2003-2007 LEMON Project").drawArrows().
    75     edgeColors(composeMap(functorMap(color), flow)).
    76     coords(coords).run();
    77 
    78 
    79   cout << "The paths are written into edge_disjoint_paths.eps" << endl;
    80 
    81   typedef SplitGraphAdaptor<SmartGraph> SGraph;
    82 
    83   SGraph sgraph(graph);
    84 
    85   typedef ConstMap<SGraph::Edge, int> SCapacity;
    86   SCapacity scapacity(1);
    87 
    88   SGraph::EdgeMap<int> sflow(sgraph);
    89 
    90   Preflow<SGraph, int, SCapacity> spreflow(sgraph, SGraph::outNode(source), 
    91                                            SGraph::inNode(target), 
    92                                            scapacity, sflow);
    93 
    94   spreflow.run();
    95 
    96   cout << "Number of node disjoint paths: " << spreflow.flowValue() << endl;
    97 
    98 
    99   graphToEps(sgraph, "node_disjoint_paths.eps").
   100     title("node disjoint paths").scaleToA4().
   101     copyright("(C) 2003-2007 LEMON Project").drawArrows().
   102     edgeColors(composeMap(functorMap(color), sflow)).
   103     coords(SGraph::combinedNodeMap(coords,
   104 				   shiftMap(coords,
   105 					    dim2::Point<double>(5, 0)))).
   106     run();
   107 
   108   cout << "The paths are written into node_disjoint_paths.eps" << endl;
   109   
   110   return 0;
   111 }