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

Posted

技术标签:

【中文标题】检索 Igraph 中所有节点的一阶连接和二阶连接【英文标题】:Retrieve 1st Degree Connections and 2nd Degree Connections for All Nodes in Igraph 【发布时间】:2016-10-09 00:45:21 【问题描述】:

我有一个 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)

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

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】:

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

【讨论】:

这很完美,而且代码比我想象的要少得多。感谢您的帮助!

以上是关于检索 Igraph 中所有节点的一阶连接和二阶连接的主要内容,如果未能解决你的问题,请参考以下文章

如何在 R 中删除 igraph 中的节点?

IGraph:直到停止节点/顶点的网络距离

MATLAB中带有两个变量的函数的一阶和二阶偏导数

什么是一阶导数和二阶导数?

matlab 计算灰度图像的一阶矩二阶矩三阶矩

如何将 igraph 拆分为连接的子图?