Top Banner
Data Structures and Algorithms – COMS21103 Dynamic Programming Largest Empty Square and Weighted Interval Scheduling Benjamin Sach
314

Dynamic Programming

Jan 24, 2017

Download

Education

Benjamin Sach
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Dynamic Programming

Data Structures and Algorithms – COMS21103

Dynamic Programming

Largest Empty Square and Weighted Interval Scheduling

Benjamin Sach

Page 2: Dynamic Programming

The name

Dynamic Programming is an approach to algorithm design. . .

why does it sound like an alternative to Agile Software Development?

Page 3: Dynamic Programming

The name

Dynamic Programming is an approach to algorithm design. . .

why does it sound like an alternative to Agile Software Development?

- Richard Bellman invented Dynamic programming around 1950

a ‘program’ referred to finding an optimal schedule or programme of activities

Serious answer:

Page 4: Dynamic Programming

The name

Dynamic Programming is an approach to algorithm design. . .

why does it sound like an alternative to Agile Software Development?

- Richard Bellman invented Dynamic programming around 1950

“The 1950s were not good years for mathematical research. We had a very interesting

gentleman in Washington named Wilson. He was Secretary of Defense, and he actually had a

pathological fear and hatred of the word, research... His face would suffuse, he would turn red,

and he would get violent if people used the term, research, in his presence. You can imagine

how he felt, then, about the term, mathematical... I thought dynamic programming was a good

name. It was something not even a Congressman could object to.”

a ‘program’ referred to finding an optimal schedule or programme of activities

Serious answer:

Real answer:

- Richard Bellman

Page 5: Dynamic Programming

What problems can Dynamic Programming solve?

• Longest Common Subsequence

• Edit Distance

• Text justification

• Seam Carving

• Solving the Towers of Hanoi

• Predicting cricket scores

• Assembly Line Scheduling

• Matrix Chain Multiplication

• Playing Tetris perfectly

• Dynamic Time Warping

• Finding optimal Binary Search Trees

• The Travelling Salesman Problem

• Knapsack

(used heavily in Bioinformatics for DNA similarity)

(Google this later, it’s really awesome)

(though still slowly)

(when you know the likely frequencies of searches)

(though still slowly)

(used heavily in Bioinformatics for sequence alignment)

(used extensively in computer vision)

and loads of other problems

Page 6: Dynamic Programming

Introduction

Dynamic programming is recursion without repetition

Dynamic programming is a technique for finding efficient algorithms for problems which

can be broken down into simpler, overlapping subproblems.

in other words. . .

The basic idea:

1. Find a recursive formula for the problem

(typically this is the hard bit)

3. Speed it up by storing the solutions to subproblems(to avoid recomputing the same thing over and over)

4. Derive an iterative algorithm by solving the subproblems in a good order

(iterative algorithms are often better in practice, easier to analyse and prettier)

- in terms of answers to subproblems.

2. Write down a naive recursive algorithm(typically this algorithm will take exponential time)

Page 7: Dynamic Programming

Part one

Largest Empty Square

Page 8: Dynamic Programming

Largest Empty Square

Problem Given an n× n monochrome image, find the largest empty square.i.e. without any black pixels

n

n

Page 9: Dynamic Programming

Largest Empty Square

Problem Given an n× n monochrome image, find the largest empty square.i.e. without any black pixels

n

n

Page 10: Dynamic Programming

Largest Empty Square

Problem Given an n× n monochrome image, find the largest empty square.i.e. without any black pixels

n

n

an empty square

Page 11: Dynamic Programming

Largest Empty Square

Problem Given an n× n monochrome image, find the largest empty square.i.e. without any black pixels

n

n

Page 12: Dynamic Programming

Largest Empty Square

Problem Given an n× n monochrome image, find the largest empty square.i.e. without any black pixels

n

n

Page 13: Dynamic Programming

Largest Empty Square

Problem Given an n× n monochrome image, find the largest empty square.i.e. without any black pixels

n

n

Page 14: Dynamic Programming

Largest Empty Square

Problem Given an n× n monochrome image, find the largest empty square.i.e. without any black pixels

n

n

largest empty square

Page 15: Dynamic Programming

1. Find a recursive formula

To find a recursive formulation of this problem, consider the following fact:

Any m×m square of pixels, S is empty

The bottom right pixel of S is empty and

The three (m− 1)× (m− 1) squares in the

top left, top right and bottom left of S are empty

if and only if

Page 16: Dynamic Programming

1. Find a recursive formula

To find a recursive formulation of this problem, consider the following fact:

Any m×m square of pixels, S is empty

The bottom right pixel of S is empty and

The three (m− 1)× (m− 1) squares in the

top left, top right and bottom left of S are empty

Proof: (by picture)

if and only if

Page 17: Dynamic Programming

1. Find a recursive formula

To find a recursive formulation of this problem, consider the following fact:

Any m×m square of pixels, S is empty

The bottom right pixel of S is empty and

The three (m− 1)× (m− 1) squares in the

top left, top right and bottom left of S are empty

Proof: (by picture)

If S is empty then all four are empty

if and only if

Page 18: Dynamic Programming

1. Find a recursive formula

To find a recursive formulation of this problem, consider the following fact:

Any m×m square of pixels, S is empty

The bottom right pixel of S is empty and

The three (m− 1)× (m− 1) squares in the

top left, top right and bottom left of S are empty

Proof: (by picture)

if and only if

Page 19: Dynamic Programming

1. Find a recursive formula

To find a recursive formulation of this problem, consider the following fact:

Any m×m square of pixels, S is empty

The bottom right pixel of S is empty and

The three (m− 1)× (m− 1) squares in the

top left, top right and bottom left of S are empty

Proof: (by picture)

If all are empty where could a black pixel in S be?

if and only if

Page 20: Dynamic Programming

1. Find a recursive formula

To find a recursive formulation of this problem, consider the following fact:

Any m×m square of pixels, S is empty

The bottom right pixel of S is empty and

The three (m− 1)× (m− 1) squares in the

top left, top right and bottom left of S are empty

Proof: (by picture)

? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?

? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?

? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?

? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?

If all are empty where could a black pixel in S be?

if and only if

Page 21: Dynamic Programming

1. Find a recursive formula

To find a recursive formulation of this problem, consider the following fact:

Any m×m square of pixels, S is empty

The bottom right pixel of S is empty and

The three (m− 1)× (m− 1) squares in the

top left, top right and bottom left of S are empty

Proof: (by picture)

? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?

? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?

? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?

? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?

empty If all are empty where could a black pixel in S be?

if and only if

Page 22: Dynamic Programming

1. Find a recursive formula

To find a recursive formulation of this problem, consider the following fact:

Any m×m square of pixels, S is empty

The bottom right pixel of S is empty and

The three (m− 1)× (m− 1) squares in the

top left, top right and bottom left of S are empty

Proof: (by picture)

? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ?

? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ?

? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ?

? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?

empty If all are empty where could a black pixel in S be?

if and only if

Page 23: Dynamic Programming

1. Find a recursive formula

To find a recursive formulation of this problem, consider the following fact:

Any m×m square of pixels, S is empty

The bottom right pixel of S is empty and

The three (m− 1)× (m− 1) squares in the

top left, top right and bottom left of S are empty

Proof: (by picture)

? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ?

? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ?

? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ?

? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?

If all are empty where could a black pixel in S be?

if and only if

Page 24: Dynamic Programming

1. Find a recursive formula

To find a recursive formulation of this problem, consider the following fact:

Any m×m square of pixels, S is empty

The bottom right pixel of S is empty and

The three (m− 1)× (m− 1) squares in the

top left, top right and bottom left of S are empty

Proof: (by picture)

? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ?

? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ?

? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ?

? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?

empty

If all are empty where could a black pixel in S be?

if and only if

Page 25: Dynamic Programming

1. Find a recursive formula

To find a recursive formulation of this problem, consider the following fact:

Any m×m square of pixels, S is empty

The bottom right pixel of S is empty and

The three (m− 1)× (m− 1) squares in the

top left, top right and bottom left of S are empty

Proof: (by picture) empty

????

? ? ? ?

????

? ? ? ?

? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ?

????

? ? ? ? ?

If all are empty where could a black pixel in S be?

if and only if

Page 26: Dynamic Programming

1. Find a recursive formula

To find a recursive formulation of this problem, consider the following fact:

Any m×m square of pixels, S is empty

The bottom right pixel of S is empty and

The three (m− 1)× (m− 1) squares in the

top left, top right and bottom left of S are empty

Proof: (by picture)

????

? ? ? ?

????

? ? ? ?

? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ?

????

? ? ? ? ?

If all are empty where could a black pixel in S be?

if and only if

Page 27: Dynamic Programming

1. Find a recursive formula

To find a recursive formulation of this problem, consider the following fact:

Any m×m square of pixels, S is empty

The bottom right pixel of S is empty and

The three (m− 1)× (m− 1) squares in the

top left, top right and bottom left of S are empty

Proof: (by picture)

????

? ? ? ?

????

? ? ? ?

? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ? ?? ? ? ?

????

? ? ? ? ?

empty

If all are empty where could a black pixel in S be?

if and only if

Page 28: Dynamic Programming

1. Find a recursive formula

To find a recursive formulation of this problem, consider the following fact:

Any m×m square of pixels, S is empty

The bottom right pixel of S is empty and

The three (m− 1)× (m− 1) squares in the

top left, top right and bottom left of S are empty

Proof: (by picture) empty

? ? ? ?? ? ? ?? ? ? ?? ? ? ? ?

If all are empty where could a black pixel in S be?

if and only if

Page 29: Dynamic Programming

1. Find a recursive formula

To find a recursive formulation of this problem, consider the following fact:

Any m×m square of pixels, S is empty

The bottom right pixel of S is empty and

The three (m− 1)× (m− 1) squares in the

top left, top right and bottom left of S are empty

Proof: (by picture)

? ? ? ?? ? ? ?? ? ? ?? ? ? ? ?

If all are empty where could a black pixel in S be?

if and only if

Page 30: Dynamic Programming

1. Find a recursive formula

To find a recursive formulation of this problem, consider the following fact:

Any m×m square of pixels, S is empty

The bottom right pixel of S is empty and

The three (m− 1)× (m− 1) squares in the

top left, top right and bottom left of S are empty

Proof: (by picture)

? ? ? ?? ? ? ?? ? ? ?? ? ? ? ?

emptyIf all are empty where could a black pixel in S be?

if and only if

Page 31: Dynamic Programming

