Design and Analysis of Algorithm Course Code: 5009
Design and Analysis of Algorithm Course Code: 5009
while (low<=higt) {
int mid=(low+high)/2;
if (a[mid]<x)
low=mid+1;
else if (x<a[mid])
high=mid-1;
else return mid;
}
return -1
}
What is a recurrence relation?
• The portion of the definition that does not contain T is called the base case of
the recurrence relation; the portion that contains T is called the recurrent or
recursive case.
• Recurrence relations are useful for expressing the running times (i.e., the
number of basic operations executed) of recursive algorithms
Forming Recurrence Relations
• For a given recursive method, the base case and the
recursive case of its recurrence relation correspond
directly to the base case and the recursive case of the
method.
• Example 1: Write the recurrence relation for the
following method.
• The base case is reached when n == 0. The method
performs one comparison. Thus, the number of
public void f (int n) {
operations when n == 0, T(0), is some
if (n constant
> 0) { a.
• When n > 0, the method performs twoSystem.out.println(n);
basic operations
f(n-1);
and then calls itself, using ONE recursive
} call, with a
}
parameter n – 1.
• Therefore the recurrence relation is:
Forming Recurrence Relations
• Example 2: Write the recurrence relation for the
following method.
• Steps:
Expand the recurrence
Express the expansion as a summation by plugging the
recurrence back into itself until you see a pattern.
Evaluate the summation
• In evaluating the summation one or more of the
following summation formulae may be used:
• Arithmetic series: •Special Cases of Geometric Series:
Geometric Series:
Solving Recurrence Relations - Iteration
method
Harmonic Series:
Others:
Analysis Of Recursive Binary Search
public int binarySearch (int target, int[] array,
int low, int high) {
if (low > high)
return -1;
else {
int middle = (low + high)/2;
if (array[middle] == target)
return middle;
else if(array[middle] < target)
return binarySearch(target, array, middle + 1, high);
else
return binarySearch(target, array, low, middle - 1);
}
}
• The recurrence relation for the running time of the method is:
T(1) = a if n = 1 (one element array)
T(n) = T(n / 2) + b if n > 1
Recursive binary search:
int bsearch(int* a[],int low, int
high, int x) {
57034261 (0)
05734261 (2)
03574261 (2)
03457261 (2)
02345761 (4)
02345671 (1)
01234567 (6)
16
Mathematical Tools for Design and Analysis of
Algorithm
• When analyzing an algorithm, the amount of
resources required is usually expressed as a function
of the input size.
• A non trivial algorithm typically consists of repeating
a set of instructions either iteratively, e.g. by
executing a for or while loop.
17
Mathematical Tools for Design and Analysis of
Algorithm
• Or recursively by invoking the same algorithm again
and again, each time reducing the input size until it
becomes small enough, in which case the algorithm
solves the input instance using a straightforward
method.
• This implies that the amount of resources used by an
algorithm can be expressed in the form of summation
or recursive formula.
18
Mathematical Tools for Design and Analysis of
Algorithm
• This mandates the need for the basic mathematical
tools that are necessary to deal with these summations
and recursive formulas in the process of analyzing an
algorithm.
• Hence we review some of the mathematical
preliminaries and discuss briefly some of these
mathematical tools that are frequently employed in
the analysis of algorithms.
19
Sequence of Mathematical Tools
• Sets
• Sequences
• Order pairs
• Cross Product
• Relation
• Functions
• Operators over above structure
Sets, Relations and Functions
• When analyzing an algorithm, its input is considered
to be a set drawn from some particular domain, e.g.
the set of integers.
• An algorithm, in the formal sense, can be thought of
as a function, which is a constrained relation, that
maps each possible input to a specific output.
• Thus, sets and functions are at the heart of
algorithmic analysis.
21
SET
22
Representation of Sets
• Three different ways to built a set
• Descriptive Form
• Set Builder Form (Comprehension Set)
• Tabular Form
• Descriptive Form
• E.g S= Set of all Prime Numbers (not useful from
computer point of view)
• Tabular Form
• S={2,3,5,7,11,13……} ->:Little bit informative from
Computer Tool point of view But
• What about large set (Finite, Infinite) cannot be write
down.
23
Representation of Sets
• Set Builder Notation (very useful)
•
24
Partitioning of a Set
• A partition P of a Set A is a collection of { A1, A2, A3, ….An }
such that following are satisfied e.g.
1. Disk partitioning
2. Task partitioning on different machines
Rules :
26
Function : Partial Functions
DEFINITION
27
Functions
DEFINITION
It is sometime necessary to record the order in which the objects are arranged. e.g.
Definition:
A group of elements in a specified order is called a sequence. A sequence can have a repeated
elements. i.e. multiset
31
Sequence
Negation
Conjunction
• The conjunction p q is true only if p and q both are
true otherwise false
• The conjunction follows the commutative property,
i.e. p q = q p
Disjunction
• The disjunction p q is false if both p and q are false
otherwise true
• The disjunction follows the commutative property as
well, i.e. p q = q p
Implication
p q r (p q r) (p (q r))
t t t t t t t tt
t t f t f t f ft
t f t f t t t tt
t f f f t t t tt
f t t f t t t tt
f t f f t t t ft
f f t f t t t tt
f f f f t t t tt
De Morgan’s Laws
1. (p q) = p q
p q pq (p q) p q p q
t t t f f f f
t f f t f t t
f t f t t f t
f f f t t t t
De Morgan’s Laws
2. (p q) = p q
p q pq (p q) p q p q
t t t f f f f
t f t f f t f
f t t f t f f
f f f t t t t
Proof using Counter Example, Contraposition
Counter Example
Is used when we have prove general statement is false.
To prove x (A(x) B(x)) is false, we show some
object x for which A(x) is true and B(x) is false.
Proof
( x (A(x) B(x)))
x, (A(x) B(x)))
x, (A(x) B(x))
x, A(x) B(x))
50
Recursive Call
• function Fibrec(n)
• If n < 2, the algorithm simply returns n, which takes some time constant
time a
• Most of the work is spent in the two recursive calls which take time T(n
- 1) and T(n - 2)
• One addition involving fn-1 and fn-2 (values returned by the recursive
calls)
51
Recursive Fibonacci (cont)
52
53
54
Supplementary (Example)
55
Example Selection Sort
Original Array
6354927
1st pass -> 2 3 5 4 9 6 7 (2 and 6 were swapped)
2nd pass -> 2 3 4 5 9 6 7 (4 and 5 were swapped)
3rd pass -> 2 3 4 5 6 9 7 (6 and 9 were swapped)
4th pass -> 2 3 4 5 6 7 9 (7 and 9 were swapped)
5th pass -> 2 3 4 5 6 7 9 (no swap)
6th pass -> 2 3 4 5 6 7 9 (no swap)
56
57
58