0% found this document useful (0 votes)
5 views46 pages

Lec 09

The document discusses dynamic programming techniques for solving problems like collecting maximum coins in a Mario game using ascending-right jumps and finding the longest increasing subsequence (LIS) in an array. It presents the sub-problems and recurrences for LIS, along with examples and questions regarding time complexity and data structures. Additionally, it introduces a segment tree data structure for efficiently querying the height of towers built in various cities.

Uploaded by

Sunaina Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views46 pages

Lec 09

The document discusses dynamic programming techniques for solving problems like collecting maximum coins in a Mario game using ascending-right jumps and finding the longest increasing subsequence (LIS) in an array. It presents the sub-problems and recurrences for LIS, along with examples and questions regarding time complexity and data structures. Additionally, it introduces a segment tree data structure for efficiently querying the height of towers built in various cities.

Uploaded by

Sunaina Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

COL 351: Analysis and Design

of Algorithms

Lecture 9

Keerti Choudhary
Department of CSE, IIT Delhi
Dynamic Programming
using
Efficient Data-Structures
Mario Game

Each brick has


exactly one coin

Question: How to collect maximum possible coins if only possible


moves are “Ascending-Right-jumps’’?
Mario Game

Each brick has


exactly one coin

Question: How to collect maximum possible coins if only possible


moves are “Ascending-Right-jumps’’?
Mario Game

7
Each brick has
exactly one coin 6

4 4

3 3 3

2 2

Question: How to collect maximum possible coins if only possible


moves are “Ascending-Right-jumps’’?
Mario Game

An optimal
jump sequence 7
Each brick has
exactly one coin 6

4 4

3 3 3

2 2

Question: How to collect maximum possible coins if only possible


moves are “Ascending-Right-jumps’’?
Longest Increasing Subsequence

Given: An array A = [a1, …, an].

Find: Longest increasing subsequence (LIS) of A.


Longest Increasing Subsequence

Given: An array A = [a1, …, an].

Find: Longest increasing subsequence (LIS) of A.

Eg: A = (8, 5, 7, 3, 9, 4, 11, 2)


Longest Increasing Subsequence

Given: An array A = [a1, …, an].

Find: Longest increasing subsequence (LIS) of A.

Eg: A = (8, 5, 7, 3, 9, 4, 11, 2)


LIS = (5, 7, 9, 11)
Longest Increasing Subsequence

Given: An array A = [a1, …, an].

Find: Longest increasing subsequence (LIS) of A.

Eg: A = (8, 5, 7, 3, 9, 4, 11, 2)


LIS = (5, 7, 9, 11)

Question:
What should be the
sub-problems,
and how to solve them?
Longest Increasing Subsequence

Given: An array A = [a1, …, an].

Find: Longest increasing subsequence (LIS) of A.

Eg: A = (8, 5, 7, 3, 9, 4, 11, 2)


LIS = (5, 7, 9, 11)

Question. Can we have a recurrence to compute the following:

LIS[i] = Length( LIS of A[0,i] )

Ans. No! This will not work for array A = [7, 8, 9, 1, 2, 3, 4, 5, 6].
Longest Increasing Subsequence

Given: An array A = [a1, …, an].

Find: Longest increasing subsequence (LIS) of A.

Eg: A = (8, 5, 7, 3, 9, 4, 11, 2)


LIS = (5, 7, 9, 11)

Question. Can we have a recurrence to compute the following:

LIS[i] = Length( LIS of A[0,i] terminating at ai )

Ans. Yes!
Longest Increasing Subsequence

Given: An array A = [a1, …, an].

Find: Longest increasing subsequence (LIS) of A.

Sub-problem
LIS[i] = Length of LIS of A[0,i] terminating at ai.

Recurrence
LIS[i] = 1 + max {LIS[ j] xj < xi, and j ∈ [1,i − 1]}
Longest Increasing Subsequence

Given: An array A = [a1, …, an].

Find: Longest increasing subsequence (LIS) of A.

Sub-problem
LIS[i] = Length of LIS of A[0,i] terminating at ai.

Recurrence
LIS[i] = 1 + max {LIS[ j] aj < ai, and j ∈ [0, i − 1]}
Longest Increasing Subsequence

Given: An array A = [a1, …, an].

Find: Longest increasing subsequence (LIS) of A.

Sub-problem
LIS[i] = Length of LIS of A[0,i] terminating at ai.

Recurrence
LIS[i] = 1 + max {LIS[ j] aj < ai, and j ∈ [0, i − 1]}

2 Question:
Time complexity: O(n )
How to improve this?
Data-Structure
Problem
Data Structure Problem

Given: There are n cities (c1, …, cn).

Delhi Mumbai B’lore Jaipur Kolkatta Dubai Chennai NJP

1 2 3 4 5 6 7 8
Data Structure Problem

