title: R shortest.paths for loops
body: <p>I have a task to <strong>repeat the function shortest.paths,but my input is too large</strong>.I wanna know <strong>how to get it fast</strong>.To all my known, the <strong>igraph</strong> is the best choice. My kernel is as follows:</p>
  
  <p>First, I got a real network and 1000 random network as 'list' format</p>
  
  <pre><code>library(igraph)
  # real ppi network
  REAL.PPI &lt;- paste0(RANDM, "real.ppi.txt")
  real.ppi &lt;- graph.data.frame(read.table(REAL.PPI, header = F), directed = F)
  ppi.gs &lt;- V(real.ppi)$name
  # random network
  random_net_names &lt;- dir(paste0(RANDM, "randomnetwork"))
  random_nets &lt;- lapply(random_net_names, function(x){
    path &lt;- paste(RANDM, "randomnetwork/", x, sep="")
    rn &lt;- read.table(path, header = F)
    graph.data.frame(rn, directed = F)
  }
  </code></pre>
  
  <p>Then, I have to <strong>compare the node sets' shortest paths in real and random networks</strong>.For this, I choose for-loop instead apply-function because the latter will not be faster.</p>
  
  <p>input format is as:</p>
  
  <pre><code>hsa-let-7a-2-3p hsa-let-7a-3p   GO:0001702  4040    10818   4089
  hsa-let-7a-2-3p hsa-let-7a-3p   GO:0001764  27185   2625    5048    429 6695
  </code></pre>
  
  <p>My kernel is as follows:</p>
  
  <pre><code># input
  ovr &lt;- strsplit(readLines("ovr.txt"), '\t')
  # for-loop
  OUT.final &lt;- outfile("out.txt", "w")
  for(i in 1 : length(ovr)){
    hyper.ppi.ovr &lt;- ovr[[i]][- c(1, 2, 3)]
    D1 &lt;- shortest.paths(real.ppi, hyper.ppi.ovr, hyper.ppi.ovr)
    CPLs &lt;- sapply(random_nets, function(r_net){
      sum(shortest.paths(r_net, hyper.ppi.ovr, hyper.ppi.ovr)) 
    }
    )
    D2.p &lt;- sum(CPLs &lt; D1) / 1000
    if(D2.p &gt; .01)next
    # output
    out.value &lt;- ovr[[i]]
    cat(out.value, sep = "\t", file = OUT.final)
    cat("\n", file = OUT.final)
  }
  close(OUT.final)
  </code></pre>