maximum contiguous subsequence sum correctness usually
play

Maximum Contiguous Subsequence Sum Correctness usually graded using - PowerPoint PPT Presentation

Maximum Contiguous Subsequence Sum Correctness usually graded using JUnit tests Exception: when we ask you to add your own tests Style No warnings remaining (per our preference file) Reasonable documentation Explanatory


  1. Maximum Contiguous Subsequence Sum

  2.  Correctness usually graded using JUnit tests ◦ Exception: when we ask you to add your own tests  Style ◦ No warnings remaining (per our preference file) ◦ Reasonable documentation ◦ Explanatory variable and method names ◦ You should format using Ctrl-Shift-F in Eclipse  Efficiency ◦ Usually reasonable efficiency will suffice  (e.q., no apparently infinite loops) ◦ Occasionally (like next week) we might give a minimum big-Oh efficiency for you to achieve Between two implementations with the same big-Oh efficiency, favor the more concise solution, unless you have data showing that the difference matters.

  3.  Finish Comparators  Maximum Contiguous Subsequence Sum Problem  Worktime for WA02 or Pascal?

  4. Uses an important “function object” example:in Java: Comparator Also Also a add a a se second anonymous s Compara rator fo r for r semip iperime imeters rs

  5.  java.util.Arrays and java.util.Collections are your friends! You can sort by any means you like: just pass your Comparator as a second argument to Arrays.sort() or Collections.sort().

  6. …but not Comparators See written assignment 2

  7. Q1 A deceptively deep problem with a surprising solution. {-3, 4, 2, 1, -8, -6, 4, 5, -2}

  8.  It’s interesting  Analyzing the obvious solution is instructive:  We can make the program more efficient

  9.  Prob roblem: Given a sequence of numbers, find the maximum sum of a contiguous subsequence.  Co Cons nsider: ◦ What if all the numbers were positive? ◦ What if they all were negative? ◦ What if we left out “contiguous”?

  10. Q2 Q2-4  In {-2, 11, 1, -4, 13 13, -5, 2}, S 2,4 = ?  In {1, -3, 4, -2, -1, 6}, what is MCSS?  If every element is negative, what’s the MCSS? 1-based indexing

  11. Q5 Q5  Design one right now. ◦ Efficiency doesn’t matter. ◦ It has to be easy to understand. ◦ 3 minutes  Examples to consider: ◦ {-3, 4, 2, 1, -8, -6, 4, 5, -2} ◦ {5, 6, -3, 2, 8, 4, -12, 7, 2}

  12. Find nd the he sum ums of all subseq sequenc uences es Where i: beginni nning ng o of subse sequence will this algorithm j: end nd of of subse sequence spend the most k: : steps ps t thro rough time? each e element of t of subse sequence How many times (exactly, as a function of N = a.length) will that statement execute?

  13.  What statement is executed the most often?  How many times?  How many triples, (i,j,k) with 1≤i≤k≤j≤n ? Outer numbers could be 0 and n – 1, and we'd still get the same answer.

  14.  By hand  Using Maple  A tangent (Related to urns and probabilities?)

  15. Q6, Q6, Q7 Q7  How many triples, (i,j,k) with 1≤i≤k≤j≤n ?  What is that as a summation?  Let’s solve it by hand to practice with sums

  16. − 1 n i n ∑ ∑ ∑ = + We have seen j j j this idea before = = = 1 1 j j j i Then we can solve for the last term to get a formula that we need on the next slide: + − − 1 n n i ( 1 ) ( 1 ) n n i i ∑ = ∑ ∑ − = − j j j 2 2 = = = 1 1 j i j j

  17.  When it gets down to “just Algebra”, Maple is our friend

  18.  If GM had kept up with technology like the computer industry has, we would all be driving $25 cars that got 1000 miles to the gallon. - Bill Gates  If the automobile had followed the same development cycle as the computer, a Rolls- Royce would today cost $100, get a million miles per gallon, and explode once a year, killing everyone inside. - Robert X. Cringely

  19.  How many triples, (i,j,k) with 1≤i≤k≤j≤n ?  The trick: ◦ Find a set that’s easi sier to c to cou ount that has a one ne-to to-one c e corresp sponden ence with the original

  20. Q8 Q8  We We want t to cou count t the number of triples, (i,j,k) with 1≤i≤k≤j≤n  First get an urn ◦ Put in n white balls labeled 1,2,…,n ◦ Put in one red ball and one blue one  Choose 3 balls ◦ If red drawn, = min of other 2 ◦ If blue drawn, = max of other 2  What numbers do we get? http://www.talaveraandmore.com, for all your urn needs!

  21.  Choose 3 balls ◦ If red drawn, = min of other 2 ◦ If blue drawn, = max of other 2 Triple riple of of ba balls lls Corre orrespondi ding tripl triple of of numbers (i, k, j) (i, k, j) (red, i, j) (i, i, j) (blue i, j) (i, j, j) (red, blue, i) (i, i, i)

  22.  There’s a formula!  It counts the ways to choose M items from a set of P items “without replacement”  "P choose M" written P C M or is:  So n+2 C 3 is

  23.  The performance is bad!

  24. This is Θ (?)

  25. Q9, Q9, Q10 Q10 Tune in next time for the exciting conclusion! http://www.etsu.edu/math/gardner/batman

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend