DM CH 3 Algorithms
DM CH 3 Algorithms
Its Applications
Lecture 3
Dr. Metwally Rashad
2020
Course Contents
# Chapter Title
1 The Foundations: Logic and Proofs
2 Basic Structures: Sets, Functions, Sequences, Sums, and Matrices
3 Algorithms
4 Number Theory and Cryptography
5 Induction and Recursion
6 Counting
7 Discrete Probability
8 Relations
9 Graphs
10 Trees
11 Boolean Algebra
12 Modeling Computation
3.1 3.2
Concept of The Growth of
Algorithms Functions
Chapter 3
Algorithms
3.3
Complexity of
Algorithms
1/21
Concept of Algorithms
Definition 1. An algorithm is a finite sequence of precise instructions
for performing a computation or for solving a problem.
Properties of an algorithm:
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.
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
number of steps for 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 algorithm should be applicable for all problems of the desired
form not just for a particular set of input values. 2/21
Concept of Algorithms
Example Describe an algorithm for finding the largest integer in a finite
sequence of integers
Solution: We perform the following steps:
1. Set the temporary maximum equal to the first integer in the sequence
2. Compare the next integer in the sequence to the temporary maximum,
and if it is larger that the temporary maximum, set the temporary
maximum equal to this integer
3. Repeat the previous step (2) 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
3/21
Concept of Algorithms
Algorithm: Finding the maximum element in a finite
sequence
or determinelinear
Procedure that it search(x:
is not in the list.” a , a , …, a : distinct
integer, 1 2 n
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} 5/21
Concept of Algorithms: Searching Algorithms
The Binary Search Algorithm
- This algorithm can be used when the list has terms occurring in order of
increasing size.
- 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.
- Is much more efficient than the linear search algorithm.
Example: To search for 19 in the list
1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22
Split the list into 2 subsets with 8 terms each
1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22
Compare 19 with the largest element of the first set 6/21
Concept of Algorithms: Searching Algorithms
Split the second subset into 2 smaller subsets
12 13 15 16 18 19 20 22
Compare 19 with 16
9/21
Concept of Algorithms: Sorting Algorithms
The Bubble Sort Algorithm
Problem : “Order the elements of a list”.
Example: Use the bubble sort to put 3, 2, 4, 1, 5 into increasing order.
Steps of the Bubble sort
3 2 2 2 2 2 2 2 1 1
2 3 3 3 3 3 1 1 2 2
4 4 4 1 1 1 3 3 3
3
1 1 1 4 4 4 4
4 4 4
5 5 5 5 5
5 5 5 5 5
1st pass 2nd pass 3rd pass 4th pass
= ordered = interchange 10/21
Concept of Algorithms: Sorting Algorithms
Algorithm: The Bubble Sort Algorithm
11/21
Concept of Algorithms: Sorting Algorithms
The Insertion Sort Algorithm
Problem : “Order the elements of a list”.
Example: Use the insertion sort to put 3, 2, 4, 1, 5 in increasing order.
3 2 2 1 1 1
2 3 3 2 2 2
4 4 4 3 3 3
1 1 1 4 4 4 Sorted List
5 5 5 5
5 5
= ordered 12/21
Concept of Algorithms: Sorting Algorithms
Algorithm: The Insertion Sort Algorithm
Procedure insertion sort (a1, …, 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 the increasing order} 13/21
The Growth of Functions
We will study the number of operations used in the algorithm
- Estimate the number of comparisons used by the linear and binary search
algorithms to find an element in a sequence of n elements.
- Estimate the number of comparisons used by the bubble sort and by the
insertion sort to sort a list of n elements
The time required to solve a problem depends on
- The number of operations it uses (usually measured by a positive integer n).
- The hardware and software used to run the program that implements the
algorithm.
We can estimate the growth of a function using big-O notation
Using big-O notation, we can compare two algorithms to determine
which is more efficient. 14/21
Big-O Estimates
Definition 1. 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 the growth of
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).”]
Note That:
- The constants C and k in the definition of big-O notation are called witnesses to the
relationship f (x) is O(g(x)).
- To establish that f (x) is O(g(x)) we need only one pair of witnesses to this relationship
- When there is one pair of witnesses to the relationship f (x) is O(g(x)), there are
infinitely many pairs of witnesses.
15/21
Big-O Estimates
Example1: Show that f (x) = x2 + 2x + 1 is O(x2).
Solution:
-We can estimate the size of f (x) when x > 1 because x<x2 and 1 < x2 when x > 1.
It follows that
x2+ 2x + 1 ≤ x2 + 2x2 + x2 = 4x2
whenever x > 1, we can take C = 4 and k = 1 as witnesses to show that f (x) is O(x2)
Theorem 2:
Theorem 3:
17/21
Complexity of Algorithms
Time Complexity: Determine the approximate number of operations
required to solve a problem of size n.
Use the Big-O notation
Count the expensive operations only
Basic operations:
Searching algorithms - key comparisons