Docsity

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

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