1. Find a recursive formula

To find a recursive formulation of this problem, consider the following fact:

Any m×m square of pixels, S is empty

The bottom right pixel of S is empty and

The three (m− 1)× (m− 1) squares in the

top left, top right and bottom left of S are empty

Proof: (by picture)

emptyIf all are empty where could a black pixel in S be?If all are empty where could a black pixel in S be?

if and only if

Page 32: Dynamic Programming

1. Find a recursive formula

To find a recursive formulation of this problem, consider the following fact:

Any m×m square of pixels, S is empty

The bottom right pixel of S is empty and

The three (m− 1)× (m− 1) squares in the

top left, top right and bottom left of S are empty

Proof: (by picture)

If all are empty where could a black pixel in S be?If all are empty where could a black pixel in S be?

if and only if

Page 33: Dynamic Programming

1. Find a recursive formula

To find a recursive formulation of this problem, consider the following fact:

Any m×m square of pixels, S is empty

The bottom right pixel of S is empty and

The three (m− 1)× (m− 1) squares in the

top left, top right and bottom left of S are empty

Proof: (by picture)

If all are empty then S is empty

If all are empty where could a black pixel in S be?If all are empty where could a black pixel in S be?

if and only if

Page 34: Dynamic Programming

1. Find a recursive formula

(x, y)

LES(x, y)

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

Page 35: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

(x, y)

LES(x, y) = 0

Page 36: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

(x, y)

LES(x, y) = 0

X

Page 37: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

(x, y)

LES(x, y) = 1

(x, y)

X

Page 38: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

LES(x, y) = 1

(x, y)

X

Page 39: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

LES(x, y) = 1

(x, y)

XX

Page 40: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

(x, y)

XX

Page 41: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

(x, y)

XX

Page 42: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) 6

(x, y)

LES(x− 1, y − 1) + 1

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

XX

Page 43: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) 6

(x, y)

LES(x− 1, y − 1) + 1

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

XX

Page 44: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) 6

(x, y)

LES(x− 1, y − 1) + 1

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

can’t be biggerthan this

LES(x, y)

XX

Page 45: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) 6

(x, y)

LES(x− 1, y − 1) + 1

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

can’t be biggerthan this

LES(x, y)

XX

Page 46: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) 6

(x, y)

LES(x− 1, y − 1) + 1

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

can’t be biggerthan this

there is a non-emptypixel in here

LES(x, y)

XX

Page 47: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

(x, y)

LES(x, y) 6

LES(x− 1, y) + 1

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

can’t be biggerthan this

LES(x, y)

XX

Page 48: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

(x, y)

LES(x, y) 6

LES(x, y − 1) + 1

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

can’t be biggerthan this

LES(x, y)

XX

Page 49: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

(x, y)can’t be bigger

than this

LES(x, y)

XX

Page 50: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

(x, y)

is this square always empty?

can’t be biggerthan this

LES(x, y)

XX

Page 51: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

(x, y)

is this square always empty?

XX

Page 52: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

(x, y)

is this square always empty?empty

XX

Page 53: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

(x, y)

is this square always empty?empty

XX

Page 54: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

(x, y)

is this square always empty?empty

XX

Page 55: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

(x, y)

is this square always empty?empty

XX

Page 56: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

(x, y)

is this square always empty?empty

XX

Page 57: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

(x, y)

is this square always empty?empty

XX

Page 58: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

(x, y)

is this square always empty?empty

XX

Page 59: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

(x, y)

is this square always empty?empty

XX

Page 60: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

(x, y)

is this square always empty?empty

XX

Page 61: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

(x, y)

is this square always empty?empty

XX

Page 62: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

(x, y)

is this square always empty?

yes, by the proof on the previous slide

empty

XX

Page 63: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

(x, y)

is this square always empty?

yes, by the proof on the previous slide

empty

XX

Page 64: Dynamic Programming

1. Find a recursive formula

Let LES(x, y) be the size (i.e. side length) of the largest empty square

whose bottom right is at (x, y)

Then:

If the pixel (x, y) is not empty then LES(x, y) = 0.

If (x, y) is empty and in the first row or column,

LES(x, y) = 1.

If (x, y) is empty and not in the first row or column,

LES(x, y) = min(LES(x− 1, y − 1), LES(x− 1, y), LES(x, y − 1)) + 1.

(x, y)

is this square always empty?

yes, by the proof on the previous slide

empty

XX

X

Page 65: Dynamic Programming

2. Write down a recursive algorithm

We can use the recursive formula to get a recursive algorithm. . .

If pixel (x, y) is not empty

Return 0

If (x = 1) or (y = 1)

Return 1

Return min(LES(x− 1, y − 1),LES(x− 1, y),LES(x, y − 1)

)+ 1

LES(x, y)

LES(x, y) computes the size of the largest empty square

whose bottom right is at (x, y)

Therefore, the maximum of LES(x, y) over all x and ygives the size of the largest empty square in the whole image

Page 66: Dynamic Programming

2. Write down a recursive algorithm

We can use the recursive formula to get a recursive algorithm. . .

If pixel (x, y) is not empty

Return 0

If (x = 1) or (y = 1)

Return 1

Return min(LES(x− 1, y − 1),LES(x− 1, y),LES(x, y − 1)

)+ 1

LES(x, y)

LES(x, y) computes the size of the largest empty square

whose bottom right is at (x, y)

Therefore, the maximum of LES(x, y) over all x and ygives the size of the largest empty square in the whole image

What is the time complexity of this algorithm?

Page 67: Dynamic Programming

How efficient is the recursive algorithm?

If pixel (x, y) is not empty

Return 0

If (x = 1) or (y = 1)

Return 1

Return min(LES(x− 1, y − 1),LES(x− 1, y),LES(x, y − 1)

)+ 1

LES(x, y)

Let’s compute LES(4, 4). . .

Page 68: Dynamic Programming

How efficient is the recursive algorithm?

If pixel (x, y) is not empty

Return 0

If (x = 1) or (y = 1)

Return 1

Return min(LES(x− 1, y − 1),LES(x− 1, y),LES(x, y − 1)

)+ 1

LES(x, y)

Let’s compute LES(4, 4). . . (and consider the recursive calls)

Page 69: Dynamic Programming

How efficient is the recursive algorithm?

If pixel (x, y) is not empty

Return 0

If (x = 1) or (y = 1)

Return 1

Return min(LES(x− 1, y − 1),LES(x− 1, y),LES(x, y − 1)

)+ 1

LES(x, y)

Let’s compute LES(4, 4). . .

(4, 4)

(and consider the recursive calls)

Page 70: Dynamic Programming

How efficient is the recursive algorithm?

If pixel (x, y) is not empty

Return 0

If (x = 1) or (y = 1)

Return 1

Return min(LES(x− 1, y − 1),LES(x− 1, y),LES(x, y − 1)

)+ 1

LES(x, y)

Let’s compute LES(4, 4). . .

(4, 4)

(and consider the recursive calls)

(4, 3)(3, 4)(3, 3)

Page 71: Dynamic Programming

How efficient is the recursive algorithm?

If pixel (x, y) is not empty

Return 0

If (x = 1) or (y = 1)

Return 1

Return min(LES(x− 1, y − 1),LES(x− 1, y),LES(x, y − 1)

)+ 1

LES(x, y)

Let’s compute LES(4, 4). . .

(4, 4)

(and consider the recursive calls)

(4, 3)(3, 4)

(2, 2) (2, 3) (3, 2) (2, 3) (2, 4) (4, 2)(3, 3)(3, 2)(3, 3)

(3, 3)

Page 72: Dynamic Programming

How efficient is the recursive algorithm?

If pixel (x, y) is not empty

Return 0

If (x = 1) or (y = 1)

Return 1

Return min(LES(x− 1, y − 1),LES(x− 1, y),LES(x, y − 1)

)+ 1

LES(x, y)

Let’s compute LES(4, 4). . .

(4, 4)

(and consider the recursive calls)

(4, 3)(3, 4)

(2, 2) (2, 3) (3, 2) (2, 3) (2, 4)

(3, 3)

(3, 3) (3, 3) (4, 2)(3, 3)(3, 2)(3, 3)

(3, 3)

Page 73: Dynamic Programming

How efficient is the recursive algorithm?

If pixel (x, y) is not empty

Return 0

If (x = 1) or (y = 1)

Return 1

Return min(LES(x− 1, y − 1),LES(x− 1, y),LES(x, y − 1)

)+ 1

LES(x, y)

Let’s compute LES(4, 4). . .

(4, 4)

(and consider the recursive calls)

(4, 3)(3, 4)

(2, 2) (2, 3) (3, 2) (2, 3) (2, 4)

(3, 3)

(3, 3) (3, 3) (4, 2)(3, 3)(3, 2)(3, 3)

(3, 3)

computed three times :s

Page 74: Dynamic Programming

How efficient is the recursive algorithm?

If pixel (x, y) is not empty

Return 0

If (x = 1) or (y = 1)

Return 1

Return min(LES(x− 1, y − 1),LES(x− 1, y),LES(x, y − 1)

)+ 1

LES(x, y)

Let’s compute LES(4, 4). . .

(4, 4)

(and consider the recursive calls)

(4, 3)(3, 4)

(2, 2) (2, 3) (3, 2) (2, 3) (2, 4) (4, 2)(3, 3)(3, 2)(3, 3)

(3, 3)

Page 75: Dynamic Programming

How efficient is the recursive algorithm?

If pixel (x, y) is not empty

Return 0

If (x = 1) or (y = 1)

Return 1

Return min(LES(x− 1, y − 1),LES(x− 1, y),LES(x, y − 1)

)+ 1

LES(x, y)

Let’s compute LES(4, 4). . .

(4, 4)

(and consider the recursive calls)

(4, 3)(3, 4)

(2, 2) (2, 3) (3, 2) (2, 3) (2, 4) (4, 2)(3, 3)(3, 2)(3, 3)

(3, 3)

Page 76: Dynamic Programming

How efficient is the recursive algorithm?

If pixel (x, y) is not empty

Return 0

If (x = 1) or (y = 1)

Return 1

Return min(LES(x− 1, y − 1),LES(x− 1, y),LES(x, y − 1)

)+ 1

LES(x, y)

Let’s compute LES(4, 4). . .

(4, 4)

(and consider the recursive calls)

(4, 3)(3, 4)

(2, 2) (2, 3) (3, 2) (2, 3) (2, 4) (4, 2)(3, 3)(3, 2)(3, 3)

