Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在igraph中使用bfs查找生成树_R_Igraph - Fatal编程技术网

在igraph中使用bfs查找生成树

在igraph中使用bfs查找生成树,r,igraph,R,Igraph,我想使用igraph函数graph.bfs在图中查找生成树。 你能告诉我怎么做吗 PS:我试图使用graph.bfs返回值的$father信息,但结果让我感到困惑。以下是一个例子: g <- graph(c(1,2,2,6,1,4,4,6,5,6,1,5,5,3,3,4), directed=FALSE) plot(g) tmp <- graph.bfs(g, root=1, neimode='all', order=TRUE, father=TRUE,callback=f) g

我想使用igraph函数
graph.bfs
在图中查找生成树。 你能告诉我怎么做吗

PS:我试图使用
graph.bfs
返回值的
$father
信息,但结果让我感到困惑。以下是一个例子:

g <- graph(c(1,2,2,6,1,4,4,6,5,6,1,5,5,3,3,4), directed=FALSE)
plot(g)

tmp <- graph.bfs(g, root=1, neimode='all', order=TRUE, father=TRUE,callback=f)

g父
向量由节点索引,即,它的顺序与
顺序
不同

library(igraph)
g <- graph(c(1,2,2,6,1,4,4,6,5,6,1,5,5,3,3,4), directed=FALSE)
r <- graph.bfs(g, root=1, neimode='all', order=TRUE, father=TRUE)
h <- graph( rbind(r$order, r$father[r$order])[,-1], directed=FALSE )
plot(h)
i
th元素的
order
是前遍历顺序中
i
th节点的名称(或索引)

父节点的
i
th元素是索引为
i
的节点的父节点的名称(或索引)——而不是
order
i
第th元素的名称。
order
i
th元素的父元素是
parent[order[i]]
。这就是我们需要定义边的地方

因此,树的边缘是:

order:  1 2 4 5 6 3
        | | | | | |
father: 0 1 1 1 2 4.

父向量由节点索引,即,它的顺序与顺序不同

library(igraph)
g <- graph(c(1,2,2,6,1,4,4,6,5,6,1,5,5,3,3,4), directed=FALSE)
r <- graph.bfs(g, root=1, neimode='all', order=TRUE, father=TRUE)
h <- graph( rbind(r$order, r$father[r$order])[,-1], directed=FALSE )
plot(h)
i
th元素的
order
是前遍历顺序中
i
th节点的名称(或索引)

父节点的
i
th元素是索引为
i
的节点的父节点的名称(或索引)——而不是
order
i
第th元素的名称。
order
i
th元素的父元素是
parent[order[i]]
。这就是我们需要定义边的地方

因此,树的边缘是:

order:  1 2 4 5 6 3
        | | | | | |
father: 0 1 1 1 2 4.
要避免以下错误: 简单索引错误(x,ii,不确定):选择未知顶点 我们需要将其中一条代码语句更改为:

h <- graph( rbind(r$order, r$father[r$order, na_ok = TRUE])[,-1], directed=FALSE )
h以避免以下错误:
简单索引错误(x,ii,不确定):选择未知顶点
我们需要将其中一条代码语句更改为:

h <- graph( rbind(r$order, r$father[r$order, na_ok = TRUE])[,-1], directed=FALSE )

h我认为
顺序
很简单。对于父亲

> r$order
6/6 vertices, from 29ab0f7:
[1] 1 2 4 5 6 3
> r$father
+ 6/6 vertices, from 29ab0f7:
[1] NA  1  4  1  1  2
  • 节点1(标签为1的节点)没有父节点-->NA

  • 节点2将节点1作为父节点

  • 节点3将节点4作为父节点

  • 节点4将节点1作为父节点

  • 节点5将节点1作为父节点

  • 节点6将节点2作为父节点*

在用星号表示的情况下,很容易产生混淆。 “为什么节点6由节点2生成,而不是节点4?”。答案是,父元素不是节点6连接到的横向序列中最近的元素,而是节点6连接到的横向序列中最早的元素。i、 e.节点6连接到节点2和节点4(节点2在横向序列中较早)。下面是一个让这个概念变得显而易见的例子

g <- sample_smallworld(1, 5, 5, 0.05)
plot(g)
r <- graph.bfs(g, root=1, neimode='all', order=TRUE, father=TRUE)

我认为
顺序
很简单。对于父亲

> r$order
6/6 vertices, from 29ab0f7:
[1] 1 2 4 5 6 3
> r$father
+ 6/6 vertices, from 29ab0f7:
[1] NA  1  4  1  1  2
  • 节点1(标签为1的节点)没有父节点-->NA

  • 节点2将节点1作为父节点

  • 节点3将节点4作为父节点

  • 节点4将节点1作为父节点

  • 节点5将节点1作为父节点

  • 节点6将节点2作为父节点*

在用星号表示的情况下,很容易产生混淆。 “为什么节点6由节点2生成,而不是节点4?”。答案是,父元素不是节点6连接到的横向序列中最近的元素,而是节点6连接到的横向序列中最早的元素。i、 e.节点6连接到节点2和节点4(节点2在横向序列中较早)。下面是一个让这个概念变得显而易见的例子

g <- sample_smallworld(1, 5, 5, 0.05)
plot(g)
r <- graph.bfs(g, root=1, neimode='all', order=TRUE, father=TRUE)

我同意上面“Vincent Zoonekynd”给出的答案。然而,它并没有像我一样起作用。所以我做了一些修改以使它工作。这是我的密码

library(igraph)
g2 <- graph(c(1,2,2,6,1,4,4,6,5,6,1,5,5,3,3,4), directed=FALSE)
r <- graph.bfs(g2, root=1, neimode='all', order=TRUE, father=TRUE)
a = as.integer(r$order)
aa = as.integer(r$father)
h <- graph( rbind(a, aa[a])[,-1], directed=FALSE )
plot(h)
库(igraph)

g2我同意上面“Vincent Zoonekynd”给出的答案。然而,它并没有像我一样起作用。所以我做了一些修改以使它工作。这是我的密码

library(igraph)
g2 <- graph(c(1,2,2,6,1,4,4,6,5,6,1,5,5,3,3,4), directed=FALSE)
r <- graph.bfs(g2, root=1, neimode='all', order=TRUE, father=TRUE)
a = as.integer(r$order)
aa = as.integer(r$father)
h <- graph( rbind(a, aa[a])[,-1], directed=FALSE )
plot(h)
库(igraph)

g2看起来是正确的。但是你能解释一下“父向量是由节点索引的”吗?我不明白。谢谢我试图补充一些解释,我想我能理解。但这是一个糟糕的设计吗?如果你没有
$order
,那么
$father
就没用了。这是一个不错的设计。您需要
order
father
,或者
order
father[order]
,您可以轻松地从一种表示切换到另一种表示。使用后一种表示法构建树更为简单。看起来是正确的。但是你能解释一下“父向量是由节点索引的”吗?我不明白。谢谢我试图补充一些解释,我想我能理解。但这是一个糟糕的设计吗?如果你没有
$order
,那么
$father
就没用了。这是一个不错的设计。您需要
order
father
,或者
order
father[order]
,您可以轻松地从一种表示切换到另一种表示。使用后一种表示法构建树更简单。为什么不使用
最小生成树()
?我只想尝试不同的方法来查找生成树,而不仅仅是最小生成树。为什么不使用
最小生成树()呢
?我只是想尝试不同的方法来寻找生成树,而不仅仅是最小生成树。