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

Slides:



Advertisements
Similar presentations
Dynamic Programming (DP)
Advertisements

MATH 224 – Discrete Mathematics
§3 Dynamic Programming Use a table instead of recursion 1. Fibonacci Numbers: F(N) = F(N – 1) + F(N – 2) int Fib( int N ) { if ( N
Dynamic Programming Nithya Tarek. Dynamic Programming Dynamic programming solves problems by combining the solutions to sub problems. Paradigms: Divide.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Dynamic Programming.
Dynamic Programming Fibonacci numbers-example- Defined by Recursion F 0 = 0 F 1 = 1 F n = F n-1 + F n-2 n >= 2 F 2 = 0+1; F 3 = 1+1 =2; F 4 = 1+2 = 3 F.
Fundamentals of Python: From First Programs Through Data Structures
Analysis of Algorithms Dynamic Programming. A dynamic programming algorithm solves every sub problem just once and then Saves its answer in a table (array),
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Computer Science II Recursion Professor: Evan Korth New York University.
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
Chapter 2: Algorithm Discovery and Design
Recursion Road Map Introduction to Recursion Recursion Example #1: World’s Simplest Recursion Program Visualizing Recursion –Using Stacks Recursion Example.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Algorithms & Data Structures for Games
CS 206 Introduction to Computer Science II 02 / 25 / 2009 Instructor: Michael Eckmann.
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
Fibonacci numbers Fibonacci numbers:Fibonacci numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... where each number is the sum of the preceding two. Recursive.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Department of Computer Engineering Recursive Problem Solving Computer Programming for International Engineers.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
ADA: 7. Dynamic Prog.1 Objective o introduce DP, its two hallmarks, and two major programming techniques o look at two examples: the fibonacci.
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
Programming Practice Programming Puzzles and Competitions CIS 4900 / 5920 Spring 2009.
CP104 Introduction to Programming Recursion 2 Lecture 29 __ 1 Recursive Function for gcd Recursive formula for the greatest common divisor of m and n,
Dynamic Programming. What is dynamic programming? Break problem into subproblems Work backwards Can use ‘recursion’ ‘Programming’ - a mathematical term.
Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized.
Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
1 CSC 222: Computer Programming II Spring 2004 See online syllabus at: Course goals:
1 CPSC 320: Intermediate Algorithm Design and Analysis July 28, 2014.
Informal Analysis of Merge Sort  suppose the running time (the number of operations) of merge sort is a function of the number of elements to sort  let.
Recursion, pt. 1 The Foundations. What is Recursion? Recursion is the idea of solving a problem in terms of itself. – For some problems, it may not possible.
Week 6 - Monday.  What did we talk about last time?  Exam 1!  Before that:  Recursion.
Dynamic Programming Louis Siu What is Dynamic Programming (DP)? Not a single algorithm A technique for speeding up algorithms (making use of.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Lecture 2 Jan Goals: Introduction to recursion Examples of recursive programs Reading: Chapter 1 of the text.
CSC 212 More Recursion, Stacks, & Queues. Linear Recursion Test for the bases cases  At least one base case needs to be defined If not at a base case,
Problem Set 5: Problem 2 22C:021 Computer Science Data Structures.
1 Chapter 15-1 : Dynamic Programming I. 2 Divide-and-conquer strategy allows us to solve a big problem by handling only smaller sub-problems Some problems.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
CPS 100, Spring Tools: Solve Computational Problems l Algorithmic techniques  Brute-force/exhaustive, greedy algorithms, dynamic programming,
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 14.
Dynamic Programming. Many problem can be solved by D&C – (in fact, D&C is a very powerful approach if you generalize it since MOST problems can be solved.
IS 2610: Data Structures Recursion, Divide and conquer Dynamic programming, Feb 2, 2004.
Lecture 151 Programming & Data Structures Dynamic Programming GRIFFITH COLLEGE DUBLIN.
Dynamic Programming & Memoization. When to use? Problem has a recursive formulation Solutions are “ordered” –Earlier vs. later recursions.
Introduction to TopCoder Programming Puzzles and Competitions CIS 4900 / 5920 Spring 2009.
Fundamental Data Structures and Algorithms Ananda Guna March 18, 2003 Dynamic Programming Part 1.
Recursion Continued Divide and Conquer Dynamic Programming.
TU/e Algorithms (2IL15) – Lecture 4 1 DYNAMIC PROGRAMMING II
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Recursion You may have seen mathematical functions defined using recursion Factorial(x) = 1 if x
Lecture 12.
CSC 222: Computer Programming II
RECURSION.
Fundamental Structures of Computer Science
COP 3503 FALL 2012 Shayan Javed Lecture 15
Recursion notes Chapter 8.
CS 211 Object Oriented Programming
Big O notation Big O notation is used in Computer Science to describe the performance or complexity of an algorithm. Big O specifically describes the worst-case.
CSE 341 Lecture 5 efficiency issues; tail recursion; print
Dynamic Programming.
Data Structures and Algorithms
Programming Puzzles and Competitions CIS 4900 / 5920 Spring 2009
Java Programming: Chapter 9: Recursion Second Edition
Introduction to Algorithm and its Complexity Lecture 1: 18 slides
Lecture 4 Dynamic Programming
Advanced Programming Techniques
Presentation transcript:

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

TopCoder Open 2009 Algorithms qualifying rounds: –Tuesday, February 24 7:00 AM –Saturday, February 28 12:00 PM –Wednesday, March 4 9:00 PM

TopCoder Open 2009 Marathon Matches: –Wednesday, February 25 12:00 PM –Wednesday, March 11 12:00 PM –Wednesday, March 25 12:00 PM

TopCoder Open 2009 Other contests as well –Visit for more informationhttp://

TopCoder Events Calendar (Feb.)

Events Calendar (March)

Dynamic Programming Programming Puzzles and Competitions CIS 4900 / 5920 Spring 2009

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

Algorithmic Complexity and Order Notation We would like to be able to describe how long a program takes to run

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

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)

Algorithmic Complexity and Order Notation We define O(*) as follows

Text Segmentation Some languages are written without spaces between the words

Text Segmentation Difficult for search engines to decipher the meaning of a search

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

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

Text Segmentation Examples: “upordown”  “up or down”

Text Segmentation Examples: “upordown”  “up or down” “upsidedown”  “upside down”

Text Segmentation Examples: “upordown”  “up or down” “upsidedown”  “upside down” “haveaniceday”  “have a nice day”

Text Segmentation Ambiguities are possible, e.g.: “theyouthevent”  ?

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”

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)

Text Segmentation: Some ambiguities whorepresents.com therapistfinder.com speedofart.com expertsexchange.com penisland.com These examples are borrowed from Peter Norvig.

Text Segmentation How can this be done?

Text Segmentation: Solution Recursion: P max (y) = max i P 0 (y[0:i]) x P max (y[i:n]); P max (“”) = 1; where n = length(y)

Text Segmentation: Solution Recursion: P max (y) = max i P 0 (y[0:i]) x P max (y[i:n]); P max (“”) = 1; where n = length(y) “base case”

Text Segmentation: Solution P max (“theyouthevent”) = max( P 0 (“t”) x P max (“heyouthevent”), P 0 (“th”) x P max (“eyouthevent”), … P 0 (“theyouthevent”) x P max (“”) );

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

Text Segmentation: Solution P 0 (“”) x P 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; }

Text Segmentation: Solution max(…) P 0 (“”) x P 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; }

