cr_large_graph.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /* LIBDGL -- a Directed Graph Library implementation
  2. * Copyright (C) 2002 Roberto Micarelli
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /*
  19. * Source best viewed with tabstop=4
  20. */
  21. #include <stdio.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <unistd.h>
  25. #include <stdlib.h>
  26. #include <fcntl.h>
  27. #include <time.h>
  28. #include <errno.h>
  29. #include <math.h>
  30. #include "../type.h"
  31. #include "../graph.h"
  32. #include "opt.h"
  33. extern int errno;
  34. #define NROWS 600
  35. #define NCOLS 100
  36. #define FACTOR 10000
  37. #define BIDIRECTIONAL 1
  38. /*
  39. #define NROWS 600
  40. #define NCOLS 400
  41. #define FACTOR 10000
  42. #define BIDIRECTIONAL 1
  43. */
  44. static int _add_edge(dglGraph_s * pgraph,
  45. dglInt32_t from, dglInt32_t to, dglInt32_t cost,
  46. dglInt32_t arc, char chDirection, int fLast)
  47. {
  48. int nret;
  49. dglInt32_t xyz_from[3], xyz_to[3], direction[2];
  50. if (!fLast) {
  51. /* setup node attributes with the x y coords in the grid by
  52. reversing the calculation done the main loop */
  53. xyz_from[0] = from % NCOLS;
  54. xyz_from[1] = from / NCOLS;
  55. xyz_from[2] = 0;
  56. xyz_to[0] = to % NCOLS;
  57. xyz_to[1] = to / NCOLS;
  58. xyz_to[2] = 0;
  59. /* chDirection says if the edge direction is 'u'=toward the top 'b'=the bot. 'l'=the left 'r'=the right
  60. 'o'=toward right-bottom 'O'=toward top-left * Account for this in the edge attributes */
  61. direction[0] = (dglInt32_t) chDirection;
  62. direction[1] = 0;
  63. nret =
  64. dglAddEdgeX(pgraph, from, to, cost, arc, xyz_from, xyz_to,
  65. direction, 0);
  66. if (nret < 0) {
  67. fprintf(stderr, "dglAddEdge error: %s\n", dglStrerror(pgraph));
  68. return 1;
  69. }
  70. }
  71. if ((arc % 1024) == 0 || fLast) {
  72. #ifndef DGL_STATS
  73. printf("add arc %07ld - from %07ld - to %07ld - cost %07ld\r",
  74. arc, from, to, cost);
  75. #else
  76. printf
  77. ("add arc %07ld - from %07ld - to %07ld - cost %07ld . Clock: tot %09ld nt %09ld o %09ld\r",
  78. arc, from, to, cost, pgraph->clkAddEdge / pgraph->cAddEdge,
  79. pgraph->clkNodeTree / pgraph->cAddEdge,
  80. (pgraph->clkAddEdge - pgraph->clkNodeTree) / pgraph->cAddEdge);
  81. #endif
  82. fflush(stdout);
  83. }
  84. return 0;
  85. }
  86. int main(int argc, char **argv)
  87. {
  88. dglGraph_s graph;
  89. int nret, fd;
  90. int version;
  91. int irow, icol, itrow, itcol;
  92. int irowsave, icolsave;
  93. dglInt32_t from, to, arc, cost;
  94. /* node attributes */
  95. dglInt32_t xyz[3];
  96. /* edge attributes */
  97. dglInt32_t direction[2];
  98. dglInt32_t opaqueset[16] = {
  99. FACTOR, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  100. };
  101. /* program options
  102. */
  103. char *pszFileout;
  104. Boolean fInterlaced;
  105. char *pszVersion;
  106. GNO_BEGIN /* short long default variable help */
  107. GNO_OPTION("g", "graph", NULL, &pszFileout, "Output Graph file")
  108. GNO_SWITCH("i", "interlaced", False, &fInterlaced,
  109. "Avoid node ids sorting at insertion - default False")
  110. GNO_OPTION("v", "version", "1", &pszVersion,
  111. "Output Graph Version {1,2,3} - default 1")
  112. GNO_END if (GNO_PARSE(argc, argv) < 0) {
  113. return 1;
  114. }
  115. if (pszFileout == NULL) {
  116. GNO_HELP("Incomplete parameters");
  117. return 1;
  118. }
  119. /*
  120. * options parsed
  121. */
  122. version = atoi(pszVersion);
  123. /*
  124. * new API call
  125. */
  126. printf("graph initialize...");
  127. fflush(stdout);
  128. dglInitialize(&graph, /* graph context to initialize */
  129. version, sizeof(xyz), /* node attributes size */
  130. sizeof(direction), /* edge attributes size */
  131. opaqueset /* opaque graph parameters */
  132. );
  133. printf("done.\n");
  134. #if 0
  135. dglSet_Options(&graph, DGL_GO_EdgePrioritize_COST);
  136. #endif
  137. from = 0;
  138. to = 0;
  139. arc = 0;
  140. printf("add horizontal and vertical edges...\n");
  141. for (irow = 0; irow < NROWS; irow++) {
  142. if (fInterlaced == True) {
  143. irowsave = irow;
  144. if (irow % 2)
  145. irow = NROWS - irow;
  146. }
  147. for (icol = 0; icol < NCOLS; icol++) {
  148. if (fInterlaced == True) {
  149. icolsave = icol;
  150. if (icol % 2)
  151. icol = NCOLS - icol;
  152. }
  153. itcol = icol + 1;
  154. itrow = irow + 1;
  155. if (itcol < NCOLS) {
  156. from = irow * NCOLS + icol;
  157. to = irow * NCOLS + itcol;
  158. cost = FACTOR;
  159. arc++;
  160. if (_add_edge(&graph, from, to, cost, arc, 'r', 0)) {
  161. return 1;
  162. }
  163. #ifdef BIDIRECTIONAL
  164. arc++;
  165. if (_add_edge(&graph, to, from, cost, arc, 'l', 0)) {
  166. return 1;
  167. }
  168. #endif
  169. }
  170. if (itrow < NROWS) {
  171. from = irow * NCOLS + icol;
  172. to = itrow * NCOLS + icol;
  173. cost = FACTOR;
  174. arc++;
  175. if (_add_edge(&graph, from, to, cost, arc, 'b', 0)) {
  176. return 1;
  177. }
  178. #ifdef BIDIRECTIONAL
  179. arc++;
  180. if (_add_edge(&graph, to, from, cost, arc, 't', 0)) {
  181. return 1;
  182. }
  183. #endif
  184. }
  185. if (fInterlaced == True)
  186. icol = icolsave;
  187. }
  188. if (fInterlaced == True)
  189. irow = irowsave;
  190. }
  191. _add_edge(&graph, to, from, cost, arc, 'x', 1);
  192. #if 1
  193. printf("\nadd oblique edges...\n");
  194. for (irow = 0; irow < NROWS; irow++) {
  195. for (icol = 0; icol < NCOLS; icol++) {
  196. itcol = icol + 1;
  197. itrow = irow + 1;
  198. if (itrow < NROWS && itcol < NCOLS) {
  199. from = irow * NCOLS + icol;
  200. to = itrow * NCOLS + itcol;
  201. cost = (dglInt32_t) (sqrt(2.0) * (double)FACTOR);
  202. arc++;
  203. if (_add_edge(&graph, from, to, cost, arc, 'o', 0)) {
  204. return 1;
  205. }
  206. #ifdef BIDIRECTIONAL
  207. arc++;
  208. if (_add_edge(&graph, to, from, cost, arc, 'O', 0)) {
  209. return 1;
  210. }
  211. #endif
  212. }
  213. }
  214. }
  215. _add_edge(&graph, to, from, cost, arc, 'x', 1);
  216. printf("\ndone.\n");
  217. #endif
  218. #if 0
  219. {
  220. dglEdgeTraverser_s t;
  221. dglInt32_t *pEdge;
  222. nret =
  223. dglEdge_T_Initialize(&graph, &t, dglGet_EdgePrioritizer(&graph));
  224. /*nret = dglEdge_T_Initialize(& graph, & t, NULL); */
  225. if (nret < 0) {
  226. fprintf(stderr, "\ndglEdge_T_Initialize error: %s\n",
  227. dglStrerror(&graph));
  228. return 1;
  229. }
  230. for (pEdge = dglEdge_T_First(&t); pEdge; pEdge = dglEdge_T_Next(&t)) {
  231. printf("edge: id=%ld cost=%ld\n",
  232. dglEdgeGet_Id(&graph, pEdge),
  233. dglEdgeGet_Cost(&graph, pEdge));
  234. }
  235. dglEdge_T_Release(&t);
  236. }
  237. #endif
  238. printf("graph flattening...");
  239. fflush(stdout);
  240. nret = dglFlatten(&graph);
  241. if (nret < 0) {
  242. fprintf(stderr, "\ndglFlatten error: %s\n", dglStrerror(&graph));
  243. return 1;
  244. }
  245. printf("done.\n");
  246. printf("graph write...");
  247. fflush(stdout);
  248. if ((fd = open(pszFileout, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
  249. perror("open");
  250. return 1;
  251. }
  252. nret = dglWrite(&graph, fd);
  253. if (nret < 0) {
  254. fprintf(stderr, "\ndglWrite error: %s\n", dglStrerror(&graph));
  255. return 1;
  256. }
  257. close(fd);
  258. printf("done.\n");
  259. printf("graph release...");
  260. fflush(stdout);
  261. dglRelease(&graph);
  262. printf("program finished.\n");
  263. return 0;
  264. }