File: ford_fulkerson_algorithm.cpp

package info (click to toggle)
seqan2 2.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 228,748 kB
  • sloc: cpp: 257,602; ansic: 91,967; python: 8,326; sh: 1,056; xml: 570; makefile: 229; awk: 51; javascript: 21
file content (40 lines) | stat: -rw-r--r-- 1,490 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <seqan/graph_algorithms.h>

using namespace seqan2;

int main()
{
    typedef Graph<Directed<> > TGraph;
    typedef VertexDescriptor<TGraph>::Type TVertexDescriptor;
    typedef Iterator<TGraph, EdgeIterator>::Type TEdgeIterator;
    typedef Size<TGraph>::Type TSize;

    // Create graph with 10 directed edges (0,1), (0,4), ...
    TSize numEdges = 10;
    TVertexDescriptor edges[] = {0, 1, 0, 4, 1, 2, 1, 4, 2, 3, 2, 4, 4, 1, 4, 5, 5, 2, 5, 3};
    TGraph g;
    addEdges(g, edges, numEdges);
    // Print graph.
    std::cout << g << "\n";

    // Create external property map for the edge capacities and assign to the graph.
    String<unsigned int> capMap;
    unsigned capacity[] =    {16, 13, 12, 10, 20, 9, 4, 14, 7, 4};
    assignEdgeMap(capMap, g, capacity);

    // Run the Ford-Fulkerson algorithm for maximum flow computation from source
    // vertex 0 to sink vertex 3.  valF is the value of the flow.
    String<unsigned int> flow;
    unsigned valF = fordFulkersonAlgorithm(flow, g, 0, 3, capMap);

    // Print the result to stdout.
    std::cout << "Ford-Fulkerson (Value of the flow = " << valF << ")\n";
    TEdgeIterator itEdge(g);
    for (; !atEnd(itEdge); goNext(itEdge))
        std::cout << "(" << sourceVertex(itEdge) << "," << targetVertex(itEdge) << "): "
                  << "Flow: " << getProperty(flow, getValue(itEdge)) << ", Capacity: "
                  << getProperty(capMap, getValue(itEdge)) << "\n";

    return 0;
}