Text Segmentation: Solution max(…) P 0 (“”) x P 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; } base case

Text Segmentation: Solution max(…) P 0 (“”) x P 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; } base case P max (y) = max i P 0 (y[0:i]) x P max (y[i:n])

Text Segmentation: Solution Recursion: P max (y) = max i P 0 (y[0:i]) x P max (y[i:n]); P max (“”) = 1; where n = length(y) base case

Text Segmentation: Solution Note that this algorithm is (extremely) inefficient

Text Segmentation: Computing P max (“ent”) P max (“ent”) P 0 (“e”) x P max (“nt”)P 0 (“en”) x P max (“t”) P 0 (“n”) x P max (“t”) P 0 (“ent”) x P max (“”) P 0 (“t”) x P max (“”) P 0 (“nt”) x P max (“”)

Text Segmentation: Computing P max (“ent”) P max (“ent”) P 0 (“e”) x P max (“nt”)P 0 (“en”) x P max (“t”) P 0 (“n”) x P max (“t”) P 0 (“ent”) x P max (“”) P 0 (“t”) x P max (“”) P 0 (“nt”) x P max (“”) Unnecessary Call

Fibonacci Numbers F 0 = 0 F 1 = 1 F n = F n-1 + F n-2 for n > 1

Fibonacci Numbers F 0 = 0 F 1 = 1 F n = F n-1 + F n-2 for n > 1 0, 1, 1, 2, 3, 5, 8, 13, …

