R 基于边权和图连通性的子图提取
给定一个描述连通图的边及其权重的矩阵(见下文),我想根据边权重的阈值x提取一个子图。在文献中,我读到人们可以搜索最大x,这样诱导子图是连通的R 基于边权和图连通性的子图提取,r,graph,igraph,threshold,R,Graph,Igraph,Threshold,给定一个描述连通图的边及其权重的矩阵(见下文),我想根据边权重的阈值x提取一个子图。在文献中,我读到人们可以搜索最大x,这样诱导子图是连通的 由于初始图假定是连接的,因此对于任何xI使用python而不是R工作的xI,必须存在一个临界阈值x-critical,即提取的子图是连接的,因此以下只是伪代码 我会处理邻接矩阵(不是igraph对象),因为这将是最快的。 设A为邻接矩阵,W为权值排序列表,W为W的元素。 基本思想是迭代邻接矩阵A、阈值A中每个权重的所有权重,并检查是否有空行(如果图形有方向
由于初始图假定是连接的,因此对于任何
xI使用python而不是R工作的xI,必须存在一个临界阈值x-critical
,即提取的子图是连接的,因此以下只是伪代码
我会处理邻接矩阵(不是igraph对象),因为这将是最快的。
设A
为邻接矩阵,W
为权值排序列表,W
为W
的元素。
基本思想是迭代邻接矩阵A
、阈值A
中每个权重的所有权重,并检查是否有空行(如果图形有方向,则检查列)
那么定向情况的伪码是:
function (A) -> w
W = sort(list(A))
for w in W:
A' = A > w
for row in A':
if sum(row) == 0:
for col in A':
if sum(column) == 0:
return w
有很多方法可以优化这一点,但这可以让人们了解基本的想法#
最快的方法可能是计算每行和每列的最大权重,maxima_行
和maxima_列
,找到它们的最小值,min_max_行
和min_max_列
,然后取这两个值中的最大值,得到w
编辑:
在python中,快速方法如下所示:
from numpy import min, max
def find_threshold_that_disjoints_graph(adjacency_matrix):
"""
For a weighted, fully connected graph, find the weight threshold that results in multiple components.
Arguments:
----------
adjacency_matrix: (N, N) ndarray of floats
Returns:
--------
threshold: float
"""
maxima_rows = max(adajacency_matrix, axis=1) # (N, ) vector
maxima_cols = max(adajacency_matrix, axis=0) # (N, ) vector
min_max_rows = min(maxima_rows) # float
min_max_cols = min(maxima_cols) # float
threshold = max([min_max_rows, min_max_cols])
return threshold
在寻求帮助时,您应该包括一个简单的示例输入和所需的输出,可用于测试和验证可能的解决方案。我编辑了我的初始帖子,并添加了一些代码示例。这样的算法是否有助于我们寻找的内容()?我认为该图是加权的,并且最初是完全连接的?这一页似乎讨论了一个未加权的案例,你想以非常不同的方式处理。是的,你的想法是对的。我也在谈论加权图。上面的伪代码实现了您链接的论文和问题描述第15页上的过程。这是否能很好地解释为“桥梁”的概念取决于这些重量实际上代表了什么。在不熟悉数据采集和处理的情况下,这个问题是无法回答的。您在此建议的最快方法是否适用于无向图?如果是,我应该只关注行吗?此外,我无法准确理解min\u max\u行
代表的内容。它是一个值向量还是仅仅是最大行的最小权重
?
library(igraph)
mat <- expand.grid(LETTERS[1:10], LETTERS[1:10])
mat$Weight <- runif(nrow(mat), 0.01, max = 1)
mat <- mat[mat$Var1!=mat$Var2, ]
g <- graph_from_data_frame(mat)
function (A) -> w
W = sort(list(A))
for w in W:
A' = A > w
for row in A':
if sum(row) == 0:
for col in A':
if sum(column) == 0:
return w
from numpy import min, max
def find_threshold_that_disjoints_graph(adjacency_matrix):
"""
For a weighted, fully connected graph, find the weight threshold that results in multiple components.
Arguments:
----------
adjacency_matrix: (N, N) ndarray of floats
Returns:
--------
threshold: float
"""
maxima_rows = max(adajacency_matrix, axis=1) # (N, ) vector
maxima_cols = max(adajacency_matrix, axis=0) # (N, ) vector
min_max_rows = min(maxima_rows) # float
min_max_cols = min(maxima_cols) # float
threshold = max([min_max_rows, min_max_cols])
return threshold