What's the most optimized algorithm (approach) for traversing a tree, ever? [closed] - algorithm

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
As it's stated in wikipedia there are multiple algorithms for traversing a tree data structure. But some of the algorithms are kind of combinations of the others, like Bidirectional search which is almost useful for other graphs rather than trees. But with a tree we almost have no idea of the end of the tree and we can only start from the root or from its children.
In this case we might be able to incorporate the multiprocessing or multithreading in search process. But I couldn't find any comprehensive approach that's described this.
Now my question is that basically what's the most optimized way of traversing a tree when we don't have access to the whole data structure (to be able to index them, etc. like a file directory)?

The most optimized algorithm is usually the one optimized for specific usecase and platform.
It does not matter whether you do inorder, preorder or postorder. Or whether you do DFS or BFS.
What matters is:
How big is the tree? Does it fit into memory?
How deep is the tree? Can you use recursion, or do you have to use explicit separate stack?
How do you find children of the node. Do you have to access harddrive/network?
What do you want to do with the node after you find in traversal. If this operation is long enough, optimizing traversal is not worth it.
How do you share data between threads?
How are the nodes in the tree distributed? Does it resemble even distribution, or are there some very long and some very short branches?
How big are the node keys (this influences data locality and how much data you can fit into one L1/L2 cache line)?

Try in order traversing of Binary Search tree. The complexity of search is nlogn and traverse is n, which is considered to be the best.
Ex: http://www.geeksforgeeks.org/binary-search-tree-set-1-search-and-insertion/

Related

Why is a complete binary tree most suited for heap implementation? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 9 months ago.
Improve this question
I could not figure out why complete binary tree is most suited for mplementing heap? why we cannot use full binary tree? WHy complete binary tree is the most suited for heap implementation?
Full binary trees for heaps?
A full binary tree is not necessarily well-balanced. For instance, this is a full binary tree:
1
/ \
2 3
/ \
4 5
/ \
6 7
/ \
8 9
/ \
10 11
In general, a full binary tree where any right child is also a leaf, has a height that is O(𝑛), more precisely, (π‘›βˆ’1)/2. This will be problematic for heaps, which rely on the tree being well balanced to keep its insert/delete operations within a time complexity of O(log𝑛).
Secondly, full binary trees always have an odd number of nodes (except when they are empty). This already makes them impractical, as obviously heaps should be able to have even sizes too.
Other alternative
However, binary heaps do not have to be complete binary trees. That is only required when their implementation is the well-known array-based one. But one could also implement a binary heap with an AVL tree, which is not necessarily a complete binary tree, but which still keeps the tree balanced and will have the same time complexities for the heap operations. But since the overhead of the pointer management is larger than working with indices in an array, the array representation leads to faster operations.
Why complete?
The choice for a complete binary tree comes into play when the implementation is array-based and not an explicit node-pointer representation. When you fill an array with values in level-order, and don't allow for gaps in the array (unused slots), then it follows that the tree is complete. Although you could imagine an array that allows for gaps, this would be an inferior choice, as it wastes space, with no gain to compensate for it.
First of all, it is not possible to create a heap structure without it being tightly packed. Every item in the array has a position in the binary tree, and this position comes from the array index.
Also, it has several advantages like the following:
Some operations of the heap have a time complexity of O(lgn) where n is the height of the tree, keeping the height of the tree at a minimum allows us to keep the time required for these operations at a minimum.
All the items of the complete binary tree are stored in a contiguous manner in an array so random access is possible by keeping the heap as a complete binary tree
The completeness ensures that there is a well-defined and efficient way to determine the new root when an element is removed, not using a complete structure would mean losing this advantage (which is why you should use heap in the first place).

