design and analysis of algorithms (daa 2018)...master’s programme in computer science juha...

26
Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018) 17/09/2018 1 Design and Analysis of Algorithms 2018 week 3

Upload: others

Post on 23-Mar-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)...Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

Master’s Programme in Computer Science

Juha Kärkkäinen

Based on slides by Veli Mäkinen

DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

17/09/2018 1 Design and Analysis of Algorithms 2018 week 3

Page 2: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)...Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

Shortest paths and network flows

Week 3

Page 3: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)...Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

Shortest paths

Some dynamic programming you might already have seen

Page 4: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)...Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

Master’s Programme in Computer Science

RECURRENCES WITH REPEATING SUBSTRUCTURES

• Example: Fibonacci numbers

• Defined by recurrence: F(i) = F(i-2) + F(i-1) and F(0)=F(1)=1

• Recursive computation is slow (O(F(n)) time): F(i) = F(i-2)+F(i-1) = F(i-4)+F(i-3)+F(i-3)+F(i-2) = … because same values computed repeatedly.

• Memoization (storing computed values) is faster (O(n) time). Proper evaluation order helps F(0)=F(1)=1, F(2)=F(0)+F(1), F(3)=F(1)+F(2), …

• In general, a proper evaluation order is a topological order in a directed acyclic graph (DAG) denoting the recurrence dependencies:

17/09/2018 Design and Analysis of Algorithms 2018 week 3 4

1 1 2 3 5 …

Page 5: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)...Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

Master’s Programme in Computer Science

SHORTEST S-T PATH IN A DAG Find shortest path from s to t in DAG G=(V,E)

• Base case: d(s) = 0

• Recurrence: d(v) = min{d(u)+c(u,v) : u is an in-neighbor of v}

• Output: d(t)

• Topological sort, compute each d(v) in |N-(v)| time, total O(|V|+|E|) time, where V and E are the set of vertices and edges of the DAG, respectively.

• Traceback to output an optimal path

17/09/2018 Design and Analysis of Algorithms 2018 week 3 5

s t

v u

N-(v)

… …

Page 6: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)...Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

Master’s Programme in Computer Science

SHORTEST PATH IN A DAG (EXAMPLE)

17/09/2018 Design and Analysis of Algorithms 2018 week 3 6

Topological sort

5

10

12

5

10

21

10

5 4 5

12

3

11

1. Topological sort

Page 7: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)...Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

Master’s Programme in Computer Science

SHORTEST PATH IN A DAG (EXAMPLE)

17/09/2018 Design and Analysis of Algorithms 2018 week 3 7

Topological sort

5

10

12

5

10

21

10

5 4 5

12

3

11

0 10 15 19 24 26

1. Topological sort

2. Compute distances

(from left to right)

Page 8: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)...Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

Master’s Programme in Computer Science

SHORTEST PATH IN A DAG (EXAMPLE)

17/09/2018 Design and Analysis of Algorithms 2018 week 3 8

Topological sort

5

10

12

5

10

21

10

5 4 5

12

3

11

0 10 15 19 24 26

1. Topological sort

2. Compute distances

3. Traceback

(from right to left)

Page 9: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)...Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

Master’s Programme in Computer Science

SHORTEST PATH IN A DAG (EXAMPLE)

17/09/2018 Design and Analysis of Algorithms 2018 week 3 10

Topological sort

5

10

12

5

10

21

10

5 4 5

12

3

11

26

Page 10: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)...Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

Master’s Programme in Computer Science

SHORTEST S-T PATH IN A GENERAL GRAPH

• General directed graph

• May have cycles

• May have negative edge weights

• But no negative cycles (why?)

• Cannot use Dijkstra’s algorithms because of negative weights

• The standard algorithm is Bellman-Ford (Section 24.1 in book)

• Recurrence: d(v) = min{d(u,v) + c(u,v) : u in N-(v)}

• No trivial topological order because of cycles

18/09/2018 Design and Analysis of Algorithms 2018 week 3 11

Page 11: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)...Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

Master’s Programme in Computer Science

