Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Algorithm_Recursion

The document provides an overview of algorithms and recursion, emphasizing their definitions, properties, and examples. It covers various algorithms for searching, sorting, and string matching, along with the concept of function growth using Big-O notation. Additionally, it introduces recursion as a method for defining functions and sequences in terms of themselves.

Uploaded by

sammyyawsnana1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Algorithm_Recursion

The document provides an overview of algorithms and recursion, emphasizing their definitions, properties, and examples. It covers various algorithms for searching, sorting, and string matching, along with the concept of function growth using Big-O notation. Additionally, it introduces recursion as a method for defining functions and sequences in terms of themselves.

Uploaded by

sammyyawsnana1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

Algorithm Recursion

Discrete structures
COE 253

P.A Kwabi

KNUST

December 12, 2024

1 / 60
Algorithm Recursion

Outline

1 Algorithm

2 Recursion

2 / 60
Algorithm Recursion

Algorithm

There are many general classes of problems that arise in discrete


mathematics. For instance: given a sequence of integers, find the
largest one; find the shortest path between two vertices. When
presented with such a problem, the first thing to do is to construct
a model that translates the problem into a mathematical context.
To complete the solution, a method is needed that will solve the
general problem using the model.

3 / 60
Algorithm Recursion

Cont’d

Ideally, what is required is a procedure that follows a sequence of


steps that leads to the desired answer. Such a sequence of steps is
called an algorithm.

Definition: An algorithm is a finite sequence of precise


instructions for performing a computation or for solving a problem.

4 / 60
Algorithm Recursion

Cont’d

Example: Describe an algorithm for finding the maximum


(largest) value in a finite sequence of integers.
STEPS:
1 Set the temporary maximum equal to the first integer in the
sequence. (The temporary maximum will be the largest
integer examined at any stage of the procedure.)
2 Compare the next integer in the sequence to the temporary
maximum, and if it is larger than the temporary maximum,
set the temporary maximum equal to this integer.

5 / 60
Algorithm Recursion

Cont’d

3 Repeat the previous step if there are more integers in the


sequence
4 Stop when there are no integers left in the sequence. The
temporary maximum at this point is the largest integer in the
sequence.

An algorithm can also be described using a computer language.


However, when that is done, only those instructions permitted in
the language can be used.

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:

Input: An algorithm has input values from a specified set.

Output: From each set of input values an algorithm produces


output values from a specified set. The output values are the
solution to the problem.

Definiteness: The steps of an algorithm must be defined


precisely.
7 / 60
Algorithm Recursion

Cont’d

Correctness: An algorithm should produce the correct


output values for each set of input values.

Finiteness: An algorithm should produce the desired output


after a finite (but perhaps large) number of steps for any
input in the set.

Effectiveness: It must be possible to perform each step of an


algorithm exactly and in a finite amount of time.

Generality: The procedure should be applicable for all


problems of the desired form, not just for a particular set of
input values.
8 / 60
Algorithm Recursion

Searching Algorithms

The problem of locating an element in an ordered list occurs in


many contexts. For instance, a program that checks the spelling of
words searches for them in a dictionary, which is just an ordered list
of words. Problems of this kind are called searching problems.

9 / 60
Algorithm Recursion

Cont’d

The general searching problem can be described as follows: Locate


an element x in a list of distinct elements a1 , a2 ,. . . , an , or
determine that it is not in the list. The solution to this search
problem is the location of the term in the list that equals x (that
is, i is the solution if x = ai ) and is 0 if x is not in the list.

10 / 60
Algorithm Recursion

Cont’d

ALGORITHM 1: The Linear Search Algorithm.


procedure linear search(x: integer, a1 , a2 , . . . , an : distinct
integers)
i := 1
while (i ≤ n and x ̸= ai )
i := i + 1
if i ≤ n then location := i
else location := 0
return location{location is the subscript of the term that equals x,
or is 0 if x is not found}
11 / 60
Algorithm Recursion

Cont’d

THE LINEAR SEARCH


The first algorithm that we will present is called the linear search,
or sequential search, algorithm.
The linear search algorithm begins by comparing x and a1 . When
x = a1 , the solution is the location of a1, namely, 1. When x ̸= a1 ,
compare x with a2 . If x = a2 , the solution is the location of a2 ,
namely, 2. When x ̸= a2 , compare x with a3 .

