data structures and algorithms 2020 10 08 lecture 12 nice code - - PowerPoint PPT Presentation

data structures and algorithms 2020 10 08 lecture 12 nice
SMART_READER_LITE
LIVE PREVIEW

data structures and algorithms 2020 10 08 lecture 12 nice code - - PowerPoint PPT Presentation

data structures and algorithms 2020 10 08 lecture 12 nice code def whatisthis(n): return (4 << n*(3+n)) // ((4 << 2*n) - (2 << n) - 1) & ((2 << n) - 1) source: Paul Hankin overview Fibonacci numbers rod cutting


slide-1
SLIDE 1

data structures and algorithms 2020 10 08 lecture 12

slide-2
SLIDE 2

nice code

def whatisthis(n): return (4 << n*(3+n)) // ((4 << 2*n) - (2 << n) - 1) & ((2 << n) - 1) source: Paul Hankin

slide-3
SLIDE 3
  • verview

Fibonacci numbers rod cutting max-subarray knapsack 0/1

slide-4
SLIDE 4
  • verview

Fibonacci numbers rod cutting max-subarray knapsack 0/1

slide-5
SLIDE 5

Fibonacci Numbers

definition of the Fibonacci Numbers Fn: F1 = 1 F2 = 1 Fn = Fn−1 + Fn−2

slide-6
SLIDE 6

naive recursive algorithm

Algorithm fib(n): if n = 1 or n = 2 then return 1 else x := fib(n − 1) y := fib(n − 2) return x + y

slide-7
SLIDE 7

analysis of the naive recursive algorithm

exponential-time algorithm see the recursion tree (for example for fib(6)) recurrence equation T(n) = 1 if n = 1 or n = 2 T(n − 1) + T(n − 2) + 1 if n ≥ 3 T(n) ∈ O(2n) analytical bound exponential in golden ratio

slide-8
SLIDE 8

use memoization

share nodes in the recursion tree re-use results

slide-9
SLIDE 9

bottom-up approach using memoization

take array r[1 . . . n] and initialize it at 0 for all indices init: array at 1 and at 2 gets value 1 for-loop: array at 3 up to n gets value computed from previous two entries Algorithm fib(n): new array r[1 . . . n] r[1] := 1 r[2] := 1 for i := 3 to n do r[i] := r[i − 1] + r[i − 2] return r[n] analysis: in O(n) (there also is a logarithmic algorithm)

slide-10
SLIDE 10

what we used

reuse of easier sub-problems

  • rder computation in order to be able to reuse

systematic storage of results

  • ptimal substructures
  • verlapping subproblems
slide-11
SLIDE 11
  • verview

Fibonacci numbers rod cutting max-subarray knapsack 0/1

slide-12
SLIDE 12

rod cutting

problem: rod of n decimeter table of prices pi for i = 1, . . . , n decimeters determine maximum revenue rn 2n−1 possibilities of cutting the rod example: n = 4 and table with prices length i 1 2 3 4 price pi 1 5 8 9

slide-13
SLIDE 13

rod cutting: recursive algorithm

input: array p[1 . . . k] with prices and integer n

  • utput: maximum revenue possible for length n (n ≤ k)

Algorithm rodCuttingRec(p, n): if n = 0 then return 0 q := − ∞ for i := 1 to n do q := max(q, p[i] + rodCuttingRec(p, n − i)) return q

slide-14
SLIDE 14

worst-case time complexity of the recursive algorithm

recurrence: T(0) = 1 T(n) = 1 + Σn−1

j=0 T(j)

with induction follows: T(n) = 2n intuition: at every possibility we either do or do not cut see recursion tree for n = 4

slide-15
SLIDE 15

rod cutting: dynamic programming algorithm

Algorithm rodCuttingDP(p, n): new array b[0 . . . n] b[0] := 0 for j := 1 to n do q := − ∞ for i := 1 to j do q := max(q, p[i] + b[j − i]) b[j] := q return b[n]

slide-16
SLIDE 16

worst-case time complexity for the DP algorithm

reconsider example for n = 4 summation n

j=1 j so in O(n2)

slide-17
SLIDE 17

rod cutting: alternatives

give not only revenue but also choice for where to cut

  • rder the pieces in increasing length

give top-down algorithm using memoization but without recursion

slide-18
SLIDE 18
  • verview

Fibonacci numbers rod cutting max-subarray knapsack 0/1

slide-19
SLIDE 19

max-subarray problem

assume an array with integers give the maximum of the sum of the elements of a sub-array example: [−10, 10, 5, −3, 2, 1] yields 15

slide-20
SLIDE 20

max-subarrray: algorithm in O(n2)

Algorithm maxSubArray(A, n): max := 0 for left := 1 to n do sum := 0 for right := left to n do sum := sum + A[right] if sum > max then max := sum return max we explore all possibilities for left, and given left all possibilities for right

slide-21
SLIDE 21

max-subarray: divide and conquer?

improve to O(n log n) using a divide and conquer approach it still can be done better array giving the max sum is either left of the middle so in A[low . . . mid]

  • r right of the middle so in A[mid . . . high]
  • r using the middle so in A[i . . . j] with mid ≤ i < j ≤ high

