首页 文章

igraph无效顶点Id

提问于
浏览
0

我正在尝试使用以下代码运行igraph的快速贪婪社区检测算法:

G = Graph()

L = []
V = []
for row in cr:

    try:
        l = []
        source = int((row[0]).strip())
        target = int((row[1]).strip())
        weight = int((row[2]).strip())
        l.append(source)
        l.append(target)

        if l not in L:
            L.append(l)

        if source not in V:
            V.append(source)

        if target not in V:
            V.append(target)
    except ValueError:
        print "Value Error"
        continue

    if weight == 1:
        continue

G.add_vertices(max(V))
G.add_edges(L)
cl = G.community_fastgreedy(weights=weight).as_clustering(10);

但这是我得到的错误:igraph._igraph.InternalError:type_indexededgelist.c上的错误:272:无法添加边,无效的顶点id

我发现了这个:Cannot add edges, Invalid vertex ID in IGraph所以我尝试添加所有顶点然后所有边缘,但我仍然得到一个错误 .

上面的代码是否与以下内容完全相同:

tupleMapping = []
for row in cr:
    if int(row[2]) < 10:
        continue

    l = [row[0], row[1], row[2]]
    tupleMapping.append(tuple(l))

g = Graph.TupleList(tupleMapping)
cl = g.community_fastgreedy().as_clustering(20)

我不必明确说出G.community_fastgreedy(权重=体重)吧?

我还有另一个问题;当我尝试以下列方式添加更多群集时:

cl = g.community_fastgreedy().as_clustering(10)
cl = g.community_fastgreedy().as_clustering(20)

我得到两个大的集群,其余的集群由一个元素组成 . 当我尝试使群集大小为5/10/20时,会发生这种情况,是否有任何方法可以使群集更均等地划分?我的数据集需要2个以上的集群 .

这是我尝试从csv文件中读取的数据的一小部分,以便我可以生成图形,然后运行社区检测算法:202,580,11 87,153,7 227,459,6 263,524,11

谢谢 .

1 回答

  • 3

    那个's right, the second code does the same. In the first example, the problem is that when you add edges, you refer to igraph'的内部顶点ID,总是从0开始,一直到 N-1 . 无论您自己的顶点名称是整数,您都需要将它们转换为igraph顶点ID .

    igraph.Graph.TupleList() 方法在这里更方便 . 但是,您需要指定元组的第三个元素是权重 . 您可以通过 weights = Trueedge_attrs = ['weight'] 参数执行此操作:

    import igraph
    
    data = '''1;2;34
    1;3;41
    1;4;87
    2;4;12
    4;5;22
    5;6;33'''
    
    L = set([])
    
    for row in data.split('\n'):
        row = row.split(';')
        L.add(
            (row[0].strip(), row[1].strip(), int(row[2].strip()))
        )
    
    G = igraph.Graph.TupleList(L, edge_attrs = ['weight'])
    

    然后,您可以创建字典以在igraph顶点ID和原始名称之间进行转换:

    vid2name = dict(zip(xrange(G.vcount()), G.vs['name']))
    name2vid = dict((name, vid) for vid, name in vid2name.iteritems())
    

    但是,第一个并不是那么需要,因为你总是可以使用 G.vs[vid]['name'] .

    为了快速,我认为你应该指定权重,至少文档不会告诉它是否自动考虑名为 weight 的属性(如果存在这样的属性) .

    fg = G.community_fastgreedy(weights = 'weight')
    fg_clust_10 = fg.as_clustering(10)
    fg_clust_20 = fg.as_clustering(20)
    

    如果fastgreedy只给你2个大型集群,我只能建议尝试其他社区检测方法 . 实际上你可以尝试在合理的时间内运行它们(它取决于图形的大小),然后比较它们的结果 . 另外因为你有一个加权图,你可以看一下moduland method family,它没有在igraph中实现,但有很好的文档,你可以设置相当复杂的设置 .

    Edit: 来自OP的评论表明原始数据描述了有向图 . fastgreedy算法无法考虑方向,如果在有向图上调用则会出错 . 这就是为什么在我的例子中我创建了一个无向的 igraph.Graph() 对象 . 如果要运行其他方法,其中一些方法可能能够处理定向网络,则应首先创建有向图:

    G = igraph.Graph.TupleList(L, directed = True, edge_attrs = ['weight'])
    G.is_directed()
    # returns True
    

    要快速运行,请将图形转换为无向图 . 由于边缘具有权重属性,因此需要指定当同一对顶点之间的2个相对方向边缘折叠到一个无向边时应该执行的操作 . 您可以使用权重执行许多操作,例如取平均值,较大值或较小值等 . 例如,要使组合边具有原始边的平均权重:

    uG = G.as_undirected(combine_edges = 'mean')
    fg = uG.community_fastgreedy(weights = 'weight')
    

    重要提示:请注意,在此操作中,以及添加或删除顶点或边时,igraph会重新索引顶点和边,因此如果您知道顶点id x 对应于原始ID y ,则重新编制索引后这将无效你需要重新创建 name2vidvid2name 词典 .

相关问题