(3, 3)

Page 77: Dynamic Programming

How efficient is the recursive algorithm?

If pixel (x, y) is not empty

Return 0

If (x = 1) or (y = 1)

Return 1

Return min(LES(x− 1, y − 1),LES(x− 1, y),LES(x, y − 1)

)+ 1

LES(x, y)

Let’s compute LES(4, 4). . .

(4, 4)

(and consider the recursive calls)

(4, 3)(3, 4)

(2, 2) (2, 3) (3, 2) (2, 3) (2, 4) (4, 2)(3, 3)(3, 2)(3, 3)

(3, 3)

(2, 2) is computed three times just while computing this (3, 3)

Page 78: Dynamic Programming

How efficient is the recursive algorithm?

If pixel (x, y) is not empty

Return 0

If (x = 1) or (y = 1)

Return 1

Return min(LES(x− 1, y − 1),LES(x− 1, y),LES(x, y − 1)

)+ 1

LES(x, y)

Let’s compute LES(4, 4). . .

(4, 4)

(and consider the recursive calls)

(4, 3)(3, 4)

(2, 2) (2, 3) (3, 2) (2, 3) (2, 4) (4, 2)(3, 3)(3, 2)(3, 3)

(3, 3)

Page 79: Dynamic Programming

How efficient is the recursive algorithm?

If pixel (x, y) is not empty

Return 0

If (x = 1) or (y = 1)

Return 1

Return min(LES(x− 1, y − 1),LES(x− 1, y),LES(x, y − 1)

)+ 1

LES(x, y)

Let’s compute LES(4, 4). . .

(4, 4)

(and consider the recursive calls)

(4, 3)(3, 4)

(2, 2) (2, 3) (3, 2) (2, 3) (2, 4) (4, 2)(3, 3)(3, 2)(3, 3)

(3, 3)

This doesn’t look good!

Page 80: Dynamic Programming

How efficient is the recursive algorithm?

If pixel (x, y) is not empty

Return 0

If (x = 1) or (y = 1)

Return 1

Return min(LES(x− 1, y − 1),LES(x− 1, y),LES(x, y − 1)

)+ 1

LES(x, y)

Let’s compute LES(4, 4). . .

(4, 4)

(and consider the recursive calls)

(4, 3)(3, 4)

(2, 2) (2, 3) (3, 2) (2, 3) (2, 4) (4, 2)(3, 3)(3, 2)(3, 3)

(3, 3)

This doesn’t look good!In fact the running time of LES(n, n) is exponential in n

Page 81: Dynamic Programming

How efficient is the recursive algorithm?

If pixel (x, y) is not empty

Return 0

If (x = 1) or (y = 1)

Return 1

Return min(LES(x− 1, y − 1),LES(x− 1, y),LES(x, y − 1)

)+ 1

LES(x, y)

Let’s compute LES(4, 4). . .

(4, 4)

(and consider the recursive calls)

(4, 3)(3, 4)

(2, 2) (2, 3) (3, 2) (2, 3) (2, 4) (4, 2)(3, 3)(3, 2)(3, 3)

(3, 3)

This doesn’t look good!In fact the running time of LES(n, n) is exponential in n

If T (n) is the runtime of LES(n, n)

then T (n) > 3T (n− 1)

Page 82: Dynamic Programming

How efficient is the recursive algorithm?

If pixel (x, y) is not empty

Return 0

If (x = 1) or (y = 1)

Return 1

Return min(LES(x− 1, y − 1),LES(x− 1, y),LES(x, y − 1)

)+ 1

LES(x, y)

Let’s compute LES(4, 4). . .

(4, 4)

(and consider the recursive calls)

(4, 3)(3, 4)

(2, 2) (2, 3) (3, 2) (2, 3) (2, 4) (4, 2)(3, 3)(3, 2)(3, 3)

(3, 3)

Page 83: Dynamic Programming

How efficient is the recursive algorithm?

If pixel (x, y) is not empty

Return 0

If (x = 1) or (y = 1)

Return 1

Return min(LES(x− 1, y − 1),LES(x− 1, y),LES(x, y − 1)

)+ 1

LES(x, y)

Let’s compute LES(4, 4). . .

(4, 4)

(and consider the recursive calls)

(4, 3)(3, 4)

(2, 2) (2, 3) (3, 2) (2, 3) (2, 4)

(3, 3)

(3, 3) (3, 3) (4, 2)(3, 3)(3, 2)(3, 3)

(3, 3)

What should we do about all this repeated computation?

Page 84: Dynamic Programming

3. Store the solutions to subproblems

If pixel (x, y) is not empty

Return 0

If (x = 1) or (y = 1)

Return 1

If LES[x, y] undefined

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

Return LES[x, y]

MemLES(x, y)

In the MemLES version of the algorithmwe store solutions to previously computed subproblems

in an (n× n) 2D array called LES

Page 85: Dynamic Programming

3. Store the solutions to subproblems

This is called memoization (not memorization)

If pixel (x, y) is not empty

Return 0

If (x = 1) or (y = 1)

Return 1

If LES[x, y] undefined

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

Return LES[x, y]

MemLES(x, y)

In the MemLES version of the algorithmwe store solutions to previously computed subproblems

in an (n× n) 2D array called LES

Page 86: Dynamic Programming

3. Store the solutions to subproblems

This is called memoization (not memorization)

If pixel (x, y) is not empty

Return 0

If (x = 1) or (y = 1)

Return 1

If LES[x, y] undefined

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

Return LES[x, y]

MemLES(x, y)

In the MemLES version of the algorithmwe store solutions to previously computed subproblems

in an (n× n) 2D array called LES

Crucially, now each entry LES[x, y] is only computed once

Page 87: Dynamic Programming

3. Store the solutions to subproblems

This is called memoization (not memorization)

If pixel (x, y) is not empty

Return 0

If (x = 1) or (y = 1)

Return 1

If LES[x, y] undefined

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

Return LES[x, y]

MemLES(x, y)

In the MemLES version of the algorithmwe store solutions to previously computed subproblems

in an (n× n) 2D array called LES

Crucially, now each entry LES[x, y] is only computed once

The time complexity of computing MemLES(n, n) is now O(n2)

Page 88: Dynamic Programming

3. Store the solutions to subproblems

This is called memoization (not memorization)

If pixel (x, y) is not empty

Return 0

If (x = 1) or (y = 1)

Return 1

If LES[x, y] undefined

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

Return LES[x, y]

MemLES(x, y)

In the MemLES version of the algorithmwe store solutions to previously computed subproblems

in an (n× n) 2D array called LES

Crucially, now each entry LES[x, y] is only computed once

The time complexity of computing MemLES(n, n) is now O(n2)(in fact, computing maxx,y MemLES(x, y) takes O(n2) time too

)

Page 89: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

LES[n, n]

What information do we need to compute LES[n, n]?

The 2D array

LES:

(for x, y > 1 and (x, y) non empty)

Page 90: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

LES[n, n]

What information do we need to compute LES[n, n]?

to compute

The 2D array

LES:

(for x, y > 1 and (x, y) non empty)

Page 91: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

LES[n, n]

What information do we need to compute LES[n, n]?

to compute

The 2D array

LES:

we need

(for x, y > 1 and (x, y) non empty)

Page 92: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

LES[n, n]

What information do we need to compute LES[n, n]?

to compute

The 2D array

LES:

we need

LES[n− 1, n− 1]

LES[n− 1, n]

and LES[n, n− 1]

(for x, y > 1 and (x, y) non empty)

Page 93: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

LES[n, n]

What information do we need to compute LES[n, n]?

to compute

The 2D array

LES:

we need

LES[n− 1, n− 1]

LES[n− 1, n]

and LES[n, n− 1]

(for x, y > 1 and (x, y) non empty)

Page 94: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

LES[n, n− 1]to compute

(for x, y > 1 and (x, y) non empty)

Page 95: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

LES[n, n− 1]to compute

(for x, y > 1 and (x, y) non empty)

Page 96: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

LES[n, n− 1]to compute

LES[n− 1, n− 1]

and LES[n, n− 2]

LES[n− 1, n− 2]

(for x, y > 1 and (x, y) non empty)

Page 97: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

LES[n, n− 1]to compute

LES[n− 1, n− 1]

and LES[n, n− 2]

LES[n− 1, n− 2]

(for x, y > 1 and (x, y) non empty)

Page 98: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

LES[n− 1, n]to compute

(for x, y > 1 and (x, y) non empty)

Page 99: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

we need

LES[n− 1, n]to compute

(for x, y > 1 and (x, y) non empty)

Page 100: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

we need

LES[n− 1, n]to compute

LES[n− 2, n]

and LES[n− 1, n− 1]

LES[n− 2, n− 1]

(for x, y > 1 and (x, y) non empty)

Page 101: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

we need

LES[n− 1, n]to compute

LES[n− 2, n]

and LES[n− 1, n− 1]

LES[n− 2, n− 1]

(for x, y > 1 and (x, y) non empty)

Page 102: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

(for x, y > 1 and (x, y) non empty)

Page 103: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

(for x, y > 1 and (x, y) non empty)

Page 104: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

(for x, y > 1 and (x, y) non empty)

Page 105: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

(for x, y > 1 and (x, y) non empty)

Page 106: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

(for x, y > 1 and (x, y) non empty)

Page 107: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

(for x, y > 1 and (x, y) non empty)

Page 108: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

(for x, y > 1 and (x, y) non empty)

Page 109: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

(for x, y > 1 and (x, y) non empty)

Page 110: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

(for x, y > 1 and (x, y) non empty)

Page 111: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

(for x, y > 1 and (x, y) non empty)

Page 112: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

(for x, y > 1 and (x, y) non empty)

Page 113: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

(for x, y > 1 and (x, y) non empty)

Page 114: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

(for x, y > 1 and (x, y) non empty)

Page 115: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

(for x, y > 1 and (x, y) non empty)

Page 116: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

(for x, y > 1 and (x, y) non empty)

Page 117: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

(for x, y > 1 and (x, y) non empty)

Page 118: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

(for x, y > 1 and (x, y) non empty)

Page 119: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

(for x, y > 1 and (x, y) non empty)

Page 120: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

(for x, y > 1 and (x, y) non empty)

Page 121: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES:

(for x, y > 1 and (x, y) non empty)

Page 122: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

(for x, y > 1 and (x, y) non empty)

