Negative cycle

Min-cost Flow
Graph algorithms
Standard Algorithms
 DFS
 BFS
 Single source shortest path
 All-pairs shortest path
 Minimum spanning tree
 Euler tour
 Bipartite matching
 Max-flow
 Min-cost Flow
Min-cost flow problem
Min-cost flow problem
 A lot like the max-flow problem, but with weights.
Min-cost problem
Min-cost problem
 Input: A graph with edge capacities cij and weight wij and the
packages that have to be moved
 Output: The minimum cost flow fij satisfying the flow constraints
w13, c13
1
3
w01, c01
w35,c35
w43,c43
w12,c12
s=4
w45,c45
w02, c02
2
Flow constraints
 0 ≤ fij ≤ cij
 Σk fki = Σk fik for all i ≠ s, t
w24, c24
4
“flow is within capacity”
“flow in = flow out”
t = -4
Negative cycle
 Lets run Max-flow
wij, cij
i
3,4
1
j
3
2,3
4,2
2,2
1,7
s=4
t = -4
2,3
2,2
2
3, 3
4
Negative cycle
 The max flow
3,4
1
3
2,3
4,2
2,2
1,7
s=4
t = -4
2,3
2,2
2
3, 3
4
Negative cycle
 Augmenting the graph
wij, cij
i
-3,2
1
3
3,2
-2,2
j
-4,2
2,1
2,2
1,7
s=4
t = -4
-2,2
-2,2
2
-3,2
3, 1
2,1
4
Negative cycle
 Run a shortest path algorithm to find a negative cycle
wij, cij
-3,2
1
3
3,2
-2,2
-4,2
2,1
2,2
1,7
s=4
t = -4
-2,2
-2,2
2
2+1+3+2-4-3-2=-1
-3,2
3, 1
2,1
4
Negative cycle
 Run a shortest path algorithm to find a negative cycle
wij, cij
1
s=4
3
3,3
-2,2
2,1
-3,1
-4,1
4,1
2,2
-1,1 1,6
-2,3
-2,2
-3,3
2
4
t = -4
Negative cycle
 Can’t find any negative cycles anymore
wij, cij
1
3
3,3
-2,2
s=4
-3,1
-4,1
4,1
2,2
2,1
-1,1 1,6
-2,3
-2,2
-3,3
2
4
t = -4
Negative cycle
 End result
wij, cij
3,4
1
1
3
1
2
2,3
1,7
1
s=4
2,2
4,2
2,2
t = -4
2,3
2
3
2
3,3
3
4
Total cost: 2*2+3*3+2*3+1*1+2*2+3*1+4*1=4+9+6+1+4+3+4=31
Negative cycle
 Pseudo code
run max-flow algorithm
2. While there are negative cycles
1. Run a shortest path algorithm from every source node untill you
find one with a negative cycle(Bellman-Ford)
2. Augment the path
1.
Why does this work
wij, cij
-3,2
1
3
3,2
-2,2
-4,2
2,1
2,2
1,7
s=4
t = -4
-2,2
-2,2
2
-3,2
3, 1
2,1
4
Successive Shortest Path
wij, cij
3,4
1
3
2,3
4,2
2,2
1,7
s=4
t = -4
2,3
2,2
2
3, 3
4
Successive Shortest Path
 Initialization
wij, cij
e(1)=0
𝜋(1)=0
3,4
1
e(3)=0
𝜋(3)=0
3
2,3
e(s)=4
𝜋(s)=0
4,2
e(t)=-4
𝜋(t)=0
2,2
1,7
2,3
2,2
2
e(2)=0
𝜋(2)=0
3, 3
4
e(4)=0
𝜋(4)=0
Successive Shortest Path
 Run a shortest path algorithm on the weight
wij, cij
e(1)=0
𝜋(1)=0
3,4
1
e(3)=0
𝜋(3)=0
3
2,3
e(s)=4
𝜋(s)=0
4,2
e(t)=-4
𝜋(t)=0
2,2
1,7
2,3
2,2
2
e(2)=0
𝜋(2)=0
3, 3
4
e(4)=0
𝜋(4)=0
Successive Shortest Path
 Update the 𝜋(j) for all nodes
wij, cij
e(1)=0
𝜋(1)=-2
3,4
1
e(3)=0
𝜋(3)=-5
3
2,3
e(s)=4
𝜋(s)=0
4,2
e(t)=-4
𝜋(t)=-7
2,2
1,7
2,3
2,2
2
e(2)=0
𝜋(2)=-2
3, 3
4
e(4)=0
𝜋(4)=-5
Successive Shortest Path
 Update the edges such that
wij, cij
the new cost of edge xij = - 𝜋(i) + 𝜋(j) + old cost of edge Xij
e(1)=0
𝜋(1)=-2
0,4
1
e(4)=0
𝜋(4)=-5
3
0,3
e(s)=4
𝜋(s)=0
2,2
e(t)=-4
𝜋(t)=-7
2,2
1,7
0,3
0,2
2
e(2)=0
𝜋(2)=-2
0, 3
4
e(3)=0
𝜋(3)=-5
Successive Shortest Path
 Augment the paths
wij, cij
e(1)=0
𝜋(1)=-2
0,4
1
e(3)=0
𝜋(3)=-5
3
0,3
e(s)=2
𝜋(s)=0
2,2
e(t)=-2
𝜋(t)=-7
2,2
1,7
0,2
0,2
0,1
0, 2
2
e(2)=0
𝜋(2)=-2
0, 1
4
e(4)=0
𝜋(4)=-5
Successive Shortest Path
 Run a shortest path algorithm on the weight
