You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cIGraph_basic_query.c 6.1 kB

19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include "igraph.h"
  2. #include "ruby.h"
  3. #include "cIGraph.h"
  4. /* call-seq:
  5. * graph.vcount() -> Fixnum
  6. *
  7. * Returns the number of vertices in the IGraph object.
  8. *
  9. * Example:
  10. *
  11. * g = IGraph.new([1,2,3,4],true)
  12. * g.vcount # returns 4
  13. *
  14. */
  15. VALUE cIGraph_vcount(VALUE self){
  16. igraph_t *graph;
  17. Data_Get_Struct(self, igraph_t, graph);
  18. return INT2NUM(igraph_vcount(graph));
  19. }
  20. /* call-seq:
  21. * graph.ecount() -> Fixnum
  22. *
  23. * Returns the number of edges in the IGraph object.
  24. *
  25. * Example:
  26. *
  27. * g = IGraph.new([1,2,3,4],true)
  28. * g.ecount # returns 2
  29. *
  30. */
  31. VALUE cIGraph_ecount(VALUE self){
  32. igraph_t *graph;
  33. Data_Get_Struct(self, igraph_t, graph);
  34. return INT2NUM(igraph_ecount(graph));
  35. }
  36. /* call-seq:
  37. * graph.edge(eid) -> [from,to]
  38. *
  39. * Returns an Array comprising the vertices making up the edge represented
  40. * by the edge id eid. You can find the id of an edge using IGraph#get_eid.
  41. *
  42. * Example:
  43. *
  44. * g = IGraph.new([1,2,3,4],true)
  45. * g.edge(1) # returns [1,2]
  46. *
  47. */
  48. VALUE cIGraph_edge(VALUE self, VALUE eid){
  49. igraph_t *graph;
  50. igraph_integer_t from = 0;
  51. igraph_integer_t to = 0;
  52. VALUE from_r;
  53. VALUE to_r;
  54. Data_Get_Struct(self, igraph_t, graph);
  55. igraph_edge(graph,NUM2INT(eid),&from,&to);
  56. from_r = cIGraph_get_vertex_object(self,from);
  57. to_r = cIGraph_get_vertex_object(self,to);
  58. return rb_ary_new3(2, from_r, to_r);
  59. }
  60. /* call-seq:
  61. * graph.get_eid(from,to,directed) -> Fixnum
  62. *
  63. * Returns the edge id of the edge connecting the vertices from and to.
  64. * directed is a boolean specifying whether to search for directed edges
  65. * in a directed graph.
  66. *
  67. * Example:
  68. *
  69. * g = IGraph.new([1,2,3,4],true)
  70. * g.get_eid(1,2,true) # returns 1
  71. *
  72. */
  73. VALUE cIGraph_get_eid(VALUE self, VALUE from, VALUE to, VALUE directed){
  74. igraph_t *graph;
  75. igraph_integer_t eid = 0;
  76. int from_i = 0;
  77. int to_i = 0;
  78. igraph_bool_t directed_b = 0;
  79. Data_Get_Struct(self, igraph_t, graph);
  80. from_i = cIGraph_get_vertex_id(self,from);
  81. to_i = cIGraph_get_vertex_id(self,to);
  82. if(directed)
  83. directed_b = 1;
  84. igraph_get_eid(graph,&eid,from_i,to_i,directed_b);
  85. return INT2NUM(eid);
  86. }
  87. /* call-seq:
  88. * graph.neighbours(vertex,mode) -> Array
  89. *
  90. * Returns an Array of the neighbouring vertices to vertex. mode defines
  91. * the way adjacent vertices are searched for directed graphs. It can have
  92. * the following values: IGraph::OUT, vertices reachable by an edge from the
  93. * specified vertex are searched, IGraph::IN, vertices from which the
  94. * specified vertex is reachable are searched. IGraph::ALL, both kind of
  95. * vertices are searched. This parameter is ignored for undirected graphs.
  96. *
  97. * Example:
  98. *
  99. * g = IGraph.new([1,2,3,4],true)
  100. * g.neighbours(1,IGraph::ALL) # returns [2]
  101. *
  102. */
  103. VALUE cIGraph_neighbors(VALUE self, VALUE v, VALUE mode){
  104. igraph_t *graph;
  105. igraph_integer_t pnode;
  106. igraph_neimode_t pmode = NUM2INT(mode);
  107. igraph_vector_t neis;
  108. int i;
  109. VALUE neighbors = rb_ary_new();
  110. igraph_vector_init_int(&neis,0);
  111. Data_Get_Struct(self, igraph_t, graph);
  112. pnode = cIGraph_get_vertex_id(self,v);
  113. igraph_neighbors(graph,&neis,pnode,pmode);
  114. for(i=0;i<igraph_vector_size(&neis);i++){
  115. rb_ary_push(neighbors,cIGraph_get_vertex_object(self,VECTOR(neis)[i]));
  116. }
  117. igraph_vector_destroy(&neis);
  118. return neighbors;
  119. }
  120. /* call-seq:
  121. * graph.adjacent(vertex,mode) -> Array
  122. *
  123. * Returns an Array of the adjacent edge ids to vertex. mode defines
  124. * the way adjacent edges are searched for directed graphs. It can have
  125. * the following values: IGraph::OUT means only outgoing edges, IGraph::IN
  126. * only incoming edges, IGraph::ALL both. This parameter is ignored for
  127. * undirected graphs.
  128. *
  129. * Example:
  130. *
  131. * g = IGraph.new([1,2,3,4],true)
  132. * g.adjacent(1,IGraph::ALL) # returns [1]
  133. *
  134. */
  135. VALUE cIGraph_adjacent(VALUE self, VALUE v, VALUE mode){
  136. igraph_t *graph;
  137. igraph_integer_t pnode;
  138. igraph_neimode_t pmode = NUM2INT(mode);
  139. igraph_vector_t eids;
  140. int i;
  141. VALUE eids_r = rb_ary_new();
  142. igraph_vector_init_int(&eids,0);
  143. Data_Get_Struct(self, igraph_t, graph);
  144. pnode = cIGraph_get_vertex_id(self,v);
  145. igraph_adjacent(graph,&eids,pnode,pmode);
  146. for(i=0;i<igraph_vector_size(&eids);i++){
  147. rb_ary_push(eids_r,INT2NUM(VECTOR(eids)[i]));
  148. }
  149. igraph_vector_destroy(&eids);
  150. return eids_r;
  151. }
  152. /* call-seq:
  153. * graph.is_directed?() -> true/false
  154. *
  155. * Returns a boolean specifying whether the graph is directed or not.
  156. *
  157. * Example:
  158. *
  159. * g = IGraph.new([1,2,3,4],true)
  160. * g.is_directed? #returns true
  161. *
  162. */
  163. VALUE cIGraph_is_directed(VALUE self){
  164. igraph_t *graph;
  165. Data_Get_Struct(self, igraph_t, graph);
  166. return igraph_is_directed(graph) ? Qtrue : Qfalse;
  167. }
  168. /* call-seq:
  169. * graph.degree(vs,mode,loops) -> Array
  170. *
  171. * Returns an Array of Integers specifying the degree of each of the
  172. * vertices specified in the vs Array. mode defines the type of the degree.
  173. * IGraph::OUT, out-degree, IGraph::IN, in-degree, IGraph::ALL, total degree
  174. * (sum of the in- and out-degree). This parameter is ignored for undirected
  175. * graphs.
  176. *
  177. * Example:
  178. *
  179. * g = IGraph.new([1,2,3,4],true)
  180. * g.is_directed? #returns true
  181. *
  182. */
  183. VALUE cIGraph_degree(VALUE self, VALUE v, VALUE mode, VALUE loops){
  184. igraph_t *graph;
  185. igraph_vs_t vids;
  186. igraph_vector_t vidv;
  187. igraph_neimode_t pmode = NUM2INT(mode);
  188. igraph_bool_t loop_mode = loops ? 1 : 0;
  189. igraph_vector_t res;
  190. int i;
  191. VALUE degree_r = rb_ary_new();
  192. //vector to hold the results of the degree calculations
  193. igraph_vector_init_int(&res,0);
  194. Data_Get_Struct(self, igraph_t, graph);
  195. //Convert an array of vertices to a vector of vertex ids
  196. igraph_vector_init_int(&vidv,0);
  197. cIGraph_vertex_arr_to_id_vec(self,v,&vidv);
  198. //create vertex selector from the vecotr of ids
  199. igraph_vs_vector(&vids,&vidv);
  200. igraph_degree(graph,&res,vids,pmode,loop_mode);
  201. for(i=0;i<igraph_vector_size(&res);i++){
  202. rb_ary_push(degree_r,INT2NUM(VECTOR(res)[i]));
  203. }
  204. igraph_vector_destroy(&vidv);
  205. igraph_vector_destroy(&res);
  206. igraph_vs_destroy(&vids);
  207. return degree_r;
  208. }

Ruby binding for the igraph library.