Page 123: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 124: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 125: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 126: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 127: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 128: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 129: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 130: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 131: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 132: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 133: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 134: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 135: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 136: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 137: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 138: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 139: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 140: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 141: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 142: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 143: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 144: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 145: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 146: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 147: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 148: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 149: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 150: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 151: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 152: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 153: Dynamic Programming

The dependency graph

LES[x, y]= min(MemLES(x− 1, y − 1),MemLES(x− 1, y),MemLES(x, y − 1)

)+ 1

What information do we need to compute LES[n, n]?

The 2D array

LES: How can we use this to get an

iterative algorithm?

Fill in the array from

the top-left!

(for x, y > 1 and (x, y) non empty)

Page 154: Dynamic Programming

4. Derive an iterative algorithm

For y = 1 to n

For x = 1 to n

If pixel (x, y) is not empty

LES[x, y] = 0

Else If (x = 1) or (y = 1)

LES[x, y] = 1

Else

LES[x, y]= min(LES[x− 1, y − 1],LES[x− 1, y],LES[x, y − 1]

)+ 1

ItLES(n)

This iterative version of the algorithm

runs in O(n2) time

and avoids making any recursive calls

Page 155: Dynamic Programming

4. Derive an iterative algorithm

For y = 1 to n

For x = 1 to n

If pixel (x, y) is not empty

LES[x, y] = 0

Else If (x = 1) or (y = 1)

LES[x, y] = 1

Else

LES[x, y]= min(LES[x− 1, y − 1],LES[x− 1, y],LES[x, y − 1]

)+ 1

ItLES(n)

This iterative version of the algorithm

runs in O(n2) time

and avoids making any recursive calls

Maximum of LES[x, y] over all x and ygives the size of the largest empty square in the whole image

this also takes O(n2) time

Page 156: Dynamic Programming

Introduction

Dynamic programming is recursion without repetition

Dynamic programming is a technique for finding efficient algorithms for problems which

can be broken down into simpler, overlapping subproblems.

in other words. . .

The basic idea:

1. Find a recursive formula for the problem

(typically this is the hard bit)

3. Speed it up by storing the solutions to subproblems (memoization)(to avoid recomputing the same thing over and over)

4. Derive an iterative algorithm by solving the subproblems in a good order

(iterative algorithms are often better in practice, easier to analyse and prettier)

- in terms of answers to subproblems.

2. Write down a naive recursive algorithm(typically this algorithm will take exponential time)

Summary

Page 157: Dynamic Programming

End of part one

Page 158: Dynamic Programming

Part two

Weighted Interval Scheduling

Page 159: Dynamic Programming

Introduction

Dynamic programming is recursion without repetition

Dynamic programming is a technique for finding efficient algorithms for problems which

can be broken down into simpler, overlapping subproblems.

in other words. . .

The basic idea:

1. Find a recursive formula for the problem

(typically this is the hard bit)

3. Speed it up by storing the solutions to subproblems (memoization)(to avoid recomputing the same thing over and over)

4. Derive an iterative algorithm by solving the subproblems in a good order

(iterative algorithms are often better in practice, easier to analyse and prettier)

- in terms of answers to subproblems.

2. Write down a naive recursive algorithm(typically this algorithm will take exponential time)

SummaryIntroduction

Page 160: Dynamic Programming

Weighted Interval Scheduling

6

Problem Given an n weighted intervals,find the schedule with largest total weight

2

1

3 5 2

4

7

time

5

Page 161: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

time

2

1

3 5 2

4

75

66

Page 162: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

time

2

1

3 5 2

4

75

66

start time

Page 163: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

time

2

1

3 5 2

4

75

66

start time

finish time

Page 164: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

time

2

1

3 5 2

4

75

66

start time

finish timeweight

Page 165: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

time

2

1

3 5 2

4

75

6

4

weight

6

start time

finish timeweight

Page 166: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

time

2

1

3 5 2

4

75

6

4

weight

6

Page 167: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

time

2

1

3 5 2

4

75

6

4

weight

6 4

Page 168: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

time

2

1

3 5 2

4

75

6

4

weight

6

compatible intervals don’t overlap

4

Page 169: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

time

2

1

3 5 2

4

75

6

4

weight

6

1

incompatible intervals overlap

Page 170: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

time

2

1

3 5 2

4

75

6

4

weight

6

1

incompatible intervals overlap

Two intervals are compatible if they don’t overlap

Page 171: Dynamic Programming

Weighted Interval Scheduling

6

Problem Given an n weighted intervals,find the schedule with largest total weight

2

1

3 5 2

4

7

time

5

4

weight

Two intervals are compatible if they don’t overlap

Page 172: Dynamic Programming

Weighted Interval Scheduling

6

Problem Given an n weighted intervals,find the schedule with largest total weight

2

1

3 5 2

4

7

time

5

4

weight

Two intervals are compatible if they don’t overlap

A schedule is a set of compatible intervals

Page 173: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

time

2

1

3 5 2

4

75

6

4

weight

6

Two intervals are compatible if they don’t overlap

A schedule is a set of compatible intervals

a schedule

3 2

4

Page 174: Dynamic Programming

Weighted Interval Scheduling

6

Problem Given an n weighted intervals,find the schedule with largest total weight

2

1

3 5 2

4

7

time

5

4

weight

Two intervals are compatible if they don’t overlap

A schedule is a set of compatible intervals

a different schedule

2

1 7

Page 175: Dynamic Programming

Weighted Interval Scheduling

6

Problem Given an n weighted intervals,find the schedule with largest total weight

2

1

3 5 2

4

7

time

5

4

weight

Two intervals are compatible if they don’t overlap

A schedule is a set of compatible intervals

Page 176: Dynamic Programming

Weighted Interval Scheduling

6

Problem Given an n weighted intervals,find the schedule with largest total weight

2

1

3 5 2

4

7

time

5

4

weight

Two intervals are compatible if they don’t overlap

A schedule is a set of compatible intervals

The weight of a schedule is the sum ofthe weight of the intervals it contains

Page 177: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

time

2

1

3 5 2

4

75

6

4

weight

6

Two intervals are compatible if they don’t overlap

A schedule is a set of compatible intervals

The weight of a schedule is the sum ofthe weight of the intervals it contains

a schedule

3 2

with total weight 15

4

Page 178: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

time

2

1

3 5 2

4

75

6

4

weight

Two intervals are compatible if they don’t overlap

A schedule is a set of compatible intervals

The weight of a schedule is the sum ofthe weight of the intervals it contains

a different schedule

2

1 7

with total weight 10

Page 179: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

time

2

1

3 5 2

4

75

6

4

weight

6

Two intervals are compatible if they don’t overlap

A schedule is a set of compatible intervals

The weight of a schedule is the sum ofthe weight of the intervals it contains

a schedule

3 2

with total weight 15

4

Page 180: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

time

2

1

3 5 2

4

75

6

4

weight

6

Two intervals are compatible if they don’t overlap

A schedule is a set of compatible intervals

The weight of a schedule is the sum ofthe weight of the intervals it contains

a schedule

3 2

with total weight 15

is this the best possible?

4

Page 181: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

time

2

1

3 5 2

4

75

6

4

weight

6

Two intervals are compatible if they don’t overlap

A schedule is a set of compatible intervals

The weight of a schedule is the sum ofthe weight of the intervals it contains

is this the best possible?

5

6

with total weight 17a schedule

2

4

Page 182: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

time

2

1

3 5 2

4

75

6

4

weight

6

Two intervals are compatible if they don’t overlap

A schedule is a set of compatible intervals

The weight of a schedule is the sum ofthe weight of the intervals it contains

is this the best possible?

5

6

with total weight 17a schedule

2

4

Page 183: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

time

2

1

3 5 2

4

75

6

4

weight

6

Two intervals are compatible if they don’t overlap

A schedule is a set of compatible intervals

The weight of a schedule is the sum ofthe weight of the intervals it contains

with total weight 18

7

a schedule

is this the best possible?

5

Page 184: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

time

2

1

3 5 2

4

75

6

4

weight

6

Two intervals are compatible if they don’t overlap

A schedule is a set of compatible intervals

The weight of a schedule is the sum ofthe weight of the intervals it contains

with total weight 18

7

a schedule

is this the best possible?

5

Page 185: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

time

2

1

3 5 2

4

75

6

4

weight

6

Two intervals are compatible if they don’t overlap

A schedule is a set of compatible intervals

The weight of a schedule is the sum ofthe weight of the intervals it contains

with total weight 18

7

a schedule

is this the best possible?

5

yes

Page 186: Dynamic Programming

Weighted Interval Scheduling

6

Problem Given an n weighted intervals,find the schedule with largest total weight

2

1

3 5 2

4

7

time

5

4

weight

Page 187: Dynamic Programming

Weighted Interval Scheduling

6

Problem Given an n weighted intervals,find the schedule with largest total weight

2

1

3 5 2

4

7

time

5

4

weight

How is the input provided?

Page 188: Dynamic Programming

Weighted Interval Scheduling

6

Problem Given an n weighted intervals,find the schedule with largest total weight

2

1

3 5 2

4

7

time

5

4

weight

How is the input provided?

The intervals are given in an array A of length n

Page 189: Dynamic Programming

Weighted Interval Scheduling

6

Problem Given an n weighted intervals,find the schedule with largest total weight

2

1

3 5 2

4

7

time

5

4

weight

How is the input provided?

The intervals are given in an array A of length n

A[i] stores a triple (si, fi, wi) which defines the i-th interval

Page 190: Dynamic Programming

Weighted Interval Scheduling

6

Problem Given an n weighted intervals,find the schedule with largest total weight

2

1

3 5 2

4

7

time

5

start time, si

4

weight

How is the input provided?

The intervals are given in an array A of length n

A[i] stores a triple (si, fi, wi) which defines the i-th interval

Page 191: Dynamic Programming

Weighted Interval Scheduling

6

Problem Given an n weighted intervals,find the schedule with largest total weight

2

1

3 5 2

4

7

time

5

start time, si

finish time, fi

4

weight

How is the input provided?

The intervals are given in an array A of length n

A[i] stores a triple (si, fi, wi) which defines the i-th interval

Page 192: Dynamic Programming

Weighted Interval Scheduling

6

Problem Given an n weighted intervals,find the schedule with largest total weight

2

1

3 5 2

4

7

time

5

start time, si

finish time, fiweight, wi

4

weight

How is the input provided?

The intervals are given in an array A of length n

A[i] stores a triple (si, fi, wi) which defines the i-th interval

Page 193: Dynamic Programming

Weighted Interval Scheduling

