Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CS253: Network Flows (2019)

Jinho D. Choi
November 18, 2019
140

CS253: Network Flows (2019)

Jinho D. Choi

November 18, 2019
Tweet

Transcript

  1. Network Flow 2 S 1 2 3 4 T Each

    edge e is associated with a capacity c.
  2. Network Flow 2 S 1 2 3 4 T 4

    2 3 1 2 3 4 2 Each edge e is associated with a capacity c.
  3. Network Flow 2 S 1 2 3 4 T 4

    2 3 1 2 3 4 2 Each edge e is associated with a capacity c. Push as much flow f as possible from S to T.
  4. Network Flow 2 S 1 2 3 4 T 4

    2 3 1 2 3 4 2 Each edge e is associated with a capacity c. Push as much flow f as possible from S to T.
  5. Network Flow 2 S 1 2 3 4 T 4

    2 3 1 2 3 4 2 Each edge e is associated with a capacity c. Push as much flow f as possible from S to T. f(e) ≤ c(e)
  6. Network Flow 2 S 1 2 3 4 T 4

    2 3 1 2 3 4 2 Each edge e is associated with a capacity c. Push as much flow f as possible from S to T. f(e) ≤ c(e) Σu f(u, v) = Σw f(v, w), where v ∉ {S, T}
  7. Network Flow 2 S 1 2 3 4 T 4

    2 3 1 2 3 4 2 Each edge e is associated with a capacity c. Push as much flow f as possible from S to T. f(e) ≤ c(e) Σu f(u, v) = Σw f(v, w), where v ∉ {S, T} Maximum flow?
  8. Network Flow 2 S 1 2 3 4 T 4

    2 3 1 2 3 4 2 Each edge e is associated with a capacity c. Push as much flow f as possible from S to T. f(e) ≤ c(e) Σu f(u, v) = Σw f(v, w), where v ∉ {S, T} 3 Maximum flow?
  9. Network Flow 2 S 1 2 3 4 T 4

    2 3 1 2 3 4 2 Each edge e is associated with a capacity c. Push as much flow f as possible from S to T. f(e) ≤ c(e) Σu f(u, v) = Σw f(v, w), where v ∉ {S, T} 3 3 Maximum flow?
  10. Network Flow 2 S 1 2 3 4 T 4

    2 3 1 2 3 4 2 Each edge e is associated with a capacity c. Push as much flow f as possible from S to T. f(e) ≤ c(e) Σu f(u, v) = Σw f(v, w), where v ∉ {S, T} 3 3 1 2 Maximum flow?
  11. Network Flow 2 S 1 2 3 4 T 4

    2 3 1 2 3 4 2 Each edge e is associated with a capacity c. Push as much flow f as possible from S to T. f(e) ≤ c(e) Σu f(u, v) = Σw f(v, w), where v ∉ {S, T} 3 3 1 2 2 Maximum flow?
  12. Network Flow 2 S 1 2 3 4 T 4

    2 3 1 2 3 4 2 Each edge e is associated with a capacity c. Push as much flow f as possible from S to T. f(e) ≤ c(e) Σu f(u, v) = Σw f(v, w), where v ∉ {S, T} 3 3 1 2 2 1+2 Maximum flow?
  13. Network Flow 2 S 1 2 3 4 T 4

    2 3 1 2 3 4 2 Each edge e is associated with a capacity c. Push as much flow f as possible from S to T. f(e) ≤ c(e) Σu f(u, v) = Σw f(v, w), where v ∉ {S, T} 3 3 1 2 2 1+2 3 Maximum flow?
  14. Network Flow 2 S 1 2 3 4 T 4

    2 3 1 2 3 4 2 Each edge e is associated with a capacity c. Push as much flow f as possible from S to T. f(e) ≤ c(e) Σu f(u, v) = Σw f(v, w), where v ∉ {S, T} 3 3 1 2 2 1+2 3 Maximum flow? Optimum?
  15. 4 4 2 Ford-Fulkerson Algorithm 3 S 1 2 3

    4 T 2 3 1 2 3 Find a path p from S to T, where ∀e p. r(e) = c(e) - f(e) > 0.
  16. 4 4 2 Ford-Fulkerson Algorithm 3 S 1 2 3

    4 T 2 3 1 2 3 Find a path p from S to T, where ∀e p. r(e) = c(e) - f(e) > 0.
  17. 4 4 2 Ford-Fulkerson Algorithm 3 S 1 2 3

    4 T 2 3 1 2 3 Find a path p from S to T, where ∀e p. r(e) = c(e) - f(e) > 0. ∀e p. c(e) = c(e) - min(f(p)).
  18. 4 4 2 Ford-Fulkerson Algorithm 3 S 1 2 3

    4 T 2 3 1 2 3 Find a path p from S to T, where ∀e p. r(e) = c(e) - f(e) > 0. ∀e p. c(e) = c(e) - min(f(p)).
  19. 4 4 2 2 0 Ford-Fulkerson Algorithm 3 S 1

    2 3 4 T 2 3 1 2 3 1 Find a path p from S to T, where ∀e p. r(e) = c(e) - f(e) > 0. ∀e p. c(e) = c(e) - min(f(p)).
  20. 4 4 2 2 0 Ford-Fulkerson Algorithm 3 S 1

    2 3 4 T 2 3 1 2 3 1 Find a path p from S to T, where ∀e p. r(e) = c(e) - f(e) > 0. ∀e p. c(e) = c(e) - min(f(p)). MaxFlow = MaxFlow + min(f(p)).
  21. 4 4 2 2 0 Ford-Fulkerson Algorithm 3 S 1

    2 3 4 T 2 3 1 2 3 1 Find a path p from S to T, where ∀e p. r(e) = c(e) - f(e) > 0. ∀e p. c(e) = c(e) - min(f(p)). MaxFlow = MaxFlow + min(f(p)). 2
  22. 4 4 2 2 0 Ford-Fulkerson Algorithm 3 S 1

    2 3 4 T 2 3 1 2 3 1 Find a path p from S to T, where ∀e p. r(e) = c(e) - f(e) > 0. ∀e p. c(e) = c(e) - min(f(p)). MaxFlow = MaxFlow + min(f(p)). 2
  23. 4 4 2 2 0 Ford-Fulkerson Algorithm 3 S 1

    2 3 4 T 2 3 1 2 3 1 Find a path p from S to T, where ∀e p. r(e) = c(e) - f(e) > 0. ∀e p. c(e) = c(e) - min(f(p)). MaxFlow = MaxFlow + min(f(p)). 2
  24. 4 4 2 2 0 Ford-Fulkerson Algorithm 3 S 1

    2 3 4 T 2 3 1 2 3 1 Find a path p from S to T, where ∀e p. r(e) = c(e) - f(e) > 0. ∀e p. c(e) = c(e) - min(f(p)). MaxFlow = MaxFlow + min(f(p)). 2
  25. 4 3 4 2 2 0 1 Ford-Fulkerson Algorithm 3

    S 1 2 3 4 T 2 3 1 2 3 1 Find a path p from S to T, where ∀e p. r(e) = c(e) - f(e) > 0. ∀e p. c(e) = c(e) - min(f(p)). MaxFlow = MaxFlow + min(f(p)). 2 0 0 2
  26. 4 3 4 2 2 0 1 Ford-Fulkerson Algorithm 3

    S 1 2 3 4 T 2 3 1 2 3 1 Find a path p from S to T, where ∀e p. r(e) = c(e) - f(e) > 0. ∀e p. c(e) = c(e) - min(f(p)). MaxFlow = MaxFlow + min(f(p)). 2 0 0 2 1
  27. 4 3 4 2 2 0 1 Ford-Fulkerson Algorithm 3

    S 1 2 3 4 T 2 3 1 2 3 1 Find a path p from S to T, where ∀e p. r(e) = c(e) - f(e) > 0. ∀e p. c(e) = c(e) - min(f(p)). MaxFlow = MaxFlow + min(f(p)). 2 0 0 2 1
  28. 4 3 4 2 2 0 1 Ford-Fulkerson Algorithm 3

    S 1 2 3 4 T 2 3 1 2 3 1 Find a path p from S to T, where ∀e p. r(e) = c(e) - f(e) > 0. ∀e p. c(e) = c(e) - min(f(p)). MaxFlow = MaxFlow + min(f(p)). 2 0 0 2 1
  29. 4 3 4 2 2 0 1 Ford-Fulkerson Algorithm 3

    S 1 2 3 4 T 2 3 1 2 3 1 Find a path p from S to T, where ∀e p. r(e) = c(e) - f(e) > 0. ∀e p. c(e) = c(e) - min(f(p)). MaxFlow = MaxFlow + min(f(p)). 2 0 0 2 1
  30. 4 3 1 4 2 2 0 1 Ford-Fulkerson Algorithm

    3 S 1 2 3 4 T 2 3 1 2 3 1 Find a path p from S to T, where ∀e p. r(e) = c(e) - f(e) > 0. ∀e p. c(e) = c(e) - min(f(p)). MaxFlow = MaxFlow + min(f(p)). 2 0 0 2 1 0 0
  31. 4 3 1 4 2 2 0 1 Ford-Fulkerson Algorithm

    3 S 1 2 3 4 T 2 3 1 2 3 1 Find a path p from S to T, where ∀e p. r(e) = c(e) - f(e) > 0. ∀e p. c(e) = c(e) - min(f(p)). MaxFlow = MaxFlow + min(f(p)). 2 0 0 2 1 0 0 2
  32. 4 3 1 4 2 2 0 1 Ford-Fulkerson Algorithm

    3 S 1 2 3 4 T 2 3 1 2 3 1 Find a path p from S to T, where ∀e p. r(e) = c(e) - f(e) > 0. ∀e p. c(e) = c(e) - min(f(p)). MaxFlow = MaxFlow + min(f(p)). 2 0 0 2 1 0 0 2
  33. 4 3 1 4 2 2 0 1 Ford-Fulkerson Algorithm

    3 S 1 2 3 4 T 2 3 1 2 3 1 Find a path p from S to T, where ∀e p. r(e) = c(e) - f(e) > 0. ∀e p. c(e) = c(e) - min(f(p)). MaxFlow = MaxFlow + min(f(p)). 2 0 0 2 1 0 0 2 No more augmenting path!
  34. MaxFlow Class 4 public class MaxFlow { private Map<Edge,Double> m_flows;

    private double d_maxFlow; public MaxFlow(Graph graph) { init(graph); } public void init(Graph graph) { m_flows = new HashMap<>(); d_maxFlow = 0; for (Edge edge : graph.getAllEdges()) m_flows.put(edge, 0d); } }
  35. MaxFlow Class 4 public class MaxFlow { private Map<Edge,Double> m_flows;

    private double d_maxFlow; public MaxFlow(Graph graph) { init(graph); } public void init(Graph graph) { m_flows = new HashMap<>(); d_maxFlow = 0; for (Edge edge : graph.getAllEdges()) m_flows.put(edge, 0d); } }
  36. MaxFlow Class 5 public void updateResidual(List<Edge> path, double flow) {

    for (Edge edge : path) updateResidual(edge, flow); d_maxFlow += flow; } public void updateResidual(Edge edge, double flow) { Double prev = m_flows.get(edge); if (prev == null) prev = 0d; m_flows.put(edge, prev + flow); } public double getResidual(Edge edge) { return edge.getWeight() - m_flows.get(edge); } public double getMaxFlow() { return d_maxFlow; }
  37. MaxFlow Class 5 public void updateResidual(List<Edge> path, double flow) {

    for (Edge edge : path) updateResidual(edge, flow); d_maxFlow += flow; } public void updateResidual(Edge edge, double flow) { Double prev = m_flows.get(edge); if (prev == null) prev = 0d; m_flows.put(edge, prev + flow); } public double getResidual(Edge edge) { return edge.getWeight() - m_flows.get(edge); } public double getMaxFlow() { return d_maxFlow; }
  38. MaxFlow Class 5 public void updateResidual(List<Edge> path, double flow) {

    for (Edge edge : path) updateResidual(edge, flow); d_maxFlow += flow; } public void updateResidual(Edge edge, double flow) { Double prev = m_flows.get(edge); if (prev == null) prev = 0d; m_flows.put(edge, prev + flow); } public double getResidual(Edge edge) { return edge.getWeight() - m_flows.get(edge); } public double getMaxFlow() { return d_maxFlow; }
  39. FordFulkerson Class 6 private Subgraph getAugmentingPath(Graph graph, MaxFlow mf, 


    Subgraph sub, int source, int target) { if (source == target) return sub; Subgraph tmp; for (Edge edge : graph.getIncomingEdges(target)) { if (sub.contains(edge.getSource())) continue; // cycle if (mf.getResidual(edge) <= 0) continue; // no residual tmp = new Subgraph(sub); tmp.addEdge(edge); tmp = getAugmentingPath(graph, mf, tmp, source, edge.getSource()); if (tmp != null) return tmp; } return null; }
  40. FordFulkerson Class 6 private Subgraph getAugmentingPath(Graph graph, MaxFlow mf, 


    Subgraph sub, int source, int target) { if (source == target) return sub; Subgraph tmp; for (Edge edge : graph.getIncomingEdges(target)) { if (sub.contains(edge.getSource())) continue; // cycle if (mf.getResidual(edge) <= 0) continue; // no residual tmp = new Subgraph(sub); tmp.addEdge(edge); tmp = getAugmentingPath(graph, mf, tmp, source, edge.getSource()); if (tmp != null) return tmp; } return null; } Complexity?
  41. FordFulkerson Class 7 public MaxFlow getMaximumFlow(Graph graph, int source, int

    target) { MaxFlow mf = new MaxFlow(graph); Subgraph sub = new Subgraph(); double min; while ((sub = getAugmentingPath(graph, mf, sub, source, target)) != null) { min = getMin(mf, sub.getEdges()); mf.updateResidual(sub.getEdges(), min); } return mf; }
  42. FordFulkerson Class 7 public MaxFlow getMaximumFlow(Graph graph, int source, int

    target) { MaxFlow mf = new MaxFlow(graph); Subgraph sub = new Subgraph(); double min; while ((sub = getAugmentingPath(graph, mf, sub, source, target)) != null) { min = getMin(mf, sub.getEdges()); mf.updateResidual(sub.getEdges(), min); } return mf; }
  43. FordFulkerson Class 7 public MaxFlow getMaximumFlow(Graph graph, int source, int

    target) { MaxFlow mf = new MaxFlow(graph); Subgraph sub = new Subgraph(); double min; while ((sub = getAugmentingPath(graph, mf, sub, source, target)) != null) { min = getMin(mf, sub.getEdges()); mf.updateResidual(sub.getEdges(), min); } return mf; } private double getMin(MaxFlow mf, List<Edge> path) { double min = mf.getResidual(path.get(0)); for (int i=1; i<path.size(); i++) min = Math.min(min, mf.getResidual(path.get(i))); return min; }
  44. 2 2 2 2 1 1 1 Ford-Fulkerson: Backward Pushing

    9 S 1 2 T 1 0 1 1 1 1 2 1 1 0
  45. 2 2 2 2 1 1 1 Ford-Fulkerson: Backward Pushing

    9 S 1 2 T 1 0 1 1 1 1 2 1 1 0
  46. 2 2 2 2 1 1 1 Ford-Fulkerson: Backward Pushing

    9 S 1 2 T 1 0 1 1 1 1 2 1 1 0
  47. 2 2 2 2 1 1 1 Ford-Fulkerson: Backward Pushing

    9 S 1 2 T 1 0 1 1 1 0 1 2 1 1 0 1 0
  48. 2 2 2 2 1 1 1 Ford-Fulkerson: Backward Pushing

    9 S 1 2 T 1 0 1 1 1 0 1 2 1 1 0 1 0 1
  49. 2 2 2 2 1 1 1 Ford-Fulkerson: Backward Pushing

    9 S 1 2 T 1 0 1 1 1 0 1 1 2 1 1 0 1 0 1 1 2
  50. 2 2 2 2 1 1 1 Ford-Fulkerson: Backward Pushing

    9 S 1 2 T 1 0 1 1 1 0 1 1 2 1 1 0 1 0 1 1 2
  51. 2 2 2 2 1 1 1 Ford-Fulkerson: Backward Pushing

    9 S 1 2 T 1 0 1 1 1 0 1 1 2 1 1 0 1 0 1 1 2
  52. 2 2 2 2 1 1 1 Ford-Fulkerson: Backward Pushing

    9 S 1 2 T 1 0 1 1 1 0 1 1 2 1 1 0 1 0 1 1 2 0 0 2 2
  53. 2 2 2 2 1 1 1 Ford-Fulkerson: Backward Pushing

    9 S 1 2 T 1 0 1 1 1 0 1 1 2 1 1 0 1 0 1 1 2 0 0 2 2 1
  54. 2 2 2 2 1 1 1 Ford-Fulkerson: Backward Pushing

    9 S 1 2 T 1 0 1 1 1 0 1 1 2 1 1 0 1 0 1 1 2 0 0 2 2 1
  55. protected void updateBackward(Graph graph, Subgraph sub, MaxFlow mf, double min)

    { boolean found; for (Edge edge : sub.getEdges()) { found = false; for (Edge rEdge : graph.getIncomingEdges(edge.getSource())) { if (rEdge.getSource() == edge.getTarget()) { mf.updateResidual(rEdge, -min); found = true; break; } } if (!found) { Edge rEdge = graph.setDirectedEdge
 (edge.getTarget(), edge.getSource(), edge.getWeight()); mf.updateResidual(rEdge, -min); } } } 10
  56. protected void updateBackward(Graph graph, Subgraph sub, MaxFlow mf, double min)

    { boolean found; for (Edge edge : sub.getEdges()) { found = false; for (Edge rEdge : graph.getIncomingEdges(edge.getSource())) { if (rEdge.getSource() == edge.getTarget()) { mf.updateResidual(rEdge, -min); found = true; break; } } if (!found) { Edge rEdge = graph.setDirectedEdge
 (edge.getTarget(), edge.getSource(), edge.getWeight()); mf.updateResidual(rEdge, -min); } } } 10
  57. protected void updateBackward(Graph graph, Subgraph sub, MaxFlow mf, double min)

    { boolean found; for (Edge edge : sub.getEdges()) { found = false; for (Edge rEdge : graph.getIncomingEdges(edge.getSource())) { if (rEdge.getSource() == edge.getTarget()) { mf.updateResidual(rEdge, -min); found = true; break; } } if (!found) { Edge rEdge = graph.setDirectedEdge
 (edge.getTarget(), edge.getSource(), edge.getWeight()); mf.updateResidual(rEdge, -min); } } } 10
  58. Max-Flow Min-Cut Theorem 11 • S-T cut - A set

    of edges whose removal disconnects S to T.
  59. Max-Flow Min-Cut Theorem 11 • S-T cut - A set

    of edges whose removal disconnects S to T. - The capacity of a cut = Σ c(e), ∀e in the cut.
  60. Max-Flow Min-Cut Theorem 11 S 1 2 3 4 T

    4 2 3 1 2 3 4 2 • S-T cut - A set of edges whose removal disconnects S to T. - The capacity of a cut = Σ c(e), ∀e in the cut.
  61. Max-Flow Min-Cut Theorem 11 S 1 2 3 4 T

    4 2 3 1 2 3 4 2 Minimum cut • S-T cut - A set of edges whose removal disconnects S to T. - The capacity of a cut = Σ c(e), ∀e in the cut.
  62. Max-Flow Min-Cut Theorem 11 S 1 2 3 4 T

    4 2 3 1 2 3 4 2 Minimum cut • S-T cut - A set of edges whose removal disconnects S to T. - The capacity of a cut = Σ c(e), ∀e in the cut.
  63. Max-Flow Min-Cut Theorem 11 S 1 2 3 4 T

    4 2 3 1 2 3 4 2 vs. Maximum flow Minimum cut • S-T cut - A set of edges whose removal disconnects S to T. - The capacity of a cut = Σ c(e), ∀e in the cut.
  64. Max-Flow Min-Cut Theorem 11 S 1 2 3 4 T

    4 2 3 1 2 3 4 2 3 3 1 2 2 3 3 vs. Maximum flow Minimum cut • S-T cut - A set of edges whose removal disconnects S to T. - The capacity of a cut = Σ c(e), ∀e in the cut.
  65. Finding Min-Cut 12 4 4 2 2 0 S 1

    2 3 4 T 2 3 1 2 3 1
  66. Finding Min-Cut 12 4 4 2 2 0 S 1

    2 3 4 T 2 3 1 2 3 1
  67. Finding Min-Cut 12 4 4 2 2 0 S 1

    2 3 4 T 2 3 1 2 3 1
  68. Finding Min-Cut 12 4 4 2 2 0 S 1

    2 3 4 T 2 3 1 2 3 1
  69. Finding Min-Cut 12 4 3 4 2 2 0 1

    S 1 2 3 4 T 2 3 1 2 3 1 0 0 2
  70. Finding Min-Cut 12 4 3 4 2 2 0 1

    S 1 2 3 4 T 2 3 1 2 3 1 0 0 2
  71. Finding Min-Cut 12 4 3 4 2 2 0 1

    S 1 2 3 4 T 2 3 1 2 3 1 0 0 2
  72. Finding Min-Cut 12 4 3 4 2 2 0 1

    S 1 2 3 4 T 2 3 1 2 3 1 0 0 2
  73. Finding Min-Cut 12 4 3 1 4 2 2 0

    1 S 1 2 3 4 T 2 3 1 2 3 1 0 0 2 0 0
  74. Finding Min-Cut 12 4 3 1 4 2 2 0

    1 S 1 2 3 4 T 2 3 1 2 3 1 0 0 2 0 0
  75. Finding Min-Cut 12 4 3 1 4 2 2 0

    1 S 1 2 3 4 T 2 3 1 2 3 1 0 0 2 0 0 • Algorithm
  76. Finding Min-Cut 12 4 3 1 4 2 2 0

    1 S 1 2 3 4 T 2 3 1 2 3 1 0 0 2 0 0 • Algorithm 1. Find a path p from S to T, remove any edge e in p, and put e to a set C.
  77. Finding Min-Cut 12 4 3 1 4 2 2 0

    1 S 1 2 3 4 T 2 3 1 2 3 1 0 0 2 0 0 • Algorithm 1. Find a path p from S to T, remove any edge e in p, and put e to a set C. 2. Repeat #1 until no path is found, and return C.
  78. Finding Min-Cut 12 4 3 1 4 2 2 0

    1 S 1 2 3 4 T 2 3 1 2 3 1 0 0 2 0 0 • Algorithm 1. Find a path p from S to T, remove any edge e in p, and put e to a set C. 2. Repeat #1 until no path is found, and return C. • How can we find a min-cut?
  79. Finding Min-Cut 12 4 3 1 4 2 2 0

    1 S 1 2 3 4 T 2 3 1 2 3 1 0 0 2 0 0 • Algorithm 1. Find a path p from S to T, remove any edge e in p, and put e to a set C. 2. Repeat #1 until no path is found, and return C. • How can we find a min-cut? - Instead of removing any edge in #1, remove an edge with the minimum original (not residual) weight.
  80. Min-Cut vs. Max-Flow 13 • Lemma 1 - There is

    no extra edge in the min-cut.
  81. Min-Cut vs. Max-Flow 13 • Lemma 1 - There is

    no extra edge in the min-cut. - Prove?
  82. Min-Cut vs. Max-Flow 13 • Lemma 1 - There is

    no extra edge in the min-cut. - Prove? • Lemma 2
  83. Min-Cut vs. Max-Flow 13 • Lemma 1 - There is

    no extra edge in the min-cut. - Prove? • Lemma 2 - All edges in the min-cut must be parts of augmenting paths.
  84. Min-Cut vs. Max-Flow 13 • Lemma 1 - There is

    no extra edge in the min-cut. - Prove? • Lemma 2 - All edges in the min-cut must be parts of augmenting paths. - Prove?
  85. Min-Cut vs. Max-Flow 13 • Lemma 1 - There is

    no extra edge in the min-cut. - Prove? • Lemma 2 - All edges in the min-cut must be parts of augmenting paths. - Prove? • Lemma 3
  86. Min-Cut vs. Max-Flow 13 • Lemma 1 - There is

    no extra edge in the min-cut. - Prove? • Lemma 2 - All edges in the min-cut must be parts of augmenting paths. - Prove? • Lemma 3 - Each edge in the min-cut is the minimum weighted edge in each augmenting path.
  87. Min-Cut vs. Max-Flow 13 • Lemma 1 - There is

    no extra edge in the min-cut. - Prove? • Lemma 2 - All edges in the min-cut must be parts of augmenting paths. - Prove? • Lemma 3 - Each edge in the min-cut is the minimum weighted edge in each augmenting path. - Prove?