lowest-common-ancestor

How to determine the closest common ancestor class

纵饮孤独 提交于 2019-12-12 20:23:13
问题 Suppose I have four classes: A , B derived from A , C derived from A , and D derived from C . (So I always have single inheritance.) In python, what is the best way to determine the closest common ancestor of any two (instances of such) classes? Specifically, I need a function clcoancl(X,Y) for which clcoancl(A, B) == A , clcoancl(B, C) == A , and clcoancl(C, D) == C . 回答1: This should work for single or multiple inheritance, with any number of classes as input: import inspect from

Algorithm to find lowest common ancestor in directed acyclic graph?

廉价感情. 提交于 2019-11-28 03:50:27
Imagine a directed acyclic graph as follows, where: "A" is the root (there is always exactly one root) each node knows its parent(s) the node names are arbitrary - nothing can be inferred from them we know from another source that the nodes were added to the tree in the order A to G (e.g. they are commits in a version control system) What algorithm could I use to determine the lowest common ancestor (LCA) of two arbitrary nodes, for example, the common ancestor of: B and E is B D and F is B Note: There is not necessarily a single path to a given node from the root (e.g. "G" has two paths), so

Algorithm to find lowest common ancestor in directed acyclic graph?

为君一笑 提交于 2019-11-27 00:11:10
问题 Imagine a directed acyclic graph as follows, where: "A" is the root (there is always exactly one root) each node knows its parent(s) the node names are arbitrary - nothing can be inferred from them we know from another source that the nodes were added to the tree in the order A to G (e.g. they are commits in a version control system) What algorithm could I use to determine the lowest common ancestor (LCA) of two arbitrary nodes, for example, the common ancestor of: B and E is B D and F is B