6

Problem Given an n weighted intervals,find the schedule with largest total weight

2

1

3 5 2

4

7

time

5

start time, si

finish time, fiweight, wi

How is the input provided?

The intervals are given in an array A of length n

A[i] stores a triple (si, fi, wi) which defines the i-th interval

4

weight, wi

Page 194: Dynamic Programming

Weighted Interval Scheduling

6

Problem Given an n weighted intervals,find the schedule with largest total weight

2

1

3 5 2

4

7

time

5

start time, si

finish time, fiweight, wi

How is the input provided?

The intervals are given in an array A of length n

A[i] stores a triple (si, fi, wi) which defines the i-th interval

The intervals are sorted by finish time i.e. fi 6 fi+1 4

weight, wi

Page 195: Dynamic Programming

Weighted Interval Scheduling

6

Problem Given an n weighted intervals,find the schedule with largest total weight

2

1

3 5 2

4

75

4

weight, wi

time

Page 196: Dynamic Programming

Weighted Interval Scheduling

6

Problem Given an n weighted intervals,find the schedule with largest total weight

2

1

3 5 2

4

75

4

weight, wi

The intervals in the input are sorted by finish time

interval i finishes before interval i+ 1 finishes

time

Page 197: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

4

weight, wi

2

1

3 5 2

4

75

62

interval 1

The intervals in the input are sorted by finish time

interval i finishes before interval i+ 1 finishes

time

Page 198: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

4

weight, wi

2

1

3 5 2

4

75

6

5

interval 2

The intervals in the input are sorted by finish time

interval i finishes before interval i+ 1 finishes

time

Page 199: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

4

weight, wi

2

1

3 5 2

4

75

6

3

interval 3

The intervals in the input are sorted by finish time

interval i finishes before interval i+ 1 finishes

time

Page 200: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

4

weight, wi

2

1

3 5 2

4

75

6

1

interval 4

The intervals in the input are sorted by finish time

interval i finishes before interval i+ 1 finishes

time

Page 201: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

4

weight, wi

2

1

3 5 2

4

75

66

interval 5

The intervals in the input are sorted by finish time

interval i finishes before interval i+ 1 finishes

time

Page 202: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

4

weight, wi

2

1

3 5 2

4

75

6

5

interval 6

The intervals in the input are sorted by finish time

interval i finishes before interval i+ 1 finishes

time

Page 203: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

4

weight, wi

2

1

3 5 2

4

75

6 4

interval 7

The intervals in the input are sorted by finish time

interval i finishes before interval i+ 1 finishes

time

Page 204: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

4

weight, wi

2

1

3 5 2

4

75

6

7

interval 8

The intervals in the input are sorted by finish time

interval i finishes before interval i+ 1 finishes

time

Page 205: Dynamic Programming

Weighted Interval Scheduling

Problem Given an n weighted intervals,find the schedule with largest total weight

4

weight, wi

2

1

3 5 2

4

75

6

2

interval 9

The intervals in the input are sorted by finish time

interval i finishes before interval i+ 1 finishes

time

Page 206: Dynamic Programming

Weighted Interval Scheduling

6

Problem Given an n weighted intervals,find the schedule with largest total weight

2

1

3 5 2

4

75

4

weight, wi

The intervals in the input are sorted by finish time

interval i finishes before interval i+ 1 finishes

time

Page 207: Dynamic Programming

Compatible Intervals

interval p(7)

interval 7

Page 208: Dynamic Programming

Compatible Intervals

Let p(i) be the rightmost interval (in order of finish time)which finishes before the i-th interval but doesn’t overlap it

interval p(7)

interval 7

For all i,

Page 209: Dynamic Programming

Compatible Intervals

Let p(i) be the rightmost interval (in order of finish time)which finishes before the i-th interval but doesn’t overlap it

For all i,

interval p(4)interval 4

Page 210: Dynamic Programming

Compatible Intervals

Let p(i) be the rightmost interval (in order of finish time)which finishes before the i-th interval but doesn’t overlap it

For all i,

interval 2

Page 211: Dynamic Programming

Compatible Intervals

Let p(i) be the rightmost interval (in order of finish time)which finishes before the i-th interval but doesn’t overlap it

For all i,

interval 2

What is p(2)?

Page 212: Dynamic Programming

Compatible Intervals

Let p(i) be the rightmost interval (in order of finish time)which finishes before the i-th interval but doesn’t overlap it

For all i,

interval 2

What is p(2)?

if no such interval exists, p(i) = 0

Page 213: Dynamic Programming

Compatible Intervals

Let p(i) be the rightmost interval (in order of finish time)which finishes before the i-th interval but doesn’t overlap it

interval p(7)

interval 7

For all i,

if no such interval exists, p(i) = 0

Page 214: Dynamic Programming

Compatible Intervals

Let p(i) be the rightmost interval (in order of finish time)which finishes before the i-th interval but doesn’t overlap it

Claim: We can precompute all p(i) in O(n logn) time

interval p(7)

interval 7

For all i,

if no such interval exists, p(i) = 0

Page 215: Dynamic Programming

Compatible Intervals

Let p(i) be the rightmost interval (in order of finish time)which finishes before the i-th interval but doesn’t overlap it

Claim: We can precompute all p(i) in O(n logn) time

interval p(7)

interval 7

For all i,

if no such interval exists, p(i) = 0

(and we’ll assume we did this already)

Page 216: Dynamic Programming

Compatible Intervals

Let p(i) be the rightmost interval (in order of finish time)which finishes before the i-th interval but doesn’t overlap it

Claim: We can precompute all p(i) in O(n logn) time

interval p(7)

interval 7

For all i,

if no such interval exists, p(i) = 0

- we’ll come back to this at the end

(and we’ll assume we did this already)

Page 217: Dynamic Programming

1. Find a recursive formula

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

more intervals not shown

Page 218: Dynamic Programming

1. Find a recursive formula

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

In particular, consider the n-th interval . . .

more intervals not shown

Page 219: Dynamic Programming

1. Find a recursive formula

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

In particular, consider the n-th interval . . .

more intervals not shown

n-th interval

Page 220: Dynamic Programming

1. Find a recursive formula

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

more intervals not shown

n-th interval

Page 221: Dynamic Programming

1. Find a recursive formula

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

Either the n-th interval is in scheduleO. . . or it isn’t

more intervals not shown

n-th interval

Page 222: Dynamic Programming

1. Find a recursive formula

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

Either the n-th interval is in scheduleO. . . or it isn’t

this gives us two cases to consider:

more intervals not shown

n-th interval

Page 223: Dynamic Programming

1. Find a recursive formula

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

Either the n-th interval is in scheduleO. . . or it isn’t

this gives us two cases to consider:

more intervals not shown

n-th interval

Case 2: The n-th interval is inO

Case 1: The n-th interval is not inO

Page 224: Dynamic Programming

1. Find a recursive formula

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

more intervals not shown

n-th interval

Case 1: The n-th interval is not inO

Page 225: Dynamic Programming

1. Find a recursive formula

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

more intervals not shown

n-th interval

Case 1: The n-th interval is not inO

is not inO

Page 226: Dynamic Programming

1. Find a recursive formula

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

more intervals not shown

n-th interval

Case 1: The n-th interval is not inO

- scheduleO is also an optimal schedule for the problem

with the input consisting of intervals {1, 2, 3 . . . , n− 1}

is not inO

Page 227: Dynamic Programming

1. Find a recursive formula

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

n-th interval

Case 1: The n-th interval is not inO

- scheduleO is also an optimal schedule for the problem

with the input consisting of intervals {1, 2, 3 . . . , n− 1}

is not inO

more intervals not shown

Page 228: Dynamic Programming

1. Find a recursive formula

so, in this case we have that OPT = OPT(n− 1)

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

n-th interval

Case 1: The n-th interval is not inO

- scheduleO is also an optimal schedule for the problem

with the input consisting of intervals {1, 2, 3 . . . , n− 1}

is not inO

more intervals not shown

Page 229: Dynamic Programming

1. Find a recursive formula

so, in this case we have that OPT = OPT(n− 1)

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

n-th interval

Case 1: The n-th interval is not inO

Notation: OPT(i) is the weight of an optimal schedule for intervals {1, 2, 3, . . . , i}

- scheduleO is also an optimal schedule for the problem

with the input consisting of intervals {1, 2, 3 . . . , n− 1}

is not inO

more intervals not shown

Page 230: Dynamic Programming

1. Find a recursive formula

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

more intervals not shown

n-th interval

Notation: OPT(i) is the weight of an optimal schedule for intervals {1, 2, 3, . . . , i}

Case 2: The n-th interval is inO

is inO

Page 231: Dynamic Programming

1. Find a recursive formula

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

more intervals not shown

n-th interval

Notation: OPT(i) is the weight of an optimal schedule for intervals {1, 2, 3, . . . , i}

Case 2: The n-th interval is inO

is inO

The only other intervals which could be inO are {1, 2, 3, . . . p(n)}

Page 232: Dynamic Programming

1. Find a recursive formula

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

more intervals not shown

n-th interval

Notation: OPT(i) is the weight of an optimal schedule for intervals {1, 2, 3, . . . , i}

Case 2: The n-th interval is inO

is inO

The only other intervals which could be inO are {1, 2, 3, . . . p(n)}

not inO (they overlap)

Page 233: Dynamic Programming

1. Find a recursive formula

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

n-th interval

Notation: OPT(i) is the weight of an optimal schedule for intervals {1, 2, 3, . . . , i}

Case 2: The n-th interval is inO

is inO

The only other intervals which could be inO are {1, 2, 3, . . . p(n)}

not inO (they overlap)

more intervals not shown

Page 234: Dynamic Programming

1. Find a recursive formula

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

n-th interval

Notation: OPT(i) is the weight of an optimal schedule for intervals {1, 2, 3, . . . , i}

Case 2: The n-th interval is inO

is inO

The only other intervals which could be inO are {1, 2, 3, . . . p(n)}

not inO (they overlap)

more intervals not shown

interval p(n)

Page 235: Dynamic Programming

1. Find a recursive formula

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

n-th interval

Notation: OPT(i) is the weight of an optimal schedule for intervals {1, 2, 3, . . . , i}

Case 2: The n-th interval is inO

is inO

The only other intervals which could be inO are {1, 2, 3, . . . p(n)}

not inO (they overlap)

(the ones which don’t overlap the n-th interval)

more intervals not shown

interval p(n)

Page 236: Dynamic Programming

