Algorithms Selected Exercises Goals Introduce the concept basic

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

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

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

Exercise 6 Give an algorithm with: – Input: List<Integer> i. List; – Output: int n; where n is the # of negative integers in i. List Copyright © Peter Cappello 4

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

A Haskell solution n. Negatives i. List = length [ m | m <- i. List, m < 0 ] Copyright © Peter Cappello 2011 6

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

Exercise 10 Solution double x 2 n( double x, int n ) { assert n >= 0; 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 ) ? x 2 n( x, z ) : 1. 0 / x 2 n( x, -z ); } Copyright © Peter Cappello 8

Program Notes • Is x 2 n a good name? • x 2 n, as written, ignores the possibility of underflow & overflow. Copyright © Peter Cappello 9

There are non-computable functions A non-constructive proof 1. The description of an algorithm is finite. 2. An algorithm can be encoded as a program: a sequence of characters. 3. Each character is an 8 -bit code. 4. Each algorithm can be encoded as a finite bit string, b. 5. Interpret b N. Copyright © Peter Cappello 10

6. The cardinality of the set of algorithms is no greater than | N |. 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. Copyright © Peter Cappello 11

• Let f. S be the characteristic function for S N. • If S or S is finite, is f. S computable (i. e. , has an algorithm)? • If f. S is not computable, S and S are infinite. • If S and S are infinite, is f. S not computable? • Let S = set of prime numbers. Is f. S computable? Copyright © Peter Cappello 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 nth coin flip is heads). • Is f. S a computable function? (Yes, no, or maybe) Copyright © Peter Cappello 13

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 true if and only if P halts on I } 2. Program kryptonite “defeats” program halts. Copyright © Peter Cappello 14

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

End of Lecture Copyright © Peter Cappello 2011 16

Faster algorithm for exp. Positive double exp. Positive( 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 and x = 2. 0. How many times is the while loop executed in exp. Positive, as a function of n? Copyright © Peter Cappello 2011 17

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

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. Copyright © Peter Cappello 2011 19
- Slides: 19