deba@2081: /* -*- C++ -*- deba@2081: * deba@2081: * This file is a part of LEMON, a generic C++ optimization library deba@2081: * deba@2081: * Copyright (C) 2003-2006 deba@2081: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@2081: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@2081: * deba@2081: * Permission to use, modify and distribute this software is granted deba@2081: * provided that this copyright notice appears in all copies. For deba@2081: * precise terms see the accompanying LICENSE file. deba@2081: * deba@2081: * This software is provided "AS IS" with no warranty of any kind, deba@2081: * express or implied, and with no claim as to its suitability for any deba@2081: * purpose. deba@2081: * deba@2081: */ deba@2081: deba@2081: /// \ingroup demos deba@2081: /// \file deba@2081: /// \brief Node and edge disjoint paths in directed graph. deba@2081: /// deba@2081: /// This demo program calculates how many edge disjoint and node disjoint deba@2081: /// paths are in a directed graph between a source and a target node. deba@2081: /// The edge disjoint paths can be computed with a flow algorithm, deba@2081: /// in this example we use the Preflow algorithm class. To get the node deba@2081: /// disjoint paths we should first adapt the graph with the SplitGraphAdaptor deba@2081: /// and just then calculate the flow. deba@2081: /// deba@2081: /// \include disjoint_paths.cc deba@2081: deba@2081: #include deba@2081: deba@2081: #include deba@2081: #include deba@2081: #include deba@2081: #include deba@2081: #include deba@2081: deba@2081: using namespace lemon; deba@2081: using namespace std; deba@2081: deba@2081: Color color(bool b) { deba@2081: return b ? Color(1.0, 0.0, 0.0) : Color(0.0, 0.0, 0.0); deba@2081: } deba@2081: deba@2081: int main() { deba@2081: cout << "This program calculates the number " << deba@2081: "of disjoint paths in a graph" << endl; deba@2081: cout << "The graph is read from the disjoint_paths.lgf file" << endl; deba@2081: typedef SmartGraph Graph; deba@2081: deba@2081: Graph graph; deba@2081: deba@2081: Graph::NodeMap > coords(graph); deba@2081: Graph::Node source, target; deba@2081: GraphReader("disjoint_paths.lgf", graph). deba@2081: readNodeMap("coords", coords). deba@2081: readNode("source", source).readNode("target", target).run(); deba@2081: deba@2081: typedef ConstMap Capacity; deba@2081: Capacity capacity(1); deba@2081: deba@2081: Graph::EdgeMap flow(graph); deba@2081: deba@2081: Preflow preflow(graph, source, target, capacity, flow); deba@2081: deba@2081: preflow.run(); deba@2081: deba@2081: cout << "Number of edge disjoint paths: " << preflow.flowValue() << endl; deba@2081: deba@2081: graphToEps(graph, "edge_disjoint.eps"). deba@2081: title("edge disjoint path").copyright("(C) 2006 LEMON Project").drawArrows(). deba@2081: edgeColors(composeMap(functorMap(color), flow)). deba@2081: coords(coords).autoNodeScale().run(); deba@2081: deba@2081: deba@2081: cout << "The paths are written into edge_disjoint.eps" << endl; deba@2081: deba@2081: typedef SplitGraphAdaptor SGraph; deba@2081: deba@2081: SGraph sgraph(graph); deba@2081: deba@2081: typedef ConstMap SCapacity; deba@2081: SCapacity scapacity(1); deba@2081: deba@2081: SGraph::EdgeMap sflow(sgraph); deba@2081: deba@2081: Preflow spreflow(sgraph, SGraph::outNode(source), deba@2081: SGraph::inNode(target), deba@2081: scapacity, sflow); deba@2081: deba@2081: spreflow.run(); deba@2081: deba@2081: cout << "Number of node disjoint paths: " << spreflow.flowValue() << endl; deba@2081: deba@2081: deba@2081: graphToEps(sgraph, "node_disjoint.eps"). deba@2081: title("node disjoint path").copyright("(C) 2006 LEMON Project").drawArrows(). deba@2081: edgeColors(composeMap(functorMap(color), sflow)). deba@2081: coords(SGraph::combinedNodeMap(coords, shiftMap(coords, xy(5, 0)))). deba@2081: autoNodeScale().run(); deba@2081: deba@2081: cout << "The paths are written into node_disjoint.eps" << endl; deba@2081: deba@2081: return 0; deba@2081: }