1-Foundations (EID REHMAN)
1-Foundations (EID REHMAN)
CSC – 333
Instructor
Dr.Eid Rehman
Faculty of Engineering and Information Technology
Department of Software Engineering
Foundation University Rawalpindi Campus
Course Introduction 2
1 2 3
Foundations Sorting and Data Structures
Order Statistics
4 5 6
Advanced Design and Graph Algorithms Selected Topics
Analysis Techniques
1 – The Role of Algorithms in
Computing
Contents 4
• Analysis
• Process of breaking a complex topic into smaller parts
• In order to gain a better understanding of it
• Algorithms
• Important for all other branches of computer science
• Routing, Cryptography, Graphics, Databases, Bioinformatics
• Plays a key role in modern technological innovation
• Help us to understand scalability
• Language for talking about program behavior
• Performance
• Performance is the currency of computing
• Feasible vs. Impossible
• Speed is fun
• A real blend of precision and creativity
Why Analyze Algorithms? 10
Problem Algorithm 2
Algorithm 3
Algorithms as a Technology 11
1. Input
• An algorithm has zero or more inputs from a specified set of values
2. Output
• An algorithm has one or more inputs from a specified set of values
3. Definiteness
• Each step of an algorithm must be precisely defined
4. Finiteness
• An algorithm must always terminate after a finite number of steps
5. Effectiveness
• Each step of an algorithm must be performed in finite amount of time
6. Generality
• An algorithm should be applicable for all problems of a desired form
Algorithm vs. Program 14
• Algorithm
• Defines specific steps required to solve a problem
• Program
• A computer program is the implementation of an algorithm
Algorithm Program
Design Implementation
Domain Knowledge Programming Knowledge
Any language Programming language
Hardware/OS Independent Hardware/OS Dependent
Analysis Testing
2 – Getting Started
Contents 16
• Getting Started
• Insertion Sort
• Analyzing Algorithms
• Designing Algorithms
Insertion Sort 17
• Pseudocode
INSERTIONSORT(A)
1 for j = 2 to A.length
2 key = A[i]
3 i = j – 1
4 while i > 0 and A[i] > key
5 A[i+1] = A[i]
6 i = i – 1
7 A[i+1] = key
19
20
Analyzing Algorithms 21
• Running time
• Depends on the input
• For instance, an already sorted sequence as input
• Depends on the input size
• For instance, a sequence of 6 elements vs. 6 × 109 elements
• Upper bounds are generally desired
• Guarantee to user
Analyzing Algorithms 23
• Kinds of analysis
• Worst Case: (usually)
• 𝑇(𝑛) = maximum time of algorithm on any input of size 𝑛
• Provides an upper bound on running time
• Average Case: (sometimes)
• 𝑇(𝑛) = expected time of algorithm over all input of size 𝑛
• Need assumption of statistical distribution of inputs
• Provides an prediction about running time
• Best Case: (bogus)
• Cheat with a slow algorithm which works fast on some input
• Provides a lower bound on running time
Analyzing Algorithms 24
5 A[i+1] ← A[i] - - - - - - - - - - - - - - - - - c5
𝒋=𝟐
(𝑡𝑗 −1)
6 i ← i – 1 - - - - - - - - - - - - - - - - - - - - - - - c6
𝒋=𝟐
(𝑡𝑗 −1)
𝑛
𝑇𝑜𝑡𝑎𝑙 𝐶𝑜𝑠𝑡 = σ𝑖=1(𝐶𝑜𝑠𝑡𝑖 × 𝑇𝑖𝑚𝑒𝑠𝑖 )
where n = number of steps
𝒏 𝒏 𝒏
𝑇(𝑛) = 𝑐1 × 𝑛 + 𝑐2 × 𝑛 − 1 + 𝑐3 × 𝑛 − 1 + 𝑐4 × 𝑡𝑗 + 𝑐5 × (𝑡𝑗 − 1) + 𝑐6 × (𝑡𝑗 − 1) + 𝑐7 × (𝑛 − 1)
𝒋=𝟐 𝒋=𝟐 𝒋=𝟐
Analyzing Algorithms 26
𝑇 𝑛 = 𝑐1 𝑛 + 𝑐2 𝑛 − 𝑐2 + 𝑐3 𝑛 − 𝑐3 + 𝑐4 𝑛 − 𝑐4 + 𝑐7 𝑛 − 𝑐7
𝑇 𝑛 = (𝑐1 + 𝑐2 + 𝑐3 + 𝑐4 + 𝑐7 )𝑛 − (𝑐2 − 𝑐3 − 𝑐4 − 𝑐7 )
𝑇 𝑛 = 𝐴𝑛 + 𝐵
𝑐4 𝑛2 𝑐4 𝑛 𝑐5 𝑛2 𝑐5 𝑛 𝑐6 𝑛2 𝑐6 𝑛
𝑇 𝑛 = 𝑐1 𝑛 + 𝑐2 𝑛 − 𝑐2 + 𝑐3 𝑛 − 𝑐3 + + + 𝑐4 + + + + + 𝑐7 𝑛 − 𝑐7
2 2 2 2 2 2
𝑐4 𝑐5 𝑐6 𝑐4 𝑐5 𝑐6
𝑇 𝑛 = + + 𝑛2 + 𝑐1 + 𝑐2 + 𝑐3 + + + 𝑛 + (𝑐2 + 𝑐3 + 𝑐4 + 𝑐7 )
2 2 2 2 2 2
𝑇 𝑛 = 𝐴𝑛2 + 𝐵𝑛 + 𝐶
• Growth of Functions
• Asymptotic Notations
• Standard Notations and Common Functions
Asymptotic Notations 30
Asymptotic Analysis
Asymptotic Notations 33
• Asymptotic Analysis
• In mathematics, also known as asymptotic
• Method of describing limiting behavior
• Asymptotic notations describe the class of function
• For example
• 𝑓(𝑛) = 𝑛2 + 3𝑛
• 𝑛 becomes very large, 3𝑛 becomes insignificant compared to 𝑛2
• Function 𝑓(𝑛) is said to be asymptotically equivalent to 𝑛2
𝑓(𝑛) ~ 𝑛2
𝑓(𝑛) is asymptotic to 𝑛2
Asymptotic Notations 34
𝟐𝒏 Exponential – when 𝒏 doubles, the run time squares. This is often the result of a
natural, “brute force” solution
Asymptotic Notations 36
𝒏𝟑
𝑻(𝒏) 𝒏𝟐
𝐥𝐨𝐠 𝒏
𝒏
Asymptotic Notations 38
• Types of Notations
• Big Oh 𝑶 Upper Bound
• Theta () Average Bound
• Big Omega () Lower Bound
• Small Oh (𝒐)
• Small Omega (𝝎)
39
Asymptotic Notations 40
• 𝑶-notation
• For a function f 𝑛 = 𝑶(𝒈(𝒏)) is defined as:
𝒇 𝒏 : ∃ 𝑝𝑜𝑠𝑖𝑡𝑖𝑣𝑒 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡𝑠 𝑐 𝑎𝑛𝑑 𝑛0 , 𝑠𝑢𝑐ℎ 𝑡ℎ𝑎𝑡
𝑶 𝒈 𝒏 =
0 ≤ 𝑓 𝑛 ≤ 𝑐𝑔 𝑛 ∀ 𝑛 ≥ 𝑛0
• 𝑶−notation
𝒇(𝒏) = 𝑶(𝒈(𝒏))
• 𝑶−notation – Example
𝒇 𝒏 = 𝟐𝒏 + 𝟑, 𝑛0 = 1
2𝑛 + 3 = 𝑂(𝑔 𝑛 )
2𝑛 + 3 ≤ 10 𝑛 ∀𝑛≥1
𝑐 = 10 and 𝑔(𝑛) = 𝑛
OR write something such that LHS ≤ RHS
2𝑛 + 3 ≤ 7𝑛 ∀𝑛≥1
OR 𝑐 = 7 and 𝑔(𝑛) = 𝑛
2𝑛 + 3 ≤ 2𝑛 + 3𝑛
2𝑛 + 3 ≤ 5𝑛 ∀𝑛≥1
𝑐 = 5 and 𝑔(𝑛) = 𝑛
We can say that for that f(n)= 𝑂 (𝑛2 )
Because f(n)< all 1 < log 𝑛 < 𝑛 < 𝑛 log 𝑛 < 𝑛2 < 𝑛3 <
43
8
Asymptotic Notations 44
• -notation
• For a function 𝑔(𝑛), the (𝒈(𝒏)) is defined as:
𝒇 𝒏 : ∃ 𝑝𝑜𝑠𝑖𝑡𝑖𝑣𝑒 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡𝑠 𝑐 𝑎𝑛𝑑 𝑛0 , 𝑠𝑢𝑐ℎ 𝑡ℎ𝑎𝑡
𝒈 𝒏 =
0 ≤ 𝑐𝑔 𝑛 ≤ 𝑓 𝑛 ∀ 𝑛 ≥ 𝑛0
• -notation
𝒇(𝒏) = (𝒈(𝒏))
• -notation – Example
𝒇 𝒏 = 𝟐𝒏 + 𝟑, 𝑛0 = 1
2𝑛 + 3 = (𝑔 𝑛 )
2𝑛 + 3 ≥ 1𝑛 ∀𝑛≥1 𝑐 = 1 and 𝑔(𝑛) = 𝑛
OR
2𝑛 + 3 ≥ 1 log 𝑛 ∀𝑛≥1 𝑐 = 1 and 𝑔(𝑛) = log 𝑛
• -notation
• For a function 𝑔(𝑛), the (𝒈(𝒏)) is defined as:
𝒇 𝒏 : ∃ 𝑝𝑜𝑠𝑖𝑡𝑖𝑣𝑒 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡𝑠 𝑐1 , 𝑐2 , 𝑎𝑛𝑑 𝑛0 , 𝑠𝑢𝑐ℎ 𝑡ℎ𝑎𝑡
𝒈 𝒏 =
𝑐1 𝑔 𝑛 ≤ 𝑓 𝑛 ≤ 𝑐2 𝑔 𝑛 ∀ 𝑛 ≥ 𝑛0
• -notation
𝒇(𝒏) = (𝒈(𝒏))
• −notation – Example
𝒇 𝒏 = 𝟐𝒏 + 𝟑, 𝑛0 = 1
2𝑛 + 3 = (𝑔 𝑛 )
1𝑛 ≤ 2𝑛 + 3 ≤ 5𝑛 ∀𝑛≥1 𝑐1 = 1, 𝑐2 = 5 and 𝑔(𝑛) = 𝑛
Asymptotic Notations 51
• 𝒐-notation
• For a function 𝑔(𝑛), the 𝒐(𝒈(𝒏)) is defined as:
𝒇 𝒏 : 𝑓𝑜𝑟 𝑎𝑛𝑦 𝑝𝑜𝑠𝑖𝑡𝑖𝑣𝑒 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡 𝑐 > 0,
𝒐 𝒈 𝒏 = ∃ 𝑎 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡 𝑛0 > 0, 𝑠𝑢𝑐ℎ 𝑡ℎ𝑎𝑡
0 ≤ 𝑓 𝑛 < 𝑐𝑔 𝑛 ∀ 𝑛 ≥ 𝑛0
• 𝒐-notation – Example
• Used to denote an upper bound that is not asymptotically tight
• 𝝎-notation
• For a function 𝑔(𝑛), the 𝝎(𝒈(𝒏)) is defined as:
𝒇 𝒏 : 𝑓𝑜𝑟 𝑎𝑛𝑦 𝑝𝑜𝑠𝑖𝑡𝑖𝑣𝑒 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡 𝑐 > 0,
𝝎 𝒈 𝒏 = ∃ 𝑎 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡 𝑛0 > 0, 𝑠𝑢𝑐ℎ 𝑡ℎ𝑎𝑡
0 ≤ 𝑐𝑔 𝑛 < 𝑓 𝑛 ∀ 𝑛 ≥ 𝑛0
• 𝝎-notation – Example
• Used to denote a lower bound that is not asymptotically tight
2𝑛2 = (𝑛2 ) is asymptotically tight
2𝑛2 = (𝑛) is not asymptotically tight
Therefore
2𝑛2 = 𝝎(𝑛) but,
2𝑛2 ≠ 𝝎(𝑛2 )
Asymptotic Notations 55
• Comparison of Notations
Let 𝑎 belongs to 𝑓(𝑛) and 𝑏 to 𝑔(𝑛)
𝑓↔𝑔≈𝑎↔𝑏
𝑓(𝑛) = 𝑶(𝑔(𝑛)) ≈ 𝑎 ≤ 𝑏
𝑓 𝑛 = 𝑔 𝑛 ≈𝑎≥𝑏
𝑓 𝑛 = 𝑔 𝑛 ≈𝑎=𝑏
𝑓 𝑛 =o 𝑔 𝑛 ≈𝑎<𝑏
𝑓 𝑛 =w 𝑔 𝑛 ≈𝑎>𝑏
Asymptotic Notations 56
• Limits of Notations
𝑓(𝑛)
0 < lim <∞ 𝑓 𝑛 ∈ 𝑔 𝑛
𝑛→∞ 𝑔(𝑛)
𝑓(𝑛)
lim <∞ 𝑓(𝑛) ∈ 𝑶(𝑔 𝑛 )
𝑛→∞ 𝑔(𝑛)
𝑓(𝑛)
0< lim 𝑓(𝑛) ∈ (𝑔 𝑛 )
𝑛→∞ 𝑔(𝑛)
𝑓(𝑛)
lim =0 𝑓(𝑛) ∈ o(𝑔 𝑛 )
𝑛→∞ 𝑔(𝑛)
𝑓(𝑛)
lim =∞ 𝑓(𝑛) ∈ w(𝑔 𝑛 )
𝑛→∞ 𝑔(𝑛)
Asymptotic Notations 57
• Properties of Notations
• Transitivity
𝑓 𝑛 = 𝑔 𝑛 and 𝑔 𝑛 = ℎ 𝑛 imply f 𝑛 = ℎ 𝑛
𝑓 𝑛 =𝑶 𝑔 𝑛 and 𝑔 𝑛 = 𝑶 ℎ 𝑛 imply f 𝑛 = 𝑶 ℎ 𝑛
𝑓 𝑛 = 𝑔 𝑛 and 𝑔 𝑛 = ℎ 𝑛 imply f 𝑛 = ℎ 𝑛
𝑓 𝑛 =𝒐 𝑔 𝑛 and 𝑔 𝑛 = 𝒐 ℎ 𝑛 imply f 𝑛 = 𝒐 ℎ 𝑛
𝑓 𝑛 =w 𝑔 𝑛 and 𝑔 𝑛 = w ℎ 𝑛 imply f 𝑛 = w ℎ 𝑛
Asymptotic Notations 58
• Properties of Notations
• Transitivity – Example
𝑓 𝑛 = 𝑛 , 𝑔 𝑛 = 𝑛2 and ℎ 𝑛 = 𝑛3
𝑛 = 𝑶(𝑛2 ) and 𝑛2 = 𝑶(𝑛3 ) imply 𝑛 = 𝑶 𝑛3
Asymptotic Notations 59
• Properties of Notations
• Reflexivity
𝑓 𝑛 = 𝑓 𝑛
𝑓 𝑛 =𝑶 𝑓 𝑛
𝑓 𝑛 = 𝑓 𝑛
• Example
𝑓 𝑛 = 𝑛2 𝑶(𝑛2 )
Asymptotic Notations 60
• Properties of Notations
• Symmetric
𝑓 𝑛 = 𝑔 𝑛 if and only if 𝑔 𝑛 = 𝑓 𝑛
• Example
𝑓 𝑛 = 𝑛2 and 𝑔 𝑛 = 𝑛2 then
𝑓 𝑛 = 𝑛2 and 𝑔 𝑛 = 𝑛2
Asymptotic Notations 61
• Properties of Notations
• Transpose Symmetric
𝑓 𝑛 =𝑶 𝑔 𝑛 if and only if 𝑔 𝑛 = 𝑓 𝑛
𝑓 𝑛 =𝒐 𝑔 𝑛 if and only if 𝑔 𝑛 = w 𝑓 𝑛
• Example
𝑓 𝑛 = 𝑛 and 𝑔 𝑛 = 𝑛2 then
𝑛 is 𝑶 𝑛2 and 𝑛2 = 𝑛 then
Asymptotic Notations 62
• Example
𝑓 𝑛 = 2𝑛2 + 3 and 𝑔 𝑛 = 𝑛2 then
𝑓 𝑛 = 𝑶 𝑛2 and 𝑓(𝑛) = 𝑛2 therefore, 𝑓 𝑛 = 𝑛2
Asymptotic Notations 63
• Divide-and-Conquer
• Recurrences
• Recurrences Solution Methods
• The Substitution Method
• The Recursion-tree Method
Recurrences 67