# Getting ALL negative weight cycles of a graph using Bellman-Ford

I'm doing a min cost assignation problem to assign doctors to their working days for a hospital.

After correctly getting the max flow with Ford-Fulkerson algorithm, I would like to use the cycle canceling algorithm to minimze the flow's cost. I'm basically using the Bellman-Ford algorithm from the sink of the residual graph with the max flow.

The problem is that Bellman-Ford gives me one negative weight cycle (the one that reduces more the cost), and in a given example, such cycle has a bottleneck capacity of 0, so I cannot send flow around it. However in the very same example, other negative weight cycles exist with a bottleneck capacity strictly greater than 0. Sending the bottleneck flow among these cycles would certainly reduce the cost of my assignment, nevertheless the Bellman-Ford implementation does not seem to be capable of yielding such cycles.

How might I get all the graph's negative cycles, to finally get the min cost assignation?

For this university project, the algorithm's performance is not relevant.

• Note that there can, in general, be exponentially many negative-weight cycles (for a trivial example, take a complete graph with every edge having negative weight) so a polynomial-time algorithm is impossible. – David Richerby Dec 7 '14 at 10:35
• I know the performance is going to be low, however this corresponds to the university course 'programming projects', so they told us not to worry about it, as that is not the focus of the subject. – Martí Bosch Dec 7 '14 at 10:56
• If costs are not an issue, just inspect all cycles. Note furthermore that min-cost flows are a thing in the literature. – Raphael Dec 7 '14 at 11:08
• @Raphael if by 'cost' you mean 'complexity of the algorithm', yes, they are not an issue. However isn't there a way to get only the negative cycles using the Bellman-Ford algorithm? After the first |V|-1 iterations, if some edge has distance(destination) > distance(source) + cost(edge), this means that is a negative cycle. What I'm trying to do is: after I've found this negative cycle, keep looking until I find all of them. Can I do that with Bellman-Ford? Thanks! – Martí Bosch Dec 7 '14 at 12:03
• What does the invariant resp. the definition of every "cell" content of BF tell you? – Raphael Dec 7 '14 at 12:15

You can use a brute force over all sequences of vertices, check if the permutation represents a negative cycle, and if so- print the cycle. This is not an efficient algorithm (runs at $O(n^{n+1})$), but a simple one

Suppose all vertices are denoted by $1,2,\ldots,n$. We denote by $C[i]$ the $i^{th}$ node in the cycle, for $i=1,2,\ldots,k$, where $3 \leq k\leq n-1$ is the size of the cycle.

A pseudo code of the algorithm goes as follows:

1. Let $k\gets 3$

2. If $k==n$, then terminate the algorithm.

3. Let $C[i]\gets 1$ for $i=1,2,\ldots,k$

4. Check if $C=(C,C,\ldots,C[k],C)$ is a negative cycle in the graph. If so- print $C$

5. If $C=C=\ldots=C[k]=n$, then $k \gets k+1$ and go to step 2.

6. Otherwise, select $C[j]=\min_i C[i]$ (if there are multiple $j$-s with the minimal value, select the one with the minimal index), assign $C[j]\gets C[j]+1$ and go to step 4.