The Iteration Method - Solving Recurrences - Lecture Slides | CS 231, Study notes of Algorithms and Programming

Material Type: Notes; Class: Fundamental Algorithms; Subject: Computer Science; University: Wellesley College; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/16/2009

koofers-user-msw
koofers-user-msw 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Selection-Sort(1, A)
Selection-Sort(i,A)
if i<length(A) then
exchange A[i] A[min-Index(i,A)]
Selection-Sort(i+1,A)
NED
UNSORTED
UNS OR TED
UNSORTED
USORT
UNSOR TED
UNSOR TED
UNSOR TED
UNSOR TED
UNSOR TED
*Where min-Index(i, A) returns the index of an occurrence of the least element in A[i .. length[A]].
Solving Recurrences: The Iteration Method
1. Iteratively construct a recursion tree by unwinding
the recurrence equation.
2. Determine the cost of the entire tree by summing the
cost of the nodes.
1if n = 1
T(n) =
T(n-1) + nif n > 1
pf3
pf4
pf5

Partial preview of the text

Download The Iteration Method - Solving Recurrences - Lecture Slides | CS 231 and more Study notes Algorithms and Programming in PDF only on Docsity!

Selection-Sort( 1 , A)

Selection-Sort( i,A )

if exchange i < length(A) A[i] then ↔ A [min-Index( i,A )]

Selection-Sort( i+1,A)

D E N

U N S O R T E D

D N S O R T E U

D E S O R T N U

O R T S U

D E N O R T S U

D E N O R T S U

D E N O R S T U

D E N O R S T U

D E N O R S T U

*Where min-Index( i , A ) returns the index of an occurrence of the least element in A [ i .. length [ A ]].

Solving Recurrences: The Iteration Method

1. Iteratively construct a recursion tree by unwinding

the recurrence equation.

2. Determine the cost of the entire tree by summing the

cost of the nodes.

 1 if n = 1

T ( n ) = 

 T ( n -1) + n if n > 1

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

levels^ n

T(n) T(n-1) T(n-2)

n n ...

n-

n n- n-

*Unless stated to the contrary, we assume the base case is a constant.

T( n ) = T( n /2) + 1

lg(n) + 1levels

T(n) T(n/2) T(n/4)

*How many times can you divide n by 2 before you obtain a number less than or equal to 1?

Geometric Sums: Σ 1 ≤ i ≤ k xi

x - 1 xk +1^ - 1

Geometric Sums and Series in General

Σ 0 ≤ i ≤ k xi^ = x

k+1 - 1

x - 1

Σ 0 ≤ i ≤∞ xi^ = limk →∞ Σ 0 ≤ i ≤ k xi

= limk →∞

xk+1^ - 1

x - 1

x - 1

1 - x

Getting it Exact: T( n ) = T( n /2) + n

lg(n) + 1levels

T(n) T(n/2) T(n/4)

n n n/

n n/ n/

Σ 0 ≤ i ≤ lg n n /2 i^ = n Σ 0 ≤ i ≤ lg n 1/2 i

*Recall Σ 0 ≤ i ≤ k x i^ = x x^ k+1 - 1^ - 1

Merge-Sort Revisited

Merge-Sort( if lo < A, lo, hi)hi

then mid ← ( lo + hi )/2

Merge-Sort(Merge-Sort( A, lo, midA, mid + 1, hi ) )

Merge( A, lo, mid, hi )

Shifting Sands

tower( if n,n >0 start, finish )

then

temp ← 6-( start+finish )

tower( n-1, start, temp )

moveDisk( start, finish )

tower( n-1, temp, finish )

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

T(n) 1 ...

T(n-1)

level 1 level 2 level 3

level n

1 = 2^0

2 = 2^1

4 = 2^2

2 n-

T(n-1) T(n-2) (^) T(n-2) T(n-2) (^) T(n-2)

*Moral: It's better to divide than subtract.