Skip to content
Prev 244666 / 398502 Next

help requested

On Sat, Dec 11, 2010 at 05:11:37AM -0800, profaar wrote:
Under Linux, with 20'000 nodes and 10 random edges from each of them, this
took abuot 108 sec (CPU 2.4 GHz). The advantage of this solution is that
there may be further functions in the package graph (see also class?graphNEL),
which could be used in your application. If not, then the conversion itself
may be done more efficiently, for example

  edges <- read.table(file=stdin())
1 2
1 3
1 4
1 5
2 3
2 4
3 2
4 1
4 3
4 5
5 2
5 4

  out1 <- split(edges$V2, edges$V1)
  out1

  $`1`
  [1] 2 3 4 5
  
  $`2`
  [1] 3 4
  
  $`3`
  [1] 2
  
  $`4`
  [1] 1 3 5
  
  $`5`
  [1] 2 4

For the example with 20'000 nodes and 10 random edges from each, this 
took about 0.2 sec.

The output out1 is a list of vectors. This may be transformed to
a vector of strings, for example

  out2 <- sapply(out1, paste, collapse=" ")
  cbind(out2) # cbind() is only for a column output

  out2     
1 "2 3 4 5"
2 "3 4"    
3 "2"      
4 "1 3 5"  
5 "2 4"    

and to a text (with a possible file= argument)

  cat(paste(names(out2), out2), sep="\n")

  1 2 3 4 5
  2 3 4
  3 2
  4 1 3 5
  5 2 4

Petr Savicky.