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_generators_deterministic.c 9.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #include "igraph.h"
  2. #include "ruby.h"
  3. #include "cIGraph.h"
  4. /* call-seq:
  5. * IGraph::Generate.adjacency(matrix,mode) -> IGraph
  6. *
  7. * Creates a graph object from an adjacency matrix.
  8. *
  9. * matrix should be given as an IGraphMatrix object. mode controls how the
  10. * matrix is interpreted:
  11. *
  12. * IGraph::ADJ_DIRECTED - The graph will be directed and an element
  13. * gives the number of edges between two vertex.
  14. *
  15. * IGraph::ADJ_UNDIRECTED - This is the same as
  16. * IGraph::ADJ_MAX, for convenience.
  17. *
  18. * IGraph::ADJ_MAX - Undirected graph will be created and the
  19. * number of edges between vertex i and j is max(A(i,j), A(j,i)).
  20. *
  21. * IGraph::ADJ_MIN - Undirected graph will be created with
  22. * min(A(i,j), A(j,i)) edges between vertex i and j.
  23. *
  24. * IGraph::ADJ_PLUS - Undirected graph will be created with
  25. * A(i,j)+A(j,i) edges between vertex i and j.
  26. *
  27. * IGraph::ADJ_UPPER - Undirected graph will be created, only the
  28. * upper right triangle (including the diagonal) is used for the number of
  29. * edges.
  30. *
  31. * IGraph::ADJ_LOWER - Undirected graph will be created, only the
  32. * lower left triangle (including th * e diagonal) is used for creating the
  33. * edges.
  34. */
  35. VALUE cIGraph_adjacency(VALUE self, VALUE matrix, VALUE mode){
  36. igraph_t *graph;
  37. igraph_matrix_t *matrixp;
  38. VALUE new_graph;
  39. new_graph = cIGraph_alloc(cIGraph);
  40. Data_Get_Struct(new_graph, igraph_t, graph);
  41. Data_Get_Struct(matrix, igraph_matrix_t, matrixp);
  42. igraph_destroy(graph);
  43. igraph_adjacency(graph, matrixp, NUM2INT(mode));
  44. return new_graph;
  45. }
  46. /* call-seq:
  47. * IGraph::Generate.star(n,mode,center) -> IGraph
  48. *
  49. * Creates a star graph, every vertex connects only to the center.
  50. *
  51. * The number of vertices should be given in n. mode gives the type of the
  52. * star graph to create. Possible values:
  53. *
  54. * IGraph::STAR_OUT - directed star graph, edges point from the center to the
  55. * other vertices.
  56. *
  57. * IGraph::STAR_IN - directed star graph, edges point to the center from the
  58. * other vertices.
  59. *
  60. * IGraph::STAR_UNDIRECTED - an undirected star graph is created.
  61. *
  62. * The id of the vertex which will be the center of the graph is given by
  63. * center
  64. */
  65. VALUE cIGraph_star(VALUE self, VALUE n, VALUE mode, VALUE center){
  66. igraph_t *graph;
  67. VALUE new_graph;
  68. new_graph = cIGraph_alloc(cIGraph);
  69. Data_Get_Struct(new_graph, igraph_t, graph);
  70. igraph_destroy(graph);
  71. igraph_star(graph, NUM2INT(n), NUM2INT(mode), NUM2INT(center));
  72. return new_graph;
  73. }
  74. /* call-seq:
  75. * IGraph::Generate.lattice(dim,directed,mutual,circular) -> IGraph
  76. *
  77. * Creates most kind of lattices.
  78. *
  79. * The dimensions of the lattice should be given as an Array of Fixnums in dim
  80. *
  81. * directed: Boolean, whether to create a directed graph. The direction of
  82. * the edges is determined by the generation algorithm and is unlikely to
  83. * suit you, so this isn't a very useful option.
  84. *
  85. * mutual: Boolean, if the graph is directed this gives whether to create
  86. * all connections as mutual.
  87. *
  88. * circular: Boolean, defines whether the generated lattice is periodic.
  89. */
  90. VALUE cIGraph_lattice(VALUE self, VALUE dim, VALUE directed, VALUE mutual, VALUE circular){
  91. igraph_t *graph;
  92. VALUE new_graph;
  93. igraph_vector_t dimvector;
  94. int i;
  95. new_graph = cIGraph_alloc(cIGraph);
  96. Data_Get_Struct(new_graph, igraph_t, graph);
  97. igraph_vector_init(&dimvector,0);
  98. for(i=0; i<RARRAY(dim)->len; i++){
  99. igraph_vector_push_back(&dimvector,NUM2INT(RARRAY(dim)->ptr[i]));
  100. }
  101. igraph_destroy(graph);
  102. igraph_lattice(graph, &dimvector, 0,
  103. directed == Qtrue ? 1 : 0,
  104. mutual == Qtrue ? 1 : 0,
  105. circular == Qtrue ? 1 : 0);
  106. igraph_vector_destroy(&dimvector);
  107. return new_graph;
  108. }
  109. /* call-seq:
  110. * IGraph::Generate.ring(n,directed,mutual,circular) -> IGraph
  111. *
  112. * Creates a ring graph, a one dimensional lattice.
  113. *
  114. * n: The number of vertices in the ring.
  115. *
  116. * directed: Logical, whether to create a directed ring.
  117. *
  118. * mutual: Logical, whether to create mutual edges in a directed ring. It is
  119. * ignored for undirected graphs.
  120. *
  121. * circular: Logical, if false, the ring will be open (this is not a real
  122. * ring actually).
  123. */
  124. VALUE cIGraph_ring(VALUE self, VALUE n, VALUE directed, VALUE mutual, VALUE circular){
  125. igraph_t *graph;
  126. VALUE new_graph;
  127. new_graph = cIGraph_alloc(cIGraph);
  128. Data_Get_Struct(new_graph, igraph_t, graph);
  129. igraph_destroy(graph);
  130. igraph_ring(graph, NUM2INT(n),
  131. directed == Qtrue ? 1 : 0,
  132. mutual == Qtrue ? 1 : 0,
  133. circular == Qtrue ? 1 : 0);
  134. return new_graph;
  135. }
  136. /* call-seq:
  137. * IGraph::Generate.tree(n,children,type) -> IGraph
  138. *
  139. * Creates a tree in which almost all vertices have the same number of
  140. * children.
  141. *
  142. * n: Integer, the number of vertices in the graph.
  143. *
  144. * children: Integer, the number of children of a vertex in the tree.
  145. *
  146. * type: Constant, gives whether to create a directed tree, and if this is
  147. * the case, also its orientation. Possible values:
  148. *
  149. * IGraph::TREE_OUT - directed tree, the edges point from the parents to
  150. * their children,
  151. *
  152. * IGraph::TREE_IN - directed tree, the edges point from the children to
  153. * their parents.
  154. *
  155. * IGraph::TREE_UNDIRECTED - undirected tree.
  156. */
  157. VALUE cIGraph_tree(VALUE self, VALUE n, VALUE children, VALUE type){
  158. igraph_t *graph;
  159. VALUE new_graph;
  160. new_graph = cIGraph_alloc(cIGraph);
  161. Data_Get_Struct(new_graph, igraph_t, graph);
  162. igraph_destroy(graph);
  163. igraph_tree(graph, NUM2INT(n), NUM2INT(children), NUM2INT(type));
  164. return new_graph;
  165. }
  166. /* call-seq:
  167. * IGraph::Generate.full(n,directed,loops) -> IGraph
  168. *
  169. * Creates a full graph (directed or undirected, with or without loops).
  170. *
  171. * In a full graph every possible edge is present, every vertex is connected
  172. * to every other vertex.
  173. *
  174. * n: Integer, the number of vertices in the graph.
  175. *
  176. * directed: Logical, whether to create a directed graph.
  177. *
  178. * loops: Logical, whether to include self-edges (loops).
  179. */
  180. VALUE cIGraph_full(VALUE self, VALUE n, VALUE directed, VALUE loops){
  181. igraph_t *graph;
  182. VALUE new_graph;
  183. new_graph = cIGraph_alloc(cIGraph);
  184. Data_Get_Struct(new_graph, igraph_t, graph);
  185. igraph_destroy(graph);
  186. igraph_full(graph, NUM2INT(n),
  187. directed == Qtrue ? 1 : 0,
  188. loops == Qtrue ? 1 : 0);
  189. return new_graph;
  190. }
  191. /* call-seq:
  192. * IGraph::Generate.atlas(number) -> IGraph
  193. *
  194. * Create a small graph from the $B!H(BGraph Atlas$B!I(B.
  195. *
  196. * The number of the graph is given as the parameter. The graphs are listed:
  197. *
  198. * 1. in increasing order of number of nodes
  199. * 2. for a fixed number of nodes, in increasing order of the number of edges
  200. * 3. for fixed numbers of nodes and edges, in increasing order of the degree
  201. * sequence, for example 111223 < 112222;
  202. * 4. for fixed degree sequence, in increasing number of automorphisms.
  203. *
  204. * The data was converted from the networkx software package, see
  205. * http://networkx.lanl.gov.
  206. *
  207. * See An Atlas of Graphs by Ronald C. Read and Robin J. Wilson, Oxford
  208. * University Press, 1998.
  209. */
  210. VALUE cIGraph_atlas(VALUE self, VALUE n){
  211. igraph_t *graph;
  212. VALUE new_graph;
  213. new_graph = cIGraph_alloc(cIGraph);
  214. Data_Get_Struct(new_graph, igraph_t, graph);
  215. igraph_destroy(graph);
  216. igraph_atlas(graph, NUM2INT(n));
  217. return new_graph;
  218. }
  219. /* call-seq:
  220. * IGraph::Generate.chordal_ring(nodes,w) -> IGraph
  221. *
  222. * Create an extended chordal ring. An extended chordal ring is regular graph,
  223. * each node has the same degree. It can be obtained from a simple ring by
  224. * adding some extra edges specified by a matrix. Let p denote the number of
  225. * columns in the W matrix. The extra edges of vertex i are added according
  226. * to column (i mod p) in W. The number of extra edges is the number of rows
  227. * in W: for each row j an edge i->i+w[ij] is added if i+w[ij] is less than
  228. * the number of total nodes.
  229. *
  230. * See also Kotsis, G: Interconnection Topologies for Parallel Processing
  231. * Systems, PARS Mitteilungen 11, 1-6, 1993.
  232. *
  233. * nodes: Integer, the number of vertices in the graph. It must be at least 3.
  234. *
  235. * w: The matrix specifying the extra edges. The number of columns should
  236. * divide the number of total vertices.
  237. */
  238. VALUE cIGraph_extended_chordal_ring(VALUE self, VALUE n, VALUE matrix){
  239. igraph_t *graph;
  240. igraph_matrix_t *matrixp;
  241. VALUE new_graph;
  242. new_graph = cIGraph_alloc(cIGraph);
  243. Data_Get_Struct(new_graph, igraph_t, graph);
  244. Data_Get_Struct(matrix, igraph_matrix_t, matrixp);
  245. igraph_destroy(graph);
  246. igraph_extended_chordal_ring(graph, NUM2INT(n), matrixp);
  247. return new_graph;
  248. }
  249. /* call-seq:
  250. * g.connect_neighborhood(order,mode) -> nil
  251. *
  252. * Connects every vertex to its neighborhood This function adds new edges to
  253. * graph. For each vertex vertices reachable by at most order steps and not
  254. * yet connected to the vertex a new edge is created.
  255. *
  256. * order: Integer, it gives the distance within which the vertices will be
  257. * connected to the source vertex.
  258. *
  259. * mode: Constant, it specifies how the neighborhood search is performed for
  260. * directed graphs. If IGraph::OUT then vertices reachable from the source
  261. * vertex will be connected, IGraph::IN is the opposite. If IGRAPH_ALL then
  262. * the directed graph is considered as an undirected one.
  263. */
  264. VALUE cIGraph_connect_neighborhood(VALUE self, VALUE order, VALUE mode){
  265. igraph_t *graph;
  266. Data_Get_Struct(self, igraph_t, graph);
  267. igraph_connect_neighborhood(graph, NUM2INT(order), NUM2INT(mode));
  268. return Qnil;
  269. }

Ruby binding for the igraph library.