kdb中字典中键和值之间的多对多关系

kdb中字典中键和值之间的多对多关系,kdb,Kdb,最近我遇到了一个问题: 如果我们的组织层级如下: "B" reports to "A" / Hence A is boss of B, C, D, E "C" and "D" reports to "B" / B is the boss of C, D, E "E" reports to "D" / D is the boss of E "G" reports to "F" / separate hierarchy - F is the boss of G 这里的关系可以是间接的,即“A”和“

最近我遇到了一个问题:
如果我们的组织层级如下:

"B" reports to "A" / Hence A is boss of B, C, D, E
"C" and "D" reports to "B" / B is the boss of C, D, E
"E" reports to "D" / D is the boss of E
"G" reports to "F" / separate hierarchy - F is the boss of G
这里的关系可以是间接的,即“A”和“C”之间的关系是“C”报告与“B”之间的关系,“B”报告与“A”之间的关系

现在,在q中编写一个函数,它接受两个参数作为输入,如果两个参数都相关,则返回true,否则返回false(例如“a”和“F”不相关)

从这两个论点中,任何一个论点都可以是从属论点或上司论点

当第一个参数是boss,第二个参数是substance时,下面的代码工作正常

f:{[i1;i2]
    d:(`A`B`D`F)!(`B;[`C`D];`E;`G);
    if[i2 in except[ raze {d[x]}\[i1];`];:1b] / Condition when i1 is boss and i2 is subordinate
    e:(value d)!key d; / dictionary to support if first arg is subordinate and second is boss
    :$[i2 in except[raze {e[x]}\[i1];`];1b;0b]; 
    }

PASS - f[`A;`E] /- output 1b --> 1st arg - boss, 2nd arg - subordinate
PASS - f[`G;`F] /- output 1b --> 1st arg - subordinate, 2nd arg - boss
PASS - f[`F;`A] /- output 0b --> as A and F are not related
FAIL - f[`C;`A] /- output 0b --> fails because dictionary e in function f does not have key `C but `C`D
有人能帮我改进这个功能吗:
1.无论是上司还是下属,该功能都能像任何参数一样正常工作。
2.优化-如何消除字典e和if条件的冗余创建

q)//dict of subordinate to boss
q)d:`B`C`D`E`G!`A`B`B`D`F
q)//func g 
q)g:{x in'flip {d@x}\[y]}
q)f:{$[0>type x;first;(::)] any 2 0N# g[x,y;y,x]}
q)f[`A;`E]
1b
q)f[`G;`F]
1b
q)f[`F;`A]
0b
q)f[`C;`A]
1b
q)f[`E;`A]
1b
q)f[`A`G`F`C`E;`E`F`A`A`A]
11011b
*编辑-矢量化


*编辑-矢量化的

函数应该在没有if条件的情况下工作,因为我们总是将x和y作为列表传递给内部函数。f:{any 2 0N#{{x in'flip y}[x]{d@x}[y] }[x,y;y,x]}函数应该在没有if条件的情况下工作,因为我们总是将x和y作为列表传递给内部函数。f:{any 2 0N#{{x in'flip y}[x]{d@x}[y] }[x,y;y,x]}