3-Introduction To Algorithms, Asymptotic Notations-27!04!2023
3-Introduction To Algorithms, Asymptotic Notations-27!04!2023
Introduction to Algorithms
• What is an Algorithm
• Steps in Designing and Implementing an
Algorithm
• Important Problem Types
ALGORITHM
• A sequence of unambiguous
instructions for solving a
problem, i.e. for obtaining the
required output for any
legitimate input in a finite
amount of time
What is an algorithm?
An algorithm is a sequence of unambiguous instructions
for solving a problem, i.e., for obtaining a required
output for any legitimate input in a finite amount of
time.
problem
algorithm
Design an algorithm
while n ≠ 0 do
r ← m mod n
m← n
n←r
return m
Other methods for computing gcd(m,n)
Consecutive integer checking algorithm
Step 1 Assign the value of min{m,n} to t
Step 2 Divide m by t. If the remainder is 0, go to
Step 3;
otherwise, go to Step 4
Step 3 Divide n by t. If the remainder is 0, return
t and stop;
otherwise, go to Step 4
Step 4 Decrease t by 1 and go to Step 2
Is this slower than Euclid’s algorithm? How much
slower? O(n), if n <= m , vs O(log n)
Other methods for gcd(m,n) [cont.]
Middle-school procedure
Step 1 Find the prime factorization of m
Step 2 Find the prime factorization of n
Step 3 Find all the common prime factors
Step 4 Compute the product of all the common prime
factors
and return it as gcd(m,n)
Is this an algorithm?