1. Find a recursive formula

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

n-th interval

Notation: OPT(i) is the weight of an optimal schedule for intervals {1, 2, 3, . . . , i}

Case 2: The n-th interval is inO

is inO

The only other intervals which could be inO are {1, 2, 3, . . . p(n)}

not inO (they overlap)

(the ones which don’t overlap the n-th interval)

more intervals not shown

for the intervals {1, 2, 3 . . . , p(n)}ScheduleO with interval n removed gives an optimal schedule

interval p(n)

Page 237: Dynamic Programming

1. Find a recursive formula

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

n-th interval

Notation: OPT(i) is the weight of an optimal schedule for intervals {1, 2, 3, . . . , i}

Case 2: The n-th interval is inO

is inO

The only other intervals which could be inO are {1, 2, 3, . . . p(n)}

not inO (they overlap)

(the ones which don’t overlap the n-th interval)

more intervals not shown

for the intervals {1, 2, 3 . . . , p(n)}ScheduleO with interval n removed gives an optimal schedule

interval p(n)

(wn is the weight of interval n)

Page 238: Dynamic Programming

1. Find a recursive formula

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

n-th interval

Notation: OPT(i) is the weight of an optimal schedule for intervals {1, 2, 3, . . . , i}

Case 2: The n-th interval is inO

is inO

The only other intervals which could be inO are {1, 2, 3, . . . p(n)}

not inO (they overlap)

(the ones which don’t overlap the n-th interval)

more intervals not shown

for the intervals {1, 2, 3 . . . , p(n)}ScheduleO with interval n removed gives an optimal schedule

interval p(n)

(wn is the weight of interval n)

so we have that OPT = OPT(p(n)) + wn

Page 239: Dynamic Programming

1. Find a recursive formula

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

more intervals not shown

n-th interval

Notation: OPT(i) is the weight of an optimal schedule for intervals {1, 2, 3, . . . , i}

(wn is the weight of interval n)

Case 2: The n-th interval is inOCase 1: The n-th interval is not inOOPT = OPT(n− 1) OPT = OPT(p(n)) + wn

Page 240: Dynamic Programming

1. Find a recursive formula

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

more intervals not shown

n-th interval

Notation: OPT(i) is the weight of an optimal schedule for intervals {1, 2, 3, . . . , i}

(wn is the weight of interval n)

Case 2: The n-th interval is inOCase 1: The n-th interval is not inOOPT = OPT(n− 1) OPT = OPT(p(n)) + wn

Well, which is it?

Page 241: Dynamic Programming

1. Find a recursive formula

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

more intervals not shown

n-th interval

Notation: OPT(i) is the weight of an optimal schedule for intervals {1, 2, 3, . . . , i}

(wn is the weight of interval n)

Case 2: The n-th interval is inOCase 1: The n-th interval is not inOOPT = OPT(n− 1) OPT = OPT(p(n)) + wn

Well, which is it? It’s the bigger one

Page 242: Dynamic Programming

1. Find a recursive formula

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

more intervals not shown

n-th interval

Notation: OPT(i) is the weight of an optimal schedule for intervals {1, 2, 3, . . . , i}

(wn is the weight of interval n)

Case 2: The n-th interval is inOCase 1: The n-th interval is not inOOPT = OPT(n− 1) OPT = OPT(p(n)) + wn

Well, which is it? It’s the bigger one

OPT = max(OPT(n− 1),OPT(p(n)) + wn)

Page 243: Dynamic Programming

1. Find a recursive formula

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , n} with weight OPT. . .

more intervals not shown

n-th interval

Notation: OPT(i) is the weight of an optimal schedule for intervals {1, 2, 3, . . . , i}

(wn is the weight of interval n)

Case 2: The n-th interval is inOCase 1: The n-th interval is not inOOPT = OPT(n− 1) OPT = OPT(p(n)) + wn

Well, which is it? It’s the bigger one

OPT = max(OPT(n− 1),OPT(p(n)) + wn)

(they both always give viable schedules)

Page 244: Dynamic Programming

1. Find a recursive formula

Consider some optimal scheduleO for intervals {1, 2, 3 . . . , i} with weight OPT(i). . .

more intervals not shown

i-th interval

Notation: OPT(i) is the weight of an optimal schedule for intervals {1, 2, 3, . . . , i}

Case 2: The i-th interval is inOCase 1: The i-th interval is not inOOPT(i) = OPT(i− 1) OPT(i) = OPT(p(i)) + wi

Well, which is it? It’s the bigger one

OPT(i) = max(OPT(i− 1),OPT(p(i)) + wi)

(they both always give viable schedules)

(wi is the weight of interval i)

Page 245: Dynamic Programming

2. Write down a recursive algorithm

Once again, we can use the recursive formula to get a recursive algorithm. . .

If (i = 0)

Return 0

Return max(WIS(i− 1),WIS(p(i)) + wi

)WIS(i)

Therefore, WIS(n) gives the weight of the optimal schedule(for the full problem)

WIS(i) computes the weight of an optimal schedule for intervals {1, 2, 3, . . . , i}

Page 246: Dynamic Programming

2. Write down a recursive algorithm

Once again, we can use the recursive formula to get a recursive algorithm. . .

If (i = 0)

Return 0

Return max(WIS(i− 1),WIS(p(i)) + wi

)WIS(i)

Therefore, WIS(n) gives the weight of the optimal schedule(for the full problem)

What is the time complexity of this algorithm?

WIS(i) computes the weight of an optimal schedule for intervals {1, 2, 3, . . . , i}

Page 247: Dynamic Programming

How efficient is the recursive algorithm?

consider this simple input with n = 6

11

11

11

If (i = 0)

Return 0

Return max(WIS(i− 1),WIS(p(i)) + wi

)WIS(i)

Page 248: Dynamic Programming

How efficient is the recursive algorithm?

consider this simple input with n = 6

If (i = 0)

Return 0

Return max(WIS(i− 1),WIS(p(i)) + wi

)WIS(i)

11

11

11

(the best schedule has weight 3)

Page 249: Dynamic Programming

How efficient is the recursive algorithm?

consider this simple input with n = 6

If (i = 0)

Return 0

Return max(WIS(i− 1),WIS(p(i)) + wi

)WIS(i)

11

11

11

(the best schedule has weight 3)

further, for all i, p(i) = i− 2

Page 250: Dynamic Programming

How efficient is the recursive algorithm?

consider this simple input with n = 6

If (i = 0)

Return 0

Return max(WIS(i− 1),WIS(p(i)) + wi

)WIS(i)

11

11

11

(the best schedule has weight 3)

further, for all i, p(i) = i− 2

so WIS(i) makes recursive calls to WIS(i− 1) and WIS(i− 2)

Page 251: Dynamic Programming

How efficient is the recursive algorithm?

If (i = 0)

Return 0

Return max(WIS(i− 1),WIS(p(i)) + wi

)WIS(i)

so WIS(i) makes recursive calls to WIS(i− 1) and WIS(i− 2)

Page 252: Dynamic Programming

How efficient is the recursive algorithm?

If (i = 0)

Return 0

Return max(WIS(i− 1),WIS(p(i)) + wi

)WIS(i)

so WIS(i) makes recursive calls to WIS(i− 1) and WIS(i− 2)

6WIS(6)

Page 253: Dynamic Programming

How efficient is the recursive algorithm?

If (i = 0)

Return 0

Return max(WIS(i− 1),WIS(p(i)) + wi

)WIS(i)

so WIS(i) makes recursive calls to WIS(i− 1) and WIS(i− 2)

6WIS(6)

5 4

Page 254: Dynamic Programming

How efficient is the recursive algorithm?

If (i = 0)

Return 0

Return max(WIS(i− 1),WIS(p(i)) + wi

)WIS(i)

so WIS(i) makes recursive calls to WIS(i− 1) and WIS(i− 2)

6WIS(6)

5 43 23

334

Page 255: Dynamic Programming

How efficient is the recursive algorithm?

If (i = 0)

Return 0

Return max(WIS(i− 1),WIS(p(i)) + wi

)WIS(i)

so WIS(i) makes recursive calls to WIS(i− 1) and WIS(i− 2)

6WIS(6)

5 43 2

02 1 1

33

2 1

343 23

Page 256: Dynamic Programming

How efficient is the recursive algorithm?

If (i = 0)

Return 0

Return max(WIS(i− 1),WIS(p(i)) + wi

)WIS(i)

so WIS(i) makes recursive calls to WIS(i− 1) and WIS(i− 2)

6WIS(6)

5 43 2

02

1 0

1

0 0

1

0 0

33

2

1 0

1

0 0

343 2

02 1 1

3

Page 257: Dynamic Programming

How efficient is the recursive algorithm?

If (i = 0)

Return 0

Return max(WIS(i− 1),WIS(p(i)) + wi

)WIS(i)

so WIS(i) makes recursive calls to WIS(i− 1) and WIS(i− 2)

6WIS(6)

5 43 2

02

1 00 0

1

0 0

1

0 0

33

2

1 00 0

1

0 0

343 2

02

1 0

1

0 0

1

0 0

3

Page 258: Dynamic Programming

How efficient is the recursive algorithm?

If (i = 0)

Return 0

Return max(WIS(i− 1),WIS(p(i)) + wi

)WIS(i)

so WIS(i) makes recursive calls to WIS(i− 1) and WIS(i− 2)

6WIS(6)

5 43 2

02

1 00 0

1

0 0

1

0 0

33

2

1 00 0

1

0 0

343 2

02

1 00 0

1

0 0

1

0 0

3

Page 259: Dynamic Programming

How efficient is the recursive algorithm?

If (i = 0)

Return 0

Return max(WIS(i− 1),WIS(p(i)) + wi

)WIS(i)

so WIS(i) makes recursive calls to WIS(i− 1) and WIS(i− 2)

6WIS(6)

5 43 2

02

1 00 0

1

0 0

1

0 0

33

2

1 00 0

1

0 0

343 2

02

1 00 0

1

0 0

1

0 0

3

This doesn’t look good (but it does look familiar)

Page 260: Dynamic Programming

How efficient is the recursive algorithm?

11

11

11

If (i = 0)

Return 0

Return max(WIS(i− 1),WIS(p(i)) + wi

)WIS(i)

if we extend this input in the same way. . .

Page 261: Dynamic Programming

How efficient is the recursive algorithm?

11

11

11

If (i = 0)

Return 0

Return max(WIS(i− 1),WIS(p(i)) + wi

)WIS(i)

