CSIT571 - 01SP23 - Module - 01 Introduction To Algorithms
CSIT571 - 01SP23 - Module - 01 Introduction To Algorithms
Asymptotic Comparison
Asymptotic Comparison
• Let’s exercise!
𝑛
= 10𝑛 𝑎𝑐 + 10 𝑎𝑑 + 𝑏𝑐 + 𝑏𝑑 (∗𝑠𝑖𝑚𝑝𝑙𝑒 𝑏𝑎𝑠𝑒 𝑐𝑎𝑠𝑒 𝑜𝑚𝑖𝑡𝑡𝑒𝑑)
2
Idea: Recursively compute ac, ad, bc, bd, then compute (*) in the
obvious way.
1. Recursively compute ac
2. Recursively compute bd
3. Recursively compute (a+b)(c+d) = ac+bc+ad+bc
• Reduce duplication
• Memoization
• Sort in place
• If the same space is used for input and output and the extra memory used by
the algorithm is constant.
• Stable Sort.
• if the relative order of same valued keys remains the same in the input and
output sequences.
• n reversed-sorted keys
• ϴ(𝑛2 )
5 4 1 8 7 2 6 3
5 4 1 8 5 4 1 8
Recursive Calls
1 4 5 8 2 3 6 7
merge
1 2 3 4 5 6 7 8
• T(n) = dn lg n
• Roughly how many levels does this recursion tree have (as a function of n,
the length of the input array)? – 𝑐, log 2 𝑛 , 𝑛, 𝑛
• What is the pattern? Fill in the blanks in the following statement: at each
level j=0,1,2,.., log 2 𝑛, there are <blank> subproblems, each of size
𝑗 𝑛
<blank>. 2 𝑎𝑛𝑑 𝑗 , respectively
2
𝒏
• Every level: ≤ 𝟐𝒋 ∗ 𝟔 = 𝟔𝒏
𝟐𝒋
𝒍𝒆𝒗𝒆 𝒋: # 𝒐𝒇 𝒍𝒆𝒗𝒆𝒍 𝒋 𝒔𝒖𝒃𝒑𝒓𝒐𝒃𝒍𝒆𝒎𝒔 ∗ 𝒔𝒊𝒛𝒆 𝒐𝒇 𝒍𝒆𝒗𝒆𝒍 𝒋 𝒔𝒖𝒃𝒑𝒓𝒐𝒃𝒍𝒆𝒎
• Total: 𝟔𝒏 ∗ 𝒍𝒐𝒈𝟐 𝒏 + 𝟏
𝑾𝒐𝒓𝒌 𝒑𝒆𝒓 𝒍𝒆𝒗𝒆𝒍 ∗ # 𝒐𝒇 𝒍𝒆𝒗𝒆𝒍𝒔.
• comparison: n-1
• n-2?
• Sort In place
• This can become in-place if instead of swapping A[index] and A[i] we shift
the keys to right starting with the key at i and ending with the key at index-1
and then concluding moving the original A[index] into A[i].
• Parallel Sort
• Probing operation
• Search, FindMin, FindMax
• Modifying operation
• Insert, Delete, ExtractMax, DeleteMax
As Opposed to
-- ”average case” analysis
-- benchmarks
Justifications
1. Way easier
2. Constants depend on architecture/compiler/programmer anyways
3. Lose very little predictive power
(as we’ll see)
𝟏 𝟐
Eg: Merge Sort 𝟔𝒏 ∗ 𝒍𝒐𝒈𝟐 𝒏 + 𝟔𝒏 “better than” Insertion Sort 𝒏
𝟐
𝑭𝒂𝒔𝒕 𝑨𝒍𝒈𝒐𝒓𝒊𝒕𝒉𝒎 ≈ 𝑾𝒐𝒓𝒔𝒕 𝒄𝒂𝒔𝒆 𝒓𝒖𝒏𝒏𝒊𝒏𝒈 𝒕𝒊𝒎𝒆 𝒈𝒓𝒐𝒘𝒔 𝒄𝒍𝒔𝒐𝒍𝒚 𝒘𝒊𝒕𝒉 𝒊𝒏𝒑𝒖𝒕 𝒔𝒊𝒛𝒆