It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
How can I check that my binary tree doens't contain duplicates?
Do you have an algorithm?
Please write the pseudocode
EDIT: or (better) using math property.
This is my Tree over an alphabet A: (a,U,V) where a \in A and U and V is respectively the left child and the right child.
If the tree is ordered like a binary search tree using a relation of ordering < (riflessive, antisymmetric, transitive, total) I can express that T=(a,U,V) is ordered without duplicates IFONLY \forall u \in flatten(U) and \forall v \in flatten(V). u < a< v and u \neq a and a \neq v and i've to check the property recursively for the tree U and tree tree V. But the question is: How can i check that the tree doesn't contains duplicates if the tree is not ordered (or is not a binary search tree)?
Apologise for my first not clear question
EDIT2: flatten is the function that return the set of the elements of the parameter tree
There is an O(n log n) obvious way, you collect the elements, sort the list and find duplicates. The problem is at least as hard as finding duplicates in an array see this. Therefore, if you rely only on comparisons, you cannot achieve better than O(n log n).
If the input is bounded, say, between the values 1 to 1000 then you can use an array, which we call A.
Go over each node with value val and let A[val]=1.
This way you mark all the elements that you encounter.
This one runs in O(n) and uses additional 1000 elements of space.
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Given n coins, some of which are heavier, algorithm for finding the number of heavy coins using O(log^2 n) weighings. Note that all heavy coins have the same weight and all the light ones share the same weight too.
You are given a balance using which you can compare the weights of two disjoint subsets of coins. Note that the balance only indicates which subset is heavier, or whether they have equal weights, and not the absolute weights.
I won't give away the whole answer, but I'll help you break it down.
Find a O(log(n)) algorithm to find a single heavy coin.
Find a O(log(n)) algorithm to split a set into two sets with equal number of heavy and light counts plus up to two leftovers (for when there are not even amounts of each).
Combine algorithms #1 and #2.
Hints:
Algorithm #1 is independent of algorithm #2.
O(log(n)) hints at binary search
How might you end up with O(log^2(n)) with two O(log(n)) algorithms?
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
i and my friend discussed following algorithm problem,
"Describe a recursive algorithm for finding the maximum element in an array A of n
elements. What is your running time and space usage?"
we conclusioned that it has O(n) time usage. Accoriding to this statement, F(n) =compare A[n] with F(n-1), at base case of recursion, it compares A[0] and A[1], then returns bigger one, which will compare with A[2]. as recursion proceeds, finally in the end, it will return maximum element in an array.
each n time recursions, it compares only one time so finally we guessed it has O(n) time usage.
my question is we aren't sure with our solution, so we want any other comments about this algorithm and our solution. thank you.
You approach for finding the time complexity is fine if the array contains integers. In case of numbers, comparing two numbers can be considered to be a unit operation. And while iterating over the array, to find the maximum value, this operations is performed n times. Hence O(n).
But if the array contains complex datatypes, say string, then comparing two strings cannot be considered as a unit operation. To compare string you may have to iterate over each character of the string. In this case the time complexity of the algorithm may also start depending on the length of the strings in your array. Similarly for other datatypes, comparing two objects may not be a unit operation. But in your case, looks like the array contains numbers, so your are good.
Yes. you are correct, it is infact O(n). How you can do quite simply is,
The basic operation of the algorithm is the comparison. And in step of the recursion the comparison is done only once.
So you can say
m(n) = m(n-1) + 1
m(n-1) = m(n-2) + 1 + 1
m(n-2) = m(n-3) + 2 + 1
generalizing we get
m(n-i) = m(n-1-i) + i + 1
now in your basecase, you would be doing no comparisons (basecase is no elements left, so you return the current largest). you can write this as
m(0) = 1
now substituting in the recurrence equation to get the base case, let i = n-1
we get
m(n) = m(0) + n - 1 + 1
but m(0) = 0
So we get
m(n) = n
Hence your algorithm is O(n). There are other ways to prove this too. And even without a mathematical proof you can logically say your algorithm is O(n) since it does only one basic operation every recursive step, and the algorithm will always recurse n steps irrespective of the input.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have been reading on here for a while, but this is the first time I have posted so I apologize if this isn't tagged correctly or anything. Anyway I am stuck on a problem which I explain below.
In the problem my job is to arrange n wifi routers to minimize the longest distance between any house and the nearest wifi router. I can assume that the houses are arranged in a one dimensional space. I am given the positions of the houses as a distance from an initial point and the positions are given in sorted order. Additionally I must solve this problem in O(m log L) where m is the number of houses and L is the maximum position that can be given.
I have tried to figure this out, but none of the algorithms that I come up with can solve it in the complexity required. Thanks for any hints on how I would go about solving this.
Here is a hint.
It is easy to write a O(m) function that takes an upper bound on distance, and tells you the minimum number of needed routers to make sure that no house is above that distance from a router.
Now search for the largest distance that uses no more than n routers.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I was looking at plenty of tree data structures and it is really confusing. Like I understand the basic Binary Trees(also its numerous implementations like the BST Red black trees etc)
But what i really need is some information on N'ary trees.
I need to study various types of N' ary trees and also their performance comparisons.
The only N' ary tree I have seen till now is B+ tree. I need to know which is the fastest N' Ary tree. i.e the most optimized time complexity wise, space complexity is no issue.
Generally, making something a K-ary tree, , does not give any asymptotic advantage over a binary tree (). For example, searching a a balanced binary tree can be done in time. Searching a balanced k-ary tree, will give you . Assuming is a constant, and for any other base, are equivalent asymptotically (the growth rates will be the same for large ). That is, is equivalent to for any and . Therefore, .
Practically, however, a k-ary tree might result in better memory access patterns, because each node contains nodes next to eachother, which means the height of the tree is shorter (wikipedia gives the height, , for a complete k-ary tree as , which is asymptotically the same for any constant ), and traversal might jump around less as well, since leaf nodes can contain multiple in-order keys.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
1) Given an array of integers (negative and positive) - what is the most efficient algorithm to return the max consecutive sum.
a) I thought to solve this with Dynamic Programing, but complexity is O(n^2). Is there another way?
b) What if we were given a infinite input of integers. Is there a way to output the current max consecutive sum? I guess not.
2) Given: an array of segments[start,end] (can elapse) ordered ascending by start point,
and a point.
What is the most efficient algorithm to return a segment that contains this point?/all segments that contain this point?
I thought to use binarySearch to hit the first segment that starts before this point the than trying to traverse right and left.
Any other idea ?
For 1) There is an algorithm that's working in O(n)
For 2) I think your approach is not bad (as long as you can't assume ordering w.r.t. ending points)
1) As long as the sum doesn't drop below zero, it's always better to continue with the consecutive summation. So you just pass through the array once (i.e. you have a linear runtime algorithm) from left to right and remember the current consecutive summation and the maximum consecutive summation so far, updating it whenever the current sum gets bigger then the max sum.
So at any point at of the array traversal, you can say what the max sum so far is. Hence you can use this algorithm for an (infinite) input stream, too.
2) Yes, binary search sounds good. But if I understand the question correctly, you can start with the right-most segment (that starts closest to the point) and then just traverse the segments to the left. Of course, the worst case runtime is still linear in the number of segments, but the average should be logarithmic.