Best data structure to implement a voting system [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I need to design a voting system on a high level that associates voters with their decision in sorted order by name.
I understand that I should implement a sorted map, and it seems like we need a map that performs best with random insertions. So I was wondering which of the above data structures would work best.
If you are sorting the objects based purely by name, I would say that a Binary Search Tree would work well.
If you are particularly worried about search time complexity, you can implement a balanced tree, such as an AVL tree or a splay tree. Doing this would get your search time complexity towards logarithmic, which is what you're after!
A heap or a BST. Trie linked list and array would have a higher search complexity.

Why are heuristics proposed? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have a small confusion with the nature of heuristics.
We know that heuristics need not give correct outputs for all input instances.
But then, why are heuristics proposed??
Heuristics are used to trade off performance (usually execution speed, but also memory consumption) with potential accuracy or generality. For example, your anti virus software uses heuristics to characterize what a virus might look like, and can take advantage of that piece of information to determine which files it should spend more time analyzing. A good heuristic has the property that it can save substantial time with minimal cost.
In graph traversal theory, a heuristic for an A* search algorithm need not be perfect. It just needs to have a predicted cost function h(x) that is less than or equal to the true cost to the goal state in order to guarantee an optimal solution. The closer h(x) equals the true cost, the quicker an optimal solution will be found.
Let me give you an example which might help you understand the importance of heuristics.
In Artificial Intelligence, search problems are mainly classified as blind search and directed search. Blind search is where you make use of algorithms such as BFS and DFS and there is a reason they are called blind search, they don't have any knowledge about the direction you should go, you just have to explore and explore until you reach the goal node, imagine the time and space complexity for those algorithms.
Now if you look at the directed search algorithm such as A*, where you have some kind of heuristic function or in simple terms an assumption about which direction you should take the next step.
Although heuristics does not guarantee the best result but rather will try to give you a better solution and sometimes even the best. There are so many classes of problems (Ex. games you play) where a better solution does the task rather than wasting so much time and space in finding the best solution.
I hope it helps.

Different Dictionary Implementations [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am preparing for an exam in algorithm analysis and after I have learned C# and implemented Dictionary in different ways I am confused about the advantages/disadvantages..
So here are my questions:
What is a reason to implement a Dictionary using unordered array instead of always sorted array?
Reaons to implement Dicitonary using always sorted array instead of unordered array?
Reasons to implement Dictionary using a Binary Search Tree instead of an always sorted array?
If you use an unordered array you can just tack items onto the end or copy the array into a new array and tack items on the end if the original fills up. O(1) or O(n) depending on these 2 cases to insert, but O(n) to do any lookups.
With an ordered array you COULD gain the ability to more quickly search it depending on its contents, either through a binary search or other cool searches, but it has increased insertion cost because you must move things around in the array every time you insert into it, unless you're only adding things already in sorted order, which case it might even be the worst case which would be O(n) for each element.
With a binary search tree you can easily find whatever node you're looking for based on whatever you're forking the tree on in O(log n) time, though this is only possible on a balanced binary tree. With an unbalanced binary search tree (basically a linked list) you could get worst case performance of O(n). With the balanced tree though, an insertion can be more expensive because it requires reorganizing the tree which is usually O(n log n), but again O is the worst case scenario, most balanced binary search trees can do most insertions more quickly.

What are the complicated data structures you should have heard of? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
This is a derivative question, but I'm inquiring as to the data structures that you should at least be familiar with for their usefulness. These structures are too hard to implement without some expertise however.
I would say a good boundary between the two is a heap -- you should be able to code a heap, but it would take you a day. Not appropriate for this would be a BST, etc. Edit: I see the point that it depends on what you are doing. I think it would be awesome to have a list with a phrase summarizing why you use it!
Here's a list to start:
B+ trees: good general indexing structure on a single key
K-d tree: spatial data
Red-black tree: self-balancing BST; also AVL or splay tree
Skip list: good hybrid structure for either random or (pseudo)sequential access
Trie: linear time string search
Bloom filters
What about:
Binomial Heaps
Fibonacci Heaps
Disjoint Set Data Structures
Splay Trees
Finger trees
That is a good start; there is a comprehensive list of data structures on wikipedia, some of them should be examined. But as to which ones you need, that depends on the area you intend to... do whatever it is that you are doing.
Embedded systems guys will have very different ideas from web guys who will strongly disagree with the business logic guys. Figure out what you want to do; languages and platform will also effect the list you need.
To quote Martin Kay:
Suffix trees constitute a well
understood, extremely elegant, but
regrettably poorly appreciated data
structure with potentially many
applications (...)
See also: What are the lesser known but cool data structures?
van Emde Boas trees. I don't literally think that you "should" have heard of them, but I do believe they're an interesting example of what kind of complexity you can achieve with "bit tricks" --- namely O(log log n), exponentially better than binary trees!
R-Tree
Closely related to the B+ tree you mentioned: B*-tree. Along with a balancing approach known as the "dancing tree" approach, these form the basis of Reiser4.
Binary Decision Diagrams, specifically Reduced Order Binary Decision Diagrams (ROBDD). These get reinvented (poorly) a lot when someone decides to create their own filtering system.
Cuckoo hashing, a simple and elegant way of resolving hash-table collisions in expected constant time.
Deterministic finite automata (DFAs), or finite state machines, useful for expressing many things, such as basic lexers, regular expressions, state transitions, etc. See also the related directed acyclic word graphs, which can be useful for storing dictionaries compactly.
I would add Hash Tables to the list. They are pretty simple in concept, but can be complicated once you look at how to implement a good hashing function and efficient probing methods.
R-Tree and its variants, such as R*-Tree, X-Tree, Pyramid-Tree. Various M-Tree variants, such as the Slim-Tree.
As often, querying the tree is easy. There might be an easy bulk-loading, too (for R-Trees, STR often does a good job). The tricky part usually is the maintainance of a good tree across updates.
You can try:
y-fast trees
Approximate ordered sets
select heap
compact arrays
Monolithic lists
Succinct lists

Resources