Chap 3 - Time Complexity
Chap 3 - Time Complexity
Time Complexity
Some Simplified Rules:
14
CSE 221: Algorithms [ARN] Ariyan Hossain
15
CSE 221: Algorithms [ARN] Ariyan Hossain
16
CSE 221: Algorithms [ARN] Ariyan Hossain
ith an iterative algorithm, we can analyze loops to get an accurate estimate of time complexity
W
in most cases. However, this becomes much trickier if recursion is involved.
ecursive Function - A recursive function has two parts: the base case, and the recursive case.
R
The recursive case is when the function calls itself. The base case is when the function doesn’t
call itself again so it doesn’t go into an infinite loop.
17
CSE 221: Algorithms [ARN] Ariyan Hossain
def position(n):
if (n==0):
return 1
else:
return 1+ position(n-1)
ecursion is used when it makes the solution clearer. There’s no performance benefit to using
R
recursion; in fact, loops are sometimes better for performance.
Recurrences:
reviously, we defined the runtime of an algorithm as a function T(n) of its input size n. We will
P
still do this when dealing with recursive functions. However, because a recursive algorithm calls
itself with smaller input size, we will write T(n) as a recurrence relation, or an equation that
defines the runtime of a problem in terms of the runtime of a recursive call on smaller input.
recurrence is an equation that describes a function in terms of its value on other, typically
A
smaller, arguments. Recurrences go hand in hand with the divide-and-conquer method because
they give us a natural way to characterize the running times of recursive algorithms
mathematically.
n "algorithmic recurrence" is just a fancy way of saying there's a formula that tells you how
A
much time your divide-and-conquer algorithm takes to solve a problem of size 'n'.
ere, T(n) represents the runtime of factorial(n), T(n− 1) represents the runtime of the recursive
H
call to factorial(n - 1), and the additional Θ(1) term represents the constant work we do outside
the recursive call. Since the recurrence relation will be used to calculate time complexities,
having an exact number for this constant term does not matter.
18
CSE 221: Algorithms [ARN] Ariyan Hossain
o solve this problem, first convert the function into a recurrence relation. This recurrence
T
relation is:
rom now on, we will substitute Θ(1) with the constant 1. This simplifies our math without
F
changing the result of our analysis.
S
● ubstitution Method
● Master Theorem Method
● Recurrence Tree Method
19
CSE 221: Algorithms [ARN] Ariyan Hossain
Substitution Method:
20
CSE 221: Algorithms [ARN] Ariyan Hossain
21
CSE 221: Algorithms [ARN] Ariyan Hossain
22
CSE 221: Algorithms [ARN] Ariyan Hossain
here a ≥ 1, b > 1, and f(n) is a positive function that isΘ(nc), then the following is true:
w
[Note: O() and Θ() are interchangeable]
2. Make sure that the Master Theorem can be used on the recurrence relation.
T
● he coefficient of the recursive call, a, must be at least one.
● The argument of the recursive call must be divided by some number, b, that is larger
than one.
● The function f(n) must be an asymptotically positive function with complexity Θ(nc) .
● There exists a base case that can be solved in constant time.
. Compare the values of a and bc to determine whichcase of the Master Theorem should be
3
used.
23
CSE 221: Algorithms [ARN] Ariyan Hossain
24
CSE 221: Algorithms [ARN] Ariyan Hossain
ecursion trees give us a way to visualize the total work that is done by a recursive algorithm.
R
Each node of a recurrence tree represents a single recursive call, and it stores the work that is
done at that recursive call.
To get the total cost or work done, we can sum the work done by each node in the tree.
● If the cost of nodes in each level is the same, we can get the total cost by multiplying the
height of the tree and the cost of each level.
● If the cost of nodes in each level is different, we generally get the total sum by using
infinite geometric sequence by adding the cost of all levels.
25
CSE 221: Algorithms [ARN] Ariyan Hossain
26
CSE 221: Algorithms [ARN] Ariyan Hossain
27