12 / 60
Algorithm Recursion

Cont’d

Continue this process, comparing x successively with each term of


the list until a match is found, where the solution is the location of
that term, unless no match occurs. If the entire list has been
searched without locating x, the solution is 0.

13 / 60
Algorithm Recursion

Cont’d

THE BINARY SEARCH


We will now consider another searching algorithm. This algorithm
can be used when the list has terms occurring in order of
increasing size (for instance: if the terms are numbers, they are
listed from smallest to largest; if they are words, they are listed in
lexicographic, or alphabetic, order). This second searching
algorithm is called the binary search algorithm.

14 / 60
Algorithm Recursion

Cont’d

It proceeds by comparing the element to be located to the middle


term of the list. The list is then split into two smaller sublists of
the same size, or where one of these smaller lists has one fewer
term than the other. The search continues by restricting the search
to the appropriate sublist based on the comparison of the element
to be located and the middle term

15 / 60
Algorithm Recursion

Cont’d

ALGORITHM 2: The Binary Search Algorithm.


procedure binary search (x: integer, a1 , a2 , . . . , an : increasing
integers)
i := 1 {i is left endpoint of search interval}
j := n {j is right endpoint of search interval}
while i < j
m := [(i + j)/2]
if x > am then i := m + 1

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

Sorting refers to arranging the elements of a list in a specified


order, typically in ascending or descending order. This operation is
fundamental in computing and serves numerous practical
applications like alphabetizing data, organizing directories, and
enabling efficient searching.

18 / 60
Algorithm Recursion

Cont’d

Classification of Sorting Algorithms

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

ALGORITHM 3: The Bubble Sort.


procedure bubblesort(a1 , . . . , an : real numbers with n ≥ 2)
for i := 1 to n - 1
for j := 1 to n - i
if aj > aj+1 then interchange aj and aj+1
{a1 , . . . , an is in increasing order}

20 / 60
Algorithm Recursion

Cont"d

ALGORITHM 4: The Insertion Sort.


procedure insertion sort(a1 , a2 , . . . , an : real numbers with n ≥ 2)
for j := 2 to n
i := 1
while aj > ai
i := i + 1
m := aj for k := 0 to j − i − 1
aj−k := aj−k − 1
ai := m
{a1 , . . . , an is in increasing order}
21 / 60
Algorithm Recursion

Cont’d

Example: Using the insertion sort to put the elements of the


list 3, 2, 4, 1, 5 in increasing order
The insertion sort first compares 2 and 3. Because 3 > 2, it places
2 in the first position, producing the list 2, 3, 4, 1, 5 (the sorted
part of the list is shown in color). At this point, 2 and 3 are in the
correct order. Next, it inserts the third element, 4, into the already
sorted part of the list by making the comparisons 4 > 2 and 4 > 3.
Because 4 > 3, 4 remains in the third position.

22 / 60
Algorithm Recursion

Cont’d

At this point, the list is 2, 3, 4, 1, 5 and we know that the ordering


of the first three elements is correct. Next, we find the correct
place for the fourth element, 1, among the already sorted elements,
2, 3, 4. Because 1 < 2, we obtain the list 1, 2, 3, 4, 5. Finally, we
insert 5 into the correct position by successively comparing it to 1,
2, 3, and 4. Because 5 > 4, it stays at the end of the list,
producing the correct order for the entire list.

23 / 60
Algorithm Recursion

String Matching

Finding where a pattern occurs in a text string is called string


matching. String matching plays an essential role in a wide variety
of applications, including text editing, spam filters, systems that
look for attacks in a computer network, search engines, plagiarism
detection, bioinformatics, and many other important applications.

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

ALGORITHM 5 Naive String Matcher.


procedure string match (n, m: positive integers, m ≤ n,
t1 , t2 , . . . , tn , p1 , p2 , . . . , pm : characters)
for s := 0 to n - m
j := 1
while ( j ≤ m and ts+j = pj )
j := j + 1
if j > m then print “s is a valid shift”

26 / 60
Algorithm Recursion

The Growth of Function

The time required to solve a problem depends on more than only


