- 74 Views
- Uploaded on
- Presentation posted in: General

Upcoming Contests

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

- TopCoder Marathon Match 49 (currently running – ends Feb. 18th)

- Algorithms qualifying rounds:
- Tuesday, February 24th @ 7:00 AM
- Saturday, February 28th @ 12:00 PM
- Wednesday, March 4th @ 9:00 PM

- Marathon Matches:
- Wednesday, February 25th @ 12:00 PM
- Wednesday, March 11th @ 12:00 PM
- Wednesday, March 25th @ 12:00 PM

- Other contests as well
- Visit http://www.topcoder.com/tco09 for more information

Dynamic Programming

Programming Puzzles and Competitions

CIS 4900 / 5920

Spring 2009

- Order Notation
- Solution to Text Segmentation
- Dynamic Programming
- Text Segmentation w/ Memoization
- Practice

- We would like to be able to describe how long a program takes to run

- We would like to be able to describe how long a program takes to run
- We should express the runtime in terms of the input size

- Absolute time measurements are not helpful, as computers get faster all the time
- Also, runtime is clearly dependent on the input (and input size)

- We define O(*) as follows

- Some languages are written without spaces between the words

- Difficult for search engines to decipher the meaning of a search

- Difficult for search engines to decipher the meaning of a search
- Difficult to return relevant results

- Given a string without spaces, what is the best segmentation of the string?

- Examples:
“upordown” “up or down”

- Examples:
“upordown” “up or down”

“upsidedown” “upside down”

- Examples:
“upordown” “up or down”

“upsidedown” “upside down”

“haveaniceday” “have a nice day”

- Ambiguities are possible, e.g.:
“theyouthevent” ?

- At least three ways to segment “theyouthevent” into valid words:
1) “they out he vent”

2) “the you the vent”

3) “the youth event”

- At least three ways to segment “theyouthevent” into valid words:
1) “they out he vent”

2) “the you the vent”

3) “the youth event” (most likely)

- whorepresents.com
- therapistfinder.com
- speedofart.com
- expertsexchange.com
- penisland.com

These examples are borrowed from Peter Norvig.

- How can this be done?

- Recursion:
Pmax(y) = maxi P0(y[0:i]) x Pmax(y[i:n]);

Pmax(“”) = 1;

where n = length(y)

- Recursion:
Pmax(y) = maxi P0(y[0:i]) x Pmax(y[i:n]);

Pmax(“”) = 1;

where n = length(y)

“base case”

Pmax(“theyouthevent”) =

max(

P0(“t”) x Pmax(“heyouthevent”),

P0(“th”) x Pmax(“eyouthevent”),

…

P0(“theyouthevent”) x Pmax(“”)

);

double get_Pmax(const string &s)

{

if(s.length() == 0)

return get_P0(s);

double max = 0.0;

for(int i = 1; i <= s.length(); ++i) {

double temp =

get_P0(s.substr(0, i - 0)) * get_Pmax(s.substr(i));

if(temp > max)

max = temp;

}

return max;

}

double get_Pmax(const string &s)

{

if(s.length() == 0)

return get_P0(s);

double max = 0.0;

for(int i = 1; i <= s.length(); ++i) {

double temp =

get_P0(s.substr(0, i - 0)) * get_Pmax(s.substr(i));

if(temp > max)

max = temp;

}

return max;

}

P0(“”)x Pmax(“”)

double get_Pmax(const string &s)

{

if(s.length() == 0)

return get_P0(s);

double max = 0.0;

for(int i = 1; i <= s.length(); ++i) {

double temp =

get_P0(s.substr(0, i - 0)) * get_Pmax(s.substr(i));

if(temp > max)

max = temp;

}

return max;

}

max(…)

P0(“”)x Pmax(“”)

double get_Pmax(const string &s)

{

if(s.length() == 0)

return get_P0(s);

double max = 0.0;

for(int i = 1; i <= s.length(); ++i) {

double temp =

get_P0(s.substr(0, i - 0)) * get_Pmax(s.substr(i));

if(temp > max)

max = temp;

}

return max;

}

