Dhanalakshmi College of Engineering
Dhanalakshmi College of Engineering
Dhanalakshmi College of Engineering
UNIT - I
UNIT- I
INTRODUCTION
PART – A
Algorithm
3. What is the formula used in Euclid’s algorithm for finding the Greatest Common Divisor
of two numbers? [A/M 17]
Formula used in Euclid’s algorithm for finding the Greatest Common Divisor of two numbers
a) GCD(m, n) = GCD(n, m mod n) until m mod n is equal to 0, since GCD(m, 0) = m.
b) It is based on repeatedly applying the equality.
4. What are the three different algorithms used to find the Greatest Common Divisor of
two numbers? [M/J 15]
Three algorithms used to find the Greatest Common Divisor of two numbers
a) Euclid’s algorithm
b) Consecutive integer checking algorithm
c) Middle school procedure
10. Mention some of the important problem types in algorithms. [N/D 13]
Important problem types
a) Sorting e) Combinatorial problems
b) Searching f) Geometric problems
c) String processing g) Numerical problems
d) Graph problems
Class Name
1 Constant
log n Logarithmic
N Linear
n log n Linearithmic
Class Name
2
n Quadratic
n3 Cubic
2n Exponential
n! Factorial
PART – B
1. Explain about algorithm with suitable example (Notion of algorithm).
An algorithm is a sequence of unambiguous instructions for solving a computational
problem, i.e., for obtaining a required output for any legitimate input in a finite amount of
time.
Step1: If n = 0, return the value of m as the answer and stop; otherwise, proceed to
Step 2.
Step2: Divide m by n and assign the value of the remainder to r.
Step 3: Assign the value of n to m and the value of r to n. Go to Step 1.
Algorithm Euclid(m, n)
//Computes gcd(m, n) by Euclid‘s algorithm
//Input: Two nonnegative, not-both-zero integers m and n
//Output: Greatest common divisor of m and n
while n ≠ 0 do
r m mod n
m n
n r
return m
About This algorithm
o Finiteness: how do we know that Euclid‘s algorithm actually comes to a stop?
o Definiteness: no ambiguity
o Effectiveness: effectively computable.
3. Discuss important problem types that you face during Algorithm Analysis.
Sorting
Rearrange the items of a given list in ascending order.
Input: A sequence of n numbers <a1, a2, …, an>
Output: A reordering <a´1, a´2, …, a´n> of the input sequence such that a
´1≤a´2 ≤… ≤a´n.
A specially chosen piece of information used to guide sorting. I.e., sort
student records by names.
Examples of sorting algorithms
Selection sort
Bubble sort
Insertion sort
Merge sort
Heap sort …
Evaluate sorting algorithm complexity: the number of key comparisons.
Two properties
Stability: A sorting algorithm is called stable if it preserves the
relative order of any two equal elements in its input.
In place: A sorting algorithm is in place if it does not require extra
memory, except, possibly for a few memory units.
Searching
Find a given value, called a search key, in a given set.
Examples of searching algorithms
o Sequential searching
o Binary searching…
String processing
Algorithm‘s efficiency
Three notations
Analyze of efficiency of Mathematical Analysis of Recursive Algorithms
Analyze of efficiency of Mathematical Analysis of non-Recursive Algorithms
Orders of Growth
o consider only the leading term of a formula
o Ignore the constant coefficient.
Worst-Case, Best-Case, and Average-Case Efficiency
o Algorithm efficiency depends on the input size n
o For some algorithms efficiency depends on type of input.
Example: Sequential Search
o Problem: Given a list of n elements and a search key K, find an element equal to K,
if any.
o Algorithm: Scan the list and compare its successive elements with K until either a
matching element is found (successful search) of the list is exhausted (unsuccessful
search)
Worst case Efficiency
Efficiency (# of times the basic operation will be executed) for the worst case input of size
n.
The algorithm runs the longest among all possible inputs of size n.
Best case
Efficiency (# of times the basic operation will be executed) for the best case input of size n.
The algorithm runs the fastest among all possible inputs of size n.
Average case:
Efficiency (#of times the basic operation will be executed) for a typical/random input of
size n.
NOT the average of worst and best case
A function t(n) is said to be in Θ (g(n)), if t(n) is bounded both above and below by
some positive constant multiples of g(n) for all large n, i.e., if there exist some positive
constant c1 and c2 and some nonnegative integer n0 such that c2 g(n) <= t(n) <= c1 g(n) for
all n > n0
maxval A[0]
for i 1 to n-1 do
if A[i] > maxval
maxval A[i]
return maxval
F(n) = 1 if n = 0
n * F(n-1) if n > 0
Algorithm F(n)
if n=0
return 1 //bast case
else
return F (n -1) * n //general case
Example Recursive evaluation of n ! (2)
Two Recurrences
M(n) ∈ Θ (n)
In this puzzle, there are n disks of different sizes and three pegs. Initially, all the disks are
on the first peg in order of size, the largest on the bottom and the smallest on top.
The goal is to move all the disks to the third peg, using the second one as an auxiliary, if
necessary. On1y one disk can be moved at a time, and it is forbidden to place a larger disk
on top of a smaller one.
1 3
2
The general plan to the Tower of Hanoi problem.
The number of disks n is the obvious choice for the input's size indicator, and so is moving one
disk as the algorithm's basic operation. Clearly, the number of moves M(n) depends on n only, and
we get the following recurrence equation for it:
M(n) = M(n-1)+1+M(n-1)
With the obvious initial condition M(1) = 1, the recurrence relation for the number of moves M(n)
is:
M(n) = 2M(n- 1) + 1 for n> 1, M(1) = 1.
The total number of calls made by the Tower of Hanoi algorithm: n-1
= 2n-1