The Complexity of Algorithms Selected Exercises Goal Introduce

  • Slides: 13
Download presentation
The Complexity of Algorithms: Selected Exercises Goal: Introduce computational complexity analysis.

The Complexity of Algorithms: Selected Exercises Goal: Introduce computational complexity analysis.

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

Exercise 10 How much time does an algorithm take for a problem of size n, if it uses 2 n 2 + 2 n bit operations, each taking 10 -9 second? a) n = 10: ( 2(10)2 + 210 )10 -9 sec ≈ 1. 224 *10 -6 sec. Copyright © Peter Cappello 2

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

Exercise 10 How much time does an algorithm take for a problem of size n, if it uses 2 n 2 + 2 n bit operations, each taking 10 -9 second? a) n = 10: ( 2(10)2 + 210 )10 -9 sec ≈ 1. 224 *10 -6 sec. b) n = 20: ( 2(20)2 + 220 )10 -9 sec ≈ 1. 05 *10 -3 sec. Copyright © Peter Cappello 3

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

Exercise 10 How much time does an algorithm take for a problem of size n, if it uses 2 n 2 + 2 n bit operations, each taking 10 -9 second? a) n = 10: ( 2(10)2 + 210 )10 -9 sec ≈ 1. 224 *10 -6 sec. b) n = 20: ( 2(20)2 + 220 )10 -9 sec ≈ 1. 05 *10 -3 sec. c) n = 50: ( 2(50)2 + 250 )10 -9 sec ≈ 1. 13 *106 sec ≈ 13 days. Copyright © Peter Cappello 4

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

Exercise 10 How much time does an algorithm take for a problem of size n, if it uses 2 n 2 + 2 n bit operations, each taking 10 -9 second? a) n = 10: ( 2(10)2 + 210 )10 -9 sec ≈ 1. 224 *10 -6 sec. b) n = 20: ( 2(20)2 + 220 )10 -9 sec ≈ 1. 05 *10 -3 sec. c) n = 50: ( 2(50)2 + 250 )10 -9 sec ≈ 1. 13 *106 sec ≈ 13 days. d) n = 100: ( 2(100)2 + 2100 )10 -9 sec ≈ 1. 27 *1021 sec ≈ 4 *1013 years. Copyright © Peter Cappello 5

Exercise 20 Analyze the worst-case [time] complexity of the program on the following slide,

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. ) Copyright © Peter Cappello 6

Method (not compiled) List<Integer> get. Biggers. List( int[] int. Array ) { List<Integer> biggers

Method (not compiled) List<Integer> get. Biggers. List( int[] int. Array ) { List<Integer> biggers = new Linked. List<Integer>(); int sum = 0; for ( int item : int. Array ) { if ( item > sum ) biggers. add( item ); sum += item; } return biggers; } Copyright © Peter Cappello 7

Exercise 20 continued • Let n = int. Array. length. • The statements outside

Exercise 20 continued • Let n = int. Array. length. • The statements outside the for statement complete in constant time (i. e. , do not depend on n), say c 1 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 ). Copyright © Peter Cappello 8

Exponentiation revisited double x 2 n( double x, int n ) { double x

Exponentiation revisited double x 2 n( double x, int n ) { double x 2 n = 1. 0; for ( int i = 0; i < n; i++ ) x 2 n *= x; return x 2 n; } double x 2 z( double x, int z ) { return ( z < 0 ) ? 1. 0 / x 2 n( x, -z ) : x 2 n( x, z ); } Give a O() estimate for the time to compute x 2 n as a function of n. Copyright © Peter Cappello 9

Program Notes Consider a faster algorithm for x 2 n. (But which continues to

Program Notes Consider a faster algorithm for x 2 n. (But which continues to ignore underflow/overflow. ) Copyright © Peter Cappello 10

Faster algorithm for x 2 n double x 2 n( double x, int n

Faster algorithm for x 2 n double x 2 n( double x, int n ) { double x 2 n = 1. 0, factor = x; while ( n > 0 ) { if ( n % 2 == 1 ) x 2 n *= factor; n /= 2; factor *= factor; } return x 2 n; } Evaluate the algorithm for n = 21. Give a O() estimate for the time to compute x 2 n as a function of n. Copyright © Peter Cappello 11

Recursive version of faster algorithm double x 2 n( double x, int n )

Recursive version of faster algorithm double x 2 n( double x, int n ) { if ( n == 0 ) return 1. 0; return ( ( n % 2 == 0 ) ? 1 : x ) * x 2 n( x * x, n / 2 ); } Evaluate x 2 n( 2. 0, 21 ). How many times is x 2 n invoked, as a function of n? We address this question when we study recurrence relations. Copyright © Peter Cappello 12

END Copyright © Peter Cappello 2011 13 13

END Copyright © Peter Cappello 2011 13 13