algorithm is a bit hairy (not for exam)

slide-22
SLIDE 22

find max-subarray in the middle

Algorithm midMaxSubArray(A, low, mid, high): leftsum := − ∞ sum := 0 for i := mid downto low do sum := sum + A[i] if sum > leftsum then leftsum := sum maxleft := i rightsum := − ∞ sum := 0 for j := mid + 1 to high do sum := sum + A[j] if sum > rightsum then rightsum := sum maxright := j return (maxleft, maxright, leftsum + rightsum)

slide-23
SLIDE 23

max-subarrray via divide and conquer in O(n log n)

Algorithm maxSubArray(A, low, high): if low = high then return (low, high, A[low]) else mid := ⌊(low + high)/2⌋ (Llow, Lhigh, Lsum) := maxSubArray(A, low, mid) (Rlow, Rhigh, Rsum) := maxSubArray(A, mid + 1, high) (Clow, Chigh, Csum) := midMaxSubArray(A, low, mid, high) if Lsum ≥ Rsum and Lsum ≥ Csum then return (Llow, Lhigh, Lsum) else if Rsum ≥ Lsum and Rsum ≥ Csum then return (Rlow, Rhigh, Rsum) else return (Clow, Chigh, Csum)

slide-24
SLIDE 24

max-subarray with divide and conquer

worst-case time complexity via a recurrence equation: T(n) = 1 if n = 1 2T( n

2) + n

if n > 1 same as for merge sort, in Θ(n log(n)) so aymptotically better than brute force which is quadratic

slide-25
SLIDE 25

max-subarray: dynamic programming

idea for B[r]: maximal sum of subarray ending at index r start: B[1] = max{A[1], 0} either empty subarray ending at r, or continue max-subarray ending at r −1: B[r] = max{0, B[r − 1] + A[r]} then: max-subarray of the array is max of all the B[i] increase step by step the possibilites

slide-26
SLIDE 26

max-subarray: algorithm in O(n)

Algorithm maxSubArray(A, n): new array B B[1] := max(A[1], 0) m := B[1] for r = 2 to n do B[r] := max(0, B[r − 1] + A[r]) m := max(m, B[r]) return m

slide-27
SLIDE 27
  • verview

Fibonacci numbers rod cutting max-subarray knapsack 0/1

slide-28
SLIDE 28

knapsack01: problem

we come back to the knapsack01 problem given: a set S with n items every item i has weight wi and benefit bi maximum total weight W goal: take subset of items T ⊆ S such that

i∈T bi maximal

under constraint

i∈T wi ≤ W

naive approach: consider all 2n possible subsets T

slide-29
SLIDE 29

knapsack01: idea algorithm

Sk contains elements 1, . . . , k (for ∈ {1, . . . , n}) B[k, w] value of best selection from Sk with total weight ≤ w how to find B[k, w]? if wk > w: we cannot take item k so B[k, w] = B[k − 1, w] if wk ≤ w: we may take item k B[k, w] = max{B[k − 1, w], B[k − 1, w − wk] + bk}

slide-30
SLIDE 30

knapsack01: algorithm

S consists of n items with bi and wi; W is the max total weight Algorithm 01Knapsack(S, W ): new B[0 . . . n, 0 . . . W ] for w := 0 to W do B[0, w] := 0 for k := 1 to n do B[k, 0] := 0 for w := 1 to W do if wk ≤ w then B[k, w] := max(B[k − 1, w], B[k − 1, w − wk] + bk) else B[k, w] := B[k − 1, w]

slide-31
SLIDE 31

knapsack01: application

  • item 1 with w1 = 3 and b1 = 9
  • item 2 with w2 = 2 and b2 = 5
  • item 3 with w3 = 2 and b3 = 5
  • max total weight W = 4
  • B[0, 0] = 0, B[0, 1] = 0, B[0, 2] = 0, B[0, 3] = 0, B[0, 4] = 0
  • B[1, 0] = 0, B[1, 1] = 0, B[1, 2] = 0, B[1, 3] = 9, B[1, 4] = 9
  • B[2, 0] = 0, B[2, 1] = 0, B[2, 2] = 5, B[2, 3] = 9, B[2, 4] = 9
  • B[3, 0] = 0, B[3, 1] = 0, B[3, 2] = 5, B[3, 3] = 9, B[3, 4] = 10
slide-32
SLIDE 32

knapsack01: time complexity

for every k = 0, . . . , n we consider Sk for every Sk we consider w = 0, . . . , W time complexity in nW (the size of the table) if W = 2n then not so nice this is a pseudo-polynomial algorithm

  • ne does not expect a polynomial algorithm will be found
slide-33
SLIDE 33

knapsack01: algorithm

alternative where only one array is used Algorithm 01Knapsack(S, W ): for w := 0 to W do B[w] := 0 for k := 1 to n do for w := W , . . . , wk do if B[w − wk] + bk > B[w] then B[w] := B[w − wk] + bk

slide-34
SLIDE 34

dynamic programming is not always the right way

give in graph longest cykel-free path sub-path of cykel-free path is not necessarily longest