# source:lemon-0.x/demo/disjoint_paths_demo.cc

Last change on this file was 2553:bfced05fa852, checked in by Alpar Juttner, 13 years ago

File size: 3.2 KB
Line
1/* -*- C++ -*-
2 *
3 * This file is a part of LEMON, a generic C++ optimization library
4 *
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>
37#include <lemon/preflow.h>
38#include <lemon/graph_to_eps.h>
39
40using namespace lemon;
41using namespace std;
42
43Color color(bool b) {
44  return b ? RED : BLACK;
45}
46
47int 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;
60
61  typedef ConstMap<Graph::Edge, int> Capacity;
62  Capacity capacity(1);
63
64  Graph::EdgeMap<int> flow(graph);
65
66  Preflow<Graph, Capacity> preflow(graph, capacity, source, target);
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().
75    edgeColors(composeMap(functorMap(color), preflow.flowMap())).
76    coords(coords).run();
77
78
79  cout << "The paths are written into edge_disjoint_paths.eps" << endl;
80
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, SCapacity> spreflow(sgraph, scapacity,
91                                      SGraph::outNode(source),
92                                      SGraph::inNode(target));
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().