base case

max(…)

P0(“”)x Pmax(“”)

double get_Pmax(const string &s)

{

if(s.length() == 0)

return get_P0(s);

double max = 0.0;

for(int i = 1; i <= s.length(); ++i) {

double temp =

get_P0(s.substr(0, i - 0)) * get_Pmax(s.substr(i));

if(temp > max)

max = temp;

}

return max;

}

base case

max(…)

P0(“”)x Pmax(“”)

Pmax(y) = maxiP0(y[0:i]) x Pmax(y[i:n])

- Recursion:
Pmax(y) = maxiP0(y[0:i]) x Pmax(y[i:n]);

Pmax(“”) = 1;

where n = length(y)

base case

- Note that this algorithm is (extremely) inefficient

Pmax(“ent”)

P0(“e”) x Pmax(“nt”)

P0(“en”) x Pmax(“t”)

P0(“ent”) x Pmax(“”)

P0(“n”) x Pmax(“t”)

P0(“nt”) x Pmax(“”)

P0(“t”) x Pmax(“”)

P0(“t”) x Pmax(“”)

Unnecessary Call

Pmax(“ent”)

P0(“e”) x Pmax(“nt”)

P0(“en”) x Pmax(“t”)

P0(“ent”) x Pmax(“”)

P0(“n”) x Pmax(“t”)

P0(“nt”) x Pmax(“”)

P0(“t”) x Pmax(“”)

P0(“t”) x Pmax(“”)

- F0 = 0
- F1 = 1
- Fn = Fn-1 + Fn-2for n > 1

- F0 = 0
- F1 = 1
- Fn = Fn-1 + Fn-2for n > 1
- 0, 1, 1, 2, 3, 5, 8, 13, …

- How would we write code for this?

- How would we write code for this?
- F0 = 0
- F1 = 1
- Fn = Fn-1 + Fn-2for n > 1

int fibonacci(int n)

{

if(n == 0 || n == 1)

return n;

else

return fibonacci(n-1) + fibonacci(n-2);

}

F(5)

F(4)

F(3)

F(3)

F(2)

F(1)

F(2)

F(1)

F(0)

F(1)

F(0)

F(2)

F(1)

F(1)

F(0)

- Same algorithm as before, but compute each value only once

- Same algorithm as before, but compute each value only once
- This significantly reduces the runtime

- Can be done in one of two ways
- iteration
- recursion w/ memoization

- Can be done in one of two ways
- iteration (bottom-up)
- recursion w/ memoization (top-down)

- Can be done in one of two ways
- iteration (bottom-up)
- recursion w/ memoization (top-down)

- We will focus on the second of these (for now)

- “Memoize”* previously computed function evaluations

*the word “memoize” stems from the word “memo”;

it is not a misspelling of the word “memorize”

- “Memoize”* previously computed function evaluations
- There is no reason to run the same computation twice

*the word “memoize” stems from the word “memo”;

it is not a misspelling of the word “memorize”

F(5)

F(4)

F(3)

F(3)

F(2)

F(1)

F(2)

F(1)

F(0)

F(1)

F(0)

F(2)

F(1)

F(1)

F(0)

F(5)

F(4)

F(3)

F(3)

F(2)

F(1)

F(2)

F(1)

F(0)

F(1)

F(0)

F(2)

F(1)

F(1)

F(0)

= re-computations

F(5)

F(4)

F(3)

F(3)

F(2)

F(2)

F(1)

F(1)

F(0)

- Implementation
- keep a map from argument values to return values:
lookup[arguments] = return value;

- keep a map from argument values to return values:

- Why does a lookup table help?

- Why does a lookup table help?
- Avoids re-computations by saving previously computed values

int fibonacci(int n)

{

static map<int, int> memo;

if(memo.find(n) != memo.end())

return memo[n];

if(n == 0 || n == 1)

memo[n] = n;

else

memo[n] = fibonacci(n-1) + fibonacci(n-2);

return memo[n];

}

