1
votes

sorry I'm rather new to this igraph package Right now I'm having a non-simple cycle, and I'm trying to simplify the cycle by going through every node and delete edges that meet certain criteria. I have been working on this problem for a couple of hours and would appreciate some suggestion to solve this problem.

Right now I am working on a function, given the vertex of a graph, then we want to eliminate all edges with length less than the maximum length of all these edges from that particular vertex

df=data.frame(node("A","A","B","B","A"),edge("B","C","C","A","B"),length(1,1,1,1,10))
g<-graph_from_data_frame(df)
delete_extra_edges<-function(g,node){
   list_of_incident_edges=incident_edges(g,node,"out")
   max_quantity<-max(unlist(list_of_incident_edges[[1]]$length))
   list_to_delete=which(list_of_incident_edges[[1]]$length<max_quantity)
   for(index in list_to_delete){
       g<-delete_edges(g,index) #or alternatively: g<-g-list_of_incident_edges[[1]][index]
   }
   return(g)
   }

The problem with this method is that (other than the fact it will still retain multiple edges that have the same length as the maximum length, but I will deal with that later), the index of the list_to_delete does not necessarily equate the edge ID in the graph g.

The problem with the alternative implementation is that it gives me the "Cannot use an edge from another graph" error

I am just working with a test data frame df as defined above

1

1 Answers

1
votes

There are a few errors in your current code.

  1. I know it's just an example but your data frame is not constructing properly.

  2. You often use '=' to assign values to variables, but in R you use '<-' instead.

  3. You need more code to catch node "C"

  4. Removing the for-loop and deleting all edges at once seems to work.

This seems to be doing what you want it to for node "A":

library(igraph)
node <- c("A","A","B","B","A")
edge <- c("B","C","C","A","B")
length <- c(1,1,1,1,10)

df<-data.frame(node,edge,length)
g<-graph_from_data_frame(df)
delete_extra_edges<-function(graph,node){
  list_of_incident_edges<-incident_edges(graph,node,"out")
  max_quantity<-max(unlist(list_of_incident_edges[[1]]$length))
  list_to_delete <- which(list_of_incident_edges[[1]]$length < max_quantity)
  newgraph <- graph - list_of_incident_edges[[1]]
  return(newgraph)
}