Public void main What do you call something that’s not static?

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann.
Advertisements

§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.1 Data Structure and Algorithm Lecture 6 Greedy Algorithm Topics Reference: Introduction to Algorithm by Cormen Chapter 17: Greedy Algorithm.
(())()(()) ((())()(())()(()))
This title is orange because of Halloween. It is totally not because it’s always orange.
Introduction to Bioinformatics Algorithms Divide & Conquer Algorithms.
Introduction to Bioinformatics Algorithms Divide & Conquer Algorithms.
I couldn’t think of anything to put here. So have a cow. Or two.
Semester 10 Time sure flies.. PotW Solution One possible solution is to randomly search the grid: o At each point in your search, look at the (up to four)
15-May-15 Dynamic Programming. 2 Algorithm types Algorithm types we will consider include: Simple recursive algorithms Backtracking algorithms Divide.
RAIK 283: Data Structures & Algorithms
1 Dynamic Programming Jose Rolim University of Geneva.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
UNIVERSITY OF SOUTH CAROLINA College of Engineering & Information Technology Bioinformatics Algorithms and Data Structures Chapter 12: Refining Core String.
Dynamic Programming CIS 606 Spring 2010.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Dynamic Programming Optimization Problems Dynamic Programming Paradigm
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
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.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
ADA: 7. Dynamic Prog.1 Objective o introduce DP, its two hallmarks, and two major programming techniques o look at two examples: the fibonacci.
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2008 Dynamic programming  top-down vs. bottom-up  divide & conquer vs. dynamic programming  examples:
Dynamic Programming. What is dynamic programming? Break problem into subproblems Work backwards Can use ‘recursion’ ‘Programming’ - a mathematical term.
Houyang 3/25/13. USACO March Contest Congrats to Johnny Ho for scoring over 900 points in the Gold Division o 7th place in US Kudos to Jonathan Uesato.
Happy Birthday Julia Courier New. USACO December Contest - Congratulations to Jon+Julia+Andy for promoting to gold And Johnny - 2th place in gold among.
Lecture 13 Recursion part 2 Richard Gesick. Advanced Recursion Sometimes recursion involves processing a collection. While it could be done using 'foreach'
Recursion When to use it and when not to use it. Basics of Recursion Recursion uses a method Recursion uses a method Within that method a call is made.
DP (not Daniel Park's dance party). Dynamic programming Can speed up many problems. Basically, it's like magic. :D Overlapping subproblems o Number of.
Honors Track: Competitive Programming & Problem Solving Optimization Problems Kevin Verbeek.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
Happy Birthday Tony Palindromes.
Topic 25 Dynamic Programming "Thus, I thought dynamic programming was a good name. It was something not even a Congressman could object to. So I used it.
The Greedy Method. The Greedy Method Technique The greedy method is a general algorithm design paradigm, built on the following elements: configurations:
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
Introduction to Algorithms Jiafen Liu Sept
CS 3343: Analysis of Algorithms Lecture 18: More Examples on Dynamic Programming.
Dynamic Programming.  Decomposes a problem into a series of sub- problems  Builds up correct solutions to larger and larger sub- problems  Examples.
Chapter 7 Dynamic Programming 7.1 Introduction 7.2 The Longest Common Subsequence Problem 7.3 Matrix Chain Multiplication 7.4 The dynamic Programming Paradigm.
Dynamic Programming (DP) By Denon. Outline Introduction Fibonacci Numbers (Review) Longest Common Subsequence (LCS) More formal view on DP Subset Sum.
Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout
Questions 4) What type of algorithmic problem-solving technique (greedy, divide-and-conquer, dynamic programming)
Fundamental Data Structures and Algorithms Ananda Guna March 18, 2003 Dynamic Programming Part 1.

Dynamic Programming … Continued
Dynamic Programming. What is Dynamic Programming  A method for solving complex problems by breaking them down into simpler sub problems. It is applicable.
Dynamic Programming Examples By Alexandra Stefan and Vassilis Athitsos.
Lecture 12.
All-pairs Shortest paths Transitive Closure
Recursion DRILL: Please take out your notes on Recursion
Discrete Optimization
Dynamic Programming.
Data Structures and Algorithms
Lecture 18 Recursion part 2 Richard Gesick.
Dynamic Programming 1/15/2019 8:22 PM Dynamic Programming.
Trevor Brown DC 2338, Office hour M3-4pm
Advanced Programming Techniques
Lecture 5 Dynamic Programming
This is not an advertisement for the profession
Presentation transcript:

