Dynamic Programming

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

Transcript of Dynamic Programming

Data Structures and Algorithms – COMS21103

Dynamic Programming

Largest Empty Square and Weighted Interval Scheduling

Benjamin Sach

The name

Dynamic Programming is an approach to algorithm design. . .

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

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:

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

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

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)

Part one

Largest Empty Square

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

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

n

n

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

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

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

n

n

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

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

n

n

largest empty square

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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?

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). . .

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)

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)

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)

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)

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)

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

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)

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)

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)

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)

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)

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!

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

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)

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)

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?

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

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

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

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)

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

)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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

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

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

End of part one

Part two

Weighted Interval Scheduling

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Compatible Intervals

interval p(7)

interval 7

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,

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

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

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)?

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

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

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

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)

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)

1. Find a recursive formula

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

more intervals not shown

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

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

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

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

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

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

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

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

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

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

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

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

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

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)}

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)

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

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)

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)

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)

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)

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

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

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?

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

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)

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)

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)

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}

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}

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)

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)

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

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)

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)

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)

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

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

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

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

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

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

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)

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. . .

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

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

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

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

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,

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

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)

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)

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)

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)

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)

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)

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

The dependency graph

What information do we need to compute WIS[i]?

The array

WIS:

The dependency graph

What information do we need to compute WIS[i]?

The array

WIS:

WIS[i]

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

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]

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)]

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]

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)

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)

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. . .

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

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

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

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

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

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)

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]

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]

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]

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]

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]

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]

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]

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]

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]

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]

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