1 algorithms csci 235, fall 2015 lecture 6 recurrences

12
1 Algorithms CSCI 235, Fall 2015 Lecture 6 Recurrences

Upload: christina-melton

Post on 19-Jan-2016

216 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: 1 Algorithms CSCI 235, Fall 2015 Lecture 6 Recurrences

1

Algorithms

CSCI 235, Fall 2015Lecture 6Recurrences

Page 2: 1 Algorithms CSCI 235, Fall 2015 Lecture 6 Recurrences

2

Recurrence EquationsA recurrence equation is a recursive function definition (i.e. a function that is defined in terms of itself).

Examples:

{ 1 if n = 1n*f(n-1) if n > 1

1 if n < 2f(n-1) + f(n-2) for n>1

Factorial:

Fibonacci:

{

f(n) =

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:

Page 3: 1 Algorithms CSCI 235, Fall 2015 Lecture 6 Recurrences

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.

Page 4: 1 Algorithms CSCI 235, Fall 2015 Lecture 6 Recurrences

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

{

Page 5: 1 Algorithms CSCI 235, Fall 2015 Lecture 6 Recurrences

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

Page 6: 1 Algorithms CSCI 235, Fall 2015 Lecture 6 Recurrences

6

Equation for Insert()

T(n) = T(n-1) + 1

Each subproblem is 1 less than original problem. Swap takesconstant time (actually (1))

Expanding out the equation:T(n) = T(n-1) + 1 (we will work out the expansion in class)

Page 7: 1 Algorithms CSCI 235, Fall 2015 Lecture 6 Recurrences

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.

Page 8: 1 Algorithms CSCI 235, Fall 2015 Lecture 6 Recurrences

8

Recursion tree for Insert

T(n) = T(n-1) + 1

We will draw this in class.

Page 9: 1 Algorithms CSCI 235, Fall 2015 Lecture 6 Recurrences

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

Page 10: 1 Algorithms CSCI 235, Fall 2015 Lecture 6 Recurrences

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

Page 11: 1 Algorithms CSCI 235, Fall 2015 Lecture 6 Recurrences

11

Recursion tree for InsertionSort

T(n) = T(n-1) + n

We will draw this in class.

Page 12: 1 Algorithms CSCI 235, Fall 2015 Lecture 6 Recurrences

12

Solving Recursion tree for Insertion Sort

T(n) = n + (n-1) + (n-2) + ... + 2 + 1

T(n) = kk=1

n

= ?