minspan.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. static int _clipper(dglGraph_s * pgraphIn,
  34. dglGraph_s * pgraphOut,
  35. dglSpanClipInput_s * pArgIn,
  36. dglSpanClipOutput_s * pArgOut, void *pvArg)
  37. {
  38. return 0;
  39. }
  40. int main(int argc, char **argv)
  41. {
  42. dglGraph_s graph, graphOut;
  43. dglInt32_t nVertex;
  44. int nret, fd;
  45. /* program options
  46. */
  47. char *pszGraph;
  48. char *pszGraphOut;
  49. char *pszVertex;
  50. GNO_BEGIN /* short long default variable help */
  51. GNO_OPTION("g", "graph", NULL, &pszGraph, "Input Graph file")
  52. GNO_OPTION("o", "graphout", NULL, &pszGraphOut, "Output Graph file")
  53. GNO_OPTION("v", "vertex", NULL, &pszVertex, "Vertex Node Id")
  54. GNO_END if (GNO_PARSE(argc, argv) < 0) {
  55. return 1;
  56. }
  57. /*
  58. * options parsed
  59. */
  60. if (pszVertex == NULL) {
  61. GNO_HELP("minspan usage");
  62. return 1;
  63. }
  64. nVertex = atol(pszVertex);
  65. printf("Graph read:\n");
  66. if ((fd = open(pszGraph, O_RDONLY)) < 0) {
  67. perror("open");
  68. return 1;
  69. }
  70. nret = dglRead(&graph, fd);
  71. if (nret < 0) {
  72. fprintf(stderr, "dglRead error: %s\n", dglStrerror(&graph));
  73. return 1;
  74. }
  75. close(fd);
  76. printf("Done.\n");
  77. printf("Graph minimum spanning:\n");
  78. nret = dglMinimumSpanning(&graph, &graphOut, nVertex, _clipper, NULL);
  79. if (nret < 0) {
  80. fprintf(stderr, "dglMinimumSpanning error: %s\n",
  81. dglStrerror(&graph));
  82. return 1;
  83. }
  84. printf("Done.\n");
  85. printf("Graph flatten:\n");
  86. nret = dglFlatten(&graphOut);
  87. printf("Done.\n");
  88. if (dglGet_EdgeCount(&graphOut) > 0) {
  89. if (pszGraphOut) {
  90. printf("Graph write:\n");
  91. if ((fd =
  92. open(pszGraphOut, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
  93. perror("open");
  94. return 1;
  95. }
  96. dglWrite(&graphOut, fd);
  97. if (nret < 0) {
  98. fprintf(stderr, "dglWrite error: %s\n",
  99. dglStrerror(&graphOut));
  100. return 1;
  101. }
  102. close(fd);
  103. printf("Done.\n");
  104. }
  105. }
  106. else {
  107. printf("Empty span. No output produced.\n");
  108. }
  109. dglRelease(&graph);
  110. dglRelease(&graphOut);
  111. return 0;
  112. }