Given: There are n cities (c1, …, cn).

Delhi Mumbai B’lore Jaipur Kolkatta Dubai Chennai NJP

1 2 3 4 5 6 7 8

Dynamic: Towers are built in cities in an arbitrary order, with one tower per city.
Data Structure Problem

Given: There are n cities (c1, …, cn).

Delhi Mumbai B’lore Jaipur Kolkatta Dubai Chennai NJP

1 2 3 4 5 6 7 8
h = 400

Dynamic: Towers are built in cities in an arbitrary order, with one tower per city.
Data Structure Problem

Given: There are n cities (c1, …, cn).

Delhi Mumbai B’lore Jaipur Kolkatta Dubai Chennai NJP

1 2 3 4 5 6 7 8
h = 400 h = 800

Dynamic: Towers are built in cities in an arbitrary order, with one tower per city.
Data Structure Problem

Given: There are n cities (c1, …, cn).

Delhi Mumbai B’lore Jaipur Kolkatta Dubai Chennai NJP

1 2 3 4 5 6 7 8
h = 400 h = 800

Dynamic: Towers are built in cities in an arbitrary order, with one tower per city.

Query: What is height of tallest tower in rst i cities?


fi
Data Structure Problem

Delhi Mumbai B’lore Jaipur Kolkatta Dubai Chennai NJP

1 2 3 4 5 6 7 8
Data Structure Problem
[1-8]

[1-4] [5-8]

[1-2] [3-4] [5-6] [7-8]

Delhi Mumbai B’lore Jaipur Kolkatta Dubai Chennai NJP

1 2 3 4 5 6 7 8
Data Structure Problem
Segment
[1-8]
Tree data-
structure

[1-4] [5-8]

[1-2] [3-4] [5-6] [7-8]

Delhi Mumbai B’lore Jaipur Kolkatta Dubai Chennai NJP

1 2 3 4 5 6 7 8

Node [i − j] stores: Height of tallest tower in cities (ci, …, cj)


Data Structure Problem
Segment
Tree data-
structure

Delhi Mumbai B’lore Jaipur Kolkatta Dubai Chennai NJP

400 800

Node [i − j] stores: Height of tallest tower in cities (ci, …, cj)


Data Structure Problem
Segment
800 Tree data-
structure
800

400 800

Delhi Mumbai B’lore Jaipur Kolkatta Dubai Chennai NJP

400 800

Node [i − j] stores: Height of tallest tower in cities (ci, …, cj)


Data Structure Problem
[1-8]

[1-4] [5-8]

[1-2] [3-4] [5-6] [7-8]

Delhi Mumbai B’lore Jaipur Kolkatta Dubai Chennai NJP

600 400 800

Question 1: Time to update nodes of tree (after each tower installation)?


Data Structure Problem
[1-8]

[1-4] [5-8]

[1-2] [3-4] [5-6] [7-8]

Delhi Mumbai B’lore Jaipur Kolkatta Dubai Chennai NJP

600 400 800

Question 1: Time to update nodes of tree (after each tower installation)?


Data Structure Problem
[1-8]

[1-4] [5-8]

[1-2] [3-4] [5-6] [7-8]

Delhi Mumbai B’lore Jaipur Kolkatta Dubai Chennai NJP

600 400 800

Question 1: Time to update nodes of tree (after each tower installation)?

Ans: O(log n)
Data Structure Problem
[1-8]

[1-4] [5-8]

[1-2] [3-4] [5-6] [7-8]

Delhi Mumbai B’lore Jaipur Kolkatta Dubai Chennai NJP

Question 2: Find height of tallest tower in rst i = 6 cities?


fi
Data Structure Problem

[1-4]

[5-6]

Delhi Mumbai B’lore Jaipur Kolkatta Dubai Chennai NJP

Question 2: Find height of tallest tower in rst i = 6 cities?


fi
Data Structure Problem

[1-4]

[5-6]

Delhi Mumbai B’lore Jaipur Kolkatta Dubai Chennai NJP

Question 2: Find height of tallest tower in rst i = 6 cities?

Ans: Query O(log2 n) internal nodes in the tree.


fi
Coming back to
Longest Increasing
subsequence (LIS)
Sub-problem

LIS[i] stores length of LIS of A[0,i] terminating at ai.

LIS[i] = 1 + max {LIS[ j] aj < ai, and j ∈ [0, i − 1]}


Sub-problem

LIS[i] stores length of LIS of A[0,i] terminating at ai.

LIS[i] = 1 + max {LIS[ j] aj < ai, and j ∈ [0, i − 1]}

A = [8, 5, 7, 3, 9, 4, 11, 2]

Sorted A: A[7] A[3] A[5] A[1] A[2] A[0] A[4] A[6]


2 3 4 5 7 8 9 11
Sub-problem

