Professional Documents
Culture Documents
0307 Analysis
0307 Analysis
(«ôPÀ⌥ó’)
Lecture 3: Analysis Tools
Hsuan-Tien Lin (ó“0)
htlin@csie.ntu.edu.tw
Roadmap
1 the one where it all began
3/25
Analysis Tools motivation
G ET-M IN(A)
1 m = 1 // store current min. index
2 for i = 2 to A.length
3 // update if i-th element smaller
4 if A[m] > A[i]
5 m=i
6 return A[m]
G ET-M IN -WASTE(A)
1 B = C OPY(A, 1, A.length)
2 I NSERTION -S ORT(B)
3 return B[1]
5/25
Analysis Tools motivation
Fun Time
Consider running G ET-M IN on an array A of length n. If line i
takes a time cost of di , and the inequality in line 4 is TRUE for t
times, what is the time complexity of G ET-M IN?
G ET-M IN(A) 1 d1 + d2 + d4 + d5 + d6
1 m = 1 // store current min. index
2 for i = 2 to A. length 2 d1 + td2 + td4 + td5 + d6
3 // update if i-th element smaller 3 d1 + nd2 + td4 + td5 + d6
4 if A[m] > A[i]
5 m = i 4 d1 + nd2 + (n 1)d4 + td5 + d6
6 return A[m]
7/25
Analysis Tools motivation
Fun Time
Consider running G ET-M IN on an array A of length n. If line i
takes a time cost of di , and the inequality in line 4 is TRUE for t
times, what is the time complexity of G ET-M IN?
G ET-M IN(A) 1 d1 + d2 + d4 + d5 + d6
1 m = 1 // store current min. index
2 for i = 2 to A. length 2 d1 + td2 + td4 + td5 + d6
3 // update if i-th element smaller 3 d1 + nd2 + td4 + td5 + d6
4 if A[m] > A[i]
5 m = i 4 d1 + nd2 + (n 1)d4 + td5 + d6
6 return A[m]
Reference Answer: 4
The loop (including ending check) in line 2 is
run n times; the condition in line 4 is checked
n 1 times, and t of those result in execution
of line 5.
7/25
cases of complexity analysis
Analysis Tools cases of complexity analysis
sorted A =) tm = 1
T (n)
n
X n
X n
X
= d1 n + d2 (n 1) + d4 (n 1) + d5 tm + d6 (tm 1) + d7 (tm 1) + d8 (n 1)
m=2 m=2 m=2
= d1 n + d2 (n 1) + d4 (n 1) + d5 (n 1) + d6 (0) + d7 (0) + d8 (n 1)
reverse-sorted A =) tm = m
T (n)
n
X n
X n
X
= d1 n + d2 (n 1) + d4 (n 1) + d5 tm + d6 (tm 1) + d7 (tm 1) + d8 (n 1)
m=2 m=2 m=2
(n+2)(n 1) n(n 1) n(n 1)
= d1 n + d2 (n 1) + d4 (n 1) + d5 ( 2
) + d6 ( 2
) + d7 ( 2
) + d8 (n 1)
other cases
A = [1, 2, 4, 3]
other cases
best cases A = [1, 4, 2, 3] worst cases
A = [1, 2, 3, 4] A = [4, 3, 2, 1]
...
other cases
A = [4, 3, 1, 2]
T (n) = F·n2 +⌅ · n + ⌥
Fun Time
Which of the following describes the best-case time complexity
of G ET-M IN on an array A of length n?
G ET-M IN(A) 1 constant to n
1 m = 1 // store current min. index
2 for i = 2 to A. length 2 linear to n
3 // update if i-th element smaller 3 quadratic to n
4 if A[m] > A[i]
5 m = i 4 none of the other choices
6 return A[m]
13/25
Analysis Tools cases of complexity analysis
Fun Time
Which of the following describes the best-case time complexity
of G ET-M IN on an array A of length n?
G ET-M IN(A) 1 constant to n
1 m = 1 // store current min. index
2 for i = 2 to A. length 2 linear to n
3 // update if i-th element smaller 3 quadratic to n
4 if A[m] > A[i]
5 m = i 4 none of the other choices
6 return A[m]
Reference Answer: 2
Even in the best case, where line 5 is
executed 0 times, the loop (including ending
check) in line 2 still needs to be run n times,
and the condition in line 4 still needs to be
checked n 1 times.
13/25
asymptotic notation
Analysis Tools asymptotic notation
‘Rough’ Notation
goal
• care more about
• larger n
roughly • leading term of n
F·n2 +⌅ · n + ⌥ ⇠ n2
• care less about
• constants
• other terms of n
notation
F · n2 + ⌅ · n + ⌥ = ⇥(|{z}
n2 )
| {z }
f (n) g(n)
f (n)
extracting the similarity: consider g(n)
15/25
Analysis Tools asymptotic notation
Modeling Rough with Asymptotic Behavior
goal
F · n2 + ⌅ · n + ⌥ = ⇥(|{z}
n2 )
| {z }
f (n) g(n)
f (n)
lim =c
n!1 g(n)
asymptotic notation:
the most used ‘language’ for time/space complexity
17/25
Analysis Tools asymptotic notation
18/25
Analysis Tools asymptotic notation
• with definition of limit, there exists ✏ > 0, n0 > 0 such that for all
f (n)
n n0 , | g(n) c| < ✏.
f (n)
• i.e. for all n n0 , c ✏< g(n) < c + ✏.
• Let c10 = c ✏, c20 = c + ✏, n00 = n0 , formal definition satisfied with
(c10 , c20 , n00 ). QED
19/25
usage of asymptotic notation
Analysis Tools usage of asymptotic notation
21/25
Analysis Tools usage of asymptotic notation
Proof
• f (n) = ⇥(loga n) () 9(c1 , c2 , n0 ) such that
c1 loga n f (n) c2 loga n for n n0
• Then, c1 loga b logb n f (n) c2 loga b logb n for n n0
• Let c10 = c1 loga b, c20 = c2 loga b, n00 = n0 , we get f (n) = ⇥(logb n)
22/25
Analysis Tools usage of asymptotic notation
S EQ -S EARCH(A, key )
1 for i = 1 to A.length
2 // return when found
3 if A[i] equals key
4 return i
5 return NIL
• best case (i.e. key at 1): T (n) = ⇥(1)
• worst case (i.e. return NIL): T (n) = ⇥(n)
• average case with respect to uniform key 2 A: E(T (n)) = ⇥(n)
23/25
Analysis Tools usage of asymptotic notation
24/25
Analysis Tools usage of asymptotic notation
Summary
25/25