if we extend this input in the same way. . .

1

Page 262: Dynamic Programming

How efficient is the recursive algorithm?

11

11

11

If (i = 0)

Return 0

Return max(WIS(i− 1),WIS(p(i)) + wi

)WIS(i)

if we extend this input in the same way. . .

11

Page 263: Dynamic Programming

How efficient is the recursive algorithm?

11

11

11

If (i = 0)

Return 0

Return max(WIS(i− 1),WIS(p(i)) + wi

)WIS(i)

if we extend this input in the same way. . .

11

1

Page 264: Dynamic Programming

How efficient is the recursive algorithm?

11

11

11

If (i = 0)

Return 0

Return max(WIS(i− 1),WIS(p(i)) + wi

)WIS(i)

if we extend this input in the same way. . .

11

11

Page 265: Dynamic Programming

How efficient is the recursive algorithm?

11

11

11

If (i = 0)

Return 0

Return max(WIS(i− 1),WIS(p(i)) + wi

)WIS(i)

if we extend this input in the same way. . .

11

11

Given n intervals set out in this manner,

Page 266: Dynamic Programming

How efficient is the recursive algorithm?

11

11

11

If (i = 0)

Return 0

Return max(WIS(i− 1),WIS(p(i)) + wi

)WIS(i)

if we extend this input in the same way. . .

11

11

Given n intervals set out in this manner,

WIS(n) runs in exponential time

Page 267: Dynamic Programming

How efficient is the recursive algorithm?

11

11

11

If (i = 0)

Return 0

Return max(WIS(i− 1),WIS(p(i)) + wi

)WIS(i)

if we extend this input in the same way. . .

11

11

Given n intervals set out in this manner,

WIS(n) runs in exponential time

If T (n) is the run time of WIS(n) using these intervals

then T (n) > 2T (n− 2)

Page 268: Dynamic Programming

3. Store the solutions to subproblems

If (i = 0)

Return 0

If WIS[i] undefined

WIS[i]= max(MemWIS(i− 1),MemWIS(p(i)) + wi

)Return WIS[i]

MemWIS(i)

Page 269: Dynamic Programming

3. Store the solutions to subproblems

In the MemWIS version of the algorithmwe store solutions to previously computed subproblems

in an n length array called WIS

If (i = 0)

Return 0

If WIS[i] undefined

WIS[i]= max(MemWIS(i− 1),MemWIS(p(i)) + wi

)Return WIS[i]

MemWIS(i)

Page 270: Dynamic Programming

3. Store the solutions to subproblems

(we have memoized the algorithm)

In the MemWIS version of the algorithmwe store solutions to previously computed subproblems

in an n length array called WIS

If (i = 0)

Return 0

If WIS[i] undefined

WIS[i]= max(MemWIS(i− 1),MemWIS(p(i)) + wi

)Return WIS[i]

MemWIS(i)

Page 271: Dynamic Programming

3. Store the solutions to subproblems

(we have memoized the algorithm)

In the MemWIS version of the algorithmwe store solutions to previously computed subproblems

in an n length array called WIS

Each entry WIS[i] is only computed once

If (i = 0)

Return 0

If WIS[i] undefined

WIS[i]= max(MemWIS(i− 1),MemWIS(p(i)) + wi

)Return WIS[i]

MemWIS(i)

Page 272: Dynamic Programming

3. Store the solutions to subproblems

(we have memoized the algorithm)

In the MemWIS version of the algorithmwe store solutions to previously computed subproblems

in an n length array called WIS

Each entry WIS[i] is only computed once

The time complexity of computing MemWIS(n) is now O(n)

If (i = 0)

Return 0

If WIS[i] undefined

WIS[i]= max(MemWIS(i− 1),MemWIS(p(i)) + wi

)Return WIS[i]

MemWIS(i)

Page 273: Dynamic Programming

3. Store the solutions to subproblems

(we have memoized the algorithm)

In the MemWIS version of the algorithmwe store solutions to previously computed subproblems

in an n length array called WIS

Each entry WIS[i] is only computed once

The time complexity of computing MemWIS(n) is now O(n)

If (i = 0)

Return 0

If WIS[i] undefined

WIS[i]= max(MemWIS(i− 1),MemWIS(p(i)) + wi

)Return WIS[i]

MemWIS(i)

because every recursion causes an unfilled entry to be filled in the array

Page 274: Dynamic Programming

The dependency graph

What information do we need to compute WIS[i]?

The array

WIS:

Page 275: Dynamic Programming

The dependency graph

What information do we need to compute WIS[i]?

The array

WIS:

WIS[i]

Page 276: Dynamic Programming

The dependency graph

What information do we need to compute WIS[i]?

to compute

The array

WIS:

WIS[i− 1] and WIS[p(i)]

WIS[i]

WIS[i] we need

Page 277: Dynamic Programming

The dependency graph

What information do we need to compute WIS[i]?

to compute

The array

WIS:

WIS[i− 1] and WIS[p(i)]

WIS[i]

WIS[i] we need

WIS[i− 1]

Page 278: Dynamic Programming

The dependency graph

What information do we need to compute WIS[i]?

to compute

The array

WIS:

WIS[i− 1] and WIS[p(i)]

WIS[i]

WIS[i] we need

WIS[i− 1]

WIS[p(i)]

Page 279: Dynamic Programming

The dependency graph

What information do we need to compute WIS[i]?

to compute

The array

WIS:

WIS[i− 1] and WIS[p(i)]

WIS[i]

WIS[i] we need

WIS[i− 1]

WIS[p(i)]

both of which are to the left of WIS[i]

Page 280: Dynamic Programming

The dependency graph

What information do we need to compute WIS[i]?

to compute

The array

WIS:

WIS[i− 1] and WIS[p(i)]

WIS[i]

WIS[i] we need

WIS[i− 1]

WIS[p(i)]

both of which are to the left of WIS[i](somewhere)

Page 281: Dynamic Programming

The dependency graph

What information do we need to compute WIS[i]?

to compute

The array

WIS:

WIS[i− 1] and WIS[p(i)]

WIS[i]

WIS[i] we need

WIS[i− 1]

WIS[p(i)]

both of which are to the left of WIS[i](somewhere)

Page 282: Dynamic Programming

The dependency graph

What information do we need to compute WIS[i]?

to compute

The array

WIS:

WIS[i− 1] and WIS[p(i)]

WIS[i]

WIS[i] we need

WIS[i− 1]

WIS[p(i)]

both of which are to the left of WIS[i](somewhere)

all of the dependencies go left. . .

Page 283: Dynamic Programming

The dependency graph

What information do we need to compute WIS[i]?

to compute

The array

WIS:

WIS[i− 1] and WIS[p(i)]

WIS[i]

WIS[i] we need

WIS[i− 1]

WIS[p(i)]

both of which are to the left of WIS[i](somewhere)

all of the dependencies go left. . .

This suggests another

iterative algorithm

Page 284: Dynamic Programming

The dependency graph

What information do we need to compute WIS[i]?

to compute

The array

WIS:

WIS[i− 1] and WIS[p(i)]

WIS[i]

WIS[i] we need

WIS[i− 1]

WIS[p(i)]

both of which are to the left of WIS[i](somewhere)

all of the dependencies go left. . .

This suggests another

iterative algorithm

Fill in the array from

the left again

Page 285: Dynamic Programming

The dependency graph

What information do we need to compute WIS[i]?

to compute

The array

WIS:

WIS[i− 1] and WIS[p(i)]

WIS[i]

WIS[i] we need

WIS[i− 1]

WIS[p(i)]

both of which are to the left of WIS[i](somewhere)

all of the dependencies go left. . .

This suggests another

iterative algorithm

Fill in the array from

the left again

Page 286: Dynamic Programming

The dependency graph

What information do we need to compute WIS[i]?

to compute

The array

WIS:

WIS[i− 1] and WIS[p(i)]

WIS[i]

WIS[i] we need

WIS[i− 1]

WIS[p(i)]

both of which are to the left of WIS[i](somewhere)

all of the dependencies go left. . .

This suggests another

iterative algorithm

Fill in the array from

the left again

Page 287: Dynamic Programming

The dependency graph

What information do we need to compute WIS[i]?

to compute

The array

WIS:

WIS[i− 1] and WIS[p(i)]

WIS[i]

WIS[i] we need

WIS[i− 1]

WIS[p(i)]

both of which are to the left of WIS[i](somewhere)

all of the dependencies go left. . .

This suggests another

iterative algorithm

Fill in the array from

the left again

Page 288: Dynamic Programming

The dependency graph

What information do we need to compute WIS[i]?

to compute

The array

WIS:

WIS[i− 1] and WIS[p(i)]

WIS[i]

WIS[i] we need

WIS[i− 1]

WIS[p(i)]

both of which are to the left of WIS[i](somewhere)

all of the dependencies go left. . .

This suggests another

iterative algorithm

Fill in the array from

the left again

Page 289: Dynamic Programming

The dependency graph

What information do we need to compute WIS[i]?

to compute

The array

WIS:

WIS[i− 1] and WIS[p(i)]

WIS[i]

WIS[i] we need

WIS[i− 1]

WIS[p(i)]

both of which are to the left of WIS[i](somewhere)

all of the dependencies go left. . .

This suggests another

iterative algorithm

Fill in the array from

the left again

Page 290: Dynamic Programming

The dependency graph

What information do we need to compute WIS[i]?

to compute

The array

WIS:

WIS[i− 1] and WIS[p(i)]

WIS[i]

WIS[i] we need

WIS[i− 1]

WIS[p(i)]

both of which are to the left of WIS[i](somewhere)

all of the dependencies go left. . .

This suggests another

iterative algorithm

Fill in the array from

the left again

Page 291: Dynamic Programming

The dependency graph

What information do we need to compute WIS[i]?

to compute

The array

WIS:

WIS[i− 1] and WIS[p(i)]

WIS[i]

WIS[i] we need

WIS[i− 1]

WIS[p(i)]

both of which are to the left of WIS[i](somewhere)

all of the dependencies go left. . .

This suggests another

iterative algorithm

Fill in the array from

the left again

Page 292: Dynamic Programming

The dependency graph

What information do we need to compute WIS[i]?

to compute

The array

WIS:

WIS[i− 1] and WIS[p(i)]

WIS[i]

WIS[i] we need

WIS[i− 1]

WIS[p(i)]

both of which are to the left of WIS[i](somewhere)

all of the dependencies go left. . .

This suggests another