int fibonacci(int n)

{

static map<int, int> memo;

if(memo.find(n) != memo.end())

return memo[n];

if(n == 0 || n == 1)

memo[n] = n;

else

memo[n] = fibonacci(n-1) + fibonacci(n-2);

return memo[n];

}

Memoization

F(5)

F(4)

F(3)

F(3)

F(2)

F(1)

F(2)

F(1)

F(0)

F(1)

F(0)

F(2)

F(1)

F(1)

F(0)

F(5)

F(4)

F(3)

F(3)

F(2)

F(1)

F(2)

F(1)

F(0)

F(1)

F(0)

F(2)

F(1)

F(1)

F(0)

Table Lookups

F(5)

F(4)

F(3)

F(3)

F(2)

F(1)

F(2)

F(1)

F(0)

F(1)

F(0)

F(2)

F(1)

F(1)

F(0)

Table Lookups

F(5)

F(4)

F(3)

F(3)

F(2)

F(2)

F(1)

F(1)

F(0)

Table Lookups

F(5)

F(4)

F(3)

F(3)

F(2)

F(2)

F(1)

F(1)

F(0)

double get_Pmax(const string &s)

{

static map<string, double> probabilities;

if(probabilities.find(s) != probabilities.end())

return probabilities[s];

if(s.length() == 0)

return get_P0(s);

double max = 0.0;

for(int i = 1; i <= s.length(); ++i) {

double temp =

get_P0(s.substr(0, i - 0)) * get_Pmax(s.substr(i));

if(temp > max)

max = temp;

}

probabilities[s] = max;

return max;

}

double get_Pmax(const string &s)

{

static map<string, double> probabilities;

if(probabilities.find(s) != probabilities.end())

return probabilities[s];

if(s.length() == 0)

return get_P0(s);

double max = 0.0;

for(int i = 1; i <= s.length(); ++i) {

double temp =

get_P0(s.substr(0, i - 0)) * get_Pmax(s.substr(i));

if(temp > max)

max = temp;

}

probabilities[s] = max;

return max;

}

Memoization

Pmax(“ent”)

P0(“e”) x Pmax(“nt”)

P0(“en”) x Pmax(“t”)

P0(“ent”) x Pmax(“”)

P0(“n”) x Pmax(“t”)

P0(“nt”) x Pmax(“”)

P0(“t”) x Pmax(“”)

P0(“t”) x Pmax(“”)

Table Lookup

Pmax(“ent”)

P0(“e”) x Pmax(“nt”)

P0(“en”) x Pmax(“t”)

P0(“ent”) x Pmax(“”)

P0(“n”) x Pmax(“t”)

P0(“nt”) x Pmax(“”)

P0(“t”) x Pmax(“”)

P0(“t”) x Pmax(“”)

Table Lookup

Pmax(“ent”)

P0(“e”) x Pmax(“nt”)

P0(“en”) x Pmax(“t”)

P0(“ent”) x Pmax(“”)

P0(“n”) x Pmax(“t”)

P0(“nt”) x Pmax(“”)

P0(“t”) x Pmax(“”)

P0(“t”) x Pmax(“”)

Table Lookup

Pmax(“ent”)

P0(“e”) x Pmax(“nt”)

P0(“en”) x Pmax(“t”)

P0(“ent”) x Pmax(“”)

P0(“n”) x Pmax(“t”)

P0(“nt”) x Pmax(“”)

P0(“t”) x Pmax(“”)

Pmax(“ent”)

P0(“e”) x Pmax(“nt”)

P0(“en”) x Pmax(“t”)

P0(“ent”) x Pmax(“”)

P0(“n”) x Pmax(“t”)

P0(“nt”) x Pmax(“”)

P0(“t”) x Pmax(“”)

- Any questions?

- TopCoder…
- Marathon Match 49
- SRM 418 – Division II

- Solve text segmentation using dynamic programming