Ankhnesmerira
Ankhnesmerira

Reputation: 1430

Subgraph on dynamic attribute of edge or vertices

I have a large graph which I need to filter and create a subgraph, by choosing the vertices that include edges of a certain attribute. The edges have many attributes atti={att1, att2, ..., attN} I want to choose edges based on a selected list of attributes

> 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) 

in this case, vertices A,B,E,F,H,I (which have edge att1 > 0) and vertices D,E,F (which have edge att3 > 0 ) should be selected.

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

selected attributes are dynamic. they change and could be many of them so i don't want to hardcode them.

Upvotes: 1

Views: 149

Answers (1)

G5W
G5W

Reputation: 37661

Continuing your example, you can refer to the edge attributes by name.

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

So loop through the selected ones and take the appropriate edges. Then make the subgraph.

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

Upvotes: 1

Related Questions