DERIVING BELLMAN-FORD 1

• Make n=|V| copies of the graph and redirect all edges from copy (i-1) to copy i for i in [1..n-1], like in example below:

• In the resulting DAG find shortest path from any s to t

17/09/2018 Design and Analysis of Algorithms 2018 week 3 12

s t

0 1 2

s t

3

s s

Page 12: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)...Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

Master’s Programme in Computer Science

DERIVING BELLMAN-FORD 2

Recurrence for Bellman-Ford

• Base case: d(s,k)=0, for all k, d(v,0)=∞ when v≠s

• Recurrence: d(v,k) = min{d(u,k-1) + c(u,v) : u in N-(v)}

• k = the number of the graph copy

• k = maximum number of edges allowed on the path from s to v

• Output: d(t) = d(t,|V|-1)

• Topological order by copy number

• Order within copy does not matter

• O(|V||E|) time

17/09/2018 Design and Analysis of Algorithms 2018 week 3 13

Page 13: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)...Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

Network flows

An application of shortest path algorithms and a

fundamental problem to reduce other problems to

Page 14: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)...Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

Master’s Programme in Computer Science

FLOWS

• Recall Kirchhoff’s laws from high-school physics

• Flow f in a directed graph (flow network):

• unique source s and unique target t

• flow f(e) defined on each edge e=(u,v), s.t. flow conservation property holds on each vertex v except s and t

• value of flow is imbalance at source and sink:

17/09/2018 Design and Analysis of Algorithms 2018 week 3 15

v

(flow conservation property)

)( )()( )(

),(),(),(),(tNu tNwsNw sNu

wtftufsufwsff

)( )(

),(),(vNu vNw

wvfvuf

Page 15: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)...Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

Master’s Programme in Computer Science

FLOW NETWORK • Flow network is a directed graph

• No self loops or antiparallel edges: if (u,v) is an edge, (v,u) is not

• Capacities c(u,v) ≥ 0 for each edge (u,v)

• A flow is feasible if it satisfies capacity constraints: 0 ≤ f(u,v) ≤ c(u,v) for every edge (u,v)

17/09/2018 Design and Analysis of Algorithms 2018 week 3 16

Unconstrained flow Flow with capacity constraint

Page 16: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)...Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

Master’s Programme in Computer Science

A FEASIBLE FLOW (EXAMPLE)

17/09/2018 Design and Analysis of Algorithms 2018 week 3 17

11/14

1/4

7/7

12/12

capacity flow

Flow value =11+8=15+4=19

Page 17: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)...Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

Master’s Programme in Computer Science

MAXIMUM FLOW PROBLEM

• Given a flow network, find a feasible flow f maximizing |f|.

17/09/2018 Design and Analysis of Algorithms 2018 week 3 18

?/14

?/4

?/7

?/12

Flow value = max

Page 18: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)...Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

Master’s Programme in Computer Science

FORD-FULKERSON METHOD (FIRST CONCEPTS)

• Idea: Given a feasible flow f, find another flow f’ s.t. augmenting f with f’ increases the flow value.

(f↑f’)(u,v) = f(u,v) + f’(u,v) - f’(v,u)

• Flow f’ is a feasible flow in a residual network Gf with capacities

• cf(u,v) = c(u,v) - f(u,v)

• cf(v,u) = f(u,v) (antiparallel edge)

• f↑ f’ is always a feasible flow and |f↑f’| = |f| + |f’| (see Lemma 26.1 in book)

18/09/2018 Design and Analysis of Algorithms 2018 week 3 19

Page 19: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)...Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

Master’s Programme in Computer Science

RESIDUAL NETWORK (EXAMPLE)

17/09/2018 Design and Analysis of Algorithms 2018 week 3 20

11/14

1/4

7/7

12/12

?/7

?/12

?/5

?/4

?/3

?/11

?/3

?/1 ?/11

?/8

?/15

Residual transform

Zero capacity edges

can be omitted

Page 20: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)...Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

Master’s Programme in Computer Science

