tags:

views:

336

answers:

6

We always see operations on a (binary) tree has O(logn) worst case running time because of the tree height is logn. I wonder if we are told that an algorithm has running time as a function of logn, e.g m + nlogn, can we conclude it must involve an (augmented) tree?

EDIT: Thanks to your comments, I now realize divide-conquer and binary tree are so similar visually/conceptually. I had never made a connection between the two. But I think of a case where O(logn) is not a divide-conquer algo which involves a tree which has no property of a BST/AVL/red-black tree.

That's the disjoint set data structure with Find/Union operations, whose running time is O(N + MlogN), with N being the # of elements and M the number of Find operations.

Please let me know if I'm missing sth, but I cannot see how divide-conquer comes into play here. I just see in this (disjoint set) case that it has a tree with no BST property and a running time being a function of logN. So my question is about why/why not I can make a generalization from this case.

+7  A: 

No, you can also binary search a sorted array (for instance). But don't take my word for it http://en.wikipedia.org/wiki/Binary_search_algorithm

John Knoeller
In terms of bits, an array doesn't have left/right child pointers. But conceptually, doesn't the binary search algorithm define a left and right child for each node as it's visited?
Potatoswatter
Potatoswatter: I suppose the the algorithm does resemble a tree traversal if you look at it and squint real hard ;)
John Knoeller
Isn't array a way to implement binary tree? So conceptually it is still a tree.
Martin
@Martin: gotta disagree with that, an array is not at all a tree like structure.
John Knoeller
Binary search is a tree search on a Ahnentafel list, which is an abstraction of a binary tree: http://en.wikipedia.org/wiki/Binary_tree#Ahnentafel_list
carl
@cvondrick: thanks! now I know its name is Ahnentafel.
Martin
+3  A: 

As a counter example:

given array 'a' with length 'n'
y = 0
for x = 0 to log(length(a))
    y = y + 1
return y

The run time is O(log(n)), but no tree here!

carl
Uh. Actually, the runtime is `O(log(n))` if and only if the time it takes to calculate `log(n)` is at most `O(log(n))`.
Eduardo León
@Eduardo: Assume we have a massive lookup table then ;)
carl
Even so, the value of `n` is not really a good characterisation of the size of the input to this algorithm.
Dave
@Dave: How about an array of length n?
carl
@cvondrick, @Dave: If you have a massive lookup table, it's `O(n)`, in the general case. The only case in which you can pick a specific element of an array in `O(log(n))` time is if the array is sorted. And, then, you would have an implicit binary search tree. No wonder the name of the algorithm you would use would is "binary search".
Eduardo León
Now, `floor(log(n))` can be calculated in `log(n)` time: `int res = 0; while (n > base) { n /= base; ++res; }`
Eduardo León
A: 

Algorithms taking logarithmic time are commonly found in operations on binary trees.

Examples of O(logn):

  • Finding an item in a sorted array with a binary search or a balanced search tree.

  • Look up a value in a sorted input array by bisection.

Leniel Macaferi
+6  A: 

What you have is exactly backwards. O(lg N) generally means some sort of divide and conquer algorithm, and one common way of implementing divide and conquer is a binary tree. While binary trees are a substantial subset of all divide-and-conquer algorithms, the are a subset anyway.

In some cases, you can transform other divide and conquer algorithms fairly directly into binary trees (e.g. comments on another answer have already made an attempt at claiming a binary search is similar). Just for another obvious example, however, a multiway tree (e.g. a B-tree, B+ tree or B* tree), while clearly a tree is just as clearly not a binary tree.

Again, if you want to badly enough, you can stretch the point that a multiway tree can be represented as sort of a warped version of a binary tree. If you want to, you can probably stretch all the exceptions to the point of saying that all of them are (at least something like) binary trees. At least to me, however, all that does is make "binary tree" synonymous with "divide and conquer". In other words, all you accomplish is warping the vocabulary and essentially obliterating a term that's both distinct and useful.

Jerry Coffin
+2  A: 

Answer is no. Binary search of a sorted array is O(log(n)).

fastcodejava
A: 

As O(log(n)) is only an upper bound also all O(1) algorithms like function (a, b) return a+b; satisfy the condition.

But I have to agree all Theta(log(n)) algorithms kinda look like tree algorithms or at least can be abstracted to a tree.

OliverS