图工具上两个顶点的共同邻居

common neighbour of two vertices on graph-tool

我想在图形工具上获取两个顶点的共同邻居。

根据 document, 有几种相似性度量可用,它们都使用共同邻居的数量。所以,我想应该很容易找到共同的邻居。但是,我找不到方法。

从 Sørensen–Dice 相似性很容易得出:

>>> g = collection.data["karate"]
>>> u, v = g.vertex(0), g.vertex(1)
>>> n = vertex_similarity(g, "dice", [(u, v)], self_loops=False) * (u.out_degree() + v.out_degree()) / 2
>>> print(n)
7

如果有人也想找出实际的共同邻居,下面的方法会起作用:

In [2]: g = gt.collection.data["karate"]

In [11]: u, v = g.vertex(32), g.vertex(33)

In [16]: set(u.out_neighbours()) & set(v.out_neighbours())
Out[16]: 
{<Vertex object with index '8' at 0x7f2b62ae82a0>,
 <Vertex object with index '14' at 0x7f2b62ae8318>,
 <Vertex object with index '15' at 0x7f2b62ae8390>,
 <Vertex object with index '18' at 0x7f2b62ae8408>,
 <Vertex object with index '20' at 0x7f2b62ae8480>,
 <Vertex object with index '22' at 0x7f2b62ae84f8>,
 <Vertex object with index '23' at 0x7f2b62ae8570>,
 <Vertex object with index '29' at 0x7f2b62ae85e8>,
 <Vertex object with index '30' at 0x7f2b62ae8660>,
 <Vertex object with index '31' at 0x7f2b62ae86d8>}

这个集合的长度给出了共同邻居的总数。