DSA Ch2 Complexity Algorithm
DSA Ch2 Complexity Algorithm
Algorithms
Le Thanh Sach
Chapter 2
Complexity of Algorithms Algorithm
Efficiency
Big-O notation
Data Structures and Algorithms Problems and
common
complexities
P and NP
Problems
Le Thanh Sach
Faculty of Computer Science and Engineering
University of Technology, VNU-HCM
2.1
Complexity of
Outcomes Algorithms
Le Thanh Sach
P and NP
classes, for examples, constant, linear, etc. Problems
2.2
Complexity of
Contents Algorithms
Le Thanh Sach
1 Algorithm Efficiency
Algorithm
Efficiency
Problems and
common
complexities
4 P and NP Problems
2.3
Complexity of
Algorithms
Le Thanh Sach
Algorithm
Efficiency
Problems and
common
complexities
P and NP
Problems
2.4
Complexity of
Algorithm Efficiency Algorithms
Le Thanh Sach
Problems and
common
measure of the difficulty degree (time complexities
P and NP
and/or space) of an algorithm. Problems
2.5
Complexity of
Algorithm Efficiency Algorithms
Le Thanh Sach
General format
Algorithm
Efficiency
efficiency = f(n) Big-O notation
data)
2.6
Complexity of
Linear Loops Algorithms
Le Thanh Sach
Big-O notation
f(n) = n
Problems and
common
complexities
P and NP
Problems
f(n) = n/2
2.7
Complexity of
Linear Loops Algorithms
Algorithm
Efficiency
Big-O notation
Problems and
common
f (n) = n/2 complexities
P and NP
Problems
n 2.8
Complexity of
Logarithmic Loops Algorithms
Le Thanh Sach
Multiply loops
i = 1
while ( i <= n )
application code
i = i x 2 Algorithm
Efficiency
Big-O notation
application code
i = i / 2
f(n) = log2 n
2.9
Complexity of
Logarithmic Loops Algorithms
time Le Thanh Sach
Algorithm
Efficiency
Big-O notation
Problems and
common
complexities
P and NP
Problems
f (n) = log2 n
n 2.10
Complexity of
Nested Loops Algorithms
Le Thanh Sach
Example
Algorithm
Efficiency
i = 1
Big-O notation
while ( i <= n )
Problems and
j = 1 common
while ( j <= n ) complexities
f (n) = n log2 n
2.11
Complexity of
Nested Loops Algorithms
time f (n) = n log2 n Le Thanh Sach
Algorithm
Efficiency
Big-O notation
Problems and
common
complexities
P and NP
Problems
n 2.12
Complexity of
Quadratic Loops Algorithms
Le Thanh Sach
Example
i = 1
while ( i <= n ) Algorithm
Efficiency
j = 1
Big-O notation
while ( j <= n )
application code Problems and
common
j = j + 1 complexities
i = i + 1 P and NP
Problems
f (n) = n2
2.13
Complexity of
Dependent Quadratic Loops Algorithms
Le Thanh Sach
Example
i = 1
while ( i <= n ) Algorithm
Efficiency
j = 1
Big-O notation
while ( j <= i )
application code Problems and
common
j = j + 1 complexities
i = i + 1 P and NP
Problems
1 + 2 + . . . + n = n(n + 1)/2
2.14
Complexity of
Quadratic Loops Algorithms
time f (n) = n2 Le Thanh Sach
Algorithm
Efficiency
Big-O notation
Problems and
common
complexities
P and NP
Problems
n 2.15
Complexity of
Asymptotic Complexity Algorithms
Le Thanh Sach
efficiency. P and NP
Problems
2.16
Complexity of
Algorithms
Le Thanh Sach
Algorithm
Efficiency
Problems and
common
complexities
P and NP
Problems
2.17
Complexity of
Big-O notation Algorithms
Le Thanh Sach
Example
f (n) = c.n ⇒ f (n) = O(n)
f (n) = n(n + 1)/2 = n2 /2 + n/2 ⇒ f (n) = O(n2 )
Algorithm
Efficiency
Big-O notation
• Set the coefficient of the term to one. Problems and
common
complexities
• Keep the largest term and discard the P and NP
Problems
others.
Some example of Big-O:
log2 n n n log2 n n2 ... nk ... 2n n!
2.18
Complexity of
Standard Measures of Efficiency Algorithms
Le Thanh Sach
P and NP
factorial O(n!) 10000! intractable Problems
2.19
Complexity of
Standard Measures of Efficiency Algorithms
Algorithm
Efficiency
Big-O notation
Problems and
common
complexities
P and NP
Problems
log2 n
n 2.20
Complexity of
Big-O Analysis Examples Algorithms
Le Thanh Sach
Le Thanh Sach
Algorithm
Efficiency
Problems and
f (size) = O(size2) common
complexities
P and NP
Problems
2.22
Complexity of
Time Costing Operations Algorithms
Le Thanh Sach
Problems and
common
• Operations under consideration: complexities
P and NP
• Comparisons Problems
• Arithmetic operations
• Assignments
2.23
Complexity of
Algorithms
Le Thanh Sach
Big-O notation
P and NP
Problems
2.24
Complexity of
Binary search Algorithms
Le Thanh Sach
Problems and
common
complexities
P and NP
Problems
1 2 3 5 8 13 21 34 55 89
T (n) = 1 + T (n/2) ⇒ T (n) = O(log2 n)
2.25
Complexity of
Binary search Algorithms
Le Thanh Sach
Big-O notation
• Worst case: when the number of steps Problems and
common
P and NP
Problems
• Average case: in between.
T (n) = O(log2 n)
2.26
Complexity of
Sequential search Algorithms
Le Thanh Sach
8 5 21 2 1 13 4 34 7 18
Algorithm
Efficiency
Problems and
• Worst case: T (n) = O(n) common
complexities
2.27
Complexity of
Quick sort Algorithms
Le Thanh Sach
19 8 3 15 28 10 22 4 12 83
Algorithm
Efficiency
Recurrence Equation
Big-O notation
P and NP
Problems
2.28
Complexity of
Algorithms
Le Thanh Sach
Algorithm
Efficiency
Problems and
common
complexities
P and NP
Problems
2.29
Complexity of
P and NP Problems Algorithms
Le Thanh Sach
Problems and
common
complexities
2.30
Complexity of
P and NP Problems Algorithms
Le Thanh Sach
Travelling Salesman Problem:
A salesman has a list of cities, each of which he must visit
exactly once. There are direct roads between each pair of
cities on the list.
Find the route the salesman should follow for the shortest Algorithm
Efficiency
possible round trip that both starts and finishes at any one
Big-O notation
of the cities.
Problems and
common
complexities
8
b c P and NP
Problems
7
9 5
15
a d
6 9
8
11
e f
2.31
Complexity of
P and NP Problems Algorithms
Le Thanh Sach
8 Big-O notation
b c Problems and
7 common
complexities
9 5 P and NP
15 Problems
a d
6 9
8
11
e f
2.32
Complexity of
P and NP Problems Algorithms
Le Thanh Sach
Algorithm
Efficiency
P Big-O notation
Problems and
common
NP complexities
P and NP
Problems
NP-complete
P = NP?
2.33