/
The Complexity of Algorithms: The Complexity of Algorithms:

The Complexity of Algorithms: - PowerPoint Presentation

marina-yarberry
marina-yarberry . @marina-yarberry
Follow
432 views
Uploaded On 2017-04-15

The Complexity of Algorithms: - PPT Presentation

Selected Exercises Goal Introduce computational complexity analysis Copyright Peter Cappello 2 2 Exercise 10 How much time does an algorithm take for a problem of size n if it uses ID: 537682

x2n sec peter copyright sec x2n copyright peter cappello time double algorithm int return exercise factor 224 operations size bit problem item

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "The Complexity of Algorithms:" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

The Complexity of Algorithms: Selected Exercises

Goal:

Introduce

computational complexity analysis.Slide2

Copyright © Peter Cappello2

2

Exercise 10

How much time does an algorithm take for a problem of size

n

,

if it uses

2n

2

+ 2

n

bit operations, each taking

10

-9

second?

n = 10

:

(

2(10)

2

+ 2

10

)10

-9

sec

1.224

*

10

-

6

sec

.

Slide3

3

Copyright © Peter Cappello

3

Exercise 10

How much time does an algorithm take for a problem of size

n

,

if it uses

2n

2

+ 2

n

bit operations, each taking

10

-9

second?

n = 10

:

(

2(10)

2

+ 2

10

)10

-9

sec

1.224

*

10

-

6

sec

.

n = 20

:

(

2(20)

2

+ 2

20

)10

-9

sec

1.05

*

10

-

3

sec

.

Slide4

4

Copyright © Peter Cappello

4

Exercise 10

How much time does an algorithm take for a problem of size

n

,

if it uses

2n

2

+ 2

n

bit operations, each taking

10

-9

second?

n = 10

:

(

2(10)

2

+ 2

10

)10

-9

sec

1.224

*

10

-

6

sec

.

n = 20

:

(

2(20)

2

+ 2

20

)10

-9

sec

1.05

*

10

-

3

sec

.

n = 50

:

(

2(50)

2

+ 2

50

)10

-9

sec

1.13

*

10

6

sec

13

days

.Slide5

5

Copyright © Peter Cappello

5

Exercise 10

How much time does an algorithm take for a problem of size

n

,

if it uses

2n

2

+ 2

n

bit operations, each taking

10

-9

second?

n = 10

:

(

2(10)

2

+ 2

10

)10

-9

sec

1.224

*

10

-

6

sec

.

n = 20

:

(

2(20)

2

+ 2

20

)10

-9

sec

1.05

*

10

-

3

sec

.

n = 50

:

(

2(50)

2

+ 2

50

)10

-9

sec

1.13

*

10

6

sec

13

days

.

n = 100

:

(

2(100)

2

+ 2

100

)10

-9

sec

1.27

*

10

21

sec

4

*

10

13

years

.Slide6

Copyright © Peter Cappello

6

Exercise 20

Analyze the

worst-case

[

time

] complexity of the program on the following slide,

in terms of

the number of elements in the input array.(That is, give a O() estimate of the time.)Slide7

Copyright © Peter Cappello

7

Method (not compiled)

List<Integer>

getBiggersList

(

int

[]

intArray

) { List<Integer> biggers = new LinkedList<Integer>(); int sum = 0; for ( int item : intArray )

{ if ( item > sum )

biggers.add

( item );

sum += item;

}

return

biggers

;

}Slide8

Copyright © Peter Cappello

8

Exercise 20 continued

Let

n

=

intArray.length

.

The statements

outside the for statement complete in constant time (i.e., do not depend on n), say c1 sec.The

body of the for statement executes

n

times.

Each iteration of the body takes

constant

time, say

c

2

sec.

Only if

the List

add

operation requires

constant

time (i.e., does not depend on

n

).

Total time, in seconds, is

c

1

+

c

2

n

, which is

O(

n

)

.Slide9

Copyright © Peter Cappello

9

Exponentiation revisited

double x2n( double x, int

n

)

{

double x2n = 1.0;

for ( int i = 0; i <

n

; i++ )

x2n *= x;

return x2n;

}

double x2z( double x, int z )

{

return ( z < 0 ) ? 1.0 / x2n( x, -z ) : x2n( x, z );

}

Give a

O()

estimate for the time to compute

x2n

as a function of

n

.Slide10

Copyright © Peter Cappello

10

Program Notes

Consider a faster algorithm for x2n

.

(But which continues to ignore underflow/overflow.)Slide11

Copyright © Peter Cappello

11

Faster algorithm for x2n

double x2n( double x, int

n

)

{

double x2n = 1.0, factor = x;

while (

n

> 0 )

{

if (

n

% 2 == 1 )

x2n *= factor;

n

/= 2;

factor *= factor;

}

return x2n;

}

Evaluate the algorithm for

n

= 21

.

Give a

O()

estimate for the time to compute

x2n

as a function of

n

.Slide12

Copyright © Peter Cappello

12

Recursive version of faster algorithm

double x2n( double x,

int

n )

{

if ( n == 0 )

return 1.0;

return ( ( n % 2 == 0 ) ? 1 : x ) * x2n( x * x, n / 2 );

}

Evaluate

x2n( 2.0

,

21 )

.

How many times is

x2n

invoked

, as a function of

n

?

We address this question when we study

recurrence relations

.Slide13

Copyright © Peter Cappello 201113

END

Copyright © Peter Cappello 2011

13