Fibonacci Numbers How would we write code for this?

Fibonacci Numbers How would we write code for this? F 0 = 0 F 1 = 1 F n = F n-1 + F n-2 for n > 1

Fibonacci Numbers int fibonacci(int n) { if(n == 0 || n == 1) return n; else return fibonacci(n-1) + fibonacci(n-2); }

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(0)F(1)

Dynamic Programming Same algorithm as before, but compute each value only once

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

Dynamic Programming Can be done in one of two ways –iteration –recursion w/ memoization

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

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)

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”

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”

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(0)F(1)

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(0)F(1) = re-computations

Fibonacci Numbers: F(5) revisited F(5) F(3)F(4) F(2)F(3) F(1)F(2) F(0)F(1)

Dynamic Programming: Recursion w/ Memoization Implementation –keep a map from argument values to return values: lookup[arguments] = return value;

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

Dynamic Programming: Recursion w/ Memoization Why does a lookup table help? Avoids re-computations by saving previously computed values

Fibonacci Numbers w/ Dynamic Programming int fibonacci(int n) { static map 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]; }

Fibonacci Numbers w/ Dynamic Programming int fibonacci(int n) { static map 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

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(0)F(1)

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(0)F(1) Table Lookups

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(0)F(1) Table Lookups

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

Fibonacci Numbers: F(5) w/ memoization F(5) F(3)F(4) F(2)F(3) F(1)F(2) F(0)F(1)

Text Segmentation w/ Dynamic Programming double get_Pmax(const string &s) { static map 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; }

Text Segmentation w/ Dynamic Programming double get_Pmax(const string &s) { static map 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

Text Segmentation: Computing P max (“ent”) P max (“ent”) P 0 (“e”) x P max (“nt”)P 0 (“en”) x P max (“t”) P 0 (“n”) x P max (“t”) P 0 (“ent”) x P max (“”) P 0 (“t”) x P max (“”) P 0 (“nt”) x P max (“”)

Text Segmentation: Computing P max (“ent”) P max (“ent”) P 0 (“e”) x P max (“nt”)P 0 (“en”) x P max (“t”) P 0 (“n”) x P max (“t”) P 0 (“ent”) x P max (“”) P 0 (“t”) x P max (“”) P 0 (“nt”) x P max (“”) Table Lookup

Text Segmentation: Computing P max (“ent”) P max (“ent”) P 0 (“e”) x P max (“nt”)P 0 (“en”) x P max (“t”) P 0 (“n”) x P max (“t”) P 0 (“ent”) x P max (“”) P 0 (“t”) x P max (“”) P 0 (“nt”) x P max (“”) Table Lookup

Text Segmentation: Computing P max (“ent”) P max (“ent”) P 0 (“e”) x P max (“nt”)P 0 (“en”) x P max (“t”) P 0 (“n”) x P max (“t”) P 0 (“ent”) x P max (“”) P 0 (“t”) x P max (“”) P 0 (“nt”) x P max (“”) Table Lookup

Text Segmentation: Computing P max (“ent”) P max (“ent”) P 0 (“e”) x P max (“nt”)P 0 (“en”) x P max (“t”) P 0 (“n”) x P max (“t”) P 0 (“ent”) x P max (“”) P 0 (“t”) x P max (“”) P 0 (“nt”) x P max (“”)

Dynamic Programming through Memoization Any questions?

Options TopCoder… –Marathon Match 49 –SRM 418 – Division II Solve text segmentation using dynamic programming