关于边或顶点的动态属性的子图

时间:2019-10-30 09:52:52

标签: r igraph

我有一个大图,需要通过选择包含某个属性的边的顶点来过滤和创建子图。边具有许多属性atti = {att1,att2,...,attN} 我想根据选定的属性列表选择边缘

> require(igraph)
> graph <- make_ring(9)  #this is my original graph
> V(graph)$name <- c("A", "B", "C", "D", "E", "F", "G", "H", "I")  #name of vertices
> E(graph)$att1 <- c(1,0,0,0,1,0,0,1,0)
> E(graph)$att2 <- c(0,3,1,0,0,1,0,0,1)
> E(graph)$att3 <- c(0,0,0,1,4,0,0,0,0)
> E(graph)$att4 <- c(1,0,1,0,2,1,0,0,0)

> # this is where I have issue. i don't know how to select vertices based on a dynamically selecting edges
> selected_atts <- c("att1", "att3")
> selected_vertices <- V(graph)[inc(E(graph)[which(selected_atts> 0)])] ## select all vertices that are linked by edges with att1 > 0 or att3 > 0 
> subgraph_list <- make_ego_graph(graph, order=1, selected_vertices) 

在这种情况下,应选择顶点A,B,E,F,H,I(边att1> 0的顶点)和顶点D,E,F(边att3> 0的顶点)。

A--B att1 = 1  att3 = 0  --> select this edge, so select nodes A and B
B--C att1 = 0  att3 = 0
C--D att1 = 0  att3 = 0
D--E att1 = 0  att3 = 1  --> select this edge, so select nodes D and E
E--F att1 = 1  att3 = 4  --> select this edge, so select nodes E and F
F--G att1 = 0  att3 = 0
G--H att1 = 0  att3 = 0
H--I att1 = 1  att3 = 0  --> select this edge, so select nodes H and I
I--A att1 = 0  att3 = 0

选定的属性是动态的。它们会发生变化,可能有很多,所以我不想对其进行硬编码。

1 个答案:

答案 0 :(得分:1)

继续您的示例,您可以按名称引用边属性。

edge_attr(graph)[["att1"]]
[1] 1 0 0 0 1 0 0 1 0

因此遍历选定的对象并占据适当的边缘。然后制作子图。

selected_atts <- c("att1", "att3")

KeepEdge = 1:ecount(graph)
for(SA in selected_atts) {
    KeepEdge = intersect(Keep, which(edge_attr(graph)[[SA]] > 0)) }

SG = subgraph.edges(graph, KeepEdge)
plot(SG)

Subgraph