Universidad Nacional de ColombiaUniversidad Nacional de Colombia Facultad de IngenieríaFacultad de Ingeniería Departamento de Sistemas- 2002Departamento de Sistemas nalysis of lgorithms
lgorithms Correctness of Recursive Programs
Introduction We shall present and discuss methods for proving properties of recursive programs. We shall consider a very restricted class of recursive programs. The semantic of recursive programs is not a matter of study in regular courses of Algorithms. Here, we prove that an algorithm is correct using only the running time, but does this algorithm really do what we want it to do? How can we interpret a recursive definition and determine what does it really do?
We shall attempt to show the mathematical techniques in order to establish on a theoretical basis the recursive definitions and we shall show both the basis theory and one of the techniques to prove that an recursive algorithm is correct
All recursive definition can be expressed as: Therefore, we can think in as a “function” what takes f as a parameter, where f is a fix point of Before talking about fix points, we have to define what are the functions and structures that allow us to build a theory about fix points. Fortunately, the partial ordered sets theory and the functions that keep this order may help us with this. Monotonic Functions
We let D + denote D U { }, assuming D by convention. Partial Ordering on D + To define monotonicity, we must introduce the partial ordering on every domain D +. The partial ordering is intended to correspond to the notion is less defined than or equal to, and accordingly we let For (D + ) n =D + xD + x…xD +, we define
Example: If D={a,b}, then D + ={a, b, } and the partial ordering on D + is ; a; b; a a; and b b ; a; b; a a; and b b If D={a,b}; a alternative way to describe the partial ordering over D + x D + is ( , ) ( , b ) ( b, ) ( a, ) ( , a ) ( b, b ) (a, b ) ( b, a ) ( a, a )
Monotonic functions An n-ary function f from (D 1 + ) n into D 2 + is said to be monotonic if x y implies f(x) f(y) for all x,y (D 1 + ) n Example: 1.The identity n-ary function I mapping any x in (D 1 + ) n into itself is monotonic 2.The totally undefined function mapping any x in (D 1 + ) n into is monotonic 3.Every 0-ary function is monotonic 4.Every n-ary constant function mapping any x in (D 1 + ) n into a fixed element c of D 2 + is monotonic
Properties of monotonic functions 1.If f is a unary function mapping D 1 + into D 2 +, then f is monotonic if and only if either f( ) is or f(a) is c for all a D For n 2, if an n-ary function f mapping (D 1 + ) n into D 2 + is monotonic, then either f( ,…, ) is or f(a 1,…,a n ) is c for all (a 1,…,a n ) (D 1 + ) n Natural Extension An n-ary (n 1) function f mapping (D 1 + ) n into D 2 + is said to be naturally extended if it has the following property: f(a 1,…,a n ) is whenever at least one the a i ’s is .
Lemma (the natural extension lemma) Every naturally extended function is monotonic Examples: The quotient function mapping (x,y) R 2 into x/y R, extended to a total function by letting x/0 be for any x in R, becomes monotonic by the natural extension: Let x/ and / y be for any x and y in R +The quotient function mapping (x,y) R 2 into x/y R, extended to a total function by letting x/0 be for any x in R, becomes monotonic by the natural extension: Let x/ and / y be for any x and y in R + The is-defined function def mapping D + into {true,false} +, defined by def(d) is true for any d D, def( ) is false; is not monotonic: for we have d while def( ) def(d) (that is, false true)The is-defined function def mapping D + into {true,false} +, defined by def(d) is true for any d D, def( ) is false; is not monotonic: for we have d while def( ) def(d) (that is, false true)
Composition of monotonic functions: An important operation on functions is composition, which allows functions to be defined in terms of simpler functions. If f, g are monotonic functions (f: D 1 D 2, g: D 2 D 3 ) then g(f(x)) is monotonic. Example: The function mapping N + into N + given by f(x): if x=0 then 1 else x Is defined by composition of 0-ary functions 0 an 1, the identity function I, the weak equality predicate =, and the if-then-else function. Since all these function are monotonic, then so is f. Remark: We let ITE(par1, par2, par3) denote if par1 then par2 else par3
Distribution of monotonic functions over conditionals: Finally, we shall discuss an important corollary which follows from the properties of monotonic functions. Let us consider two functions f 1 and f 2 given by f 1 : g(ITE(p(x), h 1 (x), h 2 (x))) f 2 : ITE(p(x), g(h 1 (x)), g(h 2 (x))) Where p, g, h 1, and h 2 are monotonic functions. Both f 1 and f 2 are monotonic (Why?). There is very interesting relation between these two functions: 1.f 2 (x) f 1 (x) for any x (D 1 + ) n 2.If g( ) is , then f 2 (x) f 1 (x) for any x (D 1 + ) n
Least Upper Bound In order to introduce and discuss functionals, we must first define a few additional notions regarding monotonic functions. let [(D 1 + ) n D 2 + ] denote the set of all monotonic functions mapping (D 1 + ) n into D Let f, g [(D 1 + ) n D 2 + ]. We say that f g if f(x) g(x) for all x in (D 1 + ) n 2.Let f, g [(D 1 + ) n D 2 + ]. We say that f g if f(x) g(x) for all x in (D 1 + ) n 3.Let f 0, f 1, … be a sequence of functions in [(D 1 + ) n D 2 + ], denoted {f i }; {f i } is called a chain if f 0 f 1 f 2 … [(D 1 + ) n D 2 + ], denoted {f i }; {f i } is called a chain if f 0 f 1 f 2 … 4. Let {f i } be a sequence of functions in [(D 1 + ) n D 2 + ], and let f [(D 1 + ) n D 2 + ]. We say that f is an upper bound of {f i } if f f i for every i 0
Lemma (The lub lemma) Every chain {f i } has a least upper bound. Example: Consider the sequence of functions f 0,f 1, f 2, … in [N + N + ] defined by f i (x): ITE(x<i, x!, ) This sequence is clearly a chain because f i f i+1 for every i. (Note that f 0 is ) By the lub lemma it follows that the sequence has an lub; it is the factorial function x!
A functional over [(D 1 + ) n D 2 + ] maps the set functions [(D 1 + ) n D 2 + ] into itself, that is, takes any monotonic function f as its argument and yields a monotonic function [f] as its value. Two interesting properties of functionals are monotonicity and continuity: 1.A functional over [(D 1 + ) n D 2 + ] is said to be monotonic if f g implies [f ] [g] for all f, g [(D 1 + ) n D 2 + ] 2.A monotonic functional over [(D 1 + ) n D 2 + ] is said to be continuous is for any chain of functions {f i }, we have [lub{f i }] lub { [f i ]} Continuous Functionals
Theorem (continuous functionals): Any functional defined by composition of monotonic functions and the function variable F, is continuous Example: the functional over [N + N + ] defined by [F](x) : ITE(x=0, 1, F(x+1)) is constructed by the composition of monotonic functions (If-Then-Else, identity, addition, weak equality, and the 0-ary functions 0 an 1) and the function variable F; therefore it is continuous
Let be any monotonic functional over [(D 1 + ) n D 2 + ]. Let us consider the sequence of functions 0 [ ], 1 [ ], …, where 0 [ ] stands for and i+1 [ ] is [ i [ ]] for i 0. Each function i [ ] is in [(D 1 + ) n D 2 + ]; moreover, since [ ], and since is monotonic, { i [ ]} must be a chain; therefore, by the lub lemma, lub{ i [ ]} must exist. Theorem (first recursion theorem, Kleene) every continuous functional has a least fixpoint denoted by f . Actually f is lub{ i [ ]} Fixpoints of functionals
Example: The functional over [I + I + ] given by [F](x): ITE(x>100, x-10, F (F (x+11))) has only fix point ITE(x>100, x-10, 91) Which is therefore the least fixpoint f
In our class a program called a recursive definition, o recursive program, over D is of the form F(x) [F](x) F(x) [F](x) Where [F](x) is a functional over [(D 1 + ) n D 2 + ] expressed by composition of known monotonic functions and predicates (called base functions and predicates) and the function variable F. We agree that the function defined by a recursive program P P: F(x) [F](x) Is f (the least fix point of ) and denote it by f P. For example, the following recursive program over the natural numbers P: F(x) ITE(x=0, 1, x*F(x-1)) Defines the factorial function (that is f P =x!) Recursive Programs