71
Upcoming Contests • TopCoder Marathon Match 49 (currently running – ends Feb. 18 th )

Upcoming Contests

  • Upload
    keona

  • View
    53

  • Download
    0

Embed Size (px)

DESCRIPTION

Upcoming Contests. TopCoder Marathon Match 49 (currently running – ends Feb. 18 th ). TopCoder Open 2009. Algorithms qualifying rounds: Tuesday, February 24 th @ 7:00 AM Saturday, February 28 th @ 12:00 PM Wednesday, March 4 th @ 9:00 PM. TopCoder Open 2009. Marathon Matches: - PowerPoint PPT Presentation

Citation preview

Page 1: Upcoming Contests

Upcoming Contests• TopCoder Marathon Match 49

(currently running – ends Feb. 18th)

Page 2: Upcoming Contests

TopCoder Open 2009• Algorithms qualifying rounds:

– Tuesday, February 24th @ 7:00 AM– Saturday, February 28th @ 12:00 PM– Wednesday, March 4th @ 9:00 PM

Page 3: Upcoming Contests

TopCoder Open 2009• Marathon Matches:

– Wednesday, February 25th @ 12:00 PM

– Wednesday, March 11th @ 12:00 PM– Wednesday, March 25th @ 12:00 PM

Page 4: Upcoming Contests

TopCoder Open 2009• Other contests as well

– Visit http://www.topcoder.com/tco09 for more information

Page 5: Upcoming Contests

TopCoderEvents Calendar (Feb.)

Page 6: Upcoming Contests

Events Calendar (March)

Page 7: Upcoming Contests

Dynamic Dynamic ProgrammingProgramming

Dynamic Dynamic ProgrammingProgramming

Programming Puzzles and CompetitionsProgramming Puzzles and CompetitionsCIS 4900 / 5920CIS 4900 / 5920

Spring 2009Spring 2009

Page 8: Upcoming Contests

Lecture Outline• Order Notation• Solution to Text Segmentation• Dynamic Programming• Text Segmentation w/

Memoization• Practice

Page 9: Upcoming Contests

Algorithmic Complexity and Order Notation

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

Page 10: Upcoming Contests

Algorithmic Complexity and Order Notation

• 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

Page 11: Upcoming Contests

Algorithmic Complexity and Order Notation

• Absolute time measurements are not helpful, as computers get faster all the time

• Also, runtime is clearly dependent on the input (and input size)

Page 12: Upcoming Contests

Algorithmic Complexity and Order Notation

• We define O(*) as follows

Page 13: Upcoming Contests

Text Segmentation• Some languages are written

without spaces between the words

Page 14: Upcoming Contests

Text Segmentation• Difficult for search engines to

decipher the meaning of a search

Page 15: Upcoming Contests

Text Segmentation• Difficult for search engines to

decipher the meaning of a search

• Difficult to return relevant results

Page 16: Upcoming Contests

Text Segmentation• Given a string without spaces,

what is the best segmentation of the string?

Page 17: Upcoming Contests

Text Segmentation• Examples:

“upordown” “up or down”

Page 18: Upcoming Contests

Text Segmentation• Examples:

“upordown” “up or down”“upsidedown” “upside down”

Page 19: Upcoming Contests

Text Segmentation• Examples:

“upordown” “up or down”“upsidedown” “upside down”“haveaniceday” “have a nice day”

Page 20: Upcoming Contests

Text Segmentation• Ambiguities are possible, e.g.:

“theyouthevent” ?

Page 21: Upcoming Contests

Text Segmentation• At least three ways to segment

“theyouthevent” into valid words:

1) “they out he vent”2) “the you the vent”3) “the youth event”

Page 22: Upcoming Contests

Text Segmentation• 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)

Page 23: Upcoming Contests

Text Segmentation:Some ambiguities

• whorepresents.com• therapistfinder.com• speedofart.com• expertsexchange.com• penisland.com

These examples are borrowed from Peter Norvig.

Page 24: Upcoming Contests

Text Segmentation• How can this be done?

Page 25: Upcoming Contests

Text Segmentation:Solution

• Recursion:

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

Pmax(“”) = 1;

where n = length(y)

Page 26: Upcoming Contests

Text Segmentation:Solution

• Recursion:

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

Pmax(“”) = 1;

where n = length(y)

“base case”

Page 27: Upcoming Contests

Text Segmentation:Solution

Pmax(“theyouthevent”) =

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

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

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

);

Page 28: Upcoming Contests

Text Segmentation:Solution

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

Page 29: Upcoming Contests

Text Segmentation:Solution

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

Page 30: Upcoming Contests

Text Segmentation:Solution

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

Page 31: Upcoming Contests

Text Segmentation:Solution

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

Page 32: Upcoming Contests

Text Segmentation:Solution

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

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

Page 33: Upcoming Contests

Text Segmentation:Solution

• Recursion:

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

Pmax(“”) = 1;

where n = length(y)

base case

Page 34: Upcoming Contests

Text Segmentation:Solution

• Note that this algorithm is (extremely) inefficient

Page 35: Upcoming Contests

Text Segmentation:Computing Pmax(“ent”)

Pmax(“ent”)

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

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

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

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

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

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

Page 36: Upcoming Contests

Text Segmentation:Computing Pmax(“ent”)

Pmax(“ent”)

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

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

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

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

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

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

Unnecessary Call

Page 37: Upcoming Contests

Fibonacci Numbers• F0 = 0

• F1 = 1

• Fn = Fn-1 + Fn-2 for n > 1

Page 38: Upcoming Contests

Fibonacci Numbers• F0 = 0

• F1 = 1

