CSI 217: Data Structures
Complexity Analysis
Sheikh Adilina
United International University
Contents
U • Motivation for Complexity Analysis
• Machine Independence
I • Best, Average and Worst Case
U • Big Oh Notation
• Examples
7/24/21 CSI 217: Data Structure 2
Motivations for Complexity
Analysis
U • There are often many different algorithms which can be used to
solve the same problem. Thus, it makes sense to develop
I techniques that allow us to:
o compare different algorithms with respect to their “efficiency”
U o choose the most efficient algorithm for the problem
• The efficiency of any algorithmic solution to a problem is a
measure of the:
o Time efficiency: the time it takes to execute.
o Space efficiency: the space (primary or secondary memory)
it uses.
• We will focus on an algorithm’s efficiency with respect to time.
7/24/21 CSI 217: Data Structure 3
Machine independence
U • The evaluation of efficiency should be as machine independent
•
as possible.
It is not useful to measure how fast the algorithm runs as this
I •
depends on which particular computer, OS, programming
language, compiler, and kind of inputs are used in testing
Instead,
U o we count the number of basic operations the algorithm
performs.
o we calculate how this number depends on the size of the
input.
• A basic operation is an operation which takes a constant
amount of time to execute.
• Hence, the efficiency of an algorithm is the number of basic
operations it performs. This number is a function of the input
size n.
7/24/21 CSI 217: Data Structure 4
Best, Average, and Worst case
U • We are usually interested in the worst case complexity: what
are the most operations that might be performed for a given
problem size. We will not discuss the other cases -- best and
I average case.
• Best case depends on the input
U • Average case is difficult to compute
• So we usually focus on worst case analysis
o Easier to compute
o Usually close to the actual running time
o Crucial to real-time systems (e.g. air-traffic control)
7/24/21 CSI 217: Data Structure 5
Big Oh Notation
U• Big O:
o Upper bound
I• Big Omega:
U o Lower bound
• Big Theta:
o Tight bound
o (both Upper and Lower)
7/24/21 CSI 217: Data Structure 6
U
I
U
7/24/21 CSI 217: Data Structure 7
U
I
U
7/24/21 CSI 217: Data Structure 8
U • Drop the Non-Dominant Terms
I
U
7/24/21 CSI 217: Data Structure 9
Example 1
U
I
U
7/24/21 CSI 217: Data Structure 10
Example 2
U
I
U
7/24/21 CSI 217: Data Structure 11
Example 3
U
I
U
7/24/21 CSI 217: Data Structure 12
Example 4
U
I
U
7/24/21 CSI 217: Data Structure 13
Example 5
U
I
U
7/24/21 CSI 217: Data Structure 14
Example 6
U
I
U
7/24/21 CSI 217: Data Structure 15