Algorithmic Solutions > LEDA > LEDA Guide > Graph Algorithms > Maximum Flow > Maximum Flow Algorithms > Example MAX_FLOW_T()

Example of How to Use MAX_FLOW_T()

The following program shows how the function MAX_FLOW_T() can be used to compute a maximum flow in a directed graph.

Remark: The graph algorithms in LEDA are generic, that is, they accept graphs as well as parameterized graphs.

In order to use the template function MAX_FLOW_T() we need to include <LEDA/templates/max_flow.t>. We use the LEDA number type integer as the number type NT for the edge capacities. In constrast to the C++ number type int, LEDA's integer does not overflow.

#include <LEDA/graph/graph.h>
#include <LEDA/graph/templates/max_flow.h>
#include <LEDA/numbers/integer.h>

using namespace leda;

In main() we first create a simple graph G with four nodes and six edges. We use an edge_array<integer> cap to store the capacities of the edges of G.

int main()
{
  graph G; 

  node n0=G.new_node(); node n1=G.new_node();
  node n2=G.new_node(); node n3=G.new_node();
  
  edge e0=G.new_edge(n0,n1); edge e1=G.new_edge(n0,n3);
  edge e2=G.new_edge(n1,n2); edge e3=G.new_edge(n1,n3);
  edge e4=G.new_edge(n3,n1); edge e5=G.new_edge(n3,n2);

  edge_array<integer> cap(G);
  cap[e0]=1; cap[e1]=5; cap[e2]=1;
  cap[e3]=2; cap[e4]=2; cap[e5]=1;

The edge_array<integer> flow is used for the result of MAX_FLOW_T(). flow[e] will contain the flow over edge e in the computed maximum flow from s to t. MAX_FLOW_T() returns the value of the maximum flow.

After calling MAX_FLOW_T() we use the function CHECK_MAX_FLOW_T() to check the result of the computation. If the result is correct CHECK_MAX_FLOW_T() returns true and we output the flow value and the flow on the edges of G.

  node s=n0, t=n2;
  edge_array<integer> flow(G);
  integer flow_value=MAX_FLOW_T(G,s,t,cap,flow);

  bool is_maximum_flow=CHECK_MAX_FLOW_T(G,s,t,cap,flow);
 
  if (is_maximum_flow) {
    std::cout << "The maximum flow has value: " 
               << flow_value << std::endl;

    edge e;
    forall_edges(e,G) {
      if (flow[e]>0) {
        G.print_edge(e);
        std::cout << " flow = " << flow[e] << std::endl;
      }
    }
  }

  else std::cout << "Error: MAX_FLOW_T() "
                 << "did not compute a maximum flow!" << std::endl;	

  return 0;
}

See also:

Maximum Flow Algorithms

Checking Result of Maximum Flow Computation

Graphs

Parameterized Graphs

Edge Arrays

Integer


Number Types

Maximum Flow

Graphs and Related Data Types


Manual Entries:

Maximum Flow




Please send any suggestions, comments or questions to leda@algorithmic-solutions.com
© Copyright 2001-2003, Algorithmic Solutions Software GmbH