Reputation: 31
I am interested in finding the distance of two nodes in a tree, in a least complexity possible. The finding process is in some queries and updates on the tree (adding and deleting a node).
This problem can use LCA as the tool for optimization. However, by these posts I found, there are some algorithm available.
https://cp-algorithms.com/graph/lca.html https://cp-algorithms.com/graph/lca_binary_lifting.html
Here is the summary,
Preprocessing time: O(N)
Query time: O(√N)
Allow to update tree: Yes
Preprocessing time: O(N)
Query time: O(log N)
Allow to update tree: Yes
Preprocessing time: O(N log N)
Query time: O(1)
Allow to update tree: No
Preprocessing time: O(N log N)
Query time: O(log N)
Allow to update tree: Yes
My question is, what is the best algorithm best to use?
Or which algorithm is best to use under what conditions?
Are there any other advantages or drawbacks that is not mentioned above for each algorithm?
Upvotes: 3
Views: 660
Reputation: 2777
From personal experience as competitive programmer:
Upvotes: 3