pydatastructs icon indicating copy to clipboard operation
pydatastructs copied to clipboard

Lowest Common Ancestor for DAG

Open jyoti246 opened this issue 4 years ago • 22 comments

Description of the problem

Using a naive approach we can find LCA between two nodes in a non cyclic graph in O(n) time. But when there are O(n) queries i.e we need to get LCA of O(n) different pairs of nodes, the time complexity becomes O(n^2). But by doing some pre processing we can reduce the complexity to O(nlogn). There are mainly two approaches: Binary lifting using Segment tree and Eulars tour

Both of these can be implemented in pydatastructs/graphs/algorithms.py as classes

jyoti246 avatar Mar 29 '20 14:03 jyoti246

Can you please provide some references(research paper, wikipedia, or university lectures) for the algorithm you are suggesting?

czgdp1807 avatar Mar 29 '20 14:03 czgdp1807

Here are they For Binary Lifting: https://cp-algorithms.com/graph/lca_binary_lifting.html For Eulars Tour , Range Minimum query https://www.geeksforgeeks.org/find-lca-in-binary-tree-using-rmq/

jyoti246 avatar Mar 29 '20 14:03 jyoti246

Let G be a tree

Quoting from [1]. This says that the graph should be a tree/DAG. The algorithm for binary trees is already available here, please see if any improvements can be made to it.

Is [2] relevant to the algorithm you are taking about?

References

.. [1] https://cp-algorithms.com/graph/lca_binary_lifting.html .. [2] https://uhra.herts.ac.uk/bitstream/handle/2299/12152/906535.pdf;jsessionid=C46932A27B417CF192F2A9026D68706D?sequence=2

czgdp1807 avatar Mar 29 '20 14:03 czgdp1807

Please follow the plan of action available here.

czgdp1807 avatar Mar 29 '20 14:03 czgdp1807

Hi @jyoti246 any update? are you still working on this ?

robotjellyzone avatar Apr 10 '20 16:04 robotjellyzone

Can I work on this issue?

Aakansha99 avatar Apr 20 '20 07:04 Aakansha99

Read the stuff above and work accordingly.

czgdp1807 avatar Apr 20 '20 08:04 czgdp1807

I would like to work on this issue. Please assign this to me as part of GSSoC'20

Rukmini-Meda avatar Apr 29 '20 13:04 Rukmini-Meda

Hi, @Rukmini-Meda yes, go ahead but please remember to follow the plan of actions and please first discuss regarding your solution and approach.

robotjellyzone avatar Apr 29 '20 13:04 robotjellyzone

Can you please assign this issue to me to avoid any confusion?

Rukmini-Meda avatar Apr 29 '20 13:04 Rukmini-Meda

No we can't as there is no such rule of assigning here ! You can just straight away work on it without being assigned and if in case others also provide solution for this issue then the most appropriate and suitable solution will be merged !

robotjellyzone avatar Apr 29 '20 13:04 robotjellyzone

Okay.

Rukmini-Meda avatar Apr 29 '20 13:04 Rukmini-Meda

Any update @Rukmini-Meda? Are you still working on it!

robotjellyzone avatar May 06 '20 11:05 robotjellyzone

Yes, I am working on it. Need little time. Will make a PR soon by tomorrow.

Rukmini-Meda avatar May 06 '20 11:05 Rukmini-Meda

Hi @Rukmini-Meda are you still on it ?

robotjellyzone avatar May 30 '20 09:05 robotjellyzone

I am sorry that I am not.

Rukmini-Meda avatar May 30 '20 09:05 Rukmini-Meda

Is it assigned? I have implemented binary-lifting in C++ for SPOJ problems. Can try it in python as well if not assigned.

Mintuagarwal avatar Dec 06 '20 12:12 Mintuagarwal

Please read https://github.com/codezonediitj/pydatastructs/wiki/Issue-Policy and then you may proceed.

prshnt19 avatar Dec 07 '20 04:12 prshnt19

can you please assign this issue to me @prshnt19

subhangi2731 avatar Dec 14 '20 05:12 subhangi2731

IMHO, since LCA requires sparse table/segment tree for finding the minimum in a segment(Sparse table is prefered due to O(1) time for range min.queries), we should firstly focus on sparse table and segment tree additions and after that it only remains an implementation of LCA.

Smit-create avatar Mar 07 '21 14:03 Smit-create

we should firstly focus on sparse table and segment tree additions and after that it only remains an implementation of LCA.

Sparse table can be added after discussion. I don't know if an issue is already open for it.

Segment Tree is already there as far as I remember.

czgdp1807 avatar Mar 07 '21 16:03 czgdp1807

Sparse table can be added after discussion.

https://github.com/codezonediitj/pydatastructs/issues/25 : Issue for Sparse Table. We should try adding this initially

Segment Tree is already there as far as I remember.

Yes, But can we make it customized for such operations: https://cp-algorithms.com/data_structures/segment_tree.html#toc-tgt-7, which uses custom (merge function taken from user in form of lambda function) and builds on the given array of size n?. Specifically I am talking about something :

>>> merge = lambda x, y: math.gcd(x, y)
>>> segTree = ODST(array, merge, dummy_variable) # array, custom merge function, dummy_variable (used while merging unwanted segment with required segment)
>>> segTree.build()
>>> segTree.query(1, 5) # Return gcd of elements array[1:6]

Smit-create avatar Mar 07 '21 17:03 Smit-create