Longest path of a weighted DAG using R igraph

I have followed the longest way of computing a weighted DAG using R igraph.

My implementation (shown below) is slow for large plots. I would really appreciate any hints to speed this up. Any thoughts on how far my implementation is from the most famous / typical algorithm is also welcome.

Thank!

# g is the igraph DAG
# g <- graph.tree(10000, 2, mode="out")
# E(g)$weight <- round(runif(length(E(g))),2) * 50 
# Topological sort
tsg <- topological.sort(g)    
# Set root path attributes
# Root distance
V(g)[tsg[1]]$rdist <- 0
# Path to root
V(g)[tsg[1]]$rpath <- tsg[1]
# Get longest path from root to every node        
for(node in tsg[-1])
{
  # Get distance from node predecessors
  w <- E(g)[to(node)]$weight
  # Get distance from root to node predecessors
  d <- V(g)[nei(node,mode="in")]$rdist
  # Add distances (assuming one-one corr.)
  wd <- w+d
  # Set node distance from root to max of added distances 
  mwd <- max(wd)
  V(g)[node]$rdist <- mwd
  # Set node path from root to path of max of added distances
  mwdn <- as.vector(V(g)[nei(node,mode="in")])[match(mwd,wd)]
  V(g)[node]$rpath <- list(c(unlist(V(g)[mwdn]$rpath), node))      
}
# Longest path length is the largest distance from root
lpl <- max(V(g)$rdist)    
# Enumerate longest path
lpm <- unlist(V(g)[match(lpl,V(g)$rdist)]$rpath)    
V(g)$critical <- 0
g <- set.vertex.attribute(g, name="critical", index=lpm, value=1)    

      

+3


source to share


1 answer


I also had a slow version of R. It took ~ 20 minutes for 200K edges and 30K vertices, so I broke and implemented get.shortest.paths()

for negative edge weight plots, which can be used to find the longest paths by inverting all edge weights. You can try my R fork igraph

here .



I experienced 100x and 1000x acceleration when switching from my R to C implementation.

+1


source







All Articles