e(1)=0
𝜋(1)=-2
0,4
1
wij, cij
e(3)=0
𝜋(3)=-5
3
0,3
e(s)=2
𝜋(s)=0
2,2
e(t)=-2
𝜋(t)=-7
2,2
1,7
0,2
0,2
0,1
0, 2
2
e(2)=0
𝜋(2)=-2
0, 1
4
e(4)=0
𝜋(4)=-5
Successive Shortest Path
 Update the 𝜋(j) for all nodes
wij, cij
e(1)=0
𝜋(1)=-2
0,4
1
e(3)=0
𝜋(3)=-5
3
0,3
e(s)=2
𝜋(s)=0
2,2
e(t)=-2
𝜋(t)=-8
2,2
1,7
0,2
0,2
0,1
0, 2
2
e(2)=0
𝜋(2)=-3
0, 1
4
e(4)=0
𝜋(4)=-6
Successive Shortest Path
 Update the edges such that
wij, cij
the new cost of edge xij = - 𝜋(i) + 𝜋(j) + old cost of edge xij
e(1)=0
𝜋(1)=-2
0,4
1
e(3)=0
𝜋(3)=-5
3
0,3
e(s)=2
𝜋(s)=0
1,2
e(t)=-2
𝜋(t)=-8
2,2
0,7
0,2
0,2
0,1
0, 2
2
e(2)=0
𝜋(2)=-3
0, 1
4
e(4)=0
𝜋(4)=-6
Successive Shortest Path
 Augment the paths
wij, cij
e(1)=0
𝜋(1)=-2
0,4
1
e(3)=0
𝜋(3)=-5
3
0,1
1,2
0,2
e(s)=1
𝜋(s)=0
0,1
2,2
0,6
0,3
0,2
0, 3
2
e(2)=0
𝜋(2)=-3
4
e(4)=0
𝜋(4)=-6
e(t)=-1
𝜋(t)=-8
Successive Shortest Path
 Run a shortest path algorithm
wij, cij
e(1)=0
𝜋(1)=-2
0,4
1
e(3)=0
𝜋(3)=-5
3
0,1
1,2
0,2
e(s)=1
𝜋(s)=0
0,1
2,2
0,6
0,3
0,2
0, 3
2
e(2)=0
𝜋(2)=-3
4
e(4)=0
𝜋(4)=-6
e(t)=-1
𝜋(t)=-8
Successive Shortest Path
 Update the 𝜋(j) for all nodes
wij, cij
e(1)=0
𝜋(1)=-2
0,4
1
e(3)=0
𝜋(3)=-5
3
0,1
1,2
0,2
e(s)=1
𝜋(s)=0
0,1
2,2
0,6
0,3
0,2
0, 3
2
e(2)=0
𝜋(2)=-3
4
e(4)=0
𝜋(4)=-6
e(t)=-1
𝜋(t)=-9
Successive Shortest Path
 Update the edges such that
wij, cij
the new cost of edge xij = - 𝜋(i) + 𝜋(j) + old cost of edge Xij
e(1)=0
𝜋(1)=-2
0,4
1
e(3)=0
𝜋(3)=-5
3
0,1
0,2
0,2
e(s)=1
𝜋(s)=0
0,1
2,2
0,6
0,3
0,2
0, 3
2
e(2)=0
𝜋(2)=-3
4
e(4)=0
𝜋(4)=-6
e(t)=-1
𝜋(t)=-9
Successive Shortest Path
 Augment the paths
wij, cij
e(1)=0
𝜋(1)=-2
0,3
1
0,1
0,2
0,2
0,1
0,1
e(s)=0
𝜋(s)=0
e(3)=0
𝜋(3)=-5
3
0,1
2,2
0,6
0,3
0,2
0, 3
2
e(2)=0
𝜋(2)=-3
4
e(4)=0
𝜋(4)=-6
e(t)=0
𝜋(t)=-9
Successive Shortest Path
1.
While list of endpoints and list of sources is not empty
1. Find the shortest path
2. Update the 𝜋(j) for all nodes
3. Update all the edges such that “the new cost of edge xij = - 𝜋(i) +
𝜋(j) + old cost of edge xij ”
4. Augment all the paths
Why does this work?
 It finds the shortest path and send the maximum amount of flow
through it
 Augments the paths such that we can’t use the same route if the
maximum flow for that path has been used
 It repeats that process until it doesn’t need to send any flow
Running times
 Running times:
 Cycle cancelling:
 Number of iterations O(V* U)
 Dijkstra O((E+V) log V)
 Total running time: O( U*V*(E+V) log V))
 Succesive shortest path:
 Number of iterations O(K)
 Dijkstra O((E+V) log V)
 Total running time: O( K*(E+V) log V))
 U = highest supply
 K = total supply
Minimum mean cost cycle
 Find all negative cycles
 Calculate the mean cost of each cycle by totalcostofcycle/#edges
 Get the minimum mean cost of all cycles
 Cost to find the minimum mean cost cycle O(EV)
 A Theorem states that number of iterations is O(VE2 log2 V)
 Total runningtime O(V2E3 log2 V)
Situations to use min-cost
 Transportation problems
 If this isn’t clear then you didn’t get anything from this presentation
 Assignment problem
 Assign workers to jobs paying at little as possible to get all the work
done
 Mail carrier problem
 Find the minimum cost to visit all the nodes at least once.
Special Situations
 What if a flow of 1 goes from b to a?
4, 6
b
a
5,3
Special Situations
-5, 1
4, 6
b
a
5, 2
Special Situations
 What would happen with negative edges?
Cost -r
b
Capacity c
a
Special Situations
Cost r
b
Capacity c
a