1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2010
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
20 ///\brief Demo program that solves maximum flow problems using the LP interface
22 /// This demo program shows how to solve the maximum flow problem using
23 /// the LEMON LP solver interface. We would like to lay the emphasis on the
24 /// simplicity of the way one can formulate LP constraints that arise in graph
25 /// theory using LEMON.
27 /// \include lp_maxflow_demo.cc
30 #include <lemon/smart_graph.h>
31 #include <lemon/lgf_reader.h>
34 using namespace lemon;
36 template <typename GR, typename CAP>
37 double maxFlow(const GR &g, const CAP &capacity,
38 typename GR::Node source, typename GR::Node target)
40 TEMPLATE_DIGRAPH_TYPEDEFS(GR);
42 // Create an instance of the default LP solver
45 // Add a column to the problem for each arc
46 typename GR::template ArcMap<Lp::Col> f(g);
49 // Capacity constraints
50 for (ArcIt a(g); a != INVALID; ++a) {
51 lp.colLowerBound(f[a], 0);
52 lp.colUpperBound(f[a], capacity[a]);
55 // Flow conservation constraints
56 for (NodeIt n(g); n != INVALID; ++n) {
57 if (n == source || n == target) continue;
59 for (OutArcIt a(g, n); a != INVALID; ++a) e += f[a];
60 for (InArcIt a(g, n); a != INVALID; ++a) e -= f[a];
66 for (OutArcIt a(g, source); a != INVALID; ++a) o += f[a];
67 for (InArcIt a(g, source); a != INVALID; ++a) o -= f[a];
71 // Solve the LP problem
78 int main(int argc, char *argv[])
80 // Check the arguments
82 std::cerr << "Usage:" << std::endl;
83 std::cerr << " lp_maxflow_demo <input_file>" << std::endl;
84 std::cerr << "The given input file has to contain a maximum flow\n"
85 << "problem in LGF format (like 'maxflow.lgf')."
90 // Read the input file
92 SmartDigraph::ArcMap<double> cap(g);
93 SmartDigraph::Node s, t;
95 digraphReader(g, argv[1])
96 .arcMap("capacity", cap)
101 // Solve the problem and print the result
102 std::cout << "Max flow value: " << maxFlow(g, cap, s, t) << std::endl;