I have collection of family trees that have a format similar to the below follows:
A
/ \
B C
/ \ / \
D E F G
/ \
.. ..
Expressed in the following two columns (which contain multiple trees):
| child | parent |
|---|---|
| A | |
| B | A |
| C | A |
| D | B |
| ... | ... |
What is the most efficient way to flatten this so that in a new column I get the upper most parent?
i.e. B = A, D = A...?
| child | parent | highest_parent |
|---|---|---|
| A | A | |
| B | A | A |
| C | A | A |
| D | B | A |
| ... | ... | ... |
Ideally I would like to do this in Spark (given the size of the dataset) but could also try Pandas?
I currently cant wrap my head around doing this efficiently without some very intensive recursive function for each level (even if my trees are at most 3 levels deep).
