Docsity

Maximum Contiguous Subsequence Sum Problem - Solutions | COP 3503, Study notes of Computer Science

Toggle sidebar
bg1
Maximum contiguous subsequence sum problem | Sol. 1
Problem:
Find
max
(
h
,
1
X
i
=
`
a
[
i
]
0
`
h
n
)
where
a
[0
::n
,
1]
is an arrayof integers (possibly negative) and
n
0
.
Solution 1:
summax
:= 0;
for
`
from
0
to
n
,
1
:
for
h
from
`
+1
to
n
:
f
sum
:= 0;
for
i
from
l
to
h
,
1
:
sum
:=
sum
+
a
[
i
];
if
summax
<
sum
then
summax
:=
sum
;
g
pf3
pf4
pf5
pf8

Partial preview of the text

Download Maximum Contiguous Subsequence Sum Problem - Solutions | COP 3503 and more Study notes Computer Science in PDF only on Docsity!

Maximum

contiguous

subsequence

sum

p

roblem

Sol.

Problem:

Find

max

h^ 1

X

i

`

a [ i

] 0  `  h  n )

where

a [

n

(^)

1]

is

an

a rra

y

of

integers

(p

ossibly

negative)

and

n

Solution

summax

for

`

from

to

n

for

h

from

`

to

n

f

sum

for

i

from

l

to

h

sum

sum

a [ i ];

if

summax

sum

then

summax

sum

g

Maximum

contiguous

subsequence

sum

p

roblem

complexit

y

of

Sol.

c 1 + n 1

X

`

c 2

n

X

h

`

c 3 + h 1

X

i

`

c 4

= c 1 + n 1

` X

=

c 2

n

X

h

`

( c 3 + c 4  ( h `

= c 1 + n 1

` X

=

c 2

c 3

c 4 (^) )

  • ( c 3 + c 4  ( n `

 ( n ` )

= c 1 + c 2  n + n 1

` X

=

 c 3 + c 4 )  ( n ` )

2! + n 1

` X

=

c 4  ( n ` ) 2

= c 1 + c 2  n + n

` X

=

 c 3 + c 4 )  `

n

X

`

c 4

 ` 2

= c 1 + c 2  n +

 c 3 + c 4

n

` X

=

`

c 4

n

` X

=

`

2

= c 1 + c 2  n +

 c 3 + c 4

2  n  ( n +

c 4

2  n  ( n +

n

n

3 )

Maximum

contiguous

subsequence

sum

p

roblem

complexit

y

of

Sol.

Problem:

Find

max

h^ 1

X

i

`

a [ i

] 0  `  h  n )

where

a [

n

(^)

1]

is

an

a rra

y

of

integers

(p

ossibly

negative)

and

n

Solution

summax

for

`

from

to

n

f

sum

for

h

from

`

to

n

f

sum

sum

a [ h

1];

if

summax

sum

then

summax

sum

g

g

Complexit

y:

c 1 + n 1

X

`

c 2

n

X

h

`

c 3! = c 1 + c 2  n + n 1

X

`

c 3  ( n ` ) = c 1 + c 2  n + c 3  n

X

`

`

2 c 1 + c 2  n + c 3  ( n +

n

n

2 )

Maximum

contiguous

subsequence

sum

p

roblem

Sol.

Problem:

Find

max

h^ 1

X

i

`

a [ i

] 0  `  h  n )

where

a [

n

(^)

1]

is

an

a rra

y

of

integers

(p

ossibly

negative)

and

n

Solution

(divide

and

conquer):

Either

the

maximum

of

the

left

half,

or

the

maximum

of

the

right

half,

or

the

max.

of

the

left

half

touching

the

center

the

max.

of

the

right

half

touching

the

center

DIVIDE-AND-CONQUER

metho

(^) d:

split

the

p roblem

into

subp

roblems

of

roughly

equal

sizes,

solve

each

subp

roblem

recursively

do

an

extra

small

amount

of

w

o rk

to

combine

the

solutions

of

the

subp

roblems

into

a

solution

of

the

p roblem.

maxsub

p;

q (^) )

=def

if

p

q

then

f

if

a [ p ] <

then

else

a [ p ]

g

else

f =  p + 2  q  =

k

b ( p + q ) = 2 c ;

lmax

maxsub

p;

k );

rmax

maxsub

k (^) ; q (^) );

lb

dmax

lb

d

for

i

from

k

to

p

f

lb

d

lb

d

a [ i ];

if

lb

dmax

lb

d

then

lb

dmax

lb

d (^) ;

g

rb

dmax

rb

d

for

i

from

k

to

p

f

rb

d

rb

d

a [ i ];

if

rb

dmax

rb

d

then

rb

dmax

rb

d (^) ;

g

return

max

lmax

rmax

lb

dmax

rb

dmax

g

Maximum

contiguous

subsequence

sum

p

roblem

Sol.

Problem:

Find

max

h^ 1

X

i

`

a [ i

] 0  `  h  n )

where

a [

n

(^)

1]

is

an

a rra

y

of

integers

(p

ossibly

negative)

and

n

Solution

summax

sum

for

j

from

to

n

f

sum

sum

a [ j (^) ];

if

summax

sum

then

summax

sum

else

if

sum

then

sum

g

Complexit

y:

n

)

W

a

rnings

ab

out

constants

If

w

e

a

re

only

interested

in

asymptotic

complexit

y

then:

constants

in

recurrences

do

not

matter

WRONG!

t 1 (^) ( n ) = 1  t 1

n

linea

r

t 2 (^) ( n ) = 2  t 2

n

exp

onential

t 2

gro

ws

faster

constants

in

non-recursive

fo

r-

mulas

do

not

matter

WRONG!

t 1 (^) ( n

)

=

n

t 2 (^) ( n

)

=

n

t 2

gro

ws

faster

multiplying

anything

b

y

a

con-

stants

do

es

not

matter

WRONG!

t 1 (^) ( n ) = 2 1 

n

t 2 (^) ( n ) = 2 2 

n y^

t 2

gro

ws

faster

adding

anywhere

a

constant

do

es

not

matter

WRONG!

t 1 (^) ( n ) = ( n +

t 2 (^) ( n ) = ( n +

t 2

gro

ws

faster

multiplying

the

nal

v

alue

b

y

a

constant

o

r

adding

a

constant

to

the

nal

v

alue

do

es

not

matter

RIGHT!