예제 #1
0
    private static void SolveMaxFlow()
    {
        Console.WriteLine("Max Flow Problem");
        int numNodes = 6;
        int numArcs  = 9;

        int[]     tails             = { 0, 0, 0, 0, 1, 2, 3, 3, 4 };
        int[]     heads             = { 1, 2, 3, 4, 3, 4, 4, 5, 5 };
        int[]     capacities        = { 5, 8, 5, 3, 4, 5, 6, 6, 4 };
        int[]     expectedFlows     = { 4, 4, 2, 0, 4, 4, 0, 6, 4 };
        int       expectedTotalFlow = 10;
        StarGraph graph             = new StarGraph(numNodes, numArcs);
        MaxFlow   maxFlow           = new MaxFlow(graph, 0, numNodes - 1);

        for (int i = 0; i < numArcs; ++i)
        {
            int arc = graph.AddArc(tails[i], heads[i]);
            maxFlow.SetArcCapacity(arc, capacities[i]);
        }
        Console.WriteLine("Solving max flow with " + numNodes + " nodes, and " +
                          numArcs + " arcs, source = 0, sink = " + (numNodes - 1));
        if (maxFlow.Solve())
        {
            long totalFlow = maxFlow.GetOptimalFlow();
            Console.WriteLine("total computed flow " + totalFlow +
                              ", expected = " + expectedTotalFlow);
            for (int i = 0; i < numArcs; ++i)
            {
                Console.WriteLine("Arc " + i + " (" + heads[i] + " -> " + tails[i] +
                                  ", capacity = " + capacities[i] + ") computed = " +
                                  maxFlow.Flow(i) + ", expected = " + expectedFlows[i]);
            }
        }
        else
        {
            Console.WriteLine("No solution found");
        }
    }