# Mincost Flow Graph algorithms Standard Algorithms n DFS

• Slides: 37

Min-cost Flow

Graph algorithms Standard Algorithms n DFS n BFS n Single source shortest path n All-pairs shortest path n Minimum spanning tree n Euler tour n Bipartite matching n Max-flow n Min-cost Flow

Min-cost flow problem n A lot like the max-flow problem, but with weights.

Min-cost problem n Input: A graph with edge capacities cij and weight wij and the packages that have to be moved n Output: The minimum cost flow fij satisfying the flow constraints w 13, c 13 1 3 w 01, c 01 w 35, c 35 w 43, c 43 w 12, c 12 s = 4 w 45, c 45 w 02, c 02 2 Flow constraints n 0 ≤ fij ≤ cij n Σk fki = Σk fik for all i ≠ s, t w 24, c 24 4 “flow is within capacity” “flow in = flow out” t = -4

Negative cycle p 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 p 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 p Augmenting the graph wij, cij i -3, 2 1 3 3, 2 -2, 2 2, 1 -4, 2 2, 2 1, 7 s = 4 -2, 2 2 -3, 2 3, 1 j 2, 1 4 t = -4

Negative cycle p Run a shortest path algorithm to find a negative cycle wij, cij -3, 2 1 3 3, 2 -2, 2 2, 1 2, 2 1, 7 s = 4 -4, 2 -2, 2 2 2+1+3+2 -4 -3 -2=-1 -3, 2 3, 1 2, 1 4 t = -4

Negative cycle p 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 2, 2 -1, 1 1, 6 -2, 3 -2, 2 -3, 3 2 4 t = -4

Negative cycle p Can’t find any negative cycles anymore wij, cij 1 3 3, 3 -2, 2 s = 4 -3, 1 -4, 1 2, 2 2, 1 -1, 1 1, 6 -2, 3 -2, 2 -3, 3 2 4 t = -4

Negative cycle p End result wij, cij 3, 4 1 1 3 1 2 2, 3 1, 7 1 s = 4 2, 2 2 2 4, 2 3, 3 3 4 t = -4 3 2, 3 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 p 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 2, 1 2, 2 1, 7 s = 4 -4, 2 -2, 2 2 -3, 2 3, 1 2, 1 4 t = -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 p Initialization wij, cij 3, 4 1 3 2, 3 4, 2 2, 2 1, 7 2, 3 2, 2 2 4 3, 3

Successive Shortest Path p Run a shortest path algorithm on the weight wij, cij 3, 4 1 3 2, 3 4, 2 2, 2 1, 7 2, 3 2, 2 2 4 3, 3

Successive Shortest Path p wij, cij 3, 4 1 3 2, 3 4, 2 2, 2 1, 7 2, 3 2, 2 2 4 3, 3

Successive Shortest Path p wij, cij 0, 4 1 3 0, 3 2, 2 1, 7 0, 3 0, 2 2 4 0, 3

Successive Shortest Path p Augment the paths wij, cij 0, 4 1 3 0, 3 2, 2 1, 7 0, 2 0, 1 0, 2 2 0, 1 4

Successive Shortest Path p Run a shortest path algorithm on the weight wij, cij 0, 4 1 3 0, 3 2, 2 1, 7 0, 2 0, 1 0, 2 2 0, 1 4

Successive Shortest Path p wij, cij 0, 4 1 3 0, 3 2, 2 1, 7 0, 2 0, 1 0, 2 2 0, 1 4

Successive Shortest Path p wij, cij 0, 4 1 3 0, 3 1, 2 2, 2 0, 7 0, 2 0, 1 0, 2 2 0, 1 4

Successive Shortest Path p Augment the paths wij, cij 0, 4 1 3 0, 1 1, 2 0, 1 2, 2 0, 6 0, 3 0, 2 0, 3 4 2

Successive Shortest Path p Run a shortest path algorithm wij, cij 0, 4 1 3 0, 1 1, 2 0, 1 2, 2 0, 6 0, 3 0, 2 0, 3 4 2

Successive Shortest Path p wij, cij 0, 4 1 3 0, 1 1, 2 0, 1 2, 2 0, 6 0, 3 0, 2 0, 3 4 2

Successive Shortest Path p wij, cij 0, 4 1 3 0, 1 0, 2 0, 1 2, 2 0, 6 0, 3 0, 2 0, 3 4 2

Successive Shortest Path p Augment the paths wij, cij 0, 3 1 0, 2 0, 1 3 0, 2 0, 1 2, 2 0, 6 0, 3 0, 2 0, 3 4 2

Successive Shortest Path p

Why does this work? p It finds the shortest path and send the maximum amount of flow through it p Augments the paths such that we can’t use the same route if the maximum flow for that path has been used p It repeats that process until it doesn’t need to send any flow

Running times p Running times: n Cycle cancelling: p Number of iterations O(V* U) p Dijkstra O((E+V) log V) p Total running time: O( U*V*(E+V) log V)) n Succesive shortest path: p Number of iterations O(K) p Dijkstra O((E+V) log V) p Total running time: O( K*(E+V) log V)) p U = highest supply p K = total supply

Minimum mean cost cycle p Find all negative cycles p Calculate the mean cost of each cycle by totalcostofcycle/#edges p Get the minimum mean cost of all cycles p Cost to find the minimum mean cost cycle O(EV) p A Theorem states that number of iterations is O(VE 2 log 2 V) p Total runningtime O(V 2 E 3 log 2 V)

Situations to use min-cost p Transportation problems n If this isn’t clear then you didn’t get anything from this presentation p Assignment problem n Assign workers to jobs paying at little as possible to get all the work done p Mail carrier problem n Find the minimum cost to visit all the nodes at least once.

Special Situations p What if a flow of 1 goes from b to a? b 4, 6 5, 3 a

Special Situations -5, 1 b 4, 6 5, 2 a

Special Situations p What would happen with negative edges? b Cost -r Capacity c a

Special Situations b Cost r Capacity c a