Compsci 201 Recitation 12 Professor Peck Jimmy Wei 4/11/2014.
-
Upload
heather-anderson -
Category
Documents
-
view
212 -
download
0
description
Transcript of Compsci 201 Recitation 12 Professor Peck Jimmy Wei 4/11/2014.
![Page 1: Compsci 201 Recitation 12 Professor Peck Jimmy Wei 4/11/2014.](https://reader036.fdocuments.us/reader036/viewer/2022090102/5a4d1ba77f8b9ab0599c9717/html5/thumbnails/1.jpg)
Compsci 201 Recitation 12
Professor PeckJimmy Wei4/11/2014
![Page 2: Compsci 201 Recitation 12 Professor Peck Jimmy Wei 4/11/2014.](https://reader036.fdocuments.us/reader036/viewer/2022090102/5a4d1ba77f8b9ab0599c9717/html5/thumbnails/2.jpg)
In this Recitation• Greedy algorithms• Brief intro• Practice
• Memoization• Brief intro• Practice
• Submit via form: http://goo.gl/66Dhvn• Today’s problems are APTs!
![Page 3: Compsci 201 Recitation 12 Professor Peck Jimmy Wei 4/11/2014.](https://reader036.fdocuments.us/reader036/viewer/2022090102/5a4d1ba77f8b9ab0599c9717/html5/thumbnails/3.jpg)
Greedy Algorithms• What are they?• 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 sub-problem• Will learn about greedy algorithms in more detail
in Compsci 330
![Page 4: Compsci 201 Recitation 12 Professor Peck Jimmy Wei 4/11/2014.](https://reader036.fdocuments.us/reader036/viewer/2022090102/5a4d1ba77f8b9ab0599c9717/html5/thumbnails/4.jpg)
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”?
![Page 5: Compsci 201 Recitation 12 Professor Peck Jimmy Wei 4/11/2014.](https://reader036.fdocuments.us/reader036/viewer/2022090102/5a4d1ba77f8b9ab0599c9717/html5/thumbnails/5.jpg)
Greedy Algorithms – Answer 1-3• Read the VoteRigging APT and write the
following method:• Answer questions 1-3 before you write this
code• What the greedy choice?
/** * Returns the minimum number of votes to buy for candidate 0 to win * the election * @param votes is an array where the ith element represents * how many individuals were originally plan to vote for the ith candidate */public int minimumVotes(int[] votes);
![Page 6: Compsci 201 Recitation 12 Professor Peck Jimmy Wei 4/11/2014.](https://reader036.fdocuments.us/reader036/viewer/2022090102/5a4d1ba77f8b9ab0599c9717/html5/thumbnails/6.jpg)
Greedy Algorithms – Answer 4-6• Read the OlympicCandles APT and write the
following method:• Answer questions 4-6 before you write this
code• What the greedy choice?
/** * Returns the maximum number of nights you can light candles * @param candles is an array where the ith element represents the height of * the ith candle you have available */public int numberOfNights(int[] candles);
![Page 7: Compsci 201 Recitation 12 Professor Peck Jimmy Wei 4/11/2014.](https://reader036.fdocuments.us/reader036/viewer/2022090102/5a4d1ba77f8b9ab0599c9717/html5/thumbnails/7.jpg)
Because of the snow• We didn’t have to to discuss memoization in
class
• Help with BSTcount (you will not be penalized for not completing BSTcount for APT set 5)
![Page 8: Compsci 201 Recitation 12 Professor Peck Jimmy Wei 4/11/2014.](https://reader036.fdocuments.us/reader036/viewer/2022090102/5a4d1ba77f8b9ab0599c9717/html5/thumbnails/8.jpg)
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 sub-problems, so we store them instead of recalculating them
• Also will learn more about this and dynamic programming in CS330
![Page 9: Compsci 201 Recitation 12 Professor Peck Jimmy Wei 4/11/2014.](https://reader036.fdocuments.us/reader036/viewer/2022090102/5a4d1ba77f8b9ab0599c9717/html5/thumbnails/9.jpg)
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);}
![Page 10: Compsci 201 Recitation 12 Professor Peck Jimmy Wei 4/11/2014.](https://reader036.fdocuments.us/reader036/viewer/2022090102/5a4d1ba77f8b9ab0599c9717/html5/thumbnails/10.jpg)
Memoization• Read the BSTCount APT and write the following
method:
• Think about the sub-problems 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 * @param values is an array holding all values that need to be stored in * the BST */public long howMany(int[] values);
![Page 11: Compsci 201 Recitation 12 Professor Peck Jimmy Wei 4/11/2014.](https://reader036.fdocuments.us/reader036/viewer/2022090102/5a4d1ba77f8b9ab0599c9717/html5/thumbnails/11.jpg)
Have a good weekend!
Don’t forget to submit!