• Fn = Fn-1 + Fn-2 for n > 1

• 0, 1, 1, 2, 3, 5, 8, 13, …

Page 39: Upcoming Contests

Fibonacci Numbers• How would we write code for this?

Page 40: Upcoming Contests

Fibonacci Numbers• How would we write code for this?

• F0 = 0

• F1 = 1

• Fn = Fn-1 + Fn-2 for n > 1

Page 41: Upcoming Contests

Fibonacci Numbersint fibonacci(int n){

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

elsereturn fibonacci(n-1) + fibonacci(n-

2);}

Page 42: Upcoming Contests

Fibonacci Numbers: Computing F(5)

F(5)

F(3)F(4)

F(2)F(3)

F(1)F(2)

F(0)F(1)

F(2)

F(0)F(1)

F(1)

F(0)F(1)

Page 43: Upcoming Contests

Dynamic Programming• Same algorithm as before, but

compute each value only once

Page 44: Upcoming Contests

Dynamic Programming• Same algorithm as before, but

compute each value only once

• This significantly reduces the runtime

Page 45: Upcoming Contests

Dynamic Programming• Can be done in one of two ways

– iteration– recursion w/ memoization

Page 46: Upcoming Contests

Dynamic Programming• Can be done in one of two ways

– iteration (bottom-up)– recursion w/ memoization (top-down)

Page 47: Upcoming Contests

Dynamic Programming• 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)

Page 48: Upcoming Contests

Dynamic Programming:Recursion w/ Memoization• “Memoize”* previously computed

function evaluations

*the word “memoize” stems from the word “memo”; it is not a misspelling of the word “memorize”

Page 49: Upcoming Contests

Dynamic Programming:Recursion w/ Memoization• “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”

Page 50: Upcoming Contests

Fibonacci Numbers:F(5) revisited

F(5)

F(3)F(4)

F(2)F(3)

F(1)F(2)

F(0)F(1)

F(2)

F(0)F(1)

F(1)

F(0)F(1)

Page 51: Upcoming Contests

Fibonacci Numbers:F(5) revisited

F(5)

F(3)F(4)

F(2)F(3)

F(1)F(2)

F(0)F(1)

F(2)

F(0)F(1)

F(1)

F(0)F(1)

= re-computations

Page 52: Upcoming Contests

Fibonacci Numbers:F(5) revisited

F(5)

F(3)F(4)

F(2)F(3)

F(1)F(2)

F(0)F(1)

Page 53: Upcoming Contests

Dynamic Programming:Recursion w/ Memoization• Implementation

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

Page 54: Upcoming Contests

Dynamic Programming:Recursion w/ Memoization• Why does a lookup table help?

Page 55: Upcoming Contests

Dynamic Programming:Recursion w/ Memoization• Why does a lookup table help?

• Avoids re-computations by saving previously computed values

Page 56: Upcoming Contests

Fibonacci Numbersw/ Dynamic Programmingint 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;

elsememo[n] = fibonacci(n-1) + fibonacci(n-

2);

return memo[n];}

Page 57: Upcoming Contests

Fibonacci Numbersw/ Dynamic Programmingint 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;

elsememo[n] = fibonacci(n-1) + fibonacci(n-

2);

return memo[n];}

Memoization

Page 58: Upcoming Contests

Fibonacci Numbers: Computing F(5)

F(5)

F(3)F(4)

F(2)F(3)

F(1)F(2)

F(0)F(1)

F(2)

F(0)F(1)

F(1)

F(0)F(1)

Page 59: Upcoming Contests

Fibonacci Numbers: Computing F(5)

F(5)

F(3)F(4)

F(2)F(3)

F(1)F(2)

F(0)F(1)

F(2)

F(0)F(1)

F(1)

F(0)F(1)

Table Lookups

Page 60: Upcoming Contests

Fibonacci Numbers: Computing F(5)

F(5)

F(3)F(4)

F(2)F(3)

F(1)F(2)

F(0)F(1)

F(2)

F(0)F(1)

F(1)

F(0)F(1)

Table Lookups

Page 61: Upcoming Contests

Fibonacci Numbers:F(5) w/ memoization

F(5)

F(3)F(4)

F(2)F(3)

F(1)F(2)

F(0)F(1)

Table Lookups

Page 62: Upcoming Contests

Fibonacci Numbers:F(5) w/ memoization

F(5)

F(3)F(4)

F(2)F(3)

F(1)F(2)

F(0)F(1)

Page 63: Upcoming Contests

Text Segmentation w/Dynamic Programming

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;

}

Page 64: Upcoming Contests

Text Segmentation w/Dynamic Programming

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

Page 65: Upcoming Contests

Text Segmentation:Computing Pmax(“ent”)

Pmax(“ent”)

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

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

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

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

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

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

Page 66: Upcoming Contests

Text Segmentation:Computing Pmax(“ent”)

Pmax(“ent”)

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

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

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

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

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

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

Table Lookup

Page 67: Upcoming Contests

Text Segmentation:Computing Pmax(“ent”)

Pmax(“ent”)

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

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

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

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

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

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

Table Lookup

Page 68: Upcoming Contests

Text Segmentation:Computing Pmax(“ent”)

Pmax(“ent”)

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

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

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

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

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

Table Lookup

Page 69: Upcoming Contests

Text Segmentation:Computing Pmax(“ent”)

Pmax(“ent”)

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

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

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

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

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

Page 70: Upcoming Contests

Dynamic Programmingthrough Memoization

• Any questions?

Page 71: Upcoming Contests

Options• TopCoder…

– Marathon Match 49– SRM 418 – Division II

• Solve text segmentation using dynamic programming