简体   繁体   中英

get degree of parent node in a graph igraph

I have a graph and I would like to calculate for each node the degree of its parent nodes.

Here is a figure as example,

在此输入图像描述

In this example, the degree of node-9 would be 9, 5 connections from node-19 and 4 connections from node-3; and for node-5 it would be 3, since its parent node-13 has 3 connections.

I want to calculate the degree starting for one node (for example node-19) and calculate the degree based on parents for nodes 2, and 3 steps away from node-19.

I was thinking in using the function ego but it also gives me the parents of the parents in step 2, and 3.

Any help?

Here is a graph from the communities tutorial of igraph:

 g <- graph_from_literal(A-B:F:C:E:D, B-A:D:C:E:F:G, C-A:B:F:E:D, D-A:B:C:F:E,
                                          E-D:A:C:B:F:V:W:U, F-H:B:A:C:D:E, G-B:J:K:L:H,
                                         H-F:G:I:J:K:L, I-J:L:H, J-I:G:H:L, K-G:H:L:M,
                                          L-H:G:I:J:K:M, M-K:L:Q:R:S:P:O:N, N-M:Q:R:P:S:O,
                                          O-N:M:P, P-Q:M:N:O:S, Q-M:N:P:V:U:W:R, R-M:N:V:W:Q,
                                          S-N:P:M:U:W:T, T-S:V:W:U, U-E:V:Q:S:W:T,
                                          V-E:U:W:T:R:Q, W-U:E:V:Q:R:S:T)


plot(g, vertex.color = "grey", edge.color = "blue")

在此输入图像描述

degree(g) gives you the degree of each node in the graph, and neighbors(g,1,'A')[[1]] gives you all the neighbors of 'A' that are 1 edge away.

degree(g)
A B F C E D G V W U H J K L I M Q R S P O N T 
5 6 6 5 8 5 5 6 7 6 6 4 4 6 3 8 7 5 6 5 3 6 4 

neighbors(g,1,'A')[[1]]
+ 6/23 vertices, named:
[1] A B F C E D

We can combine and generalize:

dist <- 1
pick <- 'A'

 neighbors_w_degree <- degree(g)[neighborhood(g,dist,pick)[[1]]]
 neighbors_w_degree
A B F C E D 
5 6 6 5 8 5 

In your example, we want to exclude the degree of the base node, so use:

sum(neighbors_w_degree[!names(neighbors_w_degree) %in% pick])
[1] 30

Which in this case is the degree of: B + C + D + E + F

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