如何仅查找某些顶点的邻域? [英] How to find the neighborhood for certain vertices only?
问题描述
问题开始了我有一个带n<100
个顶点的无向图g
.该图很简单.所有顶点的坐标都是整数(x_i, y_i), i=1, 2,..., n
,边的集合是预定义的,它们是长度为1
单位的线段.
顶点的度可以是2
,3
或4
.
I have an undirected graph g
with n<100
vertices. The graph is simple. The coordinates of all vertices are integer (x_i, y_i), i=1, 2,..., n
, the set of edges is predefinded, they are line segments with the length 1
unit.
The degree of vertices can be 2
, 3
or 4
.
library(igraph)
g <- graph_from_literal(1-2-3-4-5-6-7-8-1, 8-9-4)
B <- t(matrix(c(0,0, 0,1, 0,2, -1,2, -2,2, -2,1, -2,0, -1,0, -1,1), nrow =2));
V(g)$id <- seq_len(vcount(g))
V(g)$x <- B[,1]; V(g)$y <- B[,2]
plot(g, layout=as.matrix(B))
我需要为corner
属性的顶点设置新属性.
I need to set the new attribute for vertex the corner
attribute.
如果顶点的度数为2
并且两个入射边不在同一条线上,则我们说顶点i
是corner
顶点.在顶点上方的图中,1, 3, 5, 7
是拐角顶点,而其余的顶点2, 4, 6, 8, 9
是非拐角.
We say the vertex i
is the corner
vertex if its degree is 2
and two incident edges do not lie on the same line. On the plot above vertices 1, 3, 5, 7
are corner vertices while remaining vertices 2, 4, 6, 8, 9
are non-corner.
我的尝试
我找到了度数等于2
的顶点列表.
I have found the list of vertices that have degree equal to 2
.
idv <- V(g)[strength(g)==2]; idv # 1 2 3 5 6 7 9
然后找到第i
个顶点的邻域顶点列表.
Then the list of neighborhood vertices for i
-th vertex was found.
neigh<-neighborhood(g, idv); neigh
错误在这里,因为我看到了所有顶点的邻域顶点,而不仅是度数等于2
的顶点.例如,
The error is here, because I see the neighborhood vertices for all vertices, no only vertices that have degree equal to 2
. For instance,
neigh[[4]]; neigh[[8]];
#[1] 4 3 5 9
#[1] 8 1 7 9
问题.如何使用neighborhood
函数查找仅度为2
的顶点的邻域?
Question. How to use the neighborhood
function to find the neighborhood for vertices with degree 2
only?
推荐答案
该函数正确,但是第二个参数与感兴趣的顶点无关,它是第三个参数:
The function is correct, but the second argument isn't about vertices of interest, it's the third one:
neighborhood
# function (graph, order = 1, nodes = V(graph), mode = c("all",
# "out", "in"), mindist = 0)
# {
# ...
因此
length(neighborhood(g, nodes = idv))
# [1] 7
完成工作.
这篇关于如何仅查找某些顶点的邻域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!