检索Igraph中所有节点的第一度连接和第二度连接

时间:2016-10-09 00:45:21

标签: r social-networking igraph sna

我有一个igraph,我想提取每个节点的所有1度连接以及2度连接。表格需要分开。

图表图片的完整可重现代码如下:

library(igraph)
library(visNetwork)
B = matrix( 
  c(1, 0, 0, 0, 0, 0, 1, 0, 0, 0,
    0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
    0, 0, 1, 0, 0, 0, 0, 0, 1, 0,
    0, 0, 0, 1, 0, 0, 0, 0, 0, 1,
    0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
    0, 0, 1, 1, 0, 1, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 1, 0, 0, 1,
    0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 
    0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 1), 
  nrow=10, 
  ncol=10)
colnames(B) <- c("Alpha", "Bravo", "Colonel", "Demo", "Egg", "Felix", "Gasoline", "Hurricane", "Indigo", "Jab")
rownames(B) <- c("Alpha", "Bravo", "Colonel", "Demo", "Egg", "Felix", "Gasoline", "Hurricane", "Indigo", "Jab")

g96e = t(B) %*% B

i96e = graph.adjacency(g96e, mode = "undirected", weighted = TRUE, diag=FALSE)
visIgraph(i96e)

enter image description here

我希望我的最终结果是一个如下所示的数据框:

Node ID     1st Degree Connections
Alpha           Gasoline
Bravo           Egg
Bravo           Hurricane
Colonel         Felix
Colonel         Indigo
Demo            Felix
Demo            Gasoline
Demo            Jab
Egg             Bravo
Felix           Colonel
Felix           Demo
Felix           Hurricane
Felix           Indigo
Gasoline        Alpha
Gasoline        Demo
Gasoline        Jab
Hurricane       Bravo
Hurricane       Felix
Indigo          Colonel
Indigo          Felix
Jab             Demo
Jab             Gasoline

然后是另一个只有2度连接的数据帧,理想情况下显示第2度连接源的位置,但如果不可能那么只有第2度连接就可以了。

Node ID     2nd Degree Connections        From 1st Degree Connection
Alpha               Demo                     Gasoline
Alpha               Jab                      Gasoline
Bravo               Felix                    Hurricane
Colonel             Demo                     Felix
Colonel             Hurricane                Felix
Colonel             Indigo                   Felix
Colonel             Felix                    Indigo
Demo                Colonel                  Felix
Demo                Hurricane                Felix
Demo                Indigo                   Felix
Demo                Alpha                    Gasoline
Demo                Jab                      Gasoline
Demo                Gasoline                 Jab
Egg                 Hurricane                Bravo
Felix               Indigo                   Colonel
Felix               Gasoline                 Demo
Felix               Jab                      Demo
Felix               Bravo                    Hurricane
Felix               Colonel                  Indigo
Gasoline            Felix                    Demo
Gasoline            Jab                      Demo
Gasoline            Demo                     Jab
Hurricane           Egg                      Bravo
Hurricane           Colonel                  Felix
Hurricane           Demo                     Felix
Hurricane           Indigo                   Felix
Indigo              Felix                    Colonel
Indigo              Colonel                  Felix
Indigo              Demo                     Felix
Indigo              Hurricane                Felix
Jab                 Felix                    Demo
Jab                 Gasoline                 Demo
Jab                 Alpha                    Gasoline
Jab                 Demo                     Gasoline

任何帮助都会很棒,谢谢!

1 个答案:

答案 0 :(得分:3)

根据定义,First Degree连接是连接到图形边缘的另一个顶点,因此您可以使用get.edgelist()检索图形对象中的所有直接连接,这是一个矩阵:

library(igraph); library(dplyr);

edges <- get.edgelist(i96e)
edges

#         [,1]       [,2]       
#  [1,] "Alpha"    "Gasoline" 
#  [2,] "Bravo"    "Egg"      
#  [3,] "Bravo"    "Hurricane"
#  [4,] "Colonel"  "Felix"    
#  [5,] "Colonel"  "Indigo"   
#  [6,] "Demo"     "Felix"    
#  [7,] "Demo"     "Gasoline" 
#  [8,] "Demo"     "Jab"      
#  [9,] "Felix"    "Hurricane"
# [10,] "Felix"    "Indigo"   
# [11,] "Gasoline" "Jab"   

边缘列表仅为每条边返回一条边,因此如果您想要所有顶点的第一个连接,您可以切换from和to列并与原始边矩阵绑定,这样可以提供更方便的一阶连接数据框:

edgeDF <- rbind(edges, edges[,c(2,1)]) %>% 
          as.data.frame() %>% 
          setNames(c("NodeId", "FirstConnection"))

edgeDF %>% arrange(NodeId, FirstConnection)
#       NodeId FirstConnection
# 1      Alpha        Gasoline
# 2      Bravo             Egg
# 3      Bravo       Hurricane
# 4    Colonel           Felix
# 5    Colonel          Indigo
# 6       Demo           Felix
# 7       Demo        Gasoline
# 8       Demo             Jab
# 9        Egg           Bravo
# 10     Felix         Colonel
# 11     Felix            Demo
# 12     Felix       Hurricane
# 13     Felix          Indigo
# 14  Gasoline           Alpha
# 15  Gasoline            Demo
# 16  Gasoline             Jab
# 17 Hurricane           Bravo
# 18 Hurricane           Felix
# 19    Indigo         Colonel
# 20    Indigo           Felix
# 21       Jab            Demo
# 22       Jab        Gasoline

第二级连接是第一级连接的连接,但第二级连接不应该是原始节点,因此您可以将第一级数据框与自身连接并过滤第二级连接所在的记录等于节点本身:

SecondCon <- setNames(edgeDF, c("FirstConnection", "SecondConnection")) %>% 
             full_join(edgeDF) %>% filter(NodeId != SecondConnection)

SecondCon %>% arrange(NodeId, FirstConnection, SecondConnection)
#    FirstConnection SecondConnection    NodeId
# 1         Gasoline             Demo     Alpha
# 2         Gasoline              Jab     Alpha
# 3        Hurricane            Felix     Bravo
# 4            Felix             Demo   Colonel
# 5            Felix        Hurricane   Colonel
# 6            Felix           Indigo   Colonel
# 7           Indigo            Felix   Colonel
# 8            Felix          Colonel      Demo
# 9            Felix        Hurricane      Demo
# 10           Felix           Indigo      Demo
# 11        Gasoline            Alpha      Demo
# 12        Gasoline              Jab      Demo
# 13             Jab         Gasoline      Demo
# 14           Bravo        Hurricane       Egg
# 15         Colonel           Indigo     Felix
# 16            Demo         Gasoline     Felix
# 17            Demo              Jab     Felix
# 18       Hurricane            Bravo     Felix
# 19          Indigo          Colonel     Felix
# 20            Demo            Felix  Gasoline
# 21            Demo              Jab  Gasoline
# 22             Jab             Demo  Gasoline
# 23           Bravo              Egg Hurricane
# 24           Felix          Colonel Hurricane
# 25           Felix             Demo Hurricane
# 26           Felix           Indigo Hurricane
# 27         Colonel            Felix    Indigo
# 28           Felix          Colonel    Indigo
# 29           Felix             Demo    Indigo
# 30           Felix        Hurricane    Indigo
# 31            Demo            Felix       Jab
# 32            Demo         Gasoline       Jab
# 33        Gasoline            Alpha       Jab
# 34        Gasoline             Demo       Jab