LEC04_AnalyzingRecursiveCode
LEC04_AnalyzingRecursiveCode
● recursive definition:
base cases
Recursive cases
it will take O(n) time, because it spends a constant amount of time performing the nonrecursive part of each call. Moreover, we can also
see that the memory space used by the algorithm (in addition to the array) is also O(n), as we use a constant amount of memory space
for each of the n+1 frames in the trace at the time we make the final recursive call (with n = 0).
We note that whenever a recursive call is made, there will be two fewer elements in the relevant portion of the array. (See Figure 5.11.)
Eventually a base case is reached when the condition low < high fails, either because low == high in the case that n is odd, or because low ==
high + 1 in the case that n is even. The above argument implies that the recursive algorithm of Code Fragment 5.7 is guaranteed to terminate
𝑛
after a total of 1 + recursive calls. Because each call involves a constant amount of work, the entire process runs in O(n) time
2
2x =N
x = log2 N
2. Unrolling
then
If
then
If
○ Pro: Least complicated setup
○ Con: requires intuitive pattern matching T(1) = d
T(2) = 2T(2-1) + c = 2(d) + c
3. Tree Method T(3) = 2T(3-1) + c = 2(2(d) + c) + c = 4d + 3c
○ Pro: Plug and chug
f(4)
Master Theorem
If then
If then
If then
Master Theorem
a=1, b=2, c=0
𝑙𝑜𝑔21 = 0 == c
If then
Therefore
If then
If then 𝐹 𝑛 = Θ 𝑛0 log 𝑛 = Θ log 𝑛
If then
The log of a = c
If then ○ Recursive case evenly splits work between non recursive
If then work and passing along inputs to subsequent recursive
calls
○ Work is distributed across call stack
● a measures how many recursive calls are
triggered by each method instance The log of a > c case
○ Recursive case breaks inputs apart quickly and doesn’t do
● b measures the rate of change for input much non recursive work
● c measures the dominating term of the non
○ Most work happens near bottom of call stack
recursive work within the recursive method
● d measures the work done in the base case
Data Structures and Algorithms – Ashkezari 18
Understanding Master Theorem
More general form:
Master Theorem
If then
If then
If then
0 1
22 91
0 1 0 1 2
Pattern #2 – Constant size input and doing work 2 8 22 57 91
0 1 2 3 4
2 8 22 57 91
Master Theorem
If then
If then
If then
● Each call creates 2 more calls f(2) f(1) f(1) f(0) f(1) f(0)
● Each new call has a copy of the
input,
● Almost doubling the input at f(1) f(0)
each call
Pattern #3 – Doubling the Input
Pattern #1: Dividing the Input Pattern #2: Constant input + work Pattern #3: Exponential Input
Binary Search Θ(log2n) Merge Sort Θ(nlogn) Calculating Fibonacci Θ(2n)
Definition: Summation
= σ𝑛−1
𝑖=0 (1 + 1 + … + 1)
O(2𝑛 )
O(n)
O(n log n)
O(n log n)
O(n)
As a base case, when n = 1, the elements are trivially unique. For n ≥ 2, the elements are unique if and
only if the first n−1 elements are unique, the last n−1 items are unique, and the first and last elements are
different (as that is the only pair that was not already checked as a subcase).
Step 2: Calculate the work done or cost at each level and count total no of levels in recursion tree
Choose the longest path from root node to leaf node : n/20 -→ n/21 -→ n/22 -→ ……… -→ n/2k
At last level size of problem becomes 1 ➔ n/2k= 1
2k = n ➔ k = log2(n) تعداد:ارتفاع درخت
یالها در طوالنی ترین
مسیر
•Step 3: Count total number of nodes in the last level and calculate cost of last level
No. of nodes at level 0 = 20 = 1
No. of nodes at level 1 = 21 = 2
………
No. of nodes at level log2(n) (last level) = 2log2(n) = nlog2(2) = n
Step 2: Calculate the work done or cost at each level and count total no of levels in recursion tree
Choose the longest path from root node to leaf node :
(9/10)0n –> (9/10)1n –> (9/10)2n –> ……… –> (9/10)kn
Size of problem at last level = (9/10)kn
ارتفاع درخت
At last level size of problem becomes 1
(9/10)kn = 1 ➔ (9/10)k = 1/n ➔ k = log10/9(n)
عمق (سطح) درخت
Total no of levels in recursive tree = k +1 = log10/9(n) + 1
Data Structures and Algorithms – Ashkezari 48
Using Recursion Tree for complexity analysis
T(n) = T(n/10) + T(9n/10) + n
•Step 3: Count total number of nodes in the last level and calculate cost of last level
No. of nodes at level 0 = 20 = 1
No. of nodes at level 1 = 21 = 2
……………………
No. of nodes at level log10/9(n) =
Cost of sub problems at last level
note that it will be skewed tree as we have left child having n/10 value and right child
having 9n/10 value (division factor for the left child is much higher). So the last level
would have constant number of nodes or Θ(1) and there work done at the last level is
Θ(1) * Θ(1) = Θ(1)