简体   繁体   English

网络度

[英]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. 我已经开始在R中使用网络。我知道R中有一个名为igraph的程序包,它具有许多用于网络分析和可视化的功能。 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) . 现在,要获取每个节点的度数,我可以简单地在igraph使用命令degree ,然后说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? 您真的不想使用任何igraph函数吗? I assume that you at least want to load or generate the graph using igraph, otherwise this question does not make much sense. 我假设您至少要使用igraph加载或生成图形,否则这个问题就没有多大意义了。

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. 因此,如果要将生成的图形转换为不需要igraph处理的图形,则实际上有三个选择。

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: 如您在另一个答案中所见, get.adjlist()会给您一个邻接列表,然后程度由列表中每个向量的长度确定:

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): get.adjacency()将为您提供一个邻接矩阵,然后可以通过简单地对行(或列,如果您有向图)求和来计算度数:

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. get.edgelist()在两列矩阵中返回一个简单的边缘列表。 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. 如果您阅读igraph文档,将会发现有很多方法可以做到这一点。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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