2

I'm studying computer science/operations research, and right now I am interested in the maximum flow problem. I wrote an algorithm that solves the problem, but am having trouble figuring out the computational complexity. Python-esque pseudo-code is below:

function max_flow(graph,current_node,limit):
    if limit <= 0:
        return 0 
    else if node == graph.sink:
        return limit
    else:
        total = 0
        for each node connected to current_node:
            cap = graph.capacity(current_node,node)
            if cap > 0:
                next_limit = min(cap, limit - total)
                next_max_flow = max_flow(graph,node,next_limit)
                total += next_max_flow
                graph.capacity(current_node,node) -= next_max_flow
        return total

Where graph.capacity(a,b) means the capacity of the arc from a to b. To find the total max flow, one would call the algorithm as max_flow(graph,graph.source,infinity).

I proved to myself that the algorithm is correct (although if that is also wrong, feel free to correct me), and my belief about the complexity is that the algorithm runs in O(|V|2), where |V| is the number of vertices. The reasoning is:

  1. In the worst case, every node is connected to every other node, so it is a complete digraph. This means that at most, each node has |V| - 1 edges. However, due to skew symmetry, if the capacity from a to b is positive, the capacity from b to a must be negative. So, if the source has |V| - 1 positive-capacity edges, then the next-highest node could have at most |V| - 2 positive-capacity edges, since at least one edge must have negative capacity. Because of this, the total number of positive-capacity edges is at most (|V|-1)*(|V|-2) / 2 = O(|V|2).

And that's sort of where I get stuck. Intuitively, it sounds like going through each of |V| nodes a maximum of |V| times, which results in O(|V|2). However, part of me thinks that the actual complexity is O(|V|3), but I can't find any rigorous explanation for either of these. Can someone give me a push in the right direction?

Note: none of this is homework, or a part of any class.

Austin
  • 31
  • 2
  • I think the problem you're trying to solve is NP-complete. – allo May 10 '17 at 18:37
  • 1
    @allo The Maximum Flow Problems are not NP complete. See the links - [link](https://en.wikipedia.org/wiki/Maximum_flow_problem) and [link](https://www.hackerearth.com/practice/algorithms/graphs/maximum-flow/tutorial/) – GAURANG VYAS May 10 '17 at 19:34

1 Answers1

0

Complexity

I think this has exponential complexity for acyclic graphs because of the recursive calls. (For cyclic graphs it will never complete so has infinite complexity.)

Example

Suppose we have a start node, 100 nodes arranged in a 10*10 grid, and a sink node.

The start connects to all 10 nodes in column 1 (with capacity 1).

Each node in column x connects to all nodes in column x+1 (with capacity 1).

But no node connects to the sink node.

Then your algorithm will have to try all 10^10 ( = 10,000,000,000) possible routes through the matrix to discover that the maximum flow is equal to 0.

Peter de Rivaz
  • 33,126
  • 4
  • 46
  • 75