Compsci 201 Recitation 12 Professor Peck Jimmy Wei 11/15/2013.

Slides:



Advertisements
Similar presentations
Dynamic Programming Introduction Prof. Muhammad Saeed.
Advertisements

CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann.
§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
The Greedy Method1. 2 Outline and Reading The Greedy Method Technique (§5.1) Fractional Knapsack Problem (§5.1.1) Task Scheduling (§5.1.2) Minimum Spanning.
Types of Algorithms.
Algorithms + L. Grewe.
Dynamic Programming.
Analysis of Algorithms Dynamic Programming. A dynamic programming algorithm solves every sub problem just once and then Saves its answer in a table (array),
Identify and state the value of a penny, nickel, dime, quarter Level 1 CLICK HERE TO START LEVEL 1.
Allocation problems - The Hungarian Algorithm The Hungarian algorithm Step 1Reduce the array by both row and column subtractions Step 2Cover the zero elements.
CS 112 Intro to Computer Science II Sami Rollins Fall 2006.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 16 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 08 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 25 / 2009 Instructor: Michael Eckmann.
Dynamic Programming CSC 172 SPRING 2002 LECTURE 6.
Dynamic Programming Opening Discussion zDo you have any questions about the quiz? zWhat did we talk about last class? zDo you have any questions.
Math Review Show each amount using the fewest number of coins. 98¢ pennies nickels dimes quarters 1.
Dynamic Programming. What is dynamic programming? Break problem into subproblems Work backwards Can use ‘recursion’ ‘Programming’ - a mathematical term.
Compsci 201 Recitation 11 Professor Peck Jimmy Wei 11/8/2013.
1 Summary: Design Methods for Algorithms Andreas Klappenecker.
CSC 413/513: Intro to Algorithms Greedy Algorithms.
Penny Nickel Dime Penny Nickel Dime Quarter Half Dollar.
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Dynamic Programming. Algorithm Design Techniques We will cover in this class: ◦ Greedy Algorithms ◦ Divide and Conquer Algorithms ◦ Dynamic Programming.
Counting Coins. The Basics Quarter 25 cents Dime 10 cents.
MONEY! Pick the correct coin! Click here to get started!!
CPSC 320: Intermediate Algorithm Design & Analysis Greedy Algorithms and Graphs Steve Wolfman 1.
Touch Money Jane Hancock.
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.
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.
Compsci 201 Midterm 2 Review Jimmy Wei Make sure you sign in on the paper being passed around!
CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.
Dynamic Programming continued David Kauchak cs302 Spring 2012.
Let’s Learn About Money!
Name the United States Coins Count the Pennies 10 ¢
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.
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.
Types of Algorithms. 2 Algorithm classification Algorithms that use a similar problem-solving approach can be grouped together We’ll talk about a classification.
Compsci 201 Recitation 10 Professor Peck Jimmy Wei 11/1/2013.
The Importance of Counting Coins. Coins in everyday life  How important is being able to count coins?  What are coins used for?  When and how often.
1 Today’s Material Dynamic Programming – Chapter 15 –Introduction to Dynamic Programming –0-1 Knapsack Problem –Longest Common Subsequence –Chain Matrix.
Compsci 201 Recitation 12 Professor Peck Jimmy Wei 4/11/2014.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Addition.
Fundamental Data Structures and Algorithms Ananda Guna March 18, 2003 Dynamic Programming Part 1.
TU/e Algorithms (2IL15) – Lecture 3 1 DYNAMIC PROGRAMMING
Dynamic Programming. What is Dynamic Programming  A method for solving complex problems by breaking them down into simpler sub problems. It is applicable.
Greedy Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
CSCI 58000, Algorithm Design, Analysis & Implementation Lecture 12 Greedy Algorithms (Chapter 16)
Greedy algorithms: CSC317
Advanced Design and Analysis Techniques
Money Concept Assessment Bank
Intro to Recursion.
Types of Algorithms.
Topic 25 Dynamic Programming
Data Structures and Algorithms
Dynamic Programming.
Dynamic Programming.
Data Structures and Algorithms
Types of Algorithms.
Name the United States Coins
Discrete Mathematics CMP-101 Lecture 12 Sorting, Bubble Sort, Insertion Sort, Greedy Algorithms Abdul Hameed
Greedy Algorithms.
Analysis of Algorithms CS 477/677
CSE 326: Data Structures Lecture #24 The Algorhythmics
X y y = x2 - 3x Solutions of y = x2 - 3x y x –1 5 –2 –3 6 y = x2-3x.
Types of Algorithms.
Lecture 4 Dynamic Programming
James Wei Professor Peck 9/20/2013
Presentation transcript:

