如何为R IGRAPHE中的某些边指定边权重

如何为R IGRAPHE中的某些边指定边权重,r,igraph,R,Igraph,我想为最短路径中使用的某些边指定一个小的、非负的边权重。下面是一个示例图: library(igraph) data <- read.table(text=" 1 2 1 4 1 5 2 3 2 4 3 4 5 7 5 8 3 6", header=FALSE) gmatrix <- data.matrix(data, rownames.force = NA) #convert into a matrix to use in igraph g <- graph_from_edg

我想为最短路径中使用的某些边指定一个小的、非负的边权重。下面是一个示例图:

library(igraph)
data <- read.table(text="
1 2
1 4
1 5
2 3
2 4
3 4
5 7
5 8
3 6", header=FALSE)
gmatrix <- data.matrix(data, rownames.force = NA) #convert into a matrix to use in igraph
g <- graph_from_edgelist(gmatrix, directed = FALSE) 

我想做的是给这些边指定一个小的ε值。然后,我想调用
get.shortest.paths(g,1,3)
来测试函数是否会识别相同的路径。

好的,我可以帮助:

使用
E()
使用从
get.shortest.paths
获取的ID查询边,并将值分配给新的边属性名称(例如,“权重”或其他名称):

从1到2和从2到3的路径现在具有加权边

现在,为其他边指定零,并“get.shortest.paths”标识另一条路径:

> E(g)$weight <- ifelse(is.na(E(g)$weight), 0, E(g)$weight)
> E(g)$weight
[1] 0.1 0.0 0.0 0.1 0.0 0.0 0.0 0.0 0.0
> get.shortest.paths(g, 1, 3, weights = E(g)$weight)
$vpath
$vpath[[1]]
+ 3/8 vertices, from 605e8c7:
[1] 1 4 3


$epath
NULL

$predecessors
NULL

$inbound_edges
NULL
>E(g)$weight E(g)$weight
[1] 0.1 0.0 0.0 0.1 0.0 0.0 0.0 0.0 0.0
>get.shortest.path(g,1,3,weights=E(g)$weight)
$vpath
$vpath[[1]]
+3/8个顶点,从605e8c7开始:
[1] 1 4 3
$epath
无效的
$前辈
无效的
$U边
无效的
更简洁一点:

g <- set_edge_attr(g, "weight", value = ifelse(E(g) %in% E(g, path = p), 0.1, 0))

E(g)$weight
[1] 0.1 0.0 0.0 0.1 0.0 0.0 0.0 0.0 0.0

g在下面的代码中,我将使用函数
所有最短路径
,而不是
获取最短路径
,来获取所有最短路径。这是因为我稍后将为其中一条边指定权重

请求的路径位于顶点
3
5
之间,因为有两条路径

首先,查看
get.shortest.paths
返回的内容

get.shortest.paths(g, 3, 5)
#$vpath
#$vpath[[1]]
#+ 4/8 vertices, from 98e8e26:
#[1] 3 2 1 5
#
#
#$epath
#NULL
#
#$predecessors
#NULL
#
#$inbound_edges
#NULL
该函数只返回一条路径:
3 2 1 5
。但是在顶点
3
5
之间有两条最短路径

p <- all_shortest_paths(g, 3, 5)
p
#$res
#$res[[1]]
#+ 4/8 vertices, from fc330b4:
#[1] 3 2 1 5
#
#$res[[2]]
#+ 4/8 vertices, from fc330b4:
#[1] 3 4 1 5
#
#
#$nrgeo
#[1] 2 1 1 1 2 1 0 0
现在只有一条路径,路径
3 4 1 5
,以前的路径
p$res[[1]]
现在更重,不再是最短路径

get.shortest.paths
也返回一条路径

get.shortest.paths(g, 3, 5)
#$vpath
#$vpath[[1]]
#+ 4/8 vertices, from 98e8e26:
#[1] 3 4 1 5
#
#
#$epath
#NULL
#
#$predecessors
#NULL
#
#$inbound_edges
#NULL
编辑。

按照用户,以下代码仅使用函数
get.shortest.paths
获取最短路径。将权重分配给其边后,同一函数返回的路径不再是
p2
,它与上面的代码中的路径相同

此代码仅在图形在没有权重属性的情况下重新初始化时才有意义

p2 <- get.shortest.paths(g, 3, 5)
p2
#$vpath
#$vpath[[1]]
#+ 4/8 vertices, from 61bfc89:
#[1] 3 2 1 5
#
#
#$epath
#NULL
#
#$predecessors
#NULL
#
#$inbound_edges
#NULL


E(g, path = p2$vpath[[1]])$weight <- .Machine$double.eps^0.5
E(g)$weight
get.shortest.paths(g, 3, 5)
#$vpath
#$vpath[[1]]
#+ 4/8 vertices, from 61bfc89:
#[1] 3 4 1 5
#
#
#$epath
#NULL
#
#$predecessors
#NULL
#
#$inbound_edges
#NULL

p2提取这两条边是什么意思?要提取路径的端点并为其指定
权重
,我指的是最短路径中使用的边。假设我的最短路径是(3-7-9-4)。然后,我想将边权重指定给(3-7)/(7-9)/(9-4)。让我重新措辞。很抱歉,这个问题不清楚,但op特别想调用
get.shortest.paths
而不是
all\u shortest\u path
,因为他们只想解决一个简单的问题,如何比较两条路径以确定它们是否相同?我使用了
length(difference(p,q)
,其中p是原始最短路径,q是更新边后识别的下一条路径。我想知道是否有更好的方法?可能
all(p%在%q中)
?太好了!谢谢。最后一个问题是你知道如何删除边缘权重属性吗?我正在尝试使用此处的建议,但无法完成。或者
E(g)$weight
p <- all_shortest_paths(g, 3, 5)
p
#$res
#$res[[1]]
#+ 4/8 vertices, from fc330b4:
#[1] 3 2 1 5
#
#$res[[2]]
#+ 4/8 vertices, from fc330b4:
#[1] 3 4 1 5
#
#
#$nrgeo
#[1] 2 1 1 1 2 1 0 0
E(g, path = p$res[[1]])$weight <- .Machine$double.eps^0.5
E(g)$weight
#[1] 1.490116e-08           NA 1.490116e-08 1.490116e-08           NA
#[6]           NA           NA           NA           NA
all_shortest_paths(g, 3, 5)
#$res
#$res[[1]]
#+ 4/8 vertices, from fc330b4:
#[1] 3 4 1 5
#
#
#$nrgeo
#[1] 1 0 1 1 1 1 0 0
get.shortest.paths(g, 3, 5)
#$vpath
#$vpath[[1]]
#+ 4/8 vertices, from 98e8e26:
#[1] 3 4 1 5
#
#
#$epath
#NULL
#
#$predecessors
#NULL
#
#$inbound_edges
#NULL
p2 <- get.shortest.paths(g, 3, 5)
p2
#$vpath
#$vpath[[1]]
#+ 4/8 vertices, from 61bfc89:
#[1] 3 2 1 5
#
#
#$epath
#NULL
#
#$predecessors
#NULL
#
#$inbound_edges
#NULL


E(g, path = p2$vpath[[1]])$weight <- .Machine$double.eps^0.5
E(g)$weight
get.shortest.paths(g, 3, 5)
#$vpath
#$vpath[[1]]
#+ 4/8 vertices, from 61bfc89:
#[1] 3 4 1 5
#
#
#$epath
#NULL
#
#$predecessors
#NULL
#
#$inbound_edges
#NULL