Ancestors/Descendants Index   <<   >>


 

An attempt at finding the list of ancestors for each item, ⎕IO 0, ⎕ML 3. Note that I am returning indices back into the original arguments:

adjacencyListAncestors←{
 ⍝ ⍺: Parent; ⍵: key
  p←⍵⍳⍺
  r←≢⍵
  q←p,r
  z←⍬{
    ⍵∧.=r:⍺
    (⍺,⊂⍵)∇ q[⍵]
  }p
  (↓⍉⊃z)~¨r
}

Dyalog Forum post by paulmansour, 2018-07-04

 
  k,⍪ p
┌──────┬────┐
│Merlot│Red │
├──────┼────┤
│Beer  │    │
├──────┼────┤
│Wine  │    │
├──────┼────┤
│Red   │Wine│
├──────┼────┤
│White │Wine│
├──────┼────┤
│Rose  │Wine│
├──────┼────┤
│Lager │Beer│
├──────┼────┤
│Stout │Beer│
├──────┼────┤
│Cab   │Red │
└──────┴────┘
 
   p adjacencyListAncestors k
┌───┬┬┬─┬─┬─┬─┬─┬───┐
│3 2│││2│2│2│1│1│3 2│
└───┴┴┴─┴─┴─┴─┴─┴───┘