Module 1
Module 1
Characteristics of an algorithm
Input: Zero / more quantities are externally supplied.
Output: At least one quantity is produced.
Definiteness: Each instruction is clear and unambiguous.
Finiteness: The algorithm must terminates after a finite number of steps.
Efficiency: Every instruction must be very basic and runs in short time.
pg. 1 Anushree G
Assistant
Professor,CMRIT
Understanding the Problem:
Read the problem’s description carefully and ask questions if you have
any doubts about the problem, do a few small examples by hand, think about
special cases, and ask questions again if needed.
Also, consider the speed and amount of memory the algorithm would take
for different situations.
Proving an Algorithm’s
Correctness:
The algorithm yields a required result for every legitimate input in a finite
amount of time.A common technique for proving correctness is to use
mathematical induction because an algorithm’s iterations provide a natural
sequence of steps needed for such proofs.For an approximation algorithm, we
usually would like to show that the error produced by the algorithm does not
exceed a predefined limit.
Analysing an Algorithm:
Coding an algorithm
pg. 4 Anushree G
Assistant
Professor,CMRIT
Units for Measuring Running Time
Identify the most important operation of the algorithm, called the basic
operation, the operation contributing the most to the total running time, and
compute the number of times the basic operation is executed.
The established framework for the analysis of an algorithm’s time
efficiency suggests measuring it by counting the number of times the
algorithm’s basic operation is executed on inputs of size n.
Orders of Growth
pg. 5 Anushree G
Assistant
Professor,CMRIT
Ω-notation
Definition: A function t(n) is said to be in Ω(g(n)), denoted t(n) ∈ Ω(g(n)),
if t(n) is bounded below by some constant multiple of g(n) for all large n,
i.e., if there exist some positive constant c and some nonnegative integer
n0 such that t(n) ≥ cg(n) for all n ≥ n0
Θ-notation
Definition: A function t(n) is said to be in Θ(g(n)), denoted t(n) ∈ Θ(g(n)),
if t(n) is bounded both above and below by some 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 c2g(n) ≤ t(n) ≤ c1g(n) for all n ≥
n0.
L’Hôpital’s rule:
pg. 6 Anushree G
Assistant
Professor,CMRIT
2 Basic efficiency classes
Selection Sort
We start selection sort by scanning the entire given list to find its smallest
element and exchange it with the first element, putting the smallest
element in its final position in the sorted list. Then we scan the list,
starting with the second element, to find the smallest among the last n − 1
elements and exchange it with thesecond element, putting the second
smallest element in its final position. After n − 1 passes, the list is sorted.
Bubble Sort
Another brute-force application to the sorting problem is to compare
adjacent elements of the list and exchange them if they are out of order.
By doing it repeatedly, we end up “bubbling up” the largest element to the
last position
on the list. The next pass bubbles up the second largest element, and so
on,
until after n − 1 passes the list is sorted.
pg. 8 Anushree G
Assistant
Professor,CMRIT
The number of key comparisons:
Sequential search
ALGORITHM SequentialSearch2(A[0..n], K)
//Implements sequential search with a search key as a sentinel
//Input: An array A of n elements and a search key K
//Output: The index of the first element in A[0..n − 1] whose value is
equal to K or −1 if no such element is found
A[n] ← K
i←0
while A[i] not equal to K do
i←i+1
ifi< n return i
else return −1
Exhaustive Search
Exhaustive search is simply a brute-force approach to
combinatorial problems.
Find the shortest tour through a given set of n nn cities that visits
each city exactly once before returning to the city where it started.
pg. 10 Anushree G
Assistant
Professor,CMRIT
Generate all the subsets of the set of n items given, computing
the total weight of each subset in order to identify feasible
subsets.
Assignment Problem
pg. 11 Anushree G
Assistant
Professor,CMRIT