Subject Name: Design and Analysis of Algorithms Subject Code: 10CS43 Prepared By: Sindhuja K Department: CSE Date
Subject Name: Design and Analysis of Algorithms Subject Code: 10CS43 Prepared By: Sindhuja K Department: CSE Date
Subject Name: Design and Analysis of Algorithms Subject Code: 10CS43 Prepared By: Sindhuja K Department: CSE Date
01/21/22
OBJECTIVE
• This course aims in introducing recurrence relations, and illustrates their
role in asymptotic and probabilistic analysis of algorithms. It covers in detail
greedy strategies, divide and conquer techniques, dynamic programming and
max flow - min cut theory for designing algorithms, and illustrates them
using a number of well-known problems and applications.
• It also covers popular graph and matching algorithms, and basics of
randomized algorithms and computational complexity.
• Basic knowledge of computational complexity, approximation and
randomized algorithms.
• Teaches how to design efficient algorithms which leads to efficient
programs.
01/21/22
LEARNING OUTCOMES
• Upon completion of this course, students will be able to do
the following:
• Get a solid foundation in algorithm design and analysis.
• Analyze the asymptotic performance of algorithms.
• Write correctness proofs for algorithms.
• Demonstrate a familiarity with major algorithms and data
structures.
• Apply important algorithmic design paradigms and methods of
analysis.
01/21/22
• Text Books:
1. Anany Levitin: Introduction to The Design & Analysis of Algorithms,
2nd Edition, Pearson Education, 2007.
(Listed topics only from the Chapters 1, 2, 3, 5, 7, 8, 10, 11).
2. Ellis Horowitz, Sartaj Sahni, Sanguthevar Rajasekaran: Fundamentals
of Computer Algorithms, 2nd Edition, Universities Press, 2007.
(Listed topics only from the Chapters 3, 4, 5, 13)
• Reference Books:
1. Thomas H. Cormen, Charles E. Leiserson, Ronal L. Rivest, Clifford
Stein: Introduction to Algorithms, 3rd Edition, PHI, 2010.
2.Tomorrow
Engineered for R.C.T. Lee, S.S. Tseng, R.C. Chang & Y.T.Tsai: Introduction to the
Design and Analysis of Algorithms A Strategic Approach, Tata McGraw
Hill, 2005.
01/21/22
COURSE TOPICS
UNIT 1 – INTRODUCTION
UNIT 2 - DIVIDE AND CONQUER
UNIT 3 - THE GREEDY METHOD
UNIT 4 - DYNAMIC PROGRAMMING
UNIT 5 - DECREASE-AND-CONQUER APPROACHES, SPACE-TIME
TRADEOFFS
UNIT 6 - LIMITATIONS OF ALGORITHMIC POWER AND COPING
WITH THEM
UNIT 7 - COPING WITH LIMITATIONS OF ALGORITHMIC POWER
UNIT 8 - PRAM ALGORITHMS
01/21/22
Unit –I
INTRODUCTION
01/21/22
Unit 1- TOPICS
• Notion of Algorithm
• Review of Asymptotic Notations
• Mathematical Analysis of Non-Recursive and Recursive Algorithms
• Brute Force Approaches: Introduction
• Selection Sort and Bubble Sort
• Sequential Search and Brute Force String Matching
01/21/22
ALGORITHM
• An algorithm is an exact specification of how to solve a computational
problem
• An algorithm must specify every step completely, so a computer can
implement it without any further “understanding”
• An algorithm must work for all possible inputs of the problem.
• Algorithms must be:
• Correct: For each input produce an appropriate output
• Efficient: run as quickly as possible, and use as little memory as possible
– more about this later
• There can be many different algorithms for each computational problem.
Engineered for Tomorrow
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
NOTION OF ALGORITHM
Euclid’s algorithm
Step 1 If n = 0, return m and stop; otherwise go to Step 2
Step 2 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.
while n ≠ 0 do
r ← m mod n
m← n
n←r
return m
Engineered for Tomorrow
Problem: Find gcd(m,n), the greatest common divisor of two non-negative, not
both zero integers m and n
Sieve of Eratosthenes
Input: Integer n ≥ 2
Output: List of primes less than or equal to n
for p ← 2 to n do A[p] ← p
for p ← 2 to n do
if A[p] != 0 //p hasn’t been previously eliminated from the list
j ← p* p
while j ≤ n do
A[j] ← 0 //mark element as eliminated
j←j+p
Example: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
2 3 5 7 9 11 13 15 17 19
2 3 5 7 11 13 17 19
2 3 5 7 11 13 17 19
Engineered for Tomorrow
HOW DO WE COMPARE
ALGORITHMS?
We need to define a number of objective measures.
(1) Compare execution times?
Not good: times are specific to a particular computer !!
IDEAL SOLUTION
•
Express running time as a function of the input size n (i.e., f(n)).
•
Compare different functions corresponding to running times.
•
Such an analysis is independent of machine time, programming style, etc.
Engineered for Tomorrow
Example
•
Associate a "cost" with each statement.
•
Find the "total cost“ by finding the total number of times each statement is executed.
Algorithm 1 Cost Algorithm 2 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
Engineered for Tomorrow
Another Example
•
Algorithm 3 Cost
sum = 0; c1
sum += arr[i][j]; c3
------------
c1 + c2 x (N+1) + c2 x N x (N+1) + c3 x N2
18
Fundamentals of Algorithmic Problem solving
Understand the problem
Design an algorithm
Prove correctness
ANALYSIS OF ALGORITHMS
•
How good is the algorithm?
•
Correctness
•
Time efficiency
•
Space efficiency
ASYMPTOTIC ANALYSIS
•
To compare two algorithms with running times f(n) and g(n), we need a
rough measure that characterizes how fast each function grows.
•
Hint: use rate of growth
•
Compare functions in the limit, that is, asymptotically!
(i.e., for large values of n)
Engineered for Tomorrow
ASYMPTOTIC NOTATION
•
O notation: asymptotic “less than”:
f(n)=O(g(n)) implies: f(n) “≤” g(n)
•
Ω notation: asymptotic “greater than”:
f(n)= Ω (g(n)) implies: f(n) “≥” g(n)
•
θ notation: asymptotic “equality”:
f(n)= θ (g(n)) implies: f(n) “=” g(n)
22
Engineered for Tomorrow
ASYMPTOTIC NOTATIONS
O-notation
12/8/13
Engineered for Tomorrow
Examples
f(n)=3n2+n
f(n)<=c*g(n)
3n2+n<=4n2=o(n2)
12/8/13
Engineered for Tomorrow
Examples
f(n)=3n2+n
= 3n2+n
=3n2
f(n)>=c*g(n)
3n2+n>=3n2= Ω(n2)
Examples
C2g(n)<=f(n)<=c1g(n) for all n>=n0
3n2+n<=f(n)<=3n2+n2
3n2<=f(n)<=4n2
Where c2=3, c1=4 and n=1
Therefore, 3n2+n ∈ θ(n2)
Establishing order of growth using limits
0 order of growth of T(n) < order of growth of g(n)
Examples:
• 10n vs. n2
• n(n+1)/2 vs. n2
L’Hôpital’s rule and Stirling’s formula
L’Hôpital’s rule: If limn f(n) = limn g(n) = and
the derivatives f´, g´ exist, then
Stirling’s formula: n! (2n)1/2 (n/e)n
lim f(n) lim f ´(n)
=
n g(n) n g ´(n)
Example: log n vs. n
Example: 2n vs. n!
Orders of growth of some important
functions
• All logarithmic functions loga n belong to the same
class
(log n) no matter what the logarithm’s base a > 1 is
• All polynomials of the same degree k belong to the
same class: aknk + ak-1nk-1 + … + a0 (nk)
log n logarithmic
n linear
n log n n-log-n
n2 quadratic
n3 cubic
2n exponential
n! factorial
MATHEMATICAL ANALYSIS OF NO
RECURSIVE ALGORITHMS
General Plan for Analysis
C(n) є Θ(n)
Example 2: Element uniqueness problem
C(n) є Θ(n2)
Example 3: Matrix multiplication
C(n) є Θ(n3)
MATHEMATICAL ANALYSIS OF
RECURSIVE ALGORITHMS
• Decide on a parameter indicating an input’s size.
• Check whether the number of times the basic op. is executed may vary on
different inputs of the same size. (If it may, the worst, average, and best cases
must be investigated separately.)
• Solve the recurrence (or, at the very least, establish its solution’s order of
growth) by backward substitutions or another method.
Example 1: Recursive evaluation of n!
Definition: n ! = 1 2 … (n-1) n for n ≥ 1 and 0! = 1
Size:
Basic operation:
Recurrence relation:
Engineered for Tomorrow
Recursion
•
To see how the recursion works, let’s break down the
factorial function to solve factorial(3)
Engineered for Tomorrow
Breakdown
•
Here, we see that we start at the top level, factorial(3), and simplify the
problem into 3 x factorial(2).
•
Now, we have a slightly less complicated problem in factorial(2), and we
simplify this problem into 2 x factorial(1).
Engineered for Tomorrow
Breakdown
•
We continue this process until we are able to reach a
problem that has a known solution.
•
In this case, that known solution is factorial(0) = 1.
•
The functions then return in reverse order to complete the
solution.
Analysis of Factorial
• Recurrence Relation
M(n) = M(n-1) + 1
M(0) = 0
01/21/22
Example 2: Counting #bits
Analysis of Counting # of bits
• Recursive relation including initial conditions
A(n) = A(floor(n/2)) + 1
IC A(1) = 0
01/21/22
Example 3: Tower of Hanoi
A B C A B C
Hanoi(n, A, B, C);
(* Move n disks from A to C via B *)
begin
if (n=1) then “Move top disk from A to C”
else (* when n>1 *)
Hanoi (n-1, A, C, B);
“Move top disk from A to C”
Hanoi (n-1, B, C, A);
endif
end;
Analysis of TOH
Recursive relation for moving n discs
M(n) = M(n-1) + 1 + M(n-1) = 2M(n-1) + 1
IC: M(1) = 1
Solve using backward substitution
M(n) = 2M(n-1) + 1
= 2[2M(n-2) + 1] +1 = 22M(n-2) + 2+1
=22[2M(n-3) +1] + 2+1 = 23M(n-3) + 22 + 2 + 1
..
M(n) = 2iM(n-i) + ∑j=0-i2j = 2iM(n-i) + 2i-1
...
M(n) = 2n-1M(n-(n-1)) + 2n-1-1 = 2n-1M(1) + 2n-1-1 = 2n-1 + 2n-1-1 = 2n-1
M(n) ε Θ(2n)
01/21/22
Engineered for Tomorrow
Examples:
1. Computing an (a > 0, n a nonnegative integer)
2. Computing n!
17 | 45 68 90 29 34 89
17 29 | 68 90 45 34 89
17 29 34 45 | 90 68 89
17 29 34 45 68 | 90 89
C(n) є Θ(n2)
17 29 34 45 68 89 | 90
# of key swaps є Θ(n)
54
BUBBLE SORT
• Compare adjacent elements and exchange them if out of
order
• Essentially, it bubbles up the largest element to the last
position
?
A0, … …, Aj <-> Aj+1, … …, An-i-1 | An-i ≤ … ≤ An-1
55
ALGORITHM BubbleSort(A[0..n-1])
for i <- 0 to n-2 do
for j <- 0 to n-2-i do
if A[j+1] < A[j]
swap A[j] and A[j+1]
56
SEQUENTIAL SEARCH
ALGORITHM SequentialSearch(A[0..n-1], K)
//Output: index of the first element in A, whose //value is equal to K or -1
if no such element is found
i <- 0
while i < n and A[i] ≠ K do
i <- i+1
if i < n
return i Input size: n
else Basic op: <, ≠
return -1
Cworst(n) = n
57
BRUTE-FORCE STRING MATCHING
• pattern: a string of m characters to search for
• text: a (longer) string of n characters to search in
• problem: find a substring in the text that matches the pattern
Brute-force algorithm
Step 1 Align pattern at beginning of text
Step 2 Moving from left to right, compare each character of
pattern to the corresponding character in text until
• all characters are found to match (successful search); or
• a mismatch is detected
Step 3 While pattern is not found and the text is not yet
exhausted, realign pattern one position to the right and
repeat Step 2
Pseudocode and Efficiency