Compsci 201 Recitation 12 Professor Peck Jimmy Wei 11/15/2013

In this Recitation Greedy algorithms – Brief intro – Practice Memoization – Brief intro – Practice Submit via form: The problems we cover today are also APTs! – Apt/

Greedy Algorithms What are they? – In short, an algorithm where continuously selecting local optima leads to a globally optimal solution – This is called “greedy” because we select the best solution for every subproblem – Will learn about greedy algorithms in more detail in Compsci 330

Greedy Algorithms As an example, consider the problem of making change: – We want to use as few coins as possible that amount to our total What coins would you give for $0.63? – 2 quarters, 1 dime, 3 pennies How did you determine that solution? – Start by using as many quarters as possible, then dimes, then nickels, then pennies – Can you see how this is “greedy”?

Greedy Algorithms – Answer 1-3 Time for practice! Read over the VoteRigging APT and write the following method: Answering the questions will help you write this code—think about what the greedy choice is here and how that leads to our solution /** * Returns the minimum number of votes to buy for candidate 0 to win * Returns the minimum number of votes to buy for candidate 0 to win * the election * the election votes is an array where the ith element represents votes is an array where the ith element represents * how many individuals were originally plan to vote for the ith candidate * how many individuals were originally plan to vote for the ith candidate */ */ public int minimumVotes(int[] votes); /** * Returns the minimum number of votes to buy for candidate 0 to win * Returns the minimum number of votes to buy for candidate 0 to win * the election * the election votes is an array where the ith element represents votes is an array where the ith element represents * how many individuals were originally plan to vote for the ith candidate * how many individuals were originally plan to vote for the ith candidate */ */ public int minimumVotes(int[] votes);

Greedy Algorithms – Answer 4-6 Time for practice! Read over the OlympicCandles APT and write the following method: Answering the questions will help you write this code—think about what the greedy choice is here and how that leads to our solution /** * Returns the maximum number of nights you can light candles * Returns the maximum number of nights you can light candles candles is an array where the ith element represents the height of candles is an array where the ith element represents the height of * the ith candle you have available * the ith candle you have available */ */ public int numberOfNights(int[] candles); /** * Returns the maximum number of nights you can light candles * Returns the maximum number of nights you can light candles candles is an array where the ith element represents the height of candles is an array where the ith element represents the height of * the ith candle you have available * the ith candle you have available */ */ public int numberOfNights(int[] candles);

Memoization What is it? – No, it’s not “memorization”… no ‘r’ – Refers to the idea that as we solve problems we might sometimes need to reuse solutions to subproblems, so we store them instead of recalculating them – Also will learn more about this and dynamic programming in CS330

Memoization As an example, think of recursive fibonacci: – What’s wrong with the recursive solution below? – We recalculate fibonacci for low values of n – Instead, we could store those values in a data structure (memoize) so that we don’t have to recalculate them! public int fibonacci(int n) { return (n<=2) ? 1 : fibonacci(n-1) + fibonacci(n-2); return (n<=2) ? 1 : fibonacci(n-1) + fibonacci(n-2);} public int fibonacci(int n) { return (n<=2) ? 1 : fibonacci(n-1) + fibonacci(n-2); return (n<=2) ? 1 : fibonacci(n-1) + fibonacci(n-2);}

Memoization Time for practice! Read over the BSTCount APT and write the following method: Think about the subproblems you need to solve here and how we can store the solutions to those subproblems to avoid having to recalculate them /** * Returns the total number of possible BSTs with the given values * Returns the total number of possible BSTs with the given values values is an array holding all values that need to be stored in values is an array holding all values that need to be stored in * the BST * the BST */ */ public long howMany(int[] values); /** * Returns the total number of possible BSTs with the given values * Returns the total number of possible BSTs with the given values values is an array holding all values that need to be stored in values is an array holding all values that need to be stored in * the BST * the BST */ */ public long howMany(int[] values);

Have a good weekend! Don’t forget to submit!