【问题标题】:Subgraph on dynamic attribute of edge or vertices边或顶点动态属性的子图
【发布时间】:2019-10-30 09:52:52
【问题描述】:

我有一个大图,我需要通过选择包含某个属性的边的顶点来过滤并创建一个子图。边有许多属性 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

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

【问题讨论】:

    标签: r igraph


    【解决方案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)
    

    【讨论】:

    • edge_attr 函数非常有用!谢谢。
    猜你喜欢
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    • 2021-07-22
    • 2023-03-16
    • 1970-01-01
    • 2017-09-18
    相关资源
    最近更新 更多