LIS[i] stores length of LIS of A[0,i] terminating at ai.

LIS[i] = 1 + max {LIS[ j] aj < ai, and j ∈ [0, i − 1]}

A = [8, 5, 7, 3, 9, 4, 11, 2]

LIS[i]
Sorted A: A[7] A[3] A[5] A[1] A[2] A[0] A[4] A[6]
2 3 4 5 7 8 9 11
Sub-problem

LIS[i] stores length of LIS of A[0,i] terminating at ai.

LIS[i] = 1 + max {LIS[ j] aj < ai, and j ∈ [0, i − 1]}

A = [8, 5, 7, 3, 9, 4, 11, 2]

LIS[i] 1

Sorted A: A[7] A[3] A[5] A[1] A[2] A[0] A[4] A[6]


2 3 4 5 7 8 9 11
Sub-problem

LIS[i] stores length of LIS of A[0,i] terminating at ai.

LIS[i] = 1 + max {LIS[ j] aj < ai, and j ∈ [0, i − 1]}

A = [8, 5, 7, 3, 9, 4, 11, 2]

LIS[i] 1 1

Sorted A: A[7] A[3] A[5] A[1] A[2] A[0] A[4] A[6]


2 3 4 5 7 8 9 11
Sub-problem

LIS[i] stores length of LIS of A[0,i] terminating at ai.

LIS[i] = 1 + max {LIS[ j] aj < ai, and j ∈ [0, i − 1]}

A = [8, 5, 7, 3, 9, 4, 11, 2]

LIS[i] 1 2 1

Sorted A: A[7] A[3] A[5] A[1] A[2] A[0] A[4] A[6]


2 3 4 5 7 8 9 11
Sub-problem

LIS[i] stores length of LIS of A[0,i] terminating at ai.

LIS[i] = 1 + max {LIS[ j] aj < ai, and j ∈ [0, i − 1]}

A = [8, 5, 7, 3, 9, 4, 11, 2]

LIS[i] 1 1 2 1

Sorted A: A[7] A[3] A[5] A[1] A[2] A[0] A[4] A[6]


2 3 4 5 7 8 9 11
Sub-problem

LIS[i] stores length of LIS of A[0,i] terminating at ai.

LIS[i] = 1 + max {LIS[ j] aj < ai, and j ∈ [0, i − 1]}

A = [8, 5, 7, 3, 9, 4, 11, 2]

LIS[i] 1 1 2 1 3

Sorted A: A[7] A[3] A[5] A[1] A[2] A[0] A[4] A[6]


2 3 4 5 7 8 9 11
Sub-problem

LIS[i] stores length of LIS of A[0,i] terminating at ai.

LIS[i] = 1 + max {LIS[ j] aj < ai, and j ∈ [0, i − 1]}

A = [8, 5, 7, 3, 9, 4, 11, 2]

LIS[i] 1 ?? 1 2 1 3

Sorted A: A[7] A[3] A[5] A[1] A[2] A[0] A[4] A[6]


2 3 4 5 7 8 9 11

LIS[5] = _____________
Sub-problem

LIS[i] stores length of LIS of A[0,i] terminating at ai.

LIS[i] = 1 + max {LIS[ j] aj < ai, and j ∈ [0, i − 1]}

A = [8, 5, 7, 3, 9, 4, 11, 2]

LIS[i] 1 ?? 1 2 1 3

Sorted A: A[7] A[3] A[5] A[1] A[2] A[0] A[4] A[6]


2 3 4 5 7 8 9 11

LIS[5] = 1+max( rst two leaf nodes) = 2


fi
Sub-problem

LIS[i] stores length of LIS of A[0,i] terminating at ai.

LIS[i] = 1 + max {LIS[ j] aj < ai, and j ∈ [0, i − 1]}

A = [8, 5, 7, 3, 9, 4, 11, 2]

LIS[i] 1 ?? 1 2 1 3

Sorted A: A[7] A[3] A[5] A[1] A[2] A[0] A[4] A[6]


2 3 4 5 7 8 9 11

Remark: Time to nd LIS[ i ] is O(log n).


fi
Sub-problem

LIS[i] stores length of LIS of A[0,i] terminating at ai.

LIS[i] = 1 + max {LIS[ j] aj < ai, and j ∈ [0, i − 1]}

A = [8, 5, 7, 3, 9, 4, 11, 2]

LIS[i] 1 ?? 1 2 1 3

Sorted A: A[7] A[3] A[5] A[1] A[2] A[0] A[4] A[6]


2 3 4 5 7 8 9 11

Remark: Time to update data-structure is also O(log n).


Exercises

Can you design an e cient algorithm for the Mario Coin collection game, for the
scenario where the bricks contain coins of varying values?

Can you design an O(n log n) time algorithm for Question 4 of Tutorial sheet 5.
ffi

You might also like