繁体   English   中英

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

[英]common neighbour of two vertices on graph-tool

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

根据该文档,可以使用几种相似性度量,并且所有度量都使用公共邻居的数量。 因此,我认为与共同的邻居相处应该很容易。 但是,我找不到方法。

从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>}

该集合的长度为您提供了共同邻居的总数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM