Advanced Algorithms 南京大学 尹一通
Knapsack Problem Instance : n items i =1,2, ..., n ; weights w 1 , w 2 , ..., w n ∈ ℤ + ; values v 1 , v 2 , ..., v n ∈ ℤ + ; knapsack capacity B ∈ ℤ + ; Find a subset of items whose total weight is bounded by B and total value is maximized. capacity B weight 1 weight 6 value 6 value 1 weight 2 value 2 weight 5 weight 3 value 5 value 3 weight 4 value 4
Knapsack Problem Instance : n items i =1,2, ..., n ; weights w 1 , w 2 , ..., w n ∈ ℤ + ; values v 1 , v 2 , ..., v n ∈ ℤ + ; knapsack capacity B ∈ ℤ + ; Find an S ⊆ {1,2, ..., n } that maximizes ∑ i ∈ S v i subject to ∑ i ∈ S w i ≤ B. capacity B weight 1 weight 6 • 0-1 Knapsack value 6 value 1 problem • one of Karp’s 21 weight 2 value 2 NP -complete problems weight 5 weight 3 value 5 value 3 weight 4 value 4
Greedy Heuristics Instance : n items i =1,2, ..., n ; weights w 1 , w 2 , ..., w n ∈ ℤ + ; values v 1 , v 2 , ..., v n ∈ ℤ + ; knapsack capacity B ∈ ℤ + ; Find an S ⊆ {1,2, ..., n } that maximizes ∑ i ∈ S v i subject to ∑ i ∈ S w i ≤ B. Sort all items according to the ratio r i = v i / w i so that r 1 ≥ r 2 ≥ … ≥ r n ; for i =1,2, ..., n item i joins S if the resulting total weight ≤ B ; approximation ratio: arbitrarily bad
Dynamic Programming Instance : n items i =1,2, ..., n ; weights w 1 , w 2 , ..., w n ∈ ℤ + ; values v 1 , v 2 , ..., v n ∈ ℤ + ; knapsack capacity B ∈ ℤ + ; define: A ( i , v ) = minimum total weight of S ⊆ {1,2, ..., i } with total value exactly v A ( i , v ) = ∞ if no such S exists
Dynamic Programming Instance : n items i =1,2, ..., n ; weights w 1 , w 2 , ..., w n ∈ ℤ + ; values v 1 , v 2 , ..., v n ∈ ℤ + ; knapsack capacity B ∈ ℤ + ; define: X if ∃ S ⊆ {1,2, ..., i }, min w j ( S ⊆ { 1 , 2 ,...,i } v = ∑ j ∈ S v j j ∈ S P j ∈ S vj = v A ( i , v ) = otherwise ∞
Dynamic Programming Instance : n items i =1,2, ..., n ; weights w 1 , w 2 , ..., w n ∈ ℤ + ; values v 1 , v 2 , ..., v n ∈ ℤ + ; knapsack capacity B ∈ ℤ + ; A ( i , v ) = minimum total weight of S ⊆ {1,2, ..., i } with total value exactly v recursion: for i > 1 A ( i , v ) = min{ A ( i -1, v ), A ( i -1, v - v i ) + w i } if v = v 1 w 1 ( Dynamic programming: A (1, v ) = table size O( nV ) otherwise ∞ time complexity O( nV ) 1 ≤ i ≤ n, 1 ≤ v ≤ V = ∑ i v i
Dynamic Programming Instance : n items i =1,2, ..., n ; weights w 1 , w 2 , ..., w n ∈ ℤ + ; values v 1 , v 2 , ..., v n ∈ ℤ + ; knapsack capacity B ∈ ℤ + ; Find a subset of items whose total weight is bounded by B and total value is maximized. A ( i , v ) = minimum total weight of S ⊆ {1,2, ..., i } with total value exactly v knapsack: Polynomial Dynamic programming: table size O( nV ) max v that A ( n , v ) ≤ B Time ? time complexity O( nV )
Polynomial Time Instance : n items i =1,2, ..., n ; weights w 1 , w 2 , ..., w n ∈ ℤ + ; values v 1 , v 2 , ..., v n ∈ ℤ + ; knapsack capacity B ∈ ℤ + ; time complexity: O( nV ) where V = ∑ i v i • polynomial-time Algorithm A : ∃ constant c , ∀ input x ∈ {0,1} * , A ( x ) terminates in | x | c steps | x | = length of input x (in binary code) • pseudopolynomial-time Algorithm A : ∃ constant c , ∀ input x ∈ {0,1} * , A ( x ) terminates in | x | c steps | x | = length of input x (in unary code)
Dynamic Programming Instance : n items i =1,2, ..., n ; weights w 1 , w 2 , ..., w n ∈ ℤ + ; values v 1 , v 2 , ..., v n ∈ ℤ + ; knapsack capacity B ∈ ℤ + ; Find a subset of items whose total weight is bounded by B and total value is maximized. A ( i , v ) = minimum total weight of S ⊆ {1,2, ..., i } with total value exactly v A ( i , v ) = min{ A ( i -1, v ), A ( i -1, v - v i ) + w i } Pseudo- Dynamic programming: if v = v 1 ( w 1 Polynomial A (1, v ) = time complexity O( nV ) ∞ otherwise where V = ∑ i v i Time ! knapsack: max{ v : A ( n , v ) ≤ B }
Scaling & Rounding Instance : n items i =1,2, ..., n ; weights w 1 , w 2 , ..., w n ∈ ℤ + ; values v 1 , v 2 , ..., v n ∈ ℤ + ; knapsack capacity B ∈ ℤ + ; Set k = ; (to be fixed) for i =1,2, ..., n , let v ’ i = ⌊ v i / k ⌋ ; return the knapsack solution found by dynamic programming with new values v ’ i ; v i : n 0 v max = max 1 ≤ i ≤ n v i k
Scaling & Rounding Instance : n items i =1,2, ..., n ; weights w 1 , w 2 , ..., w n ∈ ℤ + ; values v 1 , v 2 , ..., v n ∈ ℤ + ; knapsack capacity B ∈ ℤ + ; Set k = ; (to be fixed) for i =1,2, ..., n , let v ’ i = ⌊ v i / k ⌋ ; return the knapsack solution found by dynamic programming with new values v ’ i ; time complexity: O( n V ’) = O( nV / k ) where V ’ = ∑ i v ’ i = ∑ i ⌊ v i / k ⌋ = O( V / k ) and V = ∑ i v i
Instance : n items i =1,2, ..., n ; weights w 1 , w 2 , ..., w n ∈ ℤ + ; values v 1 , v 2 , ..., v n ∈ ℤ + ; knapsack capacity B ∈ ℤ + ; Set k = ; (to be fixed) for i =1,2, ..., n , let v ’ i = ⌊ v i / k ⌋ ; return the knapsack solution found by dynamic programming with new values v ’ i ; time complexity: O( nV / k ) where V = ∑ i v i S * : optimal knapsack solution of the original instance j v i v i ⇣j v i k k ⌘ X X X X = k ≤ k + nk OPT = v i ≤ k + 1 k k k i ∈ S ∗ i ∈ S ∗ i ∈ S ∗ i ∈ S ∗ S : the solution returned by the algorithm j v i j v i k k X X (optimal solution of the scaled instance) ≥ k k i ∈ S i ∈ S ∗ j v i j v i k k X X X ≥ k ≥ k ≥ OPT − nk SOL = v i k k i ∈ S i ∈ S i ∈ S ∗
Instance : n items i =1,2, ..., n ; weights w 1 , w 2 , ..., w n ∈ ℤ + ; values v 1 , v 2 , ..., v n ∈ ℤ + ; knapsack capacity B ∈ ℤ + ; Set k = ; (to be fixed) for i =1,2, ..., n , let v ’ i = ⌊ v i / k ⌋ ; return the knapsack solution found by dynamic programming with new values v ’ i ; time complexity: O( nV / k ) where V = ∑ i v i ≤ nv max OPT : optimal value of the original instance SOL : value of the solution returned by the algorithm OPT ≥ 1 − nk SOL nk SOL ≥ OPT − nk ≥ 1 − v max OPT WLOG: OPT ≥ v max = max 1 ≤ i ≤ n v i
Instance : n items i =1,2, ..., n ; weights w 1 , w 2 , ..., w n ∈ ℤ + ; values v 1 , v 2 , ..., v n ∈ ℤ + ; knapsack capacity B ∈ ℤ + ; for any 0 ≤ ε ≤ 1 : j ✏ v max k Set k = ; where v max = max 1 ≤ i ≤ n v i n for i =1,2, ..., n , let v ’ i = ⌊ v i / k ⌋ ; return the knapsack solution found by dynamic programming with new values v ’ i ; ✓ n 2 v max ◆ ✓ n 3 ◆ time complexity: O = O k ✏ OPT : optimal value of the original instance SOL : value of the solution returned by the algorithm SOL OPT ≥ 1 − nk ≥ 1 − ✏ v max
Approximation Ratio Optimization problem : • instance I : optimum of instance I OPT( I ) = • algorithm A : returns a solution s for every instance I SOL A ( I ) = value returned by A on instance I minimization: approximation ratio of algorithm A is α SOL A ( I ) if ∀ instance I : OPT( I ) ≤ α maximization: approximation ratio of algorithm A is α SOL A ( I ) if ∀ instance I : OPT( I ) ≥ α ε -approximation: (1- ε ) OPT( I ) ≤ SOL A ( I ) ≤ (1+ ε ) OPT( I ) (maximization) (minimization)
Approximation Ratio Optimization problem : • instance I : optimum of instance I OPT( I ) = • algorithm A : returns a solution s for every instance I and 0 ≤ ε ≤ 1 SOL A ( ε , I ) = value returned by A on instance I and ε • A is a Polynomial-Time Approximation Scheme (PTAS) if: ∀ 0 ≤ ε ≤ 1, A returns in polynomial time and (1- ε ) OPT( I ) ≤ SOL A ( ε , I ) ≤ (1+ ε ) OPT( I ) (maximization) (minimization) • A is a Fully Polynomial-Time Approximation Scheme (FPTAS) if: furthermore, A returns in time Poly(1/ ε , n ) where n = | I | (in binary code)
Instance : n items i =1,2, ..., n ; weights w 1 , w 2 , ..., w n ∈ ℤ + ; values v 1 , v 2 , ..., v n ∈ ℤ + ; knapsack capacity B ∈ ℤ + ; for any 0 ≤ ε ≤ 1 : j ✏ v max k Set k = ; where v max = max 1 ≤ i ≤ n v i n for i =1,2, ..., n , let v ’ i = ⌊ v i / k ⌋ ; return the knapsack solution found by dynamic programming with new values v ’ i ; ✓ n 3 ◆ time complexity: O ✏ FPTAS SOL approximation ratio: ≥ 1 − ✏ OPT Are FPTASs the “best” approximation algorithms?
Bin Packing Instance : n items i =1,2, ..., n ; with sizes s 1 , s 2 , ..., s n ∈ ℤ + ; Find a packing of the n items into smallest number of bins with capacity B ∈ ℤ + . items: bins:
Bin Packing Instance : n items i =1,2, ..., n ; with sizes s 1 , s 2 , ..., s n ∈ (0, 1] ; Find a packing of the n items into smallest number of unit-sized bins . items: bins:
Bin Packing Instance : n items i =1,2, ..., n ; with sizes s 1 , s 2 , ..., s n ∈ (0, 1] ; Find a φ : [ n ] → [ m ] with smallest m such that ∀ j ∈ [ m ], ∑ i : φ ( i ) = j s i ≤ 1 . items: bins:
First Fit Instance : n items i =1,2, ..., n ; with sizes s 1 , s 2 , ..., s n ∈ (0, 1] ; Find a packing of the n items into smallest number of unit-sized bins . • NP -hard. FirstFit Initially k =1 ; for i =1,2, ..., n item i joins the first bin among 1,2, ..., k in which it fits ; if item i can fit into none of these k bins open a new bin k++ and item i joins it;
Recommend
More recommend