2

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

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

4

2 回答 2

4

从 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
于 2016-04-18T15:52:51.760 回答
1

如果有人还想找出实际的共同邻居,以下将起作用:

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

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

于 2017-03-06T15:39:05.120 回答