0

I'm going to write a function that needs to read the adjacent matrix of a graph created by igraph package in R. Since the function is going to run millions of times, I really want to know whether the graph, when used as an argument in a function, is passed by reference or value? The function won't change the graph itself. It just read the adjacent matrix. So there won't be any assignment to the graph.

lovetl2002
  • 910
  • 9
  • 23
  • The question is not related to igraph object , it is whether a matrix ( Matrix here) is passed by reference or copied. You can use `tracemem` to check the memory behavior. I think second answer [here](http://stackoverflow.com/questions/2603184/r-pass-by-reference) can help you. – agstudy Aug 07 '14 at 00:18

1 Answers1

0

It will be copied by reference. R only makes copies of function arguments if you modify them. If you function does not modify the graph, then it will not be copied.

Although you did not ask this, I will also say, that when you call an igraph function that is implemented in C (like most igraph functions), the graph will still not be copied. This is not trivially true, because igraph is a C library, so it works with C data structures. But igraph is carefully written to use the data in the R object, so the graph will not be copied.

(Btw. I am not sure what you mean about adjacency matrices. Igraph graphs are not adjacency matrices.)

Gabor Csardi
  • 10,705
  • 1
  • 36
  • 53
  • Thank you. I don't know how igraph object is stored, but there are functions that can access the adjacency matrix of igraph. – lovetl2002 Aug 09 '14 at 02:13
  • If you mean `g[]` if `g` is a graph, it is not really "accessed", it is created over and over again, so if you want to use it repeatedly, then store it. `g[]` is just a shorthand for `get.adjacency(g)`. – Gabor Csardi Aug 09 '14 at 02:23