recurrence relations Flashcards
insertion sort algorithm
Step 1 − If it is the first element, it is already sorted. return 1;
Step 2 − Pick next element
Step 3 − Compare with all elements in the sorted sub-list
Step 4 − Shift all the elements in the sorted sub-list that is greater than the value to be sorted
Step 5 − Insert the value
Step 6 − Repeat until list is sorted
insertion sort time complexity
O(n^2)
merge sort running time
worst case time complexity O(n log n)
merge sort algorithm
Step 1 − if it is only one element in the list it is already sorted, return.
Step 2 − divide the list recursively into two halves until it can no more be divided.
Step 3 − merge the smaller lists into new list in sorted order.
Substitution method advantages and disadvantages
Guess a solution and check if it is correct
Pros: rigorous, formal solution
Cons: requires induction, how do you guess?
Recursion trees advantages and disadvantages
Build a tree representing all recursive calls, infer the solution
Pros: intuitive, visual, can be used to guess
Cons: not too formal
Master method advantages and disadvantages
Check the cookbook and apply the appropriate case
Pros: formal, useful for most recurrences
Cons: have to memorize, useful for most recurrence
substitution method steps
Steps
*Guess the solution
*Use induction to show that the solution works
What is induction?
A method to prove that a given statement is true for all-natural numbers 𝑘
Two steps
Base case: show that the statement is true for the smallest value of 𝑛 (typically 0)
Induction step: assuming that the statement is true for any 𝑘, show that it must also hold for 𝑘+1
master method recurrence function template
𝑇(𝑛)=𝑎𝑇(𝑛/𝑏)+𝑓(𝑛)
Compare 𝑛^log_𝑏𝑎 vs. 𝑓(𝑛)
master method case 1
if f(n) < n^log_b(a)
then theta(n^log_b(a))
master method case 2
if f(n) = n^log_b(a)
then theta( n^log_b(a) * log(n))
master method case 3
if f(n) > n^log_b(a)
then theta(f(n))
and a.f(n/b) ≤ k.f(n)
master method, all three cases
Intuition: the larger of the two functions determines the solution
The three cases compare 𝑓(𝑛) with 𝑛^log_𝑏(𝑎)
Case 1: 𝑛^log_𝑏(𝑎) is larger – 𝑂 definition
Case 2: 𝑓(𝑛) and 𝑛^log_𝑏(𝑎) grow at the same rate – Θ definition
Case 3: 𝑓(𝑛) is larger – Ω definition
columns in recurrence tree method
recursive call
#nodes
tree
row sum