CS-211 Data Structure & Algorithms - Lecture4
CS-211 Data Structure & Algorithms - Lecture4
Algorithm Analysis
Lecture # 4
1
Nasir Uddin
CS211 - Data Structure and Algorithms
Introduction
• Algorithms are designed to solve problems, but a given problem can
have many different solutions.
• How then are we to determine which solution is the most efficient for
a given problem?
• One approach is to measure the execution time, but the execution
time is dependent on several factors.
• Input data
• Hardware
• programming language and compiler
2
Nasir Uddin
CS211 - Data Structure and Algorithms
Complexity Analysis
• To determine the efficiency of an algorithm,
• count the number of logical comparisons (>, <, ≤, ≥, ! =, =, …)
• data interchanges, or arithmetic operations. (+, −,×,÷, …)
3
Nasir Uddin
CS211 - Data Structure and Algorithms
Complexity Analysis
• Version 1 complexity 2𝑛2
• Version 2 complexity 𝑛2 + 𝑛
4
Nasir Uddin
CS211 - Data Structure and Algorithms
6
Nasir Uddin
CS211 - Data Structure and Algorithms
Complexity Analysis
• Constant of Proportionality
• The constant of proportionality "𝑐“ is only
crucial when two algorithms have the
same 𝑓(𝑛).
• Constructing 𝑇(𝑛)
• Assume that each basic operation or
statement, at the abstract level, takes the
same amount of time and, thus, each is
assumed to cost 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡 𝑡𝑖𝑚𝑒.
• The total number of operations required
by an algorithm can be computed as a
sum of the times required to perform
each step:
8
Nasir Uddin
CS211 - Data Structure and Algorithms
Complexity Analysis
• Choosing the Function
• The function f(n) used to categorize a particular algorithm is chosen to be the
dominant term within 𝑇(𝑛).
• That is, the term that is so large for big values of n, that we can ignore the
other terms when computing a big-O value. For example, in the expression
𝑛2 + log 2 𝑛 + 3𝑛
the term n2 dominates the other terms since for 𝑛 ≥ 3, we have
𝑛2 + log 2 𝑛 + 3𝑛 ≤ 𝑛2 + 𝑛2 + 𝑛2
𝑛2 + log 2 𝑛 + 3𝑛 ≤ 3𝑛2
9
Nasir Uddin
CS211 - Data Structure and Algorithms
Complexity Analysis
• Classes of Algorithms
10
Nasir Uddin
CS211 - Data Structure and Algorithms
Complexity Analysis
• Polynomial Algorithms
• an efficiency expressed as a polynomial of the form are characterized by a
time-complexity of 𝒪(𝑛𝑚 ) since the dominant term is the highest power of 𝑛.
11
Nasir Uddin
CS211 - Data Structure and Algorithms
12
Nasir Uddin
CS211 - Data Structure and Algorithms
13
Nasir Uddin
CS211 - Data Structure and Algorithms
14
Nasir Uddin
CS211 - Data Structure and Algorithms
15
Nasir Uddin
CS211 - Data Structure and Algorithms
16
Nasir Uddin
CS211 - Data Structure and Algorithms
17
Nasir Uddin
CS211 - Data Structure and Algorithms
18
Nasir Uddin
CS211 - Data Structure and Algorithms
20
Nasir Uddin
CS211 - Data Structure and Algorithms
21
Nasir Uddin
CS211 - Data Structure and Algorithms
22
Nasir Uddin
CS211 - Data Structure and Algorithms
24
Nasir Uddin
CS211 - Data Structure and Algorithms
25
Nasir Uddin
CS211 - Data Structure and Algorithms
26
Nasir Uddin
CS211 - Data Structure and Algorithms
27
Nasir Uddin
CS211 - Data Structure and Algorithms
28
Nasir Uddin
CS211 - Data Structure and Algorithms
29
Nasir Uddin