public void main What do you call something that’s not static?

PotW Prizes The prizes: 5 th place - $5 Fry’s gift card 4 th place - $5 Fry’s gift card (and bragging rights over the 5 th place winner) 3 rd place - $10 Fry’s gift card (and bragging rights over the 4 th and 5 th place winners) 2 nd place - $15 Fry’s gift card (and bragging rights over the 3 rd, 4 th, and 5 th place winners) 1 st place - $15 Fry’s gift card, and A SURPRISE :O …and bragging rights

PotW Winners =D 5 th place – Julia Huang 4 th place – Steven Hao 3 rd place – Qingqi Zeng 2 nd place – Tony Jiang 1 st place – A winner is you! James Hong/* Johnny Ho */Johnny Ho

Dynamic Programming Basically, it's magic. o Whenever you have a recursive method that takes a certain set of parameters Return value must be uniquely determined by this set of parameters o “Memoize” the return value of the method for that set of parameters using an array or hash table o Programs can often be sped up by exponential factors using this method

Fibonacci (naïve) int fib(int i) { // assumes i >= 0 if (i == 0) return 0; if (i == 1) return 1; return (fib(i - 1) + fib(i - 2)) % MOD; } Very easy to understand Incredibly slow o Exponential time on i

Fibonacci (recursive) int[] mem = new int[MAX_NUM]; Arrays.fill(mem, -1); int fib(int i) { // assumes i >= 0 if (i == 0) return 0; if (i == 1) return 1; if (mem[i] != -1) return mem[i]; return mem[i] = (fib(i - 1) + fib(i - 2)) % MOD; } Elegant, understandable Risk overflowing stack memory

Fibonacci (iterative) int[] mem = new int[MAX_NUM]; mem[0] = 0; mem[1] = 1; for (int i = 2; i < MAX_NUM; ++i) { mem[i] = (mem[i - 1] + mem[i - 2]) % MOD; } Faster, no risk of overflowing stack memory Becomes less understandable for more complex problems Order of iteration matters!

Binomial Coefficient (recursion in 2D) int[][] mem = new int[MAX_NUM][MAX_NUM]; for (int[] arr: mem) { Arrays.fill(arr, -1); } int choose(int n, int k) { // assumes n, k >= 0 if (n == 0 || k == 0) return 1; if (mem[n][k] != -1) return mem[n][k]; return mem[n][k] = (choose(n - 1, k - 1) + choose(n - 1, k)) % MOD; }

Knapsack Given a set of objects, each with some volume and some value, maximize the value of the objects you can fit in a container of a certain volume o This is known as the 0-1 knapsack problem Each object is either not chosen or chosen o 2D DP over index of current object and currently used volume Reconstructing the solution o Which objects should I pick? o References to previous states should be saved

More Examples Maximal sum paths o Given a rectangle of numbers, find the largest sum of numbers you can achieve walking only right/down from the top-left corner to the bottom-right corner Longest increasing subsequence o Given a sequence of numbers, find the longest strictly increasing (not necessarily consecutive) subsequence Longest common subsequence o Given two sequences of numbers, find the longest common (not necessarily consecutive) subsequence Edit (Levenshtein) distance o If you can erase/insert/swap characters of a string A, how many operations do you need to reach string B

February USACO Is this coming weekend! (Feb. 3-6) Is worth 5 points of PotW credit! (as usual) Is something you should take! (as always) USACO is already halfway over!

PotW – Magic Feed Machine Bessie has figured out how to manipulate the amount of feed she gets. On the feeder, there are N lights in a row, and Bessie only has time for at most K operations. On each operation, she can take a continuous subsequence of the lights and flip their sign (lights that were on turn off, lamps that were off turn on). She will receive feed proportional to the number of lights that are on in the end. Tell her the optimal possible result. Worth 35 points.

Magic Feed Machine (cont.) Constraints: N < 1000 K < 500 Sample Input: 1(K) (starting configuration: 1 = on, 0 = off) Sample Output: 5( and both have 5 lights on)

PotW Hints Dynamic programming! (surprise surprise) Note that each operation (substring flip) is equivalent to: o Two suffix flips o Count suffix flips instead of substring flips o Thus, you can perform up to 2*K suffix flips Recurse over: o the index of the current light o how many suffix flips have been done so far Store this in a N by 2*K array Create a recursive function Overall algorithm is O(NK)