从图形工具python界面中的邻接矩阵创建加权图 [英] Create a weighted graph from an adjacency matrix in graph-tool, python interface

查看:1003
本文介绍了从图形工具python界面中的邻接矩阵创建加权图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该如何使用python中的图形工具创建一个图形,使用邻接矩阵?
假设我们有 adj 矩阵作为邻接矩阵。



现在我所做的是这样的:

  g = graph_tool.Graph(directed = False)
g.add_vertex(len(adj))
边缘权重= g.new_edge_property('double')
为我在范围内(adj.shape [0]):
为范围内的j(adj.shape [1]):
if我> j和adj [i,j]!= 0:
e = g.add_edge(i,j)
edge_weights [e] = adj [i,j]

但它感觉不对,我们有更好的解决方案吗?

(我想这是一个适当的标记将是图形工具,但我不能添加它,一些有足够特权的人可以使标记?)

解决方案

图形工具现在包含一个函数,可以为图形添加边缘列表。你现在可以做到,例如:

  adj = numpy.random.randint(0,2,(100,100)) #随机有向图
g = Graph()
g.add_edge_list(转置(adj.nonzero()))


How should I create a graph using graph-tool in python, out of an adjacency matrix? Assume we have adj matrix as the adjacency matrix.

What I do now is like this:

        g = graph_tool.Graph(directed = False)
        g.add_vertex(len(adj))
        edge_weights = g.new_edge_property('double')
        for i in range(adj.shape[0]):
            for j in range(adj.shape[1]):
                if i > j and adj[i,j] != 0:
                    e = g.add_edge(i, j)
                    edge_weights[e] = adj[i,j]

But it doesn't feel right, do we have any better solution for this?

(and I guess a proper tag for this would be graph-tool, but I can't add it, some kind person with enough privileges could make the tag?)

解决方案

Graph-tool now includes a function to add a list of edges to the graph. You can now do, for instance:

adj = numpy.random.randint(0, 2, (100, 100)) # a random directed graph
g = Graph()
g.add_edge_list(transpose(adj.nonzero()))

这篇关于从图形工具python界面中的邻接矩阵创建加权图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