lecture3_recurrences_mergesort_iterationMethod
lecture3_recurrences_mergesort_iterationMethod
Computer Algorithms
Merge Sort
Solving Recurrences
Iteration Method
Merge Sort (cont’d)
• Example with a hand of seven cards
Merge Sort
• What makes this method more
effective than simple insertion sort?
– merging two piles is a very simple
operation
– only need to look at the two cards
currently on the top of each pile
– no need to look deeper into either
group
• In this example:
– compare 2 with 5, pick up the 2
– compare 5 with 7, pick up the 5
– ....
Merge Sort
MergeSort(A, left, right) {
if (left < right) {
mid = floor((left + right) / 2);
MergeSort(A, left, mid);
MergeSort(A, mid+1, right);
Merge(A, left, mid, right);
}
}
A = {10, 5, 7, 6, 1, 4, 8, 3, 2, 9};
Analysis of Merge Sort
Statement Effort
MergeSort(A, left, right) { T(n)
if (left < right) { (1)
mid = floor((left + right) / 2); (1)
MergeSort(A, left, mid); T(n/2)
MergeSort(A, mid+1, right); T(n/2)
Merge(A, left, mid, right); (n)
}
}
• So T(n) = (1) when n = 1, and
2T(n/2) + (n) when n > 1
• So what (more succinctly) is T(n)?
Recurrences
• The expression:
c n 1
T ( n)
2T cn n 1
n
2
is a recurrence.
– Recurrence: an equation that describes a function
in terms of its value on smaller functions
Recurrence Examples
0 n0 0 n0
s ( n) s ( n)
c s (n 1) n 0 n s (n 1) n 0
n 1
c c n 1
T ( n) T ( n)
2T c n 1
n n
2 aT cn n 1
b
Solving Recurrences
• Substitution method
• Iteration method
• Master method
Solving Recurrences
• The substitution method (CLR 4.1)
– A.k.a. the “making a good guess method”
– Guess the form of the answer, then use induction
to find the constants and show that solution
works
– Examples:
• T(n) = 2T(n/2) + (n) T(n) = (n lg n)
• T(n) = 2T(n/2) + n ???
Solving Recurrences
• The substitution method (CLR 4.1)
– A.k.a. the “making a good guess method”
– Guess the form of the answer, then use induction
to find the constants and show that solution
works
– Examples:
• T(n) = 2T(n/2) + (n) T(n) = (n lg n)
• T(n) = 2T(n/2) + n T(n) = (n lg n)
• T(n) = 2T(n/2 )+ 17) + n ???
Solving Recurrences
• The substitution method (CLR 4.1)
– A.k.a. the “making a good guess method”
– Guess the form of the answer, then use induction
to find the constants and show that solution
works
– Examples:
• T(n) = 2T(n/2) + (n) T(n) = (n lg n)
• T(n) = 2T(n/2) + n T(n) = (n lg n)
• T(n) = 2T(n/2+ 17) + n (n lg n)
Solving Recurrences
• Another option is what the book calls the
“iteration method”
– Expand the recurrence
– Work some algebra to express as a summation
– Evaluate the summation
• We will show several examples
0 n0
s ( n)
c s (n 1) n 0
• s(n) =
c + s(n-1)
c + c + s(n-2)
2c + s(n-2)
2c + c + s(n-3)
3c + s(n-3)
…
kc + s(n-k) = ck + s(n-k)
0 n0
s ( n)
c s (n 1) n 0
= i
i n k 1
s(n k )
0 n0
s ( n)
n s (n 1) n 0
• So far for n >= k we have
n
i
i n k 1
s(n k )
0 n0
s ( n)
n s (n 1) n 0
• So far for n >= k we have
n
i
i n k 1
s(n k )
• What if k = n?
0 n0
s ( n)
n s (n 1) n 0
• So far for n >= k we have
n
i
i n k 1
s(n k )
• What if k = n?
n n
n 1
i
i 1
s ( 0) i 0 n
i 1 2
0 n0
s ( n)
n s (n 1) n 0
• So far for n >= k we have
n
i
i n k 1
s(n k )
• What if k = n?
n n
n 1
i
i 1
s ( 0) i 0 n
i 1 2
• Thus in general
n 1
s ( n) n
2
c n 1
T (n) 2T n c n 1
2
• T(n) =
2T(n/2) + c
2(2T(n/2/2) + c) + c
22T(n/22) + 2c + c
22(2T(n/22/2) + c) + 3c
23T(n/23) + 4c + 3c
23T(n/23) + 7c
23(2T(n/23/2) + c) + 7c
24T(n/24) + 15c
…
2kT(n/2k) + (2k - 1)c
c n 1
n
T (n) 2T
c n 1
2