如何使用python获取某些边缘标签的度数

时间:2018-04-13 16:59:01

标签: python igraph networkx directed-graph

Directed graph with labeled edges

我想找到带有某些标签边缘的节点度,当我使用networkx的degree()方法时,它给出了一个带有{:}的dic,但是如何得到那些边的标签?我们非常感谢任何帮助,包括其他图表分析包有关此规定的信息。

2 个答案:

答案 0 :(得分:1)

在igraph中,你需要g.es.select(_source=i)。这将获得源顶点为i的所有边,此时您可以询问这些边的属性:

>>> g = igraph.Graph.Full(3) #complete graph on three vertices
>>> g.es['label'] = ['a','b','c'] #sets the first edge to have 'label' value 'a', etc.
>>> g.es.select(_source=0)['label']
['a', 'b']

答案 1 :(得分:0)

某些边缘标签的节点度

随机图函数

def rand_gen(m):
    yield random.randint(0,m)

def get_edges(n):
    tpl = []
    for _ in range(n):
        for x,y in zip(rand_gen(n), rand_gen(n)):
            tpl.append((x,y, {random.choice(['a','b','c']): random.randint(0,10)}))
    return tpl

创建图表

g = nx.Graph()

e = get_edges(10)

g.add_edges_from(e)

pos = nx.spring_layout(g)
nx.draw_networkx_nodes(g,pos)
nx.draw_networkx_labels(g,pos, font_color='w')
nx.draw_networkx_edges(g,pos)
nx.draw_networkx_edge_labels(g,pos, edge_labels={(x,y):l.keys()[0] for x,y,l in g.edges(data=True)})
plt.axis('off')
plt.show()

enter image description here

按标签选择

nds = []
nds.extend([n1, n2] for n1,n2,l in g.edges(data=True) if l.keys()[0] == 'a')
nds = [x for y in nds for x in y]

获取唯一标签的程度

[nx.degree(g,x) for x in set(nds)]

[OUT]

[2, 3, 2, 2, 5]