lect3_Asymptotic Notation_Part 1
lect3_Asymptotic Notation_Part 1
Algorithms
Lecture 3
Dr. Metwally Rashad
2022
Table of Contents
1. Foundations
- The Role of Algorithms in Computing
- Design and Analysis algorithms
- Growth of Functions
2. Sorting
- Heapsort
- Quicksort
- Sorting in Linear Time
3. Graph Algorithms
- Elementary Graph Algorithms
- Minimum Spanning Trees
4. Advanced Design and Analysis Techniques
- Dynamic Programming
- Greedy Algorithms
5. Selected Topics
- Linear Programming
- String Matching
1/18
Ch3: Growth of Functions 2/18
Analysis of Algorithms
An algorithm is a finite set of precise instructions for performing
a computation or for solving a problem
3/18
Types of Analysis
Worst Case
Provides an upper bound on running time
An absolute guarantee that the algorithm would not run longer, no matter what the inputs are
best Case
Provides a lower bound on running time
Input is the one for which the algorithm runs the fastest
Average case
Provides a prediction about the running time
Assumes that the input is random
8/18
Asymptotic Notation (cont.)
Examples (𝑶-notation)
(Ex. 1) 𝟐𝒏𝟐 = 𝑶 𝒏𝟑 : 2𝑛2 ≤ 𝑐𝑛3 2 ≤ 𝑐𝑛 𝑐 = 1 𝑎𝑛𝑑 𝑛0 = 2
10/18
Asymptotic Notation (cont.)
Examples (𝜴-notation)
(Ex. 1) 𝟓𝒏𝟐 = (𝒏)
𝑐, 𝑛0 such that: 0 𝑐𝑛 5𝑛2 𝑐𝑛 5𝑛2 𝑐 = 1 and 𝑛0 = 1
(Ex. 2) 𝟏𝟎𝟎𝒏 + 𝟓 ≠ (𝒏𝟐)
𝑐, 𝑛0 such that: 0 𝑐𝑛2 100𝑛 + 5
since 100𝑛 + 5 100𝑛 + 5𝑛 ( 𝑛 1) = 105𝑛
𝑛(𝑐𝑛 – 105) 0
Since 𝑛 is positive 𝑐𝑛 – 105 0 𝑛 105/"c"
contradiction: 𝒏 cannot be smaller than a constant 𝑐 11/18
Asymptotic Notation (cont.)
-notation (Average case)
𝑶
• Intuitively:
½ 𝑛2 − ½ 𝑛 ≥ ½ 𝑛2 − ½ 𝑛 ∗ ½ 𝑛 (𝒏 ≥ 𝟐 )
= ¼ 𝑛2 𝒄𝟏 = ¼
(Ex. 2) 𝟔𝒏 ≠ (𝒏𝟐)
𝑐1 𝑛2 ≤ 6𝑛 ≤ 𝑐2 𝑛2 only holds for: 𝑛 ≤ 1/𝑐1
13/18
Properties of Asymptotic Notation
Transitivity:
𝑓(𝑛) = (𝑔(𝑛)) and 𝑔(𝑛) = (ℎ(𝑛)) 𝑓(𝑛) = (ℎ(𝑛))
Same for 𝑂 and
Reflexivity:
𝑓(𝑛) = (𝑓(𝑛))
Same for 𝑂 and
Symmetry:
𝑓(𝑛) = (𝑔(𝑛)) if and only if 𝑔(𝑛) = (𝑓(𝑛))
Transpose symmetry:
𝑓(𝑛) = 𝑂(𝑔(𝑛)) if and only if 𝑔(𝑛) = (𝑓(𝑛)) 14/18
Logarithms
In algorithm analysis we often use the notation “log 𝑛” without specifying
the base
log n (log n )
k k
c n 1
The expression T (n)
2T n cn n 1
2
is a recurrence 16/18
Recurrences Examples
0 n0 0 n0
s ( n) s ( n)
c s (n 1) n 0 n s (n 1) n 0
c n 1 c n 1
T ( n) T ( n)
2T c n 1
n n
aT cn n 1
2 b
17/18
Solving Recurrences
Recursion-tree Method
Master Method
18/18