Make sure to have recursion and define big oh notation: Recursion & Big O Write
ID: 3839269 • Letter: M
Question
Make sure to have recursion and define big oh notation:
Recursion & Big O
Write a method called printStar(int n) which will print the following
when n=4:
****
***
**
*
*
**
***
****
What is the Big O for this function?
What I have:
public class stars
{
public static void main(String[]args)
{
pstars(4);
}
public static void pstars(int n)
{
//descending order
if(n>=1)
{
for(int i=1; i<=n; i++)
{
System.out.print("*");
}
System.out.println();
pstars(n-1);
}
else if()
{
for(int i=n; i>=n; i--)
{
System.out.print("*");
}
System.out.println();
pstars(n+1);
}
}
}
Explanation / Answer
The issue underneath showed up as AB Problem 3 on the 1996 AP exam, for which a C++ interpretation has been made.
Given a double tree, is it an inquiry tree?
To some extent An understudies are made a request to compose the capacity ValsLess:
struct Tree
{
int information;
Tree * left;
Tree * right;
Tree(int esteem, Tree * lchild, Tree * rchild)
: info(value), left(lchild), right(rchild)
{ }
};
bool ValsLess(Tree * t, int val)
/post: return genuine if and just if all qualities in t are not as much as val
In Part B, understudies are made a request to compose IsBST utilizing ValsLess and expecting that a comparative capacity ValsGreater exists. The arrangement is demonstrated as follows:
bool IsBST(Tree * t)
/postcondition: returns genuine if t speaks to a paired hunt
/tree containing no copy values;
/something else, returns false.
{
in the event that (t == NULL) return genuine;/purge tree is a hunt tree
return ValsLess(t->left,t->info) &&
ValsGreater(t->right,t->info) &&
IsBST(t->left) &&
IsBST(t->right);
}
Before proceeding with you ought to attempt to decide/figure/reason about what the intricacy of IsBST is for a n-hub tree. Accept that ValsLess and ValsGreater both keep running in O(n) time for a n-hub tree.
A capacity with comparable qualities
What is the asymptotic unpredictability of the capacity DoStuff demonstrated as follows. Why? Expect that the capacity Combine keeps running in O(n) time when |left-right| = n, i.e., when Combine is utilized to join n components in the vector a.
void DoStuff(apvector<int> and an, int left,int right)
/postcondition: a[left] <= ... <= a[right]
{
int mid = (left+right)/2;
on the off chance that (left < right)
{
DoStuff(a,left,mid);
DoStuff(a,mid+1,right);
Combine(a,left,mid,right);
}
}
You may perceive this capacity as a usage of Mergesort. You may likewise recall that the unpredictability of Mergesort is O(n log n) fo a n-component cluster/vector. How does this identify with the capacity IsBST?
We'll make a scientific definition:
The Recurrence Relation
Give T(n) a chance to be the ideal opportunity for DoStuff to execute on a n-component vector, i.e., when |left-right| = n. Take note of that the ideal opportunity for DoStuff to execute on a one component vector is O(1), consistent time.
At that point we have the accompanying relationship:
T(n) = 2 T(n/2) + O(n) [the O(n) is for Combine]
T(1) = O(1)
This relationship is known as a repeat connection on the grounds that the capacity T(..) happens on both sides of the = sign. This repeat connection totally portrays the capacity DoStuff, so on the off chance that we could fathom the repeat connection we would know the many-sided quality of DoStuff since T(n) is the ideal opportunity for DoStuff to execute.
Base Case
When you compose a repeat connection you should compose two conditions: one for the general case and one for the base case. These relate to the recursive capacity to which the repeat applies. The base case is frequently an O(1) operation, however it can be something else. In some repeat relations the base case includes contribution of size one, so we composed T(1) = O(1). Nonetheless, there are situations when the bse case has estimate zero. In such cases the base could would be T(0) = O(1).
How does this identify with the ideal opportunity for IsBST to execute? On the off chance that you take a gander at the code for IsBST you'll see that it has an indistinguishable shape from the capacity DoStuff, so that IsBST will have an indistinguishable repeat connection from DoStuff. This implies in the event that you acknowledge that DoStuff is an O(n log n) work, then IsBST is additionally an O(n log n) work.
Fathoming Recurrence Relations
You can really tackle the repeat connection given above. We'll draw how to do that here.
We'll compose n rather than O(n) in the principal line underneath in light of the fact that it makes the variable based math considerably less difficult.
T(n) = 2 T(n/2) + n
= 2 [2 T(n/4) + n/2] + n
= 4 T(n/4) + 2n
= 4 [2 T(n/8) + n/4] + 2n
= 8 T(n/8) + 3n
= (request that your class fill in this line, or you fill it in)
you ought to have composed: 16 T(n/16) + 4n
= 2k T(n/2k) + k n [this is the Eureka! line]
You could solicit understudies to fill in parts from the last line. Take note of that the last line is determined by observing an example - this is the Eureka/conviction-based move/hone with summing up scientific examples some portion of the issue.
We realize that T(1) = 1 and this is an approach to end the determination above. Specifically we need T(1) to show up on the correct hand side of the = sign. This implies we need:
n/2k = 1 OR n = 2k OR log2 n = k
Proceeding with the past deduction we get the accompanying since k = log2 n:
= 2k T(n/2k) + k n
= 2log2 n T(1) + (log2n) n
= n + n log2 n [remember that T(1) = 1]
= O(n log n)
So we've tackled the repeat connection and its answer is the thing that we "knew" it would be. To make this a formal confirmation you would need to utilize acceptance to demonstrate that O(n log n) is the answer for the given repeat connection, however the "attachment and chug" strategy demonstrated to above shows industry standards to infer the arrangement - the consequent check this is the arrangement is something that can be left to a more propelled calculations class.
Repeat Relations to Remember
My recommendation is to do what we do in our classes: demonstrate understudies the "huge five" repeat relations beneath and request that they recall what calculations these are related with. We request that our understudies illuminate other repeat relations, yet we truly need them to reason about recursive capacities utilizing the repeat relations underneath more than knowing how to fathom any given repeat connection. We additionally need understudies to have the capacity to get a repeat connection from a recursive capacity - more on that later.
T(n) = T(n/2) + O(1)
T(n) = T(n-1) + O(1)
T(n) = 2 T(n/2) + O(1)
T(n) = T(n-1) + O(n)
T(n) = 2 T(n/2) + O(n)
Before proceeding, or with your class, attempt to fit each of the above repeat relations to a calculation and in this way to its huge Oh arrangement. We'll demonstrate what these are beneath. Obviously for practice you can request that your understudies determine the answers for the repeat relations utilizing the attachment and-chug technique.
Repeat
Calculation
Enormous Oh Solution
T(n) = T(n/2) + O(1)
Parallel Search
O(log n)
T(n) = T(n-1) + O(1)
Consecutive Search
O(n)
T(n) = 2 T(n/2) + O(1)
tree traversal
O(n)
T(n) = T(n-1) + O(n)
Choice Sort (other n2 sorts)
O(n2)
T(n) = 2 T(n/2) + O(n)
Mergesort (normal case Quicksort)
O(n log n)
Hone Problem
Discover the kth biggest component in a vector where the kth biggest is more noteworthy than k components so that the Oth biggest is the littlest component, the third biggest is more noteworthy than three components (will have list 3 if the vector is sorted) et cetera. Accept that there are no copy components in the vector to make the arrangement less demanding.
The arrangement underneath effectively takes care of the issue. It makes a call to the parcel work from Quicksort. Accept that the segment work keeps running in O(n) time for a n-component vector/vector-portion. For culmination we'll incorporate a segment work toward the finish of this archive.
For a n-component vector a the call FindKth(a,0,n-1,k) gives back the kth component in a:
int FindKth(const apvector<int> and an, int left, int right)
/post: give back the k-th component in a
{
int turn = Partition(a,left,right)
in the event that (rotate == k) return a[k];
else if (k < rotate) return FindKth(a, left, turn 1, k);
else return FindKth(a,pivot+1, right, k);
}
What is the enormous Oh many-sided quality of FindKth in the most pessimistic scenario and in the normal case. Since it's hard to reason definitely about normal case without more scientific refinement than we need to utilize, expect that things act pleasantly in the normal case. Things being what they are, this gives the correct response for most meanings of normal case. In later courses we can characterize all the more absolutely what normal case implies
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.