1

I have a bunch of nodes arranged in a hierarchy structure as follows:

Node hierarchy

I would like to determine if one node is connected to another node, even if the connection between the two is separated by different levels in the hierarchy.

For example, node A is connected to node K through nodes B and D. Node A is also connected to node L either through nodes B and D or nodes C and G.

Nodes E, F, H, J and M are not connected to node L.

Without transversing the hierarchy from a parent node to some child node in order to determine whether two nodes are connected, I believe that it is possible to assign some numeric value to each node and through a formula that takes the numeric value of two nodes can determine that they are connected.

Is this possible?

Johann
  • 27,536
  • 39
  • 165
  • 279
  • Could you use something like this: https://stackoverflow.com/questions/10310809/check-if-2-tree-nodes-are-related-ancestor-descendant-in-o1-with-pre-process ? You could treat your graph as a forest and run the algorithm for every root node (A and M in the example), and assign multiple pre/post-order labels to nodes with multiple parents – Jonas Høgh Oct 30 '18 at 09:41
  • With "connected", you mean "reachable by traversing _only_ parent or _only_ child relations, but not both"? – tobias_k Oct 30 '18 at 09:47
  • Your definition of 'is connected to' needs clarification. I can clearly see lines going from E to L via D, but you're telling me they're not connected? – AakashM Oct 30 '18 at 09:49
  • By "connected", it doesn't matter whether you start at the child (or grandchild) and work your way to the parent (or grandparent) or in reverse. – Johann Oct 30 '18 at 09:49
  • You could potentially do this fairly easily with a strict hierarchy. The link from G to L, though, breaks the strict hierarchy. You end up with the potential of multiple numeric values per node. That's going to complicate matters quite a bit. – Jim Mischel Oct 30 '18 at 20:29

1 Answers1

1

Yes, by giving some kind of progressive number or pattern (Id) can help in this. Have a look of below image -(sorry for the small size image. Click on it to view it properly)

enter image description here

I have assigned each node an Id no like 1 to root then 1-1....1-N to its child. Now to check if nodes are connected, all we have to check if one nodes ID starts with another. if it is so nodes are connected else not.

Anki
  • 407
  • 3
  • 8
  • What about the connections L-G and J-M? – tobias_k Oct 30 '18 at 09:49
  • Thanks, I have updated the image. your first point is valid. my solution doesn't resolve this. let me check again. – Anki Oct 30 '18 at 09:54
  • This would work if I knew all the nodes in advance and they remained static. But assume I disconnect node B from A and connect it to node H. All the node values for D, E, F, K, L would have to be updated. And that would require traversing the nodes, which is what I want to avoid. – Johann Oct 30 '18 at 09:59
  • Actually, I think `M` should be another "parent" of `J`, and you could sort-of fix the problem (although with slightly higher query time) by giving `L` and `J` lists of IDs, like `(1-1-1-2, 1-2-1-1)` and `(1-2-3, 2-1)` respectively (with `M` having ID `2`). – tobias_k Oct 30 '18 at 10:00
  • Your solution is along the lines I am thinking but I believe that instead of using a numeric pattern like 1-1-2 which binds a node to a specific level in the hierarchy, a numeric value could be generated that identifies the relationship of two adjacent nodes, so that even if you move those two nodes elsewhere in the hierarchy, that numerical relationship value remains the same. Maybe something along the lines of a blockchain? – Johann Oct 30 '18 at 10:09
  • Yes @AndroidDev, I am getting you correctly. Let me find out some appropriate solution for this. – Anki Oct 30 '18 at 10:20
  • @AndroidDev but if you move nodes in the hierarchy, they might no longer be connected. For example, if you moved node F in the graph above to be a child of H, then B and F are no longer connected. – Jim Mischel Oct 30 '18 at 20:31