the number of operations it uses. The time also depends on the
hardware and software used to run the program that implements
the algorithm. However, when we change the hardware and
software used to implement an algorithm, we can closely
approximate the time required to solve a problem of size n by
multiplying the previous time required by a constant.

27 / 60
Algorithm Recursion

Cont’d

Meanwhile, Big-O notation can be used to estimate the growth


of a function without worrying about constant multipliers or
smaller order terms. This means that, using big-O notation, we do
not have to worry about the hardware and software used to
implement an algorithm. Furthermore, using big-O notation, we
can assume that the different operations used in an algorithm take
the same time, which simplifies the analysis considerably.

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 )|

whenever x > k. [This is read as “f (x ) is big-oh of g(x ).”]

29 / 60
Algorithm Recursion

Cont’d

Example: Show that 7x 2 is O(x 3 )


Solution:
Note that when x > 7, we have 7x 2 < x 3 . (We can obtain this
inequality by multiplying both sides of x > 7byx 2 ).
Consequently, we can take C = 1 and k = 7 as witnesses to
establish the relationship 7x 2 is O(x 3 ).
Alternatively, when x > 1, we have 7x 2 < 7x 3 , so that C = 7 and
k = 1 are also witnesses to the relationship 7x 2 is O(x 3 ).

30 / 60
Algorithm Recursion

Cont’d

Polynomials can often be used to estimate the growth of functions.


Instead of analyzing the growth of polynomials each time they
occur.
Theorem: Let f (x ) = an x n + an−1 x n−1 + · · · + a1 x + a0 , where
a0 , a1 , . . . , an−1 , an are real numbers. Then f (x ) is O(x n ).

31 / 60
Algorithm Recursion

Recursion

Sometimes it is difficult to define an object explicitly. However, it


may be easy to define this object in terms of itself. This process is
called recursion. We can use recursion to define sequences,
functions, and sets. For instance, the picture shown in Figure 1 is
produced recursively. First, an original picture is given. Then a
process of successively superimposing centered smaller pictures on
top of the previous pictures is carried out.

32 / 60
Algorithm Recursion

Figure: 1

33 / 60
Algorithm Recursion

Recursively Defined Functions

A function is said to be defined recursively or to be a recursive


function if its rule of definition refers to itself. Because of this
self-reference, it is sometimes difficult to tell whether a given
recursive function is well defined. Recursive functions are of great
importance in the theory of computation in computer science.

34 / 60
Algorithm Recursion

Cont’d

We use two steps to define a function with the set of nonnegative


integers as its domain:
BASIS STEP: Specify the value of the function at zero.
RECURSIVE STEP: Give a rule for finding its value at an integer
from its values at smaller integers.

35 / 60
Algorithm Recursion

Cont’d

Such a definition is called a recursive or inductive definition.


Note that a function f (n) from the set of nonnegative integers to
the set of a real numbers is the same as a sequence a0 , a1 , . . . ,
where ai is a real number for every nonnegative integer i.

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.

NB:Recursively defined functions are well defined.

37 / 60
Algorithm Recursion

Cont’d

2 Give a recursive definition of an , where a is a nonzero real


number and n is a nonnegative integer.
Solution:
The recursive definition contains two parts. First a0 is
specified, namely, a0 = 1. Then the rule for finding an+1 from
an , namely, an+1 = a.an , for n = 0, 1, 2, 3, . . . , is given.
These two equations uniquely define an for all nonnegative
integers n.

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

Recursively Defined Sets and Structures

Just as in the recursive definition of functions, recursive definitions


of sets have two parts, a basis step and a recursive step. In the
basis step, an initial collection of elements is specified. In the
recursive step, rules for forming new elements in the set from those
already known to be in the set are provided.

41 / 60
Algorithm Recursion

Cont’d

Recursive definitions may also include an exclusion rule, which


specifies that a recursively defined set contains nothing other than
those elements specified in the basis step or generated by
applications of the recursive step

42 / 60
Algorithm Recursion

Cont’d

Example: Consider the subset S of the set of integers recursively


defined by;
BASIS STEP: 3 ∈ S.
RECURSIVE STEP: If x ∈ S and y ∈ S, then x + y ∈ S.
The new elements found to be in S are 3 by the basis step,
3 + 3 = 6 at the first application of the recursive step, 3 + 6 = 6
+ 3 = 9 and 6 + 6 = 12 at the second application of the recursive
step, and so on.

