Dynamic Programming: Algorithms and Applications - Lecture Notes, Lecture notes of Algorithms and Programming

[Week 7] Dynamic Programming I -- Arrays, Knapsack Problem,

Typology: Lecture notes

2018/2019

Uploaded on 04/20/2019

kefart
kefart 🇺🇸

4.4

(11)

55 documents

1 / 71

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
The University of Sydney Page 1
Assessment schedule
Quiz 27:00 PM Fri 12 April 2019 (Week 7)
Assignment 2 11:59 PM Sun 14 April 2019
Quiz 37:00PM Fri 24 May 2019 (Week 12)
Assignment 3 11:00PM Fri 24 May 2019
Teaching Assistant
Darcy Townsend [email protected]dney.edu.au
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47

Partial preview of the text

Download Dynamic Programming: Algorithms and Applications - Lecture Notes and more Lecture notes Algorithms and Programming in PDF only on Docsity!

Assessment schedule

  • Quiz 2 7:00 PM Fri 12 April 2019 (Week 7)
  • Assignment 2 11:59 PM Sun 14 April 2019
  • Quiz 3 7:00PM Fri 24 May 2019 (Week 12)
  • Assignment 3 11:00PM Fri 24 May 2019
  • Teaching Assistant
  • Darcy Townsend [email protected]

1

Lecture 7:

Dynamic Programming I

Fast Fourier Transform

The University of Sydney Page 4

Algorithmic Paradigms

  • Greed. Build up a solution incrementally, myopically optimizing

some local criterion.

  • Divide-and-conquer. Break up a problem into two sub-

problems, solve each sub-problem independently, and combine

solution to sub-problems to form solution to original problem.

  • Dynamic programming. Break up a problem into a series of

overlapping sub-problems, and build up solutions to larger and

larger sub-problems.

4

The University of Sydney Page 5

Dynamic Programming Applications

  • Areas.
    • Bioinformatics.
    • Control theory.
    • Information theory.
    • Operations research.
    • Computer science: theory, graphics, AI, systems, ….
  • Some famous dynamic programming algorithms.
    • Viterbi for hidden Markov models.
    • Unix diff for comparing two files.
    • Smith-Waterman for sequence alignment.
    • Bellman-Ford for shortest path routing in networks.
    • Cocke-Kasami-Younger for parsing context free grammars.

5

Fibonacci: Recursive Call Tree

Fibonacci: Recursive Call Tree

6

5 4

4 3 3 2

(^3 2 2 1) (^2) 1 1 0

2 (^1) 1 1 (^0) 1 0

(^1 )

let's leverage all the repeats!

0

Memoisation: Don't re-do unnecessary work!

  • Memoization : Store previous results so that in future executions, you don’t have to recalculate them aka remember what you have already done!

The University of Sydney Page 11

6

5 4

4 3 3 2

(^3 2 2 1) (^2) 1 1 0

(^2 1) 1 1 (^0) 1 0 (^1) 0

let's leverage all the repeats!

0

Memoisation: Don't re-do unnecessary work!

fib(n) if n is 1 or 2, return 1; if M[n] is not zero, return M[n]; M[n] = fib(n-1) + fib(n-2); return M[n];

Recall Interval Scheduling (Lecture 5)

  • Interval scheduling.
    • Input: Set of n jobs. Each job i starts at time s (^) j and finishes at time f (^) j.
    • Two jobs are compatible if they don't overlap in time.
    • Goal: find maximum subset of mutually compatible jobs.

Time 0 1 2 3 4 5 6 7 8 9 10 11

f g h

e

a b c d

...

  • Theorem: Greedy algorithm [Earliest finish time] is optimal.
  • Proof: (by contradiction)
  • Assume greedy is not optimal, and let's see what happens.
  • Let i 1 , i 2 , ... i (^) k denote the set of jobs selected by greedy.
  • Let J 1 , J 2 , ... Jm denote the set of jobs in an optimal solution with i 1 = J 1 , i 2 = J 2 , ..., i (^) r = Jr for the largest possible value of r.

Recall Interval Scheduling (Lecture 5)

J 1 J 2 Jr

i 1 i 1 ir ir+

...

solution still feasible and optimal, but contradicts maximality of r.

Jr+

job ir+1 finishes before Jr+

Greedy:

OPT:

Exchange argument!

Weighted Interval Scheduling

  • Weighted interval scheduling problem.
    • Job j starts at s (^) j, finishes at f (^) j, and has weight or value vj.
    • Two jobs compatible if they don't overlap.
    • Goal: find maximum weight subset of mutually compatible jobs.

Time 0 1 2 3 4 5 6 7 8 9 10 11

f g h

e

a b c d

Unweighted Interval Scheduling Review

  • Recall. Greedy algorithm works if all weights are 1.
    • Consider jobs in ascending order of finish time.
    • Add job to subset if it is compatible with previously chosen jobs.
  • Observation. Greedy algorithm can fail if arbitrary weights

are allowed.

Time 0 1 2 3 4 5 6 7 8 9 10 11

b

a

weight = 999

weight = 1

doesn’t work

Dynamic Programming: Binary Choice

Notation. OPT(j) = value of optimal solution to the problem

consisting of job requests 1, 2, ..., j.

  • Case 1: OPT selects job j.
    • can't use incompatible jobs { p(j) + 1, p(j) + 2, ..., j - 1 }
    • must include optimal solution to problem consisting of remaining compatible jobs 1, 2, ..., p(j)
  • Case 2: OPT does not select job j.
    • must include optimal solution to problem consisting of remaining compatible jobs 1, 2, ..., j-

OPT ( j ) =

0 if j = 0 max (^) { v (^) j + OPT ( p ( j )), OPT ( j −1)} otherwise

  

Example

maximum