Download presentation
Presentation is loading. Please wait.
Published byTheresa Chapman Modified over 5 years ago
1
Algorithms CSCI 235, Spring 2019 Lecture 6 Recurrences
2
Recurrence Equations A recurrence equation is a recursive function definition (i.e. a function that is defined in terms of itself). Examples: 1 if n = 1 n*f(n-1) if n > 1 Factorial: f(n) = 1 if n < 2 f(n-1) + f(n-2) for n>1 Fibonacci: f(n) = Analyzing recurrence equations: 1) Characterize running time of a given algorithms by a recurrence equation. (Usually worst case running time) 2) "Solve" the recurrence equation. Express the answer in asymptotic notation.
3
Recurrence for Divide and Conquer
When the problem is small enough, the solution is trivial. This is called the base case. What is "small enough"? Often, n = 1 or n<= c where c is a constant and the solution for n<=c takes constant time.
4
Recurrence equation for divide and conquer
Base Case: T(1) = Q(1) General Case: T(n) = [# of subproblems]T(Size of each subproblem) + [cost of divide and recombine] Suppose: # subproblems = a, size of subproblems = n/b Time to divide=D(n), Time to combine = C(n) T(n) = aT(n/b) + D(n) + C(n) for n > c Q(1) for n<=c
5
Example 1: Insert() Insert will insert the nth element into a previously sorted list of length (n-1) so that the resulting n element list is sorted. We will use this function later in a recursive version of insertion sort. Insert(A, n) if n > 1 if A[n] < A[n-1] swap(A, n, n-1) {switch A[n-1] and A[n]} Insert(A, n-1)
6
Equation for Insert() T(n) = T(n-1) + 1
Each subproblem is 1 less than original problem. Swap takes constant time (actually Q(1)) Expanding out the equation: T(n) = T(n-1) (we will work out the expansion in class)
7
Methods for solving recurrences
1. The substitution method. Make a good guess and show it works by induction. 2. The iteration method. Construct a recursion tree Find the sum of the costs of the nodes. 3. The master method. Learn 3 basic rules and apply them for certain types of recurrences. We will use the iteration method.
8
Recursion tree for Insert
T(n) = T(n-1) + 1 We will draw this in class.
9
Solution for Insert() Number of levels = n Cost of each level = 1
Total cost = (# levels)*(cost of each level) = n*1 =n T(n) = n = Q(n)
10
Insertion Sort InsertionSort(A, n) if n > 0
InsertionSort(A, n-1) {Sort subarray} Insert(A, n) {Insert nth element} T(n) = T(n -1) + cost of Insert(A, n) T(n) = T(n -1) + n
11
Recursion tree for InsertionSort
T(n) = T(n-1) + n We will draw this in class.
12
Solving Recursion tree for Insertion Sort
T(n) = n + (n-1) + (n-2) = ?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.