Maximum Contiguous Subsequence Sum Check out from SVN: MCS CSSRac - - PowerPoint PPT Presentation

maximum contiguous subsequence sum
SMART_READER_LITE
LIVE PREVIEW

Maximum Contiguous Subsequence Sum Check out from SVN: MCS CSSRac - - PowerPoint PPT Presentation

Maximum Contiguous Subsequence Sum Check out from SVN: MCS CSSRac Races es Finish Comparators Maximum Contiguous Subsequence Sum Problem Worktime for WA02 or Pascal? Correctness usually graded using JUnit tests Exception:


slide-1
SLIDE 1

Maximum Contiguous Subsequence Sum

Check out from SVN: MCS CSSRac Races es

slide-2
SLIDE 2
slide-3
SLIDE 3

 Finish Comparators  Maximum Contiguous Subsequence Sum

Problem

 Worktime for WA02 or Pascal?

slide-4
SLIDE 4

 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.

slide-5
SLIDE 5

See ee Comp mpara ratorExamp mple le in in your re reposi sitory ry Uses a prime “function

  • bject” example:in Java:

Comparator Add a dd an n anon nonymous Compara rator t r to main in()

slide-6
SLIDE 6
slide-7
SLIDE 7

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

Q1

slide-8
SLIDE 8

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

slide-9
SLIDE 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”?
slide-10
SLIDE 10

 In {-2, 11,

1, -4, 13 13, -5, 2}, S2,4 = ?

 In {1, -3, 4, -2, -1, 6}, what is MCSS?  If every element is negative, what’s the MCSS?

1-based indexing

Q2 Q2-4

slide-11
SLIDE 11

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

Q5 Q5

slide-12
SLIDE 12

Where

will this algorithm spend the most time?

How many times

(exactly, as a function of N = a.length) will that statement execute?

i: beginni nning ng o

  • f

subse sequence j: end nd of

  • f

subse sequence k: : steps ps t thro rough each e element of t of subse sequence

Find nd the he sum ums of all subseq sequenc uences es

slide-13
SLIDE 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.

slide-14
SLIDE 14

 By hand  Using Maple  Magic! (not really, but a preview of Disco)

slide-15
SLIDE 15

 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

Q6, Q6, Q7 Q7

slide-16
SLIDE 16

2 ) 1 ( 2 ) 1 (

1 1 1

i i n n j j j

n j i j n i j

− − + = − =∑

∑ ∑

= − = =

∑ ∑ ∑

= − = =

+ =

n j i j n i j

j j j

1 1 1

Then we can solve for the last term to get a formula that we need on the next slide: We have seen this idea before

slide-17
SLIDE 17
slide-18
SLIDE 18

 When it gets down to “just Algebra”, Maple is

  • ur friend
slide-19
SLIDE 19
slide-20
SLIDE 20
slide-21
SLIDE 21
slide-22
SLIDE 22

 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
slide-23
SLIDE 23

 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

  • unt that has a
  • ne

ne-to to-one c e corresp sponden ence with the original

slide-24
SLIDE 24

 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!

Q8 Q8

slide-25
SLIDE 25

 Choose 3 balls

  • If red drawn, = min of other 2
  • If blue drawn, = max of other 2

Triple riple of

  • f ba

balls lls Corre

  • rrespondi

ding tripl triple of

  • f 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)

slide-26
SLIDE 26

 There’s a formula!  It counts the ways to choose M items from a

set of P items “without replacement”

 "P choose M" written PCM or

is:

 So n+2C3 is

slide-27
SLIDE 27

 The performance is bad!

slide-28
SLIDE 28

This is Θ(?)

slide-29
SLIDE 29

Tune in next time for the exciting conclusion!

http://www.etsu.edu/math/gardner/batman

Q9, Q9, Q10 Q10