Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms: Selected Exercises Goals Introduce the concept & basic properties of an algorithm.

Similar presentations


Presentation on theme: "Algorithms: Selected Exercises Goals Introduce the concept & basic properties of an algorithm."— Presentation transcript:

1 Algorithms: Selected Exercises Goals Introduce the concept & basic properties of an algorithm

2 Copyright © Peter Cappello2 Algorithm A finite sequence of finite, precise instructions for performing a computation or for solving a problem.

3 Copyright © Peter Cappello3 Properties of an Algorithm 1.Input Its input is from a specified set, I. 2.Output Its output is from a specified set, O. 3.Effective Its steps are defined precisely and can be done. 4.Finite  i  I, finitely many steps are executed, each taking finite time. 5.Correct  i  I, the output is correct.

4 Copyright © Peter Cappello4 Exercise 6 Give an algorithm with: –Input: List iList; –Output: int n; where n is the # of negative integers in iList

5 Copyright © Peter Cappello5 Exercise 6 Solution int numNegatives( List iList ) { assert iList != null; int n = 0; for ( Integer i : iList ) { if ( i < 0 ) n++; } return n; } Will numNegatives return 0, if iList is empty?

6 Copyright © Peter Cappello6 Exercise 10 Give an algorithm with: Input: int n; double x; Output: double f; where f = x n. Hint: 1.Give a procedure to compute x n, when n ≥ 0. 2.Use it to compute x n for any integer n.

7 Copyright © Peter Cappello7 Exercise 10 Solution double x2n( double x, int n ) { assert n >= 0; double x2n = 1.0; for ( int i = 0; i < n; i++ ) x2n *= x; return x2n; } double x2z( double x, int z ) { return ( z >= 0 ) ? x2n( x, z ) : 1.0 / x2n( x, -z ); }

8 Copyright © Peter Cappello8 Program Notes Is x2n a good name? x2n, as written, ignores the possibility of underflow & overflow.

9 Copyright © Peter Cappello9 There are non-computable functions A non-constructive proof 1.The description of an algorithm is finite. 2.An algorithm is encoded as a program: a sequence of characters. 3.Each character is an 8-bit code. 4.Each algorithm is a finite bit string, b. 5.Interpret b  N. 6.The cardinality of the set of algorithms is no greater than | N |.

10 Copyright © Peter Cappello10 7. Let F = { f S | f S is the characteristic function of S  N } 8. | F | = | P( N ) |. 9. A theorem of set theory:  S | P( S ) | > | S |. 10. | F | = | P( N ) | > | N |: There are more Boolean functions over N than algorithms.

11 Copyright © Peter Cappello11 Let f S be the characteristic function for S  N. If S or S is finite, is f computable? If f is not computable, S and S are infinite. If S and S are infinite, is f not computable? Let S = set of prime numbers. Is f S computable?

12 If f S is not computable, neither S nor S have an algorithm for testing membership: Both sets lack an “effective description” that is finite. Let S be “defined” by an infinite random process (e.g., n  S, if n th coin flip is heads). Is f S a computable function? (Yes, no, or maybe) Copyright © Peter Cappello12

13 Copyright © Peter Cappello13 The Halting Problem Input: A program, P, & an input, I. Problem: Does program P halt on input I? Alan Turing showed that there is no computer program that solves this problem. Proof sketch (by contradiction): 1.Assume there exists a program, halts( P, I ): halts( P, I ) { return ( P halts on I ) ? true : false; } 2.Program kryptonite “defeats” program halts.

14 Copyright © Peter Cappello14 Program sketch of kryptonite void kryptonite ( String program ) { if ( halts ( program, program ) ) { while ( true ) {} // never halt } return; // halt } Does kryptonite( kryptonite ) return (“halt”) ?

15 Copyright © Peter Cappello 201115 End of Lecture

16 Copyright © Peter Cappello 201116 Faster algorithm for expPositive double expPositive( 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 and x = 2.0. How many times is the while loop executed in expPositive, as a function of n?

17 Copyright © Peter Cappello 201117 Recursive version of faster algorithm double expPositive( double x, int n ) { if ( n == 0 ) return 1.0; double factor = expPositive( x*x, n/2 ); return ( n % 2 == 0 ) ? factor : x * factor; } Evaluate expPositive( 2.0, 21 ). How many times is expPositive invoked, as a function of n?

18 Copyright © Peter Cappello 201118 Properties of an Algorithm: I 1.Input 2.Output 3.Definiteness Its steps are defined precisely. 4.Correctness 5.Finiteness For all inputs, it has finitely many steps 6.Effectiveness It is possible to perform each step in finite time. 7.Generality It works on all inputs of the desired form.


Download ppt "Algorithms: Selected Exercises Goals Introduce the concept & basic properties of an algorithm."

Similar presentations


Ads by Google