DAA Unit1
DAA Unit1
DAA Unit1
Algorithms
(CS6402)
preferred.
8/20/2019 DAA - Unit - I Presentation Slides 3
Objectives
To gain experiences in fundamental
techniques used for algorithm analysis and
the main methodologies used for the
design of efficient algorithms.
To study the most important computer
algorithms of current practical use.
problem
algorithm
S. Algorithm Program
No
1 Algorithm is finite Program need not to be
finite
2 Algorithm is written Programs are written
using natural using a specific
language or programming
algorithmic language language
Proving correctness
• Empirical analysis
Optimality
Step 2: Comment
Each step may contain comment brackets, which identifies or
indicates the main purpose of the step.
• Example :
Step 1: [Initialize]
set K : = 1
8/20/2019 DAA - Unit - I Presentation Slides 22
STEPS FOR WRITING AN ALGORITHM (Contd…)
Step 3 : Variable Names
It uses capital letters. Example MAX, DATA. Single letter
names of variables used as counters or subscripts.
searching
string processing
graph problems
combinatorial problems
geometric problems
numerical problems
while n ≠ 0 do
r ← m mod n
m← n
n←r
return m
8/20/2019 DAA - Unit - I Presentation Slides 39
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
Is this slower t by 1How
than Euclid’s algorithm? and go to Step 2
much
slower?
Language independent
Realistic
Example:
i=1
Loop (i <=1000)
Application code
i=i+2
f(n)=n.
8/20/2019 DAA - Unit - I Presentation Slides 51
LOGARITHMIC LOOPS
Multiply Loop Divide Loop
i=1 i=1
f(n) = log2n.
Loop (i < 1000) Loop (i < 1000)
Application code Application code
i=i*2 i=i / 2
Multiply 2iterations < 1000 ; Divide 1000 / 2iterations > = 1
Multiply Divide
Iteration Value of I Iteration Value of I
1 1 1 1000
2 2 2 500
3 4 3 250
4 8 4 125
5 16 5 62
6 32 6 31
7 64 7 15
8 128 8 7
9 256 9 3
10 512 10 1
8/20/2019 (exit) DAA 1024 (exit)
- Unit - I Presentation Slides 0 52
NESTED LOOPS
When we analyze loops, we must determine how many iterations each
loop completes. The total is then the product of the number of iterations
for the inner loop and the number of iterations in the outer loop.
Iterations = outer loop iterations x inner loop iterations
i=1
Loop (i < = 10)
j=1
Loop (j<=10)
Application code
j=j*2
i=i+1
• The number of iterations in the inner loop is log210. In the above program
code, the inner loop is controlled by an outer loop. The above formula
must be multiplied by the number of times the outer loop executes, which
is 10. this gives us,
PUZZLE(x)
while x != 1
if x is even
then x = x / 2
else x = 3x + 1
Sample run: 7, 22, 11, 34, 17, 52, 26, 13, 40, 20,
10, 5, 16, 8, 4, 2, 1
Log n n nlog n n2 n3 … nk 2n n!
Log2n N nlog2n n2 n3 2n
0 1 0 1 1 2
1 2 2 4 8 4
2 4 8 16 64 16
3 8 24 64 512 256
4 16 64 256 4096 65536
5 32 160 1024 32768 4294967296
62
Analysis
• Worst case
– Provides an upper bound on running time
– An absolute guarantee
• Average case
– Provides the expected running time
– Very useful, but treat with care: what is “average”?
• Random (equally likely) inputs
• Real-life inputs
63
Asymptotic Notation – Big – Oh
• The idea is to establish a relative order among
functions for large n
– Given function f(n) and g(n) , we say that f(n) is
O(g(n)) if there are positive constants c and n0
such that f(n) ≤ cg(n) for n ≥ n0.
Example:
an = 2an-1 + an-2
Two fundamental rules
–Must always have a base case
–Each recursive call must be a case
that eventually leads toward a base
case
Recurrence Relation of Fibonacci
Number fib(n):
Algorithm 1 fib(n)
if n = 0 then
return (0)
if n = 1then
return (1)
return (fib(n − 1) + fib(n − 2))
Recurrences
• The expression:
c n 1
T ( n)
2T n cn n 1
2
is a recurrence.
– Recurrence: an equation that describes a function
in terms of its value on smaller functions
Recurrence Examples
0 n0 0 n0
s ( n) s ( n)
c s(n 1) n 0 n s(n 1) n 0
n 1
c c n 1
T ( n) T ( n)
2T n c n 1 n
2 aT cn n 1
b
Recursion Methods
• Substitution Method
• Recursion Tree or Iteration Method
• Master Method
Substitution Method
i
i n k 1
s(n k )
0 n0
s ( n)
n s(n 1) n 0
• So far for n >= k we have
n
i
i n k 1
s(n k )
0 n0
s ( n)
n s(n 1) n 0
• So far for n >= k we have
n
i
i n k 1
s(n k )
• What if k = n?
0 n0
s ( n)
n s(n 1) n 0
• So far for n >= k we have
n
i
i n k 1
s(n k )
• What if k = n?
n
n 1
n
i 1
i s(0) i 0 n
i 1 2
0 n0
s ( n)
n s(n 1) n 0
• So far for n >= k we have
n
i
i n k 1
s(n k )
• What if k = n?
n
n 1
n
i 1
i s(0) i 0 n
i 1 2
• Thus in general
n 1
s ( n) n
2
c n 1
n
T (n) 2T
c n 1
2
• T(n) =
2T(n/2) + c
2(2T(n/2/2) + c) + c
22T(n/22) + 2c + c
22(2T(n/22/2) + c) + 3c
23T(n/23) + 4c + 3c
23T(n/23) + 7c
23(2T(n/23/2) + c) + 7c
24T(n/24) + 15c
…
2kT(n/2k) + (2k - 1)c
c n 1
n
T (n) 2T
c n 1
2
• So far for n > 2k we have
– T(n) = 2kT(n/2k) + (2k - 1)c
• What if k = lg n?
– T(n) = 2lg n T(n/2lg n) + (2lg n - 1)c
= n T(n/n) + (n - 1)c
= n T(1) + (n-1)c
= nc + (n-1)c = (2n - 1)c
c n 1
n
T (n) aT
cn n 1
b
• T(n) =
aT(n/b) + cn
a(aT(n/b/b) + cn/b) + cn
a2T(n/b2) + cna/b + cn
a2T(n/b2) + cn(a/b + 1)
a2(aT(n/b2/b) + cn/b2) + cn(a/b + 1)
a3T(n/b3) + cn(a2/b2) + cn(a/b + 1)
a3T(n/b3) + cn(a2/b2 + a/b + 1)
…
akT(n/bk) + cn(ak-1/bk-1 + ak-2/bk-2 + … + a2/b2 + a/b + 1)
c n 1
n
T (n) aT
cn n 1
b
• So we have
– T(n) = akT(n/bk) + cn(ak-1/bk-1 + ... + a2/b2 + a/b + 1)
• For k = logb n
– n = bk
– T(n) = akT(1) + cn(ak-1/bk-1 + ... + a2/b2 + a/b + 1)
= akc + cn(ak-1/bk-1 + ... + a2/b2 + a/b + 1)
= cak + cn(ak-1/bk-1 + ... + a2/b2 + a/b + 1)
= cnak /bk + cn(ak-1/bk-1 + ... + a2/b2 + a/b + 1)
= cn(ak/bk + ... + a2/b2 + a/b + 1)
c n 1
n
T (n) aT
cn n 1
b
• So with k = logb n
– T(n) = cn(ak/bk + ... + a2/b2 + a/b + 1)
• What if a = b?
– T(n) = cn(k + 1)
= cn(logb n + 1)
= (n log n)
c n 1
n
T (n) aT
cn n 1
b
• So with k = logb n
– T(n) = cn(ak/bk + ... + a2/b2 + a/b + 1)
• What if a < b?
c n 1
n
T (n) aT
cn n 1
b
• So with k = logb n
– T(n) = cn(ak/bk + ... + a2/b2 + a/b + 1)
• What if a < b?
– Recall that (xk + xk-1 + … + x + 1) = (xk+1 -1)/(x-1)
c n 1
n
T (n) aT
cn n 1
b
• So with k = logb n
– T(n) = cn(ak/bk + ... + a2/b2 + a/b + 1)
• What if a < b?
– Recall that (xk + xk-1 + … + x + 1) = (xk+1 -1)/(x-1)
– So:
a k a k 1 a
k 1 1
a b k 1 1
1 a b
k 1
1
k
b b b a b 1 1 a b 1 a b
c n 1
n
T (n) aT
cn n 1
b
• So with k = logb n
– T(n) = cn(ak/bk + ... + a2/b2 + a/b + 1)
• What if a < b?
– Recall that (xk + xk-1 + … + x + 1) = (xk+1 -1)/(x-1)
– So:
a k a k 1 a
k 1 1
a b k 1 1
1 a b
k 1
1
k
b b b a b 1
– T(n) = cn ·(1) = (n)
1 a b 1 a b
c n 1
n
T (n) aT
cn n 1
b
• So with k = logb n
– T(n) = cn(ak/bk + ... + a2/b2 + a/b + 1)
• What if a > b?
c n 1
n
T (n) aT
cn n 1
b
• So with k = logb n
– T(n) = cn(ak/bk + ... + a2/b2 + a/b + 1)
• What if a > b?
a k a k 1 a
k 1 1
a b k 1 1
a b
k
k
b b b a b 1
c n 1
n
T (n) aT
cn n 1
b
• So with k = logb n
– T(n) = cn(ak/bk + ... + a2/b2 + a/b + 1)
• What if a > b?
a k a k 1 a
k 1 1
a b k 1 1
a b
k
k
b b b a b 1
– T(n) = cn · (ak / bk)
c n 1
n
T (n) aT
cn n 1
b
• So with k = logb n
– T(n) = cn(ak/bk + ... + a2/b2 + a/b + 1)
• What if a > b?
a k a k 1 a
k 1 1
a b k 1 1
a b
k
k
b b b a b 1
– T(n) = cn · (ak / bk)
= cn · (alog n / blog n) = cn · (alog n / n)
c n 1
n
T (n) aT
cn n 1
b
• So with k = logb n
– T(n) = cn(ak/bk + ... + a2/b2 + a/b + 1)
• What if a > b?
a k a k 1 a
k 1 1
a b k 1 1
a b
k
k
b b b a b 1
– T(n) = cn · (ak / bk)
= cn · (alog n / blog n) = cn · (alog n / n)
recall logarithm fact: alog n = nlog a
c n 1
n
T (n) aT
cn n 1
b
• So with k = logb n
– T(n) = cn(ak/bk + ... + a2/b2 + a/b + 1)
• What if a > b?
a k a k 1 a
k 1 1
a b k 1 1
a b
k
k
b b b a b 1
– T(n) = cn · (ak / bk)
= cn · (alog n / blog n) = cn · (alog n / n)
recall logarithm fact: alog n = nlog a
= cn · (nlog a / n) = (cn · nlog a / n)
c n 1
n
T (n) aT
cn n 1
b
• So with k = logb n
– T(n) = cn(ak/bk + ... + a2/b2 + a/b + 1)
• What if a > b?
a k a k 1 a
k 1 1
a b k 1 1
a b
k
k
b b b a b 1
– T(n) = cn · (ak / bk)
= cn · (alog n / blog n) = cn · (alog n / n)
recall logarithm fact: alog n = nlog a
= cn · (nlog a / n) = (cn · nlog a / n)
= (nlog a )
c n 1
n
T (n) aT
cn n 1
b
• So…
n ab
T (n) n log b n ab
n logb a
ab
Master Method
Example: Tn 2Tn 1 1, T0 0
n 0 1 2 3 4 5 6 7
0 1 3 7 15 31 63 127
Prove that
Tn 2 n 1 by induction: