简体   繁体   English

使用线图计算子图的程度

[英]Calculate degree of a subgraph using r igraph

I know the degree of my global graph, but now I need to find the degrees of nodes within a subgraph. 我知道全局图的程度,但是现在我需要找到子图中的节点的程度。 So, John has 4 friends in his school, but three friends in his class. 因此,约翰在学校有4个朋友,但在班上有3个朋友。 How do I instruct igraph to count those three friends in his class, but not the rest in his school? 我该如何指示igraph数他班上的那三个朋友,而不是他学校里的其余三个朋友?

My global graph 我的全球图

library(igraph)
school <- read.table(text="
    A   B   C   D   E   F   G
A   0   1   0   1   0   1   1
B   1   0   1   1   0   1   0
C   0   0   0   0   0   0   1
D   1   1   0   0   1   0   0
E   0   0   0   1   0   1   1
F   0   1   0   0   1   0   1
G   1   0   1   0   1   1   0", header=TRUE)

mat <- as.matrix(school)
g <- graph.adjacency(mat, mode="undirected", add.rownames = T)

My affiliation matrix for classes P, Q, and R 我对P,Q和R类的隶属关系矩阵

x <- read.table(text="
                    P   Q   R
                A   1   1   0
                B   0   0   1
                C   0   0   0
                D   1   0   1
                E   1   1   0
                F   0   1   0
                G   1   1   1", header=TRUE)

inc <- as.matrix(x)
ginc <- graph.incidence(inc)

My subgraph for class P 我的P类子图

class_nodes <- names(which(inc[,"P"] == 1))
class_adj   <- mat[class_nodes, class_nodes]
class_graph <- graph.adjacency(class_adj, mode = "undirected")

I need to calculate the degree of nodes in subgraph "class_graph", but counting only their ties within the subgraph, not the global graph. 我需要计算子图“ class_graph”中的节点的程度,但只计算它们在子图中的联系,而不是全局图。

You can find all the nodes in class P with (we specifically extract the names so we can look them up in a different graph object). 您可以使用找到P类中的所有节点(我们专门提取了名称,以便可以在其他图形对象中查找它们)。

V(ginc)[.nei("P")]$name

Then you can extract just that subset of connections from the main graph with 然后您可以使用以下方法从主图中提取连接的子集

subg <- induced.subgraph(g, V(ginc)[.nei("P")]$name)

and you can calculate the degree of those nodes with 您可以使用

degree(subg)
# A D E G 
# 2 2 2 2

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

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