190 likes | 289 Views
A. nalysis of. A. lgorithms. Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas- 2002. A. nalysis of. A. lgorithms. Correctness of Recursive Programs. Introduction.
E N D
A nalysis of A lgorithms • Universidad Nacional de Colombia • Facultad de Ingeniería • Departamento de Sistemas- 2002
A nalysis of A 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
Monotonic Functions 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.
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
(a, a) (b, a) (a, b) (b, b) (, a) (a, ) (b, ) (, b) (, ) Example: If D={a,b}, then D+={a, b, } and the partial ordering on D+is ; a; b; a a; and b b IfD={a,b}; a alternative way to describe the partial ordering over D+x D+ is
Monotonic functions • An n-ary function ffrom (D1+)n into D2+ is said to be monotonic if x y implies f(x) f(y) for all x,y(D1+)n • Example: • The identity n-ary function I mapping any x in (D1+)ninto itself is monotonic • The totally undefined function mapping any x in (D1+)n into is monotonic • Every 0-ary function is monotonic • Every n-ary constant function mapping any x in (D1+)n into a fixed element c of D2+ is monotonic
Properties of monotonic functions • If f is a unary function mapping D1+ into D2+, then f is monotonic if and only if either f() is or f(a) is c for all a D1+ • 2. For n 2, if an n-ary function f mapping (D1+)n into D2+ is monotonic, then either f(,…, ) is or f(a1,…,an) is c forall (a1,…,an) (D1+)n • Natural Extension • An n-ary (n 1) function f mapping (D1+)n into D2+ is said to be naturally extended if it has the following property: f(a1,…,an) is whenever at least one the ai’s is .
• Lemma (the natural extension lemma) • Every naturally extended function is monotonic • Examples: • The quotient function mapping (x,y) R2 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, falsetrue)
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: D1 D2 , g: D2 D3) 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 f1 and f2given by • f1: g(ITE(p(x), h1(x), h2(x))) • f2: ITE(p(x), g(h1(x)), g(h2(x))) • Where p, g, h1, and h2aremonotonicfunctions. Both f1and f2are monotonic (Why?).There is veryinteresting relation between these two functions: • f2(x) f1(x) forany x(D1+)n • If g() is, then f2(x) f1(x) forany x(D1+)n
Least Upper Bound • In order to introduce and discuss functionals, we must first define a few additional notions regarding monotonic functions. let [(D1+)n D2+] denote the set of all monotonic functions mapping (D1+)n into D2+. • Let f,g [(D1+)n D2+]. We say that f g if f(x) g(x) for all x in (D1+)n • Let f,g [(D1+)n D2+]. We say that f g if f(x) g(x) for all x in (D1+)n • Let f0 ,f1 , … be a sequence of functions in • [(D1+)n D2+], denoted {fi }; {fi } is called a chain if f0 f1f2 … • 4. Let {fi } be a sequence of functions in [(D1+)n D2+], and let f [(D1+)n D2+]. We say that f is an upper bound of {fi } if f fifor every i 0
Lemma (The lub lemma) Every chain {fi } has a least upper bound. Example: Consider the sequence of functions f0,f1 , f2, … in [N+N+] defined by fi (x): ITE(x<i, x!, ) This sequence is clearly a chain because fi fi+1 for every i. (Note that f0 is) By the lub lemma it follows that the sequence has an lub; it is the factorial function x!
Continuous Functionals • A functional over [(D1+)n D2+] maps the set functions [(D1+)n D2+] 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: • A functional over [(D1+)n D2+] is said to be monotonic if f g implies [f ] [g] for all f, g [(D1+)n D2+] • A monotonic functional over [(D1+)n D2+] is said to be continuous is for any chain of functions {fi }, we have [lub{fi}] lub {[fi]}
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
Fixpoints of functionals Let be any monotonic functional over [(D1+)n D2+]. 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 [(D1+)n D2+]; 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 . Actuallyfis lub{i[]}
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
Recursive Programs In our class a program called a recursive definition, o recursive program, over D is of the form F(x) [F](x) Where [F](x) is a functional over [(D1+)n D2+] 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 fP. 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 fP=x!)