FORD-FULKERSON METHOD (SINGLE STEP)

• Let P be a simple s-t path in the residual network Gf.

• No edges with zero residual capacity

• P is called augmenting path

• Let cf(P) = min{cf(u,v) : (u,v) in P}

• Bottleneck capacity of P

• Let fP(u,v) = cf(P) if (u,v) is in P and fP(u,v)=0 otherwise

• Maximum augmenting flow along P

LEMMA (Lemma 26.3 in book)

• f↑fP is a feasible flow and |f↑fP| = |f| + cf(P)

19/09/2018 Design and Analysis of Algorithms 2018 week 3 21

Page 21: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)...Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

Master’s Programme in Computer Science

FORD-FULKERSON METHOD (SINGLE STEP)

Proof sketch.

• Local changes in flow along P:

• → v → : inflow and outflow increase by cf(P)

• ← v → : outflow increases by cf(P) - cf(P) = 0

• → v ← : inflow increases by cf(P) - cf(P) = 0

• ← v ← : inflow and outflow decrease by cf(P)

• s →: ouflow increases by cf(P)

• s ←: inflow decreases by cf(P)

• → t: inflow increases by cf(P)

• ← t: ouflow decreases by cf(P)

• u → v: flow increases by cf(P) ≤ cf(u,v) = c(u,v)-f(u,v)

• u ← v: flow decreases by cf(P) ≤ cf(u,v) = f(u,v)

18/09/2018 Design and Analysis of Algorithms 2018 week 3 22

Flow

conservation

maintained

Flow value

increases

by cf(P)

Capacity

constraints

maintained

Page 22: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)...Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

Master’s Programme in Computer Science

AUGMENTING PATH (EXAMPLE)

17/09/2018 Design and Analysis of Algorithms 2018 week 3 23

11/14

1/4

7/7

12/12

?/7

?/12

?/5

?/4

?/3

?/11

?/3

?/1 ?/11

?/8

?/15

11/14

1/4

7/7

12/12

Page 23: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)...Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

Master’s Programme in Computer Science

FORD-FULKERSON METHOD (ALL STEPS)

• Section 26.2 in book

• Start with a zero flow f.

• Repeat until flow f does not improve anymore

• Find an augmenting s-t path P in the residual graph and augment f with the bottleneck flow induced by path P.

• Running time is O(|f*||E|), where f* is the maximum flow in a graph with integral capacities.

• Worst case: always choose augmenting paths with tight bottleneck

18/09/2018 Design and Analysis of Algorithms 2018 week 3 24

1

Page 24: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)...Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

Master’s Programme in Computer Science

FORD-FULKERSON METHOD (EXAMPLE)

17/09/2018 Design and Analysis of Algorithms 2018 week 3 25

11/14

1/4

7/7

12/12

?/7

?/12

?/5

?/4

?/3

?/11

?/3

?/1 ?/11

?/8

?/15

11/14

1/4

7/7

12/12

?/7

?/12

?/3

?/11

?/3

?/1 ?/11

?/1

?/19

No augmenting path to increase the flow.

Page 25: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)...Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

Master’s Programme in Computer Science

EDMONDS-KARP

• Augmenting path = shortest s-t path in the residual graph

• Shortest = fewest edges

• Otherwise identical to Ford-Fulkerson

• O(|V||E|2) running time!

• Lecture blackboard or Theorem 26.8 in book

17/09/2018 Design and Analysis of Algorithms 2018 week 3 26

Page 26: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)...Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2018)

Master’s Programme in Computer Science

APPLICATIONS OF MAX FLOW

• Vertex-disjoint paths

• Find the maximum number of vertex disjoint paths from a set of source vertices to a set of sink vertices

• Maximum bipartite matching

• Bipartite graph: vertices partitioned into two distinct subsets, no edges within subset

• Matching: subset of edges with no common end vertices

• Find maximum matching in a bipartite graph

• Both problems have a reduction to max-flow

• See lecture blackboard and separate writeup

18/09/2018 Design and Analysis of Algorithms 2018 week 3 27