简体   繁体   中英

Degree of a Network

I have started working with networks in R. I know there is a package in R called igraph that has many functions for network analysis and visualization. I want to calculate the degree of a small network. eg

mydegree=c(2,3,5)
g=degree.sequence.game(mydegree)

Now to get the degree of each node I can simply use the command degree in igraph and say degree(g) . But what formula can I use to calculate the degree if I don't want to use this degree command?

Do you really don't want to use any igraph functions at all? I assume that you at least want to load or generate the graph using igraph, otherwise this question does not make much sense.

So if you want to convert the generated graph into something that does not require igraph to deal with, then you have essentially three options.

get.adjlist() , as you have seen in another answer, will give you an adjacency list, and then the degree is given by the length of each vector in the list:

g <- degree.sequence.game(rep(2,10))
al.g <- get.adjlist(g)
sapply(al.g, length)
# [1] 2 2 2 2 2 2 2 2 2 2

get.adjacency() will give you an adjacency matrix, and then the degree can be calculated by simple summing the rows (or columns if you have a directed graph):

adj.g <- get.adjacency(g)
rowSums(adj.g)
# [1] 2 2 2 2 2 2 2 2 2 2

get.edgelist() returns a simple edge list in a two-column matrix. You'll need to count the number of times each vertex appears in this matrix to get their degree:

el.g <- get.edgelist(g)
table(el.g)
# el.g
#  1  2  3  4  5  6  7  8  9 10 
#  2  2  2  2  2  2  2  2  2  2 

If you read the igraph docs you'll see there are many ways to do this. Here's one. Get the adjancency list, compute the length of each element:

mydegree=c(2,3,5)
g=degree.sequence.game(mydegree)
unlist(lapply(get.adjlist(g),length))
[1] 2 3 5

And another:

> mydegree = c(3,5,2,2,4,2,5,6,7)
> g=degree.sequence.game(mydegree)
> unlist(lapply(get.adjlist(g),length))
[1] 3 5 2 2 4 2 5 6 7

And another:

> degree(graph.famous("Zachary"))
 [1] 16  9 10  6  3  4  4  4  5  2  3  1  2  5  2  2  2  2  2  3  2  2  2  5  3
[26]  3  2  4  3  4  4  6 12 17
> unlist(lapply(get.adjlist(graph.famous("Zachary")),length))
 [1] 16  9 10  6  3  4  4  4  5  2  3  1  2  5  2  2  2  2  2  3  2  2  2  5  3
[26]  3  2  4  3  4  4  6 12 17

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM