Given a binary tree, is it a search tree?
In part A students are asked to write the function ValsLess: =
In Part B, students are asked to write IsBST using = ValsLess=20 and assuming that a similar function ValsGreater exists. The = solution=20 is shown below:=20
Before continuing you should try to determine/guess/reason about what = the=20 complexity of IsBST is for an n-node tree. Assume that = ValsLess and ValsGreater both run in O(n) time for an=20 n-node tree.=20
What is the asymptotic complexity of the function DoStuff = shown=20 below. Why? Assume that the function Combine runs in = O(n) time=20 when |left-right| =3D n, i.e., when Combine is used to = combine n=20 elements in the vector a.
You may recognize this function as an implementation of Mergesort. = You may=20 also remember that the complexity of Mergesort is O(n log n) fo = an=20 n-element array/vector. How does this relate to the function=20 IsBST?=20
We'll make a mathematical definition:=20
Then we have the following relationship:
T(n) =3D 2 = T(n/2) + O(n) [the O(n) is for Combine] T(1) =3D O(1)
This relationship is called a recurrence relation because = the=20 function T(..) occurs on both sides of the =3D sign. This = recurrence=20 relation completely describes the function DoStuff, so if we = could=20 solve the recurrence relation we would know the complexity of = DoStuff=20 since T(n) is the time for DoStuff to execute.=20
How does this relate to the time for IsBST to execute? If = you look=20 carefully at the code for IsBST you'll see that it has the same = form as=20 the function DoStuff, so that IsBST will have the same = recurrence relation as DoStuff. This means that if you accept = that=20 DoStuff is an O(n log n) function, then IsBST = is also=20 an O(n log n) function.
We'll write n instead of O(n) in the first line = below=20 because it makes the algebra much simpler.
T(n) =3D 2 T(n/2) + n =20 =3D 2 [2 T(n/4) + = n/2] + n =20 =3D 4 T(n/4) + 2n =3D 4 [2 T(n/8) + = n/4] + 2n =3D 8 T(n/8) + 3n =3D (ask your class to fill in this line, or you fill it = in) you should have written: 16 T(n/16) + 4n =3D 2^{k} T(n/2^{k}) + k n [this is the = Eureka! line] =20
You could ask students to fill in parts of the last line. Note that = the last=20 line is derived by seeing a pattern --- this is the Eureka/leap of=20 faith/practice with generalizing mathematical patterns part of the = problem.=20
We know that T(1) =3D 1 and this is a way to end the = derivation above.=20 In particular we want T(1) to appear on the right hand side of = the =3D=20 sign. This means we want:=20
n/2^{k} =3D 1 OR n =3D 2^{k} = OR log_{2} n =3D k
Continuing with the previous derivation we get the following since = k =3D=20 log_{2} n:
=3D 2^{k} T(n/2^{k}) + k n =3D 2^{log2 n} T(1) + (log_{2}n) n =3D n + n log_{2} n [remember that T(1) =3D = 1] =3D O(n log n)
So we've solved the recurrence relation and its solution is what we = "knew" it=20 would be. To make this a formal proof you would need to use induction to = show=20 that O(n log n) is the solution to the given recurrence relation, but = the "plug=20 and chug" method shown above shows how to derive the solution --- the = subsequent=20 verification that this is the solution is something that can be left to = a more=20 advanced algorithms class.=20
Before continuing, or with your class, try to fit each of the above=20 recurrence relations to an algorithm and thus to its big-Oh solution. = We'll show=20 what these are below. Of course for practice you can ask your students = to derive=20 the solutions to the recurrence relations using the plug-and-chug = method.=20
Recurrence=20 | Algorithm=20 | Big-Oh Solution=20 |
---|---|---|
T(n) =3D T(n/2) + O(1) | Binary Search=20 | O(log n)=20 |
T(n) =3D T(n-1) + O(1) | Sequential Search=20 | O(n)=20 |
T(n) =3D 2 T(n/2) + O(1) | tree traversal=20 | O(n) |
T(n) =3D T(n-1) + O(n) | Selection Sort (other n^{2} sorts)=20 | O(n^{2})=20 |
T(n) =3D 2 T(n/2) + O(n) | Mergesort (average case Quicksort)=20 | O(n log n) |
The solution below correctly solves the problem. It makes a call to = the=20 partition function from Quicksort. Assume that the partition function = runs in=20 O(n) time for an n-element vector/vector-segment. For = completeness=20 we'll include a partition function at the end of this document.=20
For an n-element vector a the call=20 FindKth(a,0,n-1,k) returns the k^{th} element in = a:=20
What is the big-Oh complexity of FindKth in the worst-case = and in=20 the average-case. Since it's difficult to reason precisely about = average-case=20 without more mathematical sophistication than we want to use, assume = that things=20 behave nicely in the average-case. As it turns out, this gives the right = answer=20 for most definitions of average-case. In later courses we can define = more=20 precisely what average case means.=20
If T(n) is the time for FindKth to execute for an=20 n-element vector, the recurrence relation in the worst-case is: =
Where the O(n) term comes from Partition. Note that = there=20 is only one recursive call made in FindKth.=20
This is one of the big-five recurrences, it's solution is=20 O(n^{2}) so that FindKth in the worst-case is = an=20 n^{2} function.=20
The recurrence relation for the average case is
This isn't one of the "big five", so you'll have to solve it yourself = to=20 determine the average-case complexity of FindKth. Hint: it's = pretty=20 good.=20