Analysis of 2 Dimension Maxima - Algorithm - Lecture Slides, Slides of Algorithms and Programming

Key points of this lecture are: Analysis, 2 Dimensions, Maxima, Times, Output, Loop, Statement, Summations.

Typology: Slides

2011/2012

Uploaded on 10/25/2012

ramprasad
ramprasad 🇮🇳

4.3

(28)

119 documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Analysis of 2-dimension maxima
MAXIMA(int n, Point P[1...n])
1for i1to nn times
2do maximal true
3for j1to nn times
4do
5if (i6=j)&(P[i].x P[j].x)&(P[i].y P[j].y)4 accesses
6then maximal false break
7if maximal
8then output P[i].x, P[i].y 2 accesses
Algorithms p. 53
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download Analysis of 2 Dimension Maxima - Algorithm - Lecture Slides and more Slides Algorithms and Programming in PDF only on Docsity!

Analysis of 2-dimension maxima MAXIMA(int n, Point P

[1... n

])

1 for^ i

←^1 to

n^ n times 2 do^ maximal

←^ true 3 for

j^ ←^1 to

n^ n times 4 do 5

if^ (i^6 =^ j

)&(P[i]

.x^ ≤^ P[

j].x)&(

P[i].y^ ≤

P[j].y)

4 accesses

6

then^ maximal

←^ false

break

7 if

maximal 8

then^ output

P[i].x, P

[i].y^

2 accesses

Analysis of 2-dimension maxima^ •^ The outer loop runs

n^ times

-^ For each

i^ iteration, the inner loop runs

n

time. • P^ is accessed four times in the

if^ statement.

-^ The output statement accesses

P^ 2 times.

-^ In the worst case, every point is maximal soevery point is output.

Analysis of 2-dimension maxima^ Worst-case running time:

T^ (n) =

( n∑^2 i= 1

n∑+ j=^1

∑n (j=^1 4 ) =^ 4n

, and so^ n∑ T (n) =^ i=

(4n^ + 1

= (4n

+^2 )n

=^ 4n

2 +^ 2n

Analysis of 2-dimension maxima^ •^ For small values of

n, any algorithm is fast

enough. • What happens when

n^ gets large?

-^ Running time does become an issue. •^ When

n^ is large,

(^2) nterm will be much larger

than the

n^ term and will dominate the running time.

Summations^ •^ The analysis involved computing asummation.^ •^ Summation should be familiar but let usreview a bit here.

Summations^ •^ Given a finite sequence of values^ a^1

, a,... , a^2

,n

-^ their sum

a+^1

a+^...^2

+^ an^

is expressed in

summation notation

as^ n∑^ ai^ i=^1

-^ If^ n^

=^0 , then the sum is additive identity,

  1. Docsity.com^ Algorithms – p. 60

Summations^ Some important summations that should becommitted to memory.^ Arithmetic series

: n ∑^ i^ =^ i=^1

1 +^2

+^...^

+^ n n(n =

+^1 ) 2

=^ Θ(n

Summations^ Quadratic series

n∑^2 i= i=^1

1 +^4

+^9 +

...^ +^

(^2) n (^3) 2n = 2 +^ 3n

+^ n= 6

(^3) Θ(n

Summations^ Harmonic series

: For^ n

≥^0

H=n^

n∑^1 i i=^11 = 1 +^ +^2

1 +^... 3

(^1) + ≈ n^ ln^ n

=^ Θ(ln

n)

A Harder Example^ NESTED

  • LOOPS

1 for

i^ ←^1

to^ n 2 do 3

for^ j^ ←

1 to^ 2i 4

do^ k^ =

j... 5

while^ (

k^ ≥^0 ) 6

do^ k^ =

k^ −^ 1...

A Harder Example^ NESTED

  • LOOPS

1 for

i^ ←^1

to^ n 2 do 3

for^ j^ ←

1 to^ 2i 4

do^ k^ =

j... 5

while^ (

k^ ≥^0

6

do^ k^ =

k^ −^ 1... The answer is we write out the loops assummations and then solve the summations.

A Harder Example^ NESTED

  • LOOPS

1 for

i^ ←^1

to^ n 2 do 3

for^ j^ ←

1 to^ 2i 4

do^ k^ =

j... 5

while^ (

k^ ≥^0

6

do^ k^ =

k^ −^ 1... To convert loops into summations, we work frominside-out.

A Harder Example^ NESTED

  • LOOPS() 1 for i^ ←^1 to

n 2 do for

j^ ←^1 to

2i 3 do

k^ =^ j 4

while^ (k

≥^0 )^ J 5

do^ k^ =^

k^ −^1

-^ Time spent inside thewhile loop is constant. •^ Let^ I

()^ be the timespent in the while loop.Thus^ j^ ∑ I(j) =^ k=^0

1 =^ j^ +

A Harder Example^ NESTED

  • LOOPS() 1 for i^ ←^1 to

n 2 do for

j^ ←^1 to

2i^ J 3 do

k^ =^ j 4

while^ (k

≥^0 ) 5

do^ k^ =^

k^ −^1

-^ Consider the

middle for loop. • It’s running time is de-termined by

i.