iterative algorithm

Fill in the array from

the left again

Page 293: Dynamic Programming

The dependency graph

What information do we need to compute WIS[i]?

to compute

The array

WIS:

WIS[i− 1] and WIS[p(i)]

WIS[i]

WIS[i] we need

WIS[i− 1]

WIS[p(i)]

both of which are to the left of WIS[i](somewhere)

all of the dependencies go left. . .

This suggests another

iterative algorithm

Fill in the array from

the left again

Page 294: Dynamic Programming

4. Derive an iterative algorithm

This is an iterative dynamic programming algorithm

it runs in O(n) time

ItWIS(n)

If (i = 0)

Return 0

For i = 1 to n

WIS[i]= max(WIS[i− 1],WIS[p(i)]+wi

)Return WIS[i]

for Weighted Interval Scheduling

Page 295: Dynamic Programming

4. Derive an iterative algorithm

This is an iterative dynamic programming algorithm

it runs in O(n) time

. . . but it requires than you precomputed all the p(i) values

ItWIS(n)

If (i = 0)

Return 0

For i = 1 to n

WIS[i]= max(WIS[i− 1],WIS[p(i)]+wi

)Return WIS[i]

for Weighted Interval Scheduling

Page 296: Dynamic Programming

How do you find all those p(i) values?

Revised Claim: We can precompute any p(i) in O(logn) time

interval p(7)

interval 7

Page 297: Dynamic Programming

How do you find all those p(i) values?

Revised Claim: We can precompute any p(i) in O(logn) time

interval p(7)

interval 7

Recall that si is the start time of interval iand fi is the finish time of interval i

Page 298: Dynamic Programming

How do you find all those p(i) values?

Revised Claim: We can precompute any p(i) in O(logn) time

interval p(7)

interval 7

Recall that si is the start time of interval i

We want to find the unique value j = p(i) such that

and fi is the finish time of interval i

fj < si < fj+1.

Page 299: Dynamic Programming

How do you find all those p(i) values?

Revised Claim: We can precompute any p(i) in O(logn) time

interval p(7)

interval 7

Recall that si is the start time of interval i

We want to find the unique value j = p(i) such that

and fi is the finish time of interval i

fj < si < fj+1.

si

f1 f2 f3 f4 f5f6f7

Page 300: Dynamic Programming

How do you find all those p(i) values?

Revised Claim: We can precompute any p(i) in O(logn) time

interval p(7)

interval 7

Recall that si is the start time of interval i

We want to find the unique value j = p(i) such that

and fi is the finish time of interval i

fj < si < fj+1.

As the input is sorted by finish times, we can find j by binary search in O(logn) time

si

f1 f2 f3 f4 f5f6f7

Page 301: Dynamic Programming

How do you find all those p(i) values?

Revised Claim: We can precompute any p(i) in O(logn) time

interval p(7)

interval 7

Page 302: Dynamic Programming

How do you find all those p(i) values?

Revised Claim: We can precompute any p(i) in O(logn) time

interval p(7)

interval 7

Original Claim: We can precompute all p(i) in O(n logn) time

Page 303: Dynamic Programming

How do you find all those p(i) values?

Revised Claim: We can precompute any p(i) in O(logn) time

interval p(7)

interval 7

Original Claim: We can precompute all p(i) in O(n logn) time

(by using the revised claim n times)

Page 304: Dynamic Programming

Wait, did you want the actual schedule?

but doesn’t find the actual scheduleItWIS(n) finds the weight of the optimal schedule

ItWIS(n)

If (i = 0)

Return 0

For i = 1 to n

WIS[i]= max(WIS[i− 1],WIS[p(i)]+wi

)Return WIS[i]

Page 305: Dynamic Programming

Wait, did you want the actual schedule?

but doesn’t find the actual schedule

There is an optimal schedule for {1, 2, . . . , i} containing

interval i if and only if

WIS[i− 1]6WIS[p(i)]+wi

ItWIS(n) finds the weight of the optimal schedule

ItWIS(n)

If (i = 0)

Return 0

For i = 1 to n

WIS[i]= max(WIS[i− 1],WIS[p(i)]+wi

)Return WIS[i]

Page 306: Dynamic Programming

Wait, did you want the actual schedule?

but doesn’t find the actual schedule

There is an optimal schedule for {1, 2, . . . , i} containing

interval i if and only if

WIS[i− 1]6WIS[p(i)]+wi

(by the argument we saw earlier)

ItWIS(n) finds the weight of the optimal schedule

ItWIS(n)

If (i = 0)

Return 0

For i = 1 to n

WIS[i]= max(WIS[i− 1],WIS[p(i)]+wi

)Return WIS[i]

Page 307: Dynamic Programming

Wait, did you want the actual schedule?

There is an optimal schedule for {1, 2, . . . , i} containing

interval i if and only if

WIS[i− 1]6WIS[p(i)]+wi

(by the argument we saw earlier)

ItWIS(n) finds the weight of the optimal scheduleand FINDWIS(n) finds the actual schedule

FindWIS(i)

If (i = 0)

Return nothing

If WIS[i− 1]6WIS[p(i)]+wi

Return FindWIS(p(i)) then i

Return FindWIS(i− 1)

This is called backtracking and works for lots of Dynamic Programming algorithms

ItWIS(n)

If (i = 0)

Return 0

For i = 1 to n

WIS[i]= max(WIS[i− 1],WIS[p(i)]+wi

)Return WIS[i]

Page 308: Dynamic Programming

Wait, did you want the actual schedule?

There is an optimal schedule for {1, 2, . . . , i} containing

interval i if and only if

WIS[i− 1]6WIS[p(i)]+wi

(by the argument we saw earlier)

ItWIS(n) finds the weight of the optimal scheduleand FINDWIS(n) finds the actual schedule

FindWIS(i)

If (i = 0)

Return nothing

If WIS[i− 1]6WIS[p(i)]+wi

Return FindWIS(p(i)) then i

Return FindWIS(i− 1)

This is called backtracking and works for lots of Dynamic Programming algorithms

ItWIS(n)

If (i = 0)

Return 0

For i = 1 to n

WIS[i]= max(WIS[i− 1],WIS[p(i)]+wi

)Return WIS[i]

Page 309: Dynamic Programming

The final algorithm

ItWIS(n) finds the weight of the optimal scheduleand FINDWIS(n) finds the actual schedule

The final algorithm:

Step 1: Find all the p(i) values

Step 2: Run ItWIS(n) to find the optimal weight

Step 3: Run FindWIS(n) to find the schedule

FindWIS(i)

If (i = 0)

Return nothing

If WIS[i− 1]6WIS[p(i)]+wi

Return FindWIS(p(i)) then i

Return FindWIS(i− 1)

ItWIS(n)

If (i = 0)

Return 0

For i = 1 to n

WIS[i]= max(WIS[i− 1],WIS[p(i)]+wi

)Return WIS[i]

Page 310: Dynamic Programming

The final algorithm

ItWIS(n) finds the weight of the optimal scheduleand FINDWIS(n) finds the actual schedule

The final algorithm:

Step 1: Find all the p(i) values

Step 2: Run ItWIS(n) to find the optimal weight

Step 3: Run FindWIS(n) to find the schedule

O(n logn) time

FindWIS(i)

If (i = 0)

Return nothing

If WIS[i− 1]6WIS[p(i)]+wi

Return FindWIS(p(i)) then i

Return FindWIS(i− 1)

ItWIS(n)

If (i = 0)

Return 0

For i = 1 to n

WIS[i]= max(WIS[i− 1],WIS[p(i)]+wi

)Return WIS[i]

Page 311: Dynamic Programming

The final algorithm

ItWIS(n) finds the weight of the optimal scheduleand FINDWIS(n) finds the actual schedule

The final algorithm:

Step 1: Find all the p(i) values

Step 2: Run ItWIS(n) to find the optimal weight

Step 3: Run FindWIS(n) to find the schedule

O(n logn) time

O(n) time

FindWIS(i)

If (i = 0)

Return nothing

If WIS[i− 1]6WIS[p(i)]+wi

Return FindWIS(p(i)) then i

Return FindWIS(i− 1)

ItWIS(n)

If (i = 0)

Return 0

For i = 1 to n

WIS[i]= max(WIS[i− 1],WIS[p(i)]+wi

)Return WIS[i]

Page 312: Dynamic Programming

The final algorithm

ItWIS(n) finds the weight of the optimal scheduleand FINDWIS(n) finds the actual schedule

The final algorithm:

Step 1: Find all the p(i) values

Step 2: Run ItWIS(n) to find the optimal weight

Step 3: Run FindWIS(n) to find the schedule

O(n logn) time

O(n) time

O(n) time

FindWIS(i)

If (i = 0)

Return nothing

If WIS[i− 1]6WIS[p(i)]+wi

Return FindWIS(p(i)) then i

Return FindWIS(i− 1)

ItWIS(n)

If (i = 0)

Return 0

For i = 1 to n

WIS[i]= max(WIS[i− 1],WIS[p(i)]+wi

)Return WIS[i]

Page 313: Dynamic Programming

The final algorithm

ItWIS(n) finds the weight of the optimal scheduleand FINDWIS(n) finds the actual schedule

The final algorithm:

Step 1: Find all the p(i) values

Step 2: Run ItWIS(n) to find the optimal weight

Step 3: Run FindWIS(n) to find the schedule

O(n logn) time

O(n) time

O(n) time

Overall this takes O(n logn) time

FindWIS(i)

If (i = 0)

Return nothing

If WIS[i− 1]6WIS[p(i)]+wi

Return FindWIS(p(i)) then i

Return FindWIS(i− 1)

ItWIS(n)

If (i = 0)

Return 0

For i = 1 to n

WIS[i]= max(WIS[i− 1],WIS[p(i)]+wi

)Return WIS[i]

Page 314: Dynamic Programming

Introduction

Dynamic programming is recursion without repetition

Dynamic programming is a technique for finding efficient algorithms for problems which

can be broken down into simpler, overlapping subproblems.

in other words. . .

The basic idea:

1. Find a recursive formula for the problem

(typically this is the hard bit)

3. Speed it up by storing the solutions to subproblems (memoization)(to avoid recomputing the same thing over and over)

4. Derive an iterative algorithm by solving the subproblems in a good order

(iterative algorithms are often better in practice, easier to analyse and prettier)

- in terms of answers to subproblems.

2. Write down a naive recursive algorithm(typically this algorithm will take exponential time)

SummaryIntroduction

Summary