Unit 2faculty.pictinc.org/LectureNotes/Unit 2.pdf · Title: Microsoft PowerPoint - Unit 2 Author:...
Transcript of Unit 2faculty.pictinc.org/LectureNotes/Unit 2.pdf · Title: Microsoft PowerPoint - Unit 2 Author:...
Models and Design
INTRODUCTION
• Declarative Model: Functional Model
what is to be achieved rather than how to achieve it
• Imperative Model : Structured Programming model
imperative == command
how to do something, rather than what is to be done
FUNCTIONAL MODEL
• Very close to mathematics
• Easy to analyze in terms of their correctness and efficiency
• Functional algorithm can serve as a specification for the development of algorithms in other models of computation
• Every problem is viewed as an evaluation of a function
• The solution to a given problem is specified by a complete and unambiguous functional description
• A good model of computation
1. Primitive expressions which represent the simplest objects with which the model is concerned.
2. Methods of combination which specify how the primitive expressions can be combined with one another to obtain compound expressions.
3. Methods of abstraction which specify how the compound objects can be named and manipulated as units.
Features of Functional Model
1. The Primitive Expressions
2. Definition of one function in terms of another (substitution)
3. Definition of functions using conditionals
4. Inductive definition of functions
The Primitive Expressions
• The basic primitives of the functional model are constants, variables, and functions
• Elements of the sets , , are the constants. Also, the elements of the set B = {true, false} are constants.
• There can be other kind of constants as well.
• Variables are identifiers which refer to data objects (constants).• E.g. the declarations x = 5 and y = true bind the variables x and y to the
values 5 and true, respectively.
Availability of Primitive functions of the type
f: × →
and
f: × →
• E.g. addi on (+), subtrac on (−), and mul plica on (*)
The functional mapping symbolism like f : × → is also called the signature of that function.
• If a and b and a = q * b + r for some integers q and 0 ≤ r < b then div(a, b) = q and mod(a, b) = r.
• The division function / : × → will be assumed to be valid only for real numbers.
• In addition to these, we will assume the relational functions =,≤,<,≥,>, and ≠ which are of the type
f : × → B
or
f : × → B depending on the context.
Substitution of Functions
• Definition of one function in terms of another
• Evaluate such functions through substitution.
Example 1
Finding the square of a natural number.
We can directly specify the function square, which is of the type
square : →
As, square(n) = n * n
Where, * : × → as
Example 1
Finding the square of a natural number.
We can directly specify the function square, which is of the type
square : →
As, square(n) = n * n
Where, * : × → as
To find square(5), we have to thus evaluate 5 * 5
Example 2: Finding the sum of two squares
• More complex functions from simpler ones
sumsquares : × → as follows:
sum_squares(x, y) = square(x) + square(y)
The function sum_squares is thus defined in terms of the functions + and square.
Example 3: Using local variables
• Suppose:
f(x, y) = x(1 + xy)2 + y(1 − y) + (1 + xy)(1 − y)
We can express, a = 1 + xy,
b = 1 − y,
Then f(x, y) = xa2 + yb + ab.
avoids multiple computations
Definition of Functions using Conditionals
• Example: Finding the larger of two numbers
max : × →
The function accepts a pair of natural numbers as its input, and gives a single natural number as its output
Definition of Functions using Conditionals
• Example: Finding the larger of two numbers
max : × →
The function accepts a pair of natural numbers as its input, and gives a single natural number as its output
Assumed that two natural numbers can be compared using the ≥ function
Definition of Functions using Conditionals
The basic primitive used in this case is if-then-else. Thus if a ≥ b, the function returns a as the output, else it returns b.
Note that for every pair of natural numbers as its input, max returns a unique number as the output and hence it adheres to the definition of a function.
Example: Finding the absolute value of x
• abs : → as
Inductive Definition of Functions
• Till now ,
Functions which can be evaluated by substitutions or evaluation of conditions
Inductively defined functional algorithm
Example: Computing the GCD of two numbers
gcd : × → as,
Function because:for every pair of positive integers as input, it gives a positive integer as the output. It is also a finite computational process: Unambiguously tells us how to compute the solution The process terminates after a finite number of steps.
Example: Computing the GCD of two numbers
gcd : × → as,
For example, gcd(18, 12),
gcd (18, 12) = gcd (12, 6) = gcd (6, 6) = 6.
Not all mathematically valid specifications of functions
Mathematically a perfectly valid
How to evaluate the function
Another Example
f: →
f(n) = 0 for n = 0 and
f(n) = f(n + 1) – 1 for all n
Unique solution However try to evaluate f(1)
Infinite computational process
rewrite
g(0) = 0
g(1) = g(0) + 1 = 1
g(2) = g(1) + 1 = g(0) + 1 + 1 = 2
⋮
Yet another…
f(n) = f(n)
• Every function is a solution to this trivial definition, but it is computationally undefined.
A specification of a function is an algorithm ??
only if defines a precise computational procedure to evaluate it.
1. It is directly specified in terms of a pre-defined function which is either primitive or there exists an algorithm to compute the function.
2. It is specified in terms of the evaluation of a condition.
3. It is inductively defined and the validity of its description can be established through the principle of Mathematical Induction.
4. It is obtained through a finite number of combinations of the steps (1), (2), and (3) using substitutions.
Complex functions can be algorithmically defined in terms of two main types of processes
1. Recursive
2. Iterative.
Recursive Processes
• chain of deferred operations
Example
Factorial Computation: Given n ≥ 0, compute the factorial of n(n!).
factorial : → as
A computation such as this is characterized by a growing (recursive descent) and a shrinking (recursive ascent) process.
factorial(5) = (5 × factorial(4)) = (5 × (4 × factorial(3))) = (5 × (4 × (3 × factorial(2))))
= (5 × (4 × (3 × (2 × factorial(1))))) = (5 × (4 × (3 × (2 × (1 × factorial(0))))))
= (5 × (4 × (3 × (2 × (1 × 1))))) = (5 × (4 × (3 × (2 × 1)))) = (5 × (4 × (3 × 2)))
= (5 × (4 × 6)) = (5 × 24) = 120
Analysis of Correctness and Efficiency
• Correctness : Principle of Mathematical Induction (PMI)
• Proof By PMI on n.
• Basis When n = 0, factorial(n) = 1 = 0! by definitions of factorial and 0!.
• Induction hypothesis For k = n − 1 , k ≥ 0, we have factorial(k) = k!.
• Induction step Consider factorial(n).factorial(n) = n × factorial(n − 1)
= n × (n ∕ 1)! by the induction hypothesis
= n! by the definition of n!
Efficiency :
In order to compute factorial(n), the n integers will have to be remembered somewhere (or stacked up)
This leads to a space requirement of about n.
Let T (n) be the number of multiplications required for a problem of size n
T(0) is obviously 0T(n) = T(n − 1) + 1
= T(n − 2) + 2
= T(0) + n= n
More Examples of Recursive Algorithms
• Computing xn. Given an integer x > 0, compute xn, where n ≥ 0
• power : × → .
More Examples of Recursive Algorithms
1. To count the number of primes between integers a and b (both inclusive).
2. Computing
3. Determining whether a positive integer is a perfect number.
A positive integer is called a perfect number if the sum of its proper divisors add up to the number itself.
To count the number of primes between integers a and b (both inclusive).
Correctness To show that the function count_primes(a, b) returns the count of the number of primes between a and b, assuming the function prime(n) is correct.
Proof By PMI—strong version, on (b − a + 1).
Basis If a > b, the interval is empty and count_primes(a, b) returns 0.
Induction hypothesis count_primes(a, b − 1) returns the count of the number of primes between a and b − 1 for a, b such that (b − a + 1) ≥ 0.
Induction step If b is a prime then count_primes(a, b) returns count_primes(a, b − 1) + 1. Otherwise, it returns count_primes(a, b − 1).
Computing
• We will assume that the function f(n) is available. We can then define the function sum : × → , inductively, as:
Determining whether a positive integer is a perfect number.
We define a function perfect: → {true, false} for determining whether a number is perfect or not as follows:
perfect(n) = n = addfactors(n)
add factors: → :
addfactors(n) = sum(1 , n div 2)
Note that the n used in the definition of f(i) is the same as in the function perfect.
Determining whether a positive integer is a perfect number.
We define a function perfect: → {true, false} for determining whether a number is perfect or not as follows:
perfect(n) = n = addfactors(n)
add factors: → :
addfactors(n) = sum(1 , n div 2)
Note that the n used in the definition of f(i) is the same as in the function perfect.
Top-down design and step-wise refinement
Scope Rules
a and b are natural numbers and f is a function on natural numbers.
But what they denote???
Hence the names a, b, and f arecalled free in the expression
Scope Rules
However n is said to be bound in the sense that the expression makes it clear that nranges over the interval [a, b]
the scope of n is limited to the summation expression and we say that n is local to the summation function.
factorial(5) = (5 × factorial(4)) = (5 × (4 × factorial(3))) = (5 × (4 × (3 × factorial(2))))
= (5 × (4 × (3 × (2 × factorial(1))))) = (5 × (4 ×(3 × (2 × (1 × factorial(0))))))
= (5 × (4 × (3 × (2 × (1 × 1))))) = (5 × (4 × (3 ×(2 × 1)))) = (5 × (4 × (3 × 2)))
= (5 × (4 × 6)) = (5 × 24) = 120
factorial(5) = (5 × factorial(4)) = (5 × (4 × factorial(3))) = (5 × (4 × (3 × factorial(2))))
= (5 × (4 × (3 × (2 × factorial(1))))) = (5 × (4 ×(3 × (2 × (1 × factorial(0))))))
= (5 × (4 × (3 × (2 × (1 × 1))))) = (5 × (4 × (3 ×(2 × 1)))) = (5 × (4 × (3 × 2)))
= (5 × (4 × 6)) = (5 × 24) = 120
Space??
Tail-Recursion
factorial(5) = fact_iter(5, 1, 0) = fact_iter(5, 1, 1) = fact_iter(5, 2, 2) = fact_iter(5, 6, 3)
= fact_iter(5, 24, 4)
= fact_iter(5, 120, 5)
= 120
Tail-Recursion
factorial(5) = fact_iter(5, 1, 0) = fact_iter(5, 1, 1) = fact_iter(5, 2, 2) = fact_iter(5, 6, 3)
= fact_iter(5, 24, 4)
= fact_iter(5, 120, 5)
= 120Space??
Fibonacci numbers: Computation of the nth Fibonacci number, n ≥ 1.
• fib : →
Computation mechanism of iterative processes reveals that final state is obtained from a starting state
3
3
Space??Time??
• Computation speed:• Start with the two smallest Fibonacci numbers• Retain the values of the last two Fibonacci numbers as the state• Use a counter count, to represent the stages of the computation.
• An invariant condition for this process for n ≥ 3 is
(n ≥ 3) Λ (3 ≤ count ≤ n) Λ (a = fib(count − 2)) Λ (b = fib(count − 1))
n−2 additions to compute fib(n) for n ≥ 3
O(n) time and O(1) space for computing the nth Fibonacci number.
The Primitives for the Imperative Model
• State of a computation as a collection of instantaneous values of certain quantities.
• A state change occurs if at least one of the quantities comprising the state is changed.
• The imperative model of computation uses instructions or commands to make the desired state changes.
Imperative model constructs
• Variables and the assignment instruction
• Assertions
• The if-then-else instruction
• The while-do instruction
• Functions and procedures in the imperative model
Step-Wise Refinement
• Determining whether a given positive integer n is a prime • 1 is not a prime. • Since all even numbers are divisible by 2, the only even prime is 2 itself. • Hence a prime other than 2 is necessarily an odd number. • If n is an odd number, we can determine whether n is a prime by testing the
divisibility of n by test divisors chosen successively from the sequence 3, 5, 7, 9 ….
prime(n) = (n = 2) V (odd(n) Λ (smallest_divisor(n) = n))
• The function odd: → {true, false} can be defined as
odd(n) = ((n mod 2) = 1)
• smallest_divisor(n) = find_iter(n, 3)
Proof Rule
• Proof rules are generally expressed as:
H1, H2, … Hn / H
if H1, H2, … are all true then H is also true.
For example, {P} S {R}, R Q / {P} S {Q}
proof rules for simple statements
{P} S {R}, R Q / {P} S {Q} (I)
For example, from {(x > 0) (y > 0)} S {(z + u * y = x * y) (u = 0)} and (z + u * y = x * y) (u = 0) (z = x * y), we conclude that
{(x > 0) (y > 0)} S {z = x * y}
P R, {R} S {Q} / {P} S {Q} (II)
Consequence Rules.