0% found this document useful (0 votes)
27 views14 pages

Chap 3 - Time Complexity

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views14 pages

Chap 3 - Time Complexity

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

‭CSE 221: Algorithms [ARN] Ariyan Hossain‬

‭Time Complexity‬
‭Some Simplified Rules:‬

‭14‬
‭CSE 221: Algorithms [ARN] Ariyan Hossain‬

‭Some Series Rules:‬

‭Iterative Time Complexities:‬

‭15‬
‭CSE 221: Algorithms [ARN] Ariyan Hossain‬

‭16‬
‭CSE 221: Algorithms [ARN] Ariyan Hossain‬

‭Recursive Time Complexities:‬

‭ 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'.‬

‭Linear Recurrence Relations:‬

‭ 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.‬

‭To solve recurrences, there are 3 ways:‬

‭‬ 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‬

‭Master Theorem Method:‬

‭If the recurrence relation of an algorithm is of the form:‬

‭ here a ≥ 1, b > 1, and f(n) is a positive function that is‬‭Θ(n‬‭c‬‭)‭,‬ then the following is true:‬
w
‭[Note: O() and Θ() are interchangeable]‬

‭The steps for using the Master Theorem are as follows:‬

‭1. Determine the values of a, b, and c.‬

‭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 Θ(n‬‭c‭)‬ .‬
‭●‬ ‭There exists a base case that can be solved in constant time.‬

‭ . Compare the values of a and b‬‭c‬ ‭to determine which‬‭case of the Master Theorem should be‬
3
‭used.‬

‭23‬
‭CSE 221: Algorithms [ARN] Ariyan Hossain‬

‭24‬
‭CSE 221: Algorithms [ARN] Ariyan Hossain‬

‭Recurrence Tree Method:‬

‭ 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.‬

‭●‬ I‭f 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‬

You might also like