# [graph-tool] Two ways of calculating the degree sequence

gogurt gogurt at gmail.com
Mon Feb 13 01:10:04 CET 2017

```So I'm trying to calculate the degree sequence of G, an undirected, connected
simple graph. As far as I see it, there are two ways to do this. Iterate
over all the vertices, or sum the rows of the adjacency matrix. So:

def degree_sequence1(G):
degs = []
for v in G.vertices():
degs.append(v.out_degree() + v.in_degree())
degs.sort()

return degs

def degree_sequence2(G):
mat = gt.adjacency(G)
degs = np.squeeze(np.asarray(mat.sum(axis=0)))
degs.sort()

return degs

The first way definitely works. But when I try to use the second method, I
find that even if I run this code on a connected network I get zeroes in my
degree sequence. Which shouldn't be possible if the graph is connected. So I
know that I'm understanding something wrong here.

Can somebody point out where I went wrong here?

Thanks!

--
View this message in context: http://main-discussion-list-for-the-graph-tool-project.982480.n3.nabble.com/Two-ways-of-calculating-the-degree-sequence-tp4027026.html
Sent from the Main discussion list for the graph-tool project mailing list archive at Nabble.com.

```

More information about the graph-tool mailing list