Unit 1 Daa PPT
Unit 1 Daa PPT
CSE
Course : Design & Analysis of Algorithms
Topic: Algorithms, Complexity and Analysis
algorithm
while n ≠ 0 do
r ← m mod n
m← n
n←r
return m
Department of CSE, ANITS –CSE316 Design & Analysis of
Algorithms
Other Approach to Compute gcd(m,n)
Is this an algorithm?
Example: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Department of CSE, ANITS –CSE316 Design & Analysis of
Algorithms
Why Study Algorithms?
• Theoretical importance
• Practical importance
input size
T(n) ≈ copC(n)
running time execution time Number of times
for basic operation basic operation
is executed
n0
Figure 1. Illustrating “big O”
•Is 6 · 2n + n2 = O(2n)?
f(n) n! 2n n3
n2
n log n
n (linear time)
log n
n
Figure Growth rates of some important complexity classes
Department of CSE, ANITS –CSE316 Design & Analysis of
Algorithms
Note:
log n
n(linear)
n log n polynomial time (easy or tractable)
n2
n3
2n
n! exponential time (hard or intractable)
n0
Figure . Illustrating Ω
•Is ?
value of n0
•Is ?
When n is bigger, 2n will grow faster than n100. ( Yes, you can find n0 )
such that
n0
Figure. Illustrating Θ
• Is 3n + 2 = Θ(n2)?
• Is ?
1) Transitive:
If and then
If and then
If and then
2) Reflexive:
3) Symmetric:
iff
iff
The problem:
Sort n keys in nondecreasing sequence.
Solving strategy:
Insertion sort – insert the ith item into a sorted list of length (i – 1) by looking at numbers
one at a time, ∀ i >1.
Example: sort 3,5,4,1
step 1 step 2 step 3 step 4
3 3 3 1
5 5 4 3
4 4 5 4
1 1 1 5
3) Logb 1 = 0 ∀b
4) Logb xa = a · Logb x
9) geometric sums
12)
Note:
Log2 = Lg
Loge = Ln
Algorithm GaussianElimination(A[0..n-1,0..n])
//Implements Gaussian elimination of an n-by-(n+1) matrix A
for i ← 0 to n - 2 do
for j ← i + 1 to n - 1 do
for k ← i to n do
A[j,k] ← A[j,k] - A[i,k] * A[j,i] / A[i,i]