Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Algorithms CSCI 235, Fall 2015 Lecture 6 Recurrences.

Similar presentations


Presentation on theme: "1 Algorithms CSCI 235, Fall 2015 Lecture 6 Recurrences."— Presentation transcript:

1 1 Algorithms CSCI 235, Fall 2015 Lecture 6 Recurrences

2 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 1 if n < 2 f(n-1) + f(n-2) for n>1 Factorial: Fibonacci: f(n) = 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. Analyzing recurrence equations:

3 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 4 Recurrence equation for divide and conquer Base Case: T(1) =  (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  (1) for n<=c

5 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 then if A[n] < A[n-1] then swap(A, n, n-1){switch A[n-1] and A[n]} Insert(A, n-1)

6 6 Equation for Insert() T(n) = T(n-1) + 1 Each subproblem is 1 less than original problem. Swap takes constant time (actually  (1)) Expanding out the equation: T(n) = T(n-1) + 1 (we will work out the expansion in class)

7 7 Methods for solving recurrences 1. The substitution method. Make a good guess and show it works by induction. 2. The iteration method. 1)Construct a recursion tree 2)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 8 Recursion tree for Insert T(n) = T(n-1) + 1 We will draw this in class.

9 9 Solution for Insert() Total cost = (# levels)*(cost of each level) = n*1 =n T(n) = n =  (n) Number of levels = n Cost of each level = 1

10 10 Insertion Sort {InsertionSort(A, n)} if n > 0 then 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 11 Recursion tree for InsertionSort T(n) = T(n-1) + n We will draw this in class.

12 12 Solving Recursion tree for Insertion Sort T(n) = n + (n-1) + (n-2) +... + 2 + 1 = ?


Download ppt "1 Algorithms CSCI 235, Fall 2015 Lecture 6 Recurrences."

Similar presentations


Ads by Google