Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get degree of each nodes in a graph by Networkx in python

Suppose I have a data set like below that shows an undirected graph:

1   2
1   3
1   4
3   5
3   6
7   8
8   9
10  11

I have a python script like it:

for s in ActorGraph.degree():
    print(s)

that is a dictionary consist of key and value that keys are node names and values are degree of nodes:

('9', 1)
('5', 1)
('11', 1)
('8', 2)
('6', 1)
('4', 1)
('10', 1)
('7', 1)
('2', 1)
('3', 3)
('1', 3)

In networkx documentation suggest to use values() for having nodes degree. now I like to have just keys that are degree of nodes and I use this part of script but it does't work and say object has no attribute 'values':

for s in ActorGraph.degree():
        print(s.values())

how can I do it?

like image 227
Fatemeh khodaparast Avatar asked Nov 22 '17 11:11

Fatemeh khodaparast


People also ask

How do I find the degree of a node in NetworkX?

degree(). The node degree is the number of edges adjacent to the node. The weighted node degree is the sum of the edge weights for edges incident to that node. This object provides an iterator for (node, degree) as well as lookup for the degree for a single node.

How do I find the degree of a node in Python?

and the in-degree is the number of incoming edges onto a node kini=∑jaij. The total degree of the node is the sum of its in- and out-degree ktoti=kini+kouti. For this undirected network, the degrees are k1=1, k2=3, k3=1, k4=1, k5=2, k6=5, k7=3, k8=3, k9=2, and k10=1.

How do you find the degree of a node in a graph?

The average degree of an undirected graph is used to measure the number of edges compared to the number of nodes. To do this we simply divide the summation of all nodes' degree by the total number of nodes. For example in the graph above the nodes have the following degrees: A=2, B=2, C=4, D=2, E=3, F=2, G=2, H=1.


2 Answers

You are using version 2.0 of networkx. Which changed from using a dict for G.degree() to using a dict-like (but not dict) DegreeView. See this guide.

To have the degrees in a list you can use a list-comprehension:

degrees = [val for (node, val) in G.degree()]
like image 146
rodgdor Avatar answered Oct 24 '22 21:10

rodgdor


I'd like to add the following: if you're initializing the undirected graph with nx.Graph() and adding the edges afterwards, just beware that networkx doesn't guarrantee the order of nodes will be preserved -- this also applies to degree(). This means that if you use the list comprehension approach then try to access the degree by list index the indexes may not correspond to the right nodes. If you'd like them to correspond, you can instead do:

degrees = [val for (node, val) in sorted(G.degree(), key=lambda pair: pair[0])]

Here's a simple example to illustrate this:

>>> edges = [(0, 1), (0, 3), (0, 5), (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (2, 5)]
>>> g = nx.Graph()
>>> g.add_edges_from(edges)
>>> print(g.degree())
[(0, 3), (1, 4), (3, 3), (5, 2), (2, 4), (4, 2)]
>>> print([val for (node, val) in g.degree()])
[3, 4, 3, 2, 4, 2]
>>> print([val for (node, val) in sorted(g.degree(), key=lambda pair: pair[0])])
[3, 4, 4, 3, 2, 2]
like image 25
Victor-Alexandru Darvariu Avatar answered Oct 24 '22 20:10

Victor-Alexandru Darvariu