Algorithm_Recursion
Algorithm_Recursion
Discrete structures
COE 253
P.A Kwabi
KNUST
1 / 60
Algorithm Recursion
Outline
1 Algorithm
2 Recursion
2 / 60
Algorithm Recursion
Algorithm
3 / 60
Algorithm Recursion
Cont’d
4 / 60
Algorithm Recursion
Cont’d
5 / 60
Algorithm Recursion
Cont’d
6 / 60
Algorithm Recursion
Cont’d
PROPERTIES OF ALGORITHMS.
There are several properties that algorithms generally share. They
are useful to keep in mind when algorithms are described. These
properties are:
Cont’d
Searching Algorithms
9 / 60
Algorithm Recursion
Cont’d
10 / 60
Algorithm Recursion
Cont’d
Cont’d
12 / 60
Algorithm Recursion
Cont’d
13 / 60
Algorithm Recursion
Cont’d
14 / 60
Algorithm Recursion
Cont’d
15 / 60
Algorithm Recursion
Cont’d
16 / 60
Algorithm Recursion
Cont’d
else j := m
if x = ai then location := i
else location := 0
return location{location is the subscript i of the term ai equal to
x, or 0 if x is not found}
17 / 60
Algorithm Recursion
Sorting
18 / 60
Algorithm Recursion
Cont’d
Bubble Sort
A simple algorithm that repeatedly steps through the list,
compares adjacent items, and swaps them if they are in the
wrong order.
Insertion Sort
This algorithm builds the sorted list one element at a time by
comparing and inserting each new element into its correct
position in the already sorted part of the list.
19 / 60
Algorithm Recursion
20 / 60
Algorithm Recursion
Cont"d
Cont’d
22 / 60
Algorithm Recursion
Cont’d
23 / 60
Algorithm Recursion
String Matching
24 / 60
Algorithm Recursion
Cont’d
For instance, we can ask whether the pattern CAG occurs in the
text CATCACAGAGA. The answer is yes, because it occurs with a
shift of five characters. Solving questions about the genome
requires the use of efficient algorithms for string matching,
especially because a string representing a human genome is about
3 × 109 characters long.
We will now describe a brute force algorithm, Algorithm 5, for
string matching, called the naive string matcher.
25 / 60
Algorithm Recursion
Cont’d
26 / 60
Algorithm Recursion
27 / 60
Algorithm Recursion
Cont’d
28 / 60
Algorithm Recursion
Cont’d
Definition:
Let f and g be functions from the set of integers or the set of real
numbers to the set of real numbers. We say that f (x ) is O(g(x)) if
there are constants C and k such that
|f (x )| ≤ C |g(x )|
29 / 60
Algorithm Recursion
Cont’d
30 / 60
Algorithm Recursion
Cont’d
31 / 60
Algorithm Recursion
Recursion
32 / 60
Algorithm Recursion
Figure: 1
33 / 60
Algorithm Recursion
34 / 60
Algorithm Recursion
Cont’d
35 / 60
Algorithm Recursion
Cont’d
36 / 60
Algorithm Recursion
Cont’d
Examples:
1 Suppose that f is defined recursively by:
f (0) = 3, and f (n + 1) = 2f (n) + 3.
Find f (1), f (2), f (3), and f (4) Solution:
f (1) = 2f (0) + 3 = 2 . 3 + 3 = 9
f (2) = 2f (1) + 3 = 2 . 9 + 3 = 21
f (3) = 2f (2) + 3 = 2 . 21 + 3 = 45
f (4) = 2f (3) + 3 = 2 . 45 + 3 = 93.
37 / 60
Algorithm Recursion
Cont’d
38 / 60
Algorithm Recursion
Cont’d
n
Giving a recursive definition of
P
3 ak
k=0
0
The first part of the recursive definition is
P
a0
k=0
n+1 n
The second part is ak = ( ak ) + an+1
P P
k=0 k=0
39 / 60
Algorithm Recursion
Cont’d
LAME’S THEOREM
Let a and b be positive integers with a ≥ b. Then the number of
divisions used by the Euclidean algorithm to find gcd(a, b) is less
than or equal to five times the number of decimal digits in b
40 / 60
Algorithm Recursion
41 / 60
Algorithm Recursion
Cont’d
42 / 60
Algorithm Recursion
Cont’d
43 / 60
Algorithm Recursion
Cont’d
Definition:
The set * of strings over the alphabet is defined recursively by
P P
no symbols).
RECURSIVE STEP: If w ∈ * and x ∈ , then wx ∈ *
P P P
44 / 60
Algorithm Recursion
Cont’d
Example:
If = {0, 1}, the strings found to be in *, the set of all bit
P P
formed during the first application of the recursive step, 00, 01, 10,
and 11 formed during the second application of the recursive step,
and so on.
45 / 60
Algorithm Recursion
Cont’d
Definition:
Two strings can be combined via the operation of concatenation.
Let be a set of symbols and * the set of strings formed from
P P
46 / 60
Algorithm Recursion
Cont’d
then
w1 . (w2 x ) = (w1 . w2 )x.
47 / 60
Algorithm Recursion
Cont’d
48 / 60
Algorithm Recursion
Recursive Algorithms
49 / 60
Algorithm Recursion
Cont’d
50 / 60
Algorithm Recursion
Cont’d
51 / 60
Algorithm Recursion
Cont’d
52 / 60
Algorithm Recursion
Cont’d
53 / 60
Algorithm Recursion
Cont’d
54 / 60
Algorithm Recursion
55 / 60
Algorithm Recursion
Cont’d
56 / 60
Algorithm Recursion
Cont’d
z := x + y
x := y
y := z
return y
{output is the nth Fibonacci number}
57 / 60
Algorithm Recursion
58 / 60
Algorithm Recursion
Cont’d
Example: Use the merge sort to put the terms of the list 8, 2, 4, 6,
9, 7, 10, 1, 5, 3 in increasing order.
Solution: A merge sort begins by splitting the list into individual
elements by successively splitting lists in two. Sorting is done by
successively merging pairs of lists. At the first stage, pairs of
individual elements are merged into lists of length two in increasing
order. Then successive merges of pairs of lists are performed until
the entire list is put into increasing order.
59 / 60
Algorithm Recursion
Cont’d
60 / 60