Design and Analysis of Algorithms Guide
Design and Analysis of Algorithms Guide
Algorithm(DAA)
BCS - 503
More Information
Textbook
Introduction to Algorithms ,Cormen, Leiserson, Rivest, Printice
Hall of India.
Others
Fundamentals of Computer Algorithms 2nd Edition, Sartaj
Sahni, .
Algorithms, Richard Johnsonbaugh, Marcus Schaefer, Prentice
Hall, 2004.
Introduction to The Design and Analysis of Algorithms 2nd
Edition, Anany Levitin, Adison-Wesley, 2007.
Unit-1
Introduction: Algorithms, Analyzing Algorithms, Complexity of Algorithms,
Growth of Functions, Performance Measurements, Sorting and Order Statistics - Shell
Sort, Quick Sort, Merge Sort, Heap Sort, Comparison of Sorting Algorithms, Sorting in
Linear Time.
Unit-2
Advanced Data Structures: Red-Black Trees, B – Trees, Binomial Heaps, Fibonacci
Heaps, Tries, Skip List
Unit-3
Divide and Conquer with Examples Such as Sorting, Matrix Multiplication, Convex
Hull and Searching. Greedy Methods with Examples Such as Optimal Reliability
Allocation, Knapsack, Minimum Spanning Trees – Prim’s and Kruskal’s Algorithms,
Single Source Shortest Paths - Dijkstra’s and Bellman Ford Algorithms
Unit-4
Dynamic Programming with Examples Such as Knapsack. All Pair Shortest Paths –
Warshal’s and Floyd’s Algorithms, Resource Allocation Problem. Backtracking,
Branch and Bound with Examples Such as Travelling Salesman Problem, Graph
Coloring, n-Queen Problem, Hamiltonian Cycles and Sum of Subsets.
Unit-5
Selected Topics: Algebraic Computation, Fast Fourier Transform, String Matching,
Theory of NP Completeness, Approximation Algorithms and Randomized
Algorithms
-> is any well-defined computational procedure that takes
some value, or set of values, as input and produces some
value, or set of values, as output.
a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
4 1 7 0 5 2 9 3 6 8
i 10
9
78
56
234
10
Which algorithm is better?
The algorithms are correct, but which is the best?
Measure the running time (number of operations
needed).
Measure the amount of memory used.
Note that the running time of the algorithms increase
as the size of the input increases.
What do we need?
Correctness: Whether the algorithm computes
the correct solution for all instances
13
Algorithm : Linear search
Given a list, find a specific element in the list
List does NOT have to be sorted!
14
Algorithm : Linear search, take 1
procedure linear_search (x: integer; a1, a2, …, an: integers)
i := 1
while ( i ≤ n and x ≠ ai )
i := i + 1 x 3
if i ≤ n then location := i
location 8
else location := 0
a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
4 1 7 0 5 2 9 3 6 8
i 1
78
56
234
15
Algorithm : Linear search, take 2
procedure linear_search (x: integer; a1, a2, …, an: integers)
i := 1
while ( i ≤ n and x ≠ ai )
i := i + 1 x 11
if i ≤ n then location := i
location 0
else location := 0
a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
4 1 7 0 5 2 9 3 6 8
i 90
78
56
1234
11
16
Linear search running time
How long does this take?
17
Algorithm : Binary search
Given a list, find a specific element in the list
List MUST be sorted!
Each time it iterates through, it cuts the list in half
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
begin
m := (i+j)/2 { m is the point in the middle }
if x > am then i := m+1
else j := m
end
if x = ai then location := i
else location := 0
i 1
76 m 5
78
6 j 10
78
19
Algorithm : Binary search, take 2
procedure binary_search (x: integer; a1, a2, …, an: increasing integers)
i := 1 while i < j if x = ai then location := Ii
j := n begin else location := 0
m := (i+j)/2
if x > am then i := m+1 x 15
else j := m
end location 0
a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
2 4 6 8 10 12 14 16 18 20
i 1
6
8 m 5
78 j 10
8
20
Binary search running time
How long does this take (worst case)?
21
Algorithm Analysis
Measures the efficiency of an algorithm or its
implementation as a program as the input size
becomes very large
We evaluate a new algorithm by comparing its
performance with that of previous approaches
Comparisons are asymtotic analyses of classes of
algorithms
We usually analyze the time required for an
algorithm and the space required for a
datastructure
22
Algorithm Analysis
Many criteria affect the running time of an
algorithm, including
speed of CPU, bus and peripheral hardware
design think time, programming time and
debugging time
language used and coding efficiency of the
programmer
quality of input (good, bad or average)
23
Algorithm Analysis
For a given input size n we express the time T
to run the algorithm as a function T(n).
24
ALGORITHM
REPRESENTATION
Flowcharts for three constructs
Pseudo code for three constructs
Example 1
Value of function
function
eventually
becomes
larger...
fB(n)=n2+1
Increasing n
The Growth of Functions
“Popular” functions g(n) are
n log n, 1, 2n, n2, n!, n, n3, log n
Expression Name
--------------------------------------------
1 constant
log n logarithmic
log2n log squared
n linear
n log n n log n
n2 quadratic
n3 cubic
2n exponential
--------------------------------------------
Common time complexities
BETTER O(1) constant time
O(log n) log time
O(n) linear time
O(n log n) log linear time
O(n2) quadratic time
O(n3) cubic time
O(2n) exponential time
WORSE
Basic Efficiency Classes
Class Name Comments
1 constant May be in best cases
lgn logarithmic Halving problem size at
each iteration
n linear Scan a list of size n
n×lgn linearithmic Divide and conquer
algorithms, e.g., mergesort
50
Complexity of Algorithm
Complexity of an algorithm is a measure of the amount of
time and/or space required by an algorithm for an input of
a given size (n).
For example: In bubble sort, when the input array is already sorted, the
time taken by the algorithm is linear i.e. the best case.
But, when the input array is in reverse condition, the algorithm takes
the maximum time (quadratic) to sort the elements i.e. the worst case.
When the input array is neither sorted nor in reverse order, then it takes
average time. These durations are denoted using asymptotic notations.
53
Asymptotic Notation
54
Big O-notation
Big-O notation represents the upper bound of the running
time of an algorithm. Thus, it gives the worst case
complexity of an algorithm.
56
F(n)=3n+2
F(n)=O(g(n))
F(n)<=cg(n)
3n+2<=3n+n n>=2
3n+2<=4n
C=4
g(n)=n
n0=2
F(n)=O(n)
Points about the definition
Note that f is O(g) as long as any values of c
and k exist that satisfy the definition.
But: The particular c, k, values that make the
statement true are not unique: Any larger
value of c and/or k will also work.
You are not required to find the smallest c and
k values that work.
58
Omega Notation(W -notation)
Omega notation represents the lower bound of the running
time of an algorithm. Thus, it provides best case complexity of
an algorithm.
6*2n+n2= W(n100)
6*2n+n2= W(n50.2)
6*2n+n2= W(n2)
6*2n+n2= W(1)
60
Theta Notation(-notation)
Theta notation encloses the function from above and below.
Since it represents the upper and the lower bound of the
running time of an algorithm, it is used for analyzing the
average case complexity of an algorithm.
Θ(g(n)) = { f(n): there exist positive constants c1, c2 and
n0 such that 0 ≤ c1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ n0 }
. The above expression can be described as a
function f(n) belongs to the set Θ(g(n)) if
there exist positive constants c1 and c2 such
that it can be sandwiched
between c1g(n) and c2g(n), for sufficiently
large n.
. If a function f(n) lies anywhere in
between c1g(n) and c2 > g(n) for all n ≥ n0,
then f(n) is said to be asymptotically tight
bound. 61
Intuition for Asymptotic Notation
Big-Oh
f(n) is O(g(n)) if f(n) is asymptotically less than
or equal to g(n)
Big-Omega
f(n) is W(g(n)) if f(n) is asymptotically greater
than or equal to g(n)
Big-Theta
f(n) is (g(n)) if f(n) is asymptotically equal to
g(n)
62
No Uniqueness
There is no unique set of values for n0 and c in proving the
asymptotic bounds
Prove that 100n + 5 = O(n2)
100n + 5 ≤ 100n + n = 101n ≤ 101n2
for all n ≥ 5
n0 = 5 and c = 101 is a solution
100n + 5 ≤ 100n + 5n = 105n ≤ 105n2
for all n ≥ 1
n0 = 1 and c = 105 is also a solution
Must find SOME constants c and n0 that satisfy the asymptotic notation relation
63
Relations Between , W, O
Theorem : For any two functions g(n) and f(n),
f(n) = (g(n)) iff
f(n) = O(g(n)) and f(n) = W(g(n)).
64
FINDING THE COMPLEXITY OF
SMALL ITERATIVE ALGORITHMS
Example 01– Linear Search
INPUT: a sequence of n numbers, key to search for.
OUTPUT: true if key occurs in the sequence, false otherwise.
Linear Search(A, key) cost times
1
n
1 i1
1
1 i 2
66
Example 02
To determine the maximum number in an Array.
67
Example 03
Algorithm Averages(X, n)
Input array X of n integers
Output array A of prefix averages of X #operations
s0 1
for i 0 to n 1 do n+1
s s X[i] n
A[i] s (i 1) n
return A 1
68
Example-04
Alg.: MIN (a[1], …, a[n])
m ← a[1];
for i ← 2 to n
if a[i] < m
then m ← a[i];
Running time:
the number of primitive operations (steps) executed
before termination
T(n) =1 [first step] + (n) [for loop] + (n-1) [if condition] +
(n-1) [the assignment in then] = 3n - 1
Order (rate) of growth:
The leading term of the formula
Expresses the asymptotic behavior of the algorithm
69
Example 06
Associate a "cost" with each statement and find
the "total cost“ by finding the total number of
times each statement is executed.
Express running time in terms of the size of the
problem.
Algorithm 1 Algorithm 2
Cost Cost
arr[0] = 0; c1 for(i=0; i<N; i++) c2
arr[1] = 0; c1 arr[i] = 0; c1
arr[2] = 0; c1
...
arr[N-1] = 0; c1
----------- -------------
c1+c1+...+c1 = c1 x N (N+1) x c2 + N x c1 =
(c2 + c1) x N + c2
70
Example 07
Cost
sum = 0; c1
for(i=0; i<N; i++) c2
for(j=0; j<N; j++) c2
sum += arr[i][j]; c3
------------
c1 + c2 x (N+1) + c2 x N x (N+1) + c3 x N x N
71
Example 08
Algorithm 1 Algorithm 2
Cost Cost
arr[0] = 0; c1 for(i=0; i<N; i++) c2
arr[1] = 0; c1 arr[i] = 0; c1
arr[2] = 0; c1
...
arr[N-1] = 0; c1
----------- -------------
c1+c1+...+c1 = c1 x N (N+1) x c2 + N x c1 =
(c2 + c1) x N + c2
O(n)
72
Example 09
Cost
sum = 0; c1
for(i=0; i<N; i++) c2
for(j=0; j<N; j++) c2
sum += arr[i][j]; c3
------------
c1 + c2 x (N+1) + c2 x N x (N+1) + c3 x N x N
O(n2)
73
Example 10
int FindMaxElement(int[] array)
{
int max = array[0];
for (int i=0; i<[Link]; i++)
{
if (array[i] > max)
{
max = array[i];
}
}
return max;
}
75
Example 12
decimal Sum3(int n)
{
decimal sum = 0;
for (int a=0; a<n; a++)
for (int b=0; b<n; b++)
for (int c=0; c<n; c++)
sum += a*b*c;
return sum;
}
76
Example 13
long SumMN(int n, int m)
{
long sum = 0;
for (int x=0; x<n; x++)
for (int y=0; y<m; y++)
sum += x*y;
return sum;
}
77
Example 17
78
LOGARITHMIC COMPLEXITY
An algorithm is O(log n) if it takes a constant time to cut the problem size by a
fraction (usually by 1/2). As an example let us consider the following program
for(i=1;i<=n;)
i=i*2;
If we observe carefully , the value of i is doubling every time. Initially i=1 , in the
next step i=2 and in subsequent steps i=4,8 and so on. Let us assume that loop is
executed “k” times. At kth step 2k=n and we come out of loop. Taking logarithmic
on both sides , gives
log(2k)=log(n) k log 2=log n k=log2n
Total time=O(log2n)=O(lg n). Similarly for the below case also , worst case rate of
growth is O(lg n).The same discussion holds for decreasing sequence as well .
for(i=n;i>=1;)
i=i/2;
Above loop is used in BINARY SEARCH(Finding a word in a dictionary of n pages).
79
LOGARITHMIC COMPLEXITY
An algorithm is O(log n) if it takes a constant time to cut the problem
size by a fraction (in below given case it is 1/3). As an example let us
consider the following program
for(i=n;i>=1;i=i/3)
print ”HAPPY FRIENDSHIP DAY ”;
If we observe carefully , the value of i is reduced by 1/3 every time.
Initially i=n , in the next step i=n/3 and in subsequent steps
i=n/9,n/27 and so on. Let us assume that loop is executed “k” times.
At kth step n/3k=1 i.e, n=3k and we come out of loop. Taking
logarithmic base 2 on both sides , gives
log2n=klog23
k=log2n/log23=log3n
Total time=O(log3n)
80
LOGARITHMIC COMPLEXITY
DAA(n)
{ Cost Number of Times
1. while(n>1) C1 log2n+1
{
2. nn/2 C2 log2n
3. print n C3 log2n
}
}
Total cost =T(n)=C1[log2n+1] + C2[log2n] + C3[log2n]
=(C1+C2+C3) log2n +C1
=Logarithmic in nature.
81
LOGARITHMIC COMPLEXITY
82
LOGARITHMIC COMPLEXITY
83
LOGARITHMIC COMPLEXITY
.What is the complexity of the program
void function (int n){
int i,j,k,count=0;
for(i=n/2;i<=n;i++)
for(j=1;j+n/2<=n;j=j++)
for(k=1;k<=n;k=k*2)
count++;
84
LOGARITHMIC COMPLEXITY
85
Family of Polynomials
Constant function
f(n)=1
Linear function
F(n)=n
Quadratic function
f(n)=n2
Cubic function
f(n)=n3
A general polynomials
f(n)=a0+a1n+a2n2+a3n3+…+adnd
86
The Logarithm Function
f(n)=log2(n)=log(n)
The default base is 2.
Definition of logarithm
Some identities
More… 1 1
y ln x y ' dx ln x
x x
The base is
e=2.718281828…. 87
The Exponential Function
f(n)=an
Some identities (for positive a, b, and c)
a(b+c) = aba c
abc = (ab)c
ab /ac = a(b-c)
b = a logab
bc = a c*logab
88
log n!
Recall that 1! = 1 and n! = (n-1)! n.
89
log n!
On the other hand,
log n! = log 1 + log 2 + … + log n
>= log ((n+1)/2) + … + log n
>= ((n+1)/2) log ((n+1)/2)
>= n/2 log(n/2)
= W(n log n)
For the last step, note that
lim infn-> (n/2 log(n/2))/(n log n) = ½.
90
Monotonicity
f(n) is
monotonically increasing if m n f(m) f(n).
monotonically decreasing if m n f(m) f(n).
strictly increasing if m < n f(m) < f(n).
strictly decreasing if m > n f(m) > f(n).
91
Important Summation Formulas
92
Exponentials
Useful Identities:
1 1
a
a
(a m ) n a mn
a m a n a m n
nb
lim n 0
n a
n b o( a n )
93
Logarithms
x = logba is the a b logb a
exponent for a = bx.
log c (ab) log c a log c b
Natural log: ln a = logea log b a n log b a
n