43 / 60
Algorithm Recursion

Cont’d

Definition:
The set * of strings over the alphabet is defined recursively by
P P

BASIS STEP: λ ∈ * (where λ is the empty string containing


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

strings, are λ, specified to be in * in the basis step, 0 and 1


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

symbols in . We can define the concatenation of two strings,


P

denoted by, recursively as follows.

46 / 60
Algorithm Recursion

Cont’d

BASIS STEP: If w ∈ *, then w . λ = w,


P

where λ is the empty string.


RECURSIVE STEP: If w1 ∈ and w2 ∈ and x ∈ ,
P P P

then
w1 . (w2 x ) = (w1 . w2 )x.

47 / 60
Algorithm Recursion

Cont’d

The concatenation of the strings w1 and w2 is often written as


w1w2 rather than w1 . w2 . By repeated application of the
recursive definition, it follows that the concatenation of two strings
w1 and w2 consists of the symbols in w1 followed by the symbols in
w2 . For instance, the concatenation of w1 = abra and w2 =
cadabra is w1 w2 = abracadabra.

48 / 60
Algorithm Recursion

Recursive Algorithms

Definition: An algorithm is called recursive if it solves a problem


by reducing it to an instance of the same problem with smaller
input.
Examples:

Giving a recursive algorithm for computing an , where a is a


nonzero real number and n is a nonnegative integer.

49 / 60
Algorithm Recursion

Cont’d

We can base a recursive algorithm on the recursive definition of an .


This definition states that an+1 = a . an for n > 0 and the initial
condition a0 = 1. To find an , successively use the recursive step to
reduce the exponent until it becomes zero.

50 / 60
Algorithm Recursion

Cont’d

ALGORITHM 1: A Recursive Algorithm for Computing n!.


procedure factorial(n: nonnegative integer)
if n = 0 then return 1
else return n . factorial(n - 1)
{output is n! }

51 / 60
Algorithm Recursion

Cont’d

ALGORITHM 2: A Recursive Algorithm for Computing an .


procedure power(a: nonzero real number, n: nonnegative integer)
if n = 0 then return 1
else return a . power(a, n - 1)
{output is an }

52 / 60
Algorithm Recursion

Cont’d

ALGORITHM 3: A Recursive Algorithm for Computing gcd(a, b)


b gcd(a, b: nonnegative integers with a < b)
if a = 0 then return b
else return gcd(b mod a, a)
{output is gcd(a, b) }

53 / 60
Algorithm Recursion

Cont’d

ALGORITHM 4: A Recursive Linear Search Algorithm.


procedure search(i, j, x: integers, 1 ≤ i ≤ j ≤ n) if ai = x then
return i
else if i = j then
return 0
else
return search(i + 1, j, x)
{output is the location of x in a1 , a2, . . . , an if it appears; otherwise
it is 0}

54 / 60
Algorithm Recursion

Recursion and Iteration

A recursive definition expresses the value of a function at a positive


integer in terms of the values of the function at smaller integers.
This means that we can devise a recursive algorithm to evaluate a
recursively defined function at a positive integer.

55 / 60
Algorithm Recursion

Cont’d

ALGORITHM 5: An Iterative Algorithm for Computing


Fibonacci Numbers.
procedure iterative fibonacci(n: nonnegative integer)
if n = 0 then return 0
else
x := 0
y := 1
for i := 1 to n 0 - 1

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

The Merge Sort

We now describe a recursive sorting algorithm called the merge


sort algorithm.
The Merge Sort algorithm is a divide-and-conquer algorithm that
sorts an array by first breaking it down into smaller arrays, and
then building the array back together the correct way so that it is
sorted.

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

ALGORITHM 9 A Recursive Merge Sort.


procedure mergesort(L = a1 , . . . , an )
if n > 1 then
m := [n/2]
L1 := a1 , a2 , . . . , am
L2 := am+1 , am+2 , . . . , an
L := merge(mergesort(L1 ), mergesort(L2 ))
{L is now sorted into elements in nondecreasing order}

60 / 60

You might also like