CSC 41/513: Intro to Algorithms Linear-Time Sorting Algorithms.
CSC 413/513: Intro to Algorithms
description
Transcript of CSC 413/513: Intro to Algorithms
![Page 1: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/1.jpg)
CSC 413/513: Intro to Algorithms
Merge Sort
Solving Recurrences
![Page 2: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/2.jpg)
Updates
Homework 1 posted Due on Sep-01
Office hours have been updated MW: 1:00-2:15 Tues: 1:00-2:30 Thurs: 2:00-3:00
Note taker?
![Page 3: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/3.jpg)
Review: Asymptotic Notation
Upper Bound Notation: f(n) is O(g(n)) if there exist positive constants c
and n0 such that f(n) c g(n) for all n n0
Formally, O(g(n)) = { f(n): positive constants c and n0 such that f(n) c g(n) n n0
Big O fact: A polynomial of degree k is O(nk)
![Page 4: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/4.jpg)
Review: Asymptotic Notation
Asymptotic lower bound: f(n) is (g(n)) if positive constants c and n0 such
that 0 cg(n) f(n) n n0
Asymptotic tight bound: f(n) is (g(n)) if positive constants c1, c2, and n0
such that c1 g(n) f(n) c2 g(n) n n0
f(n) = (g(n)) if and only if f(n) = O(g(n)) AND f(n) = (g(n))
![Page 5: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/5.jpg)
Other Asymptotic Notations
A function f(n) is o(g(n)) if for any positive constant c, n0 such that
f(n) < c g(n) n n0
A function f(n) is (g(n)) if for any positive constant c, n0 such that
c g(n) < f(n) n n0
Intuitively, o() is like < O() is like
() is like > () is like
() is like =
![Page 6: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/6.jpg)
How comfortable are you with recursion?
![Page 7: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/7.jpg)
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);
}
}
// Merge() takes two sorted subarrays of A and
// merges them into a single sorted subarray of A
// (how long should this take?)
![Page 8: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/8.jpg)
How Merge works
![Page 9: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/9.jpg)
Merge Sort: Example
Show MergeSort() running on the array
A = {10, 5, 7, 6, 1, 4, 8, 3, 2, 9};
![Page 10: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/10.jpg)
Neat little example from the book
![Page 11: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/11.jpg)
Analysis of Merge Sort
Statement Effort
So T(n) = (1) when n = 1, and
2T(n/2) + (n) when n > 1 So what (more succinctly) is T(n)?
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) }}
![Page 12: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/12.jpg)
Recurrences
The expression:
is a recurrence. Recurrence: an equation that describes a function
in terms of itself on smaller inputs
1
22
1
)(ncn
nT
nc
nT
![Page 13: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/13.jpg)
Recurrence Examples
0
0
)1(
0)(
n
n
nscns
0)1(
00)(
nnsn
nns
1
22
1
)(nc
nT
nc
nT
1
1
)(
ncnb
naT
nc
nT
![Page 14: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/14.jpg)
Solving Recurrences
Substitution method Iteration method Master method
![Page 15: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/15.jpg)
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 ???
Tree method often used
![Page 16: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/16.jpg)
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 ???
![Page 17: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/17.jpg)
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)
![Page 18: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/18.jpg)
Substitution examples
T(n) = 2T(n/2) + kn, T(1)=c’ (e.g., merge sort) Guess T(n) <= c.nlgn does not work Guess T(n) <= c.nlgn +dn works for d>=c’, c>=k
T(n) = 2T(n/2) + k, T(1)=c’ (e.g., D&C search) Guess T(n) <= cn does not work Guess T(n) <= cn-d works for d>=k, c>=d+c’
T(n) = 2T(√n) + k.lg n Change variable first
Must prove the exact form guessed; otherwise revise your guess
![Page 19: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/19.jpg)
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
![Page 20: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/20.jpg)
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)1(
00)(
nnsc
nns
![Page 21: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/21.jpg)
So far for n >= k we have s(n) = ck + s(n-k)
What if n=k? s(n) = cn + s(0) = cn
0)1(
00)(
nnsc
nns
![Page 22: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/22.jpg)
So far for n >= k we have s(n) = ck + s(n-k)
What if k = n? s(n) = cn + s(0) = cn
So
Thus in general s(n) = cn
0)1(
00)(
nnsc
nns
0)1(
00)(
nnsc
nns
![Page 23: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/23.jpg)
s(n)
= n + s(n-1)
= n + n-1 + s(n-2)
= n + n-1 + n-2 + s(n-3)
= n + n-1 + n-2 + n-3 + s(n-4)
= …
= n + n-1 + n-2 + n-3 + … + n-(k-1) + s(n-k)
0)1(
00)(
nnsn
nns
![Page 24: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/24.jpg)
s(n)
= n + s(n-1)
= n + n-1 + s(n-2)
= n + n-1 + n-2 + s(n-3)
= n + n-1 + n-2 + n-3 + s(n-4)
= …
= n + n-1 + n-2 + n-3 + … + n-(k-1) + s(n-k)
=
0)1(
00)(
nnsn
nns
)(1
knsin
kni
![Page 25: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/25.jpg)
So far for n >= k we have
0)1(
00)(
nnsn
nns
)(1
knsin
kni
![Page 26: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/26.jpg)
So far for n >= k we have
What if k = n?
0)1(
00)(
nnsn
nns
)(1
knsin
kni
![Page 27: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/27.jpg)
So far for n >= k we have
What if k = n?
0)1(
00)(
nnsn
nns
)(1
knsin
kni
2
10)0(
11
nnisi
n
i
n
i
![Page 28: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/28.jpg)
So far for n >= k we have
What if k = n?
Thus in general
0)1(
00)(
nnsn
nns
)(1
knsin
kni
2
10)0(
11
nnisi
n
i
n
i
2
1)(
nnns
![Page 29: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/29.jpg)
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
1
22
1)( nc
nT
ncnT
![Page 30: CSC 413/513: Intro to Algorithms](https://reader036.fdocuments.in/reader036/viewer/2022070411/56814863550346895db56fad/html5/thumbnails/30.jpg)
So far for n > 2k-1 we have T(n) = 2kT(n/2k) + (2k - 1)c
What if n=2k, i.e., k = lg n? T(n) = 2lg n T(n/2lg n) + (2lg n - 1)c
= n T(n/n) + (n - 1)c
= n T(1) + (n-1)c
= nc + (n-1)c = (2n - 1)c
1
22
1)( nc
nT
ncnT