Lecture#2 - Analysis of Algorithm
Lecture#2 - Analysis of Algorithm
• Compare algorithms
• Multimedia • Computers
✔ CD player, DVD, MP3, JPG, DivX, HDTV ✔ Circuit layout, file systems
• Internet • Science
✔ Packet routing, data retrieval (Google) ✔ Human genome
• Communication • Transportation
✔ Cell-phones, e-commerce
✔ Airline crew scheduling, ARAMEX and
DHL deliveries
Greedy Algorithms 6
ROADMAP
• Different problems
✔ Searching
✔ Sorting
✔ Graph problems
Analysis of Algorithm 7
ANALYZING ALGORITHMS
• Running time : the number of primitive operations (steps) executed before termination
✔ Arithmetic operations (+, -, *), data movement, control, decision making (if, while),
comparison
2000/100 = 20
times better!!
• Running time:
MIN (a[1], …, a[n]) • the number of primitive operations (steps) executed
before termination
m ← a[1];
T(n) =1 [first step] + (n) [for loop] + (n-1) [if condition] +
for i ← 2 to n
if a[i] < m then (n-1) [the assignment in then] = 3n - 1
m ← a[i]; • Order (rate) of growth:
• The leading term of the formula
• Expresses the asymptotic behavior of the algorithm
Analysis of Algorithm 12
TYPICAL RUNNING TIME FUNCTIONS ....
• N2 (quadratic)
✔ Typical for algorithms that process all pairs of data items (double nested loops)
• N3(cubic)
✔ Processing of triples of data (triple nested loops)
• NK (polynomial)
• 2N (exponential)
✔ Few exponential algorithms are appropriate for practical use
Analysis of Algorithm 13
PRACTICAL COMPLEXITY
Analysis of Algorithm 14
PRACTICAL COMPLEXITY ....
Analysis of Algorithm 15
PRACTICAL COMPLEXITY ....
Analysis of Algorithm 16
WHY DO WE NEED FASTER ALGORITHMS?
Analysis of Algorithm 17
ASYMPTOTIC NOTATIONS
Analysis of Algorithm 18
ASYMPTOTIC NOTATIONS - EXAMPLES
• Θ notation
– n2/2 – n/2 = Θ
(n+2)1)
– (6n3 + 1)lgn/(n = Θ
2
– n vs. n2 n ≠ Θ (n lgn)
• Ω notation (n2)
• O notation
– n3 vs. n2 n3 = Ω – 2n2 vs. n3 2n2 = O(n3)
– n vs. logn (n2) – n2 vs. n2
n = Ω n2 = O(n2)
– n vs. n2 (logn)
n ≠ Ω – n3 vs. nlogn n3 ≠
(n2) O(nlgn)
Analysis of Algorithm 19
RECURSIVE ALGORITHMS
Analysis of Algorithm 20
RECURRENCES
Analysis of Algorithm 21
SORTING
Iterative methods
• Insertion sort
• Bubble sort
• Selection sort
Analysis of Algorithm 22
TYPES OF ANALYSIS
Analysis of Algorithm 23
GREEDY ALGORITHM
Explore it on NEXT DAY