GRASS Programmer's Manual  6.4.2(2012)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
minspan.c
Go to the documentation of this file.
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 /*
20  * Source best viewed with tabstop=4
21  */
22 
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <fcntl.h>
29 #include <time.h>
30 #include <errno.h>
31 #include <math.h>
32 
33 #include "../type.h"
34 #include "../graph.h"
35 
36 #include "opt.h"
37 
38 static int _clipper(dglGraph_s * pgraphIn,
39  dglGraph_s * pgraphOut,
40  dglSpanClipInput_s * pArgIn,
41  dglSpanClipOutput_s * pArgOut, void *pvArg)
42 {
43  return 0;
44 }
45 
46 int main(int argc, char **argv)
47 {
48  dglGraph_s graph, graphOut;
49  dglInt32_t nVertex;
50  int nret, fd;
51 
52  /* program options
53  */
54  char *pszGraph;
55  char *pszGraphOut;
56  char *pszVertex;
57 
58  GNO_BEGIN /* short long default variable help */
59  GNO_OPTION("g", "graph", NULL, &pszGraph, "Input Graph file")
60  GNO_OPTION("o", "graphout", NULL, &pszGraphOut, "Output Graph file")
61  GNO_OPTION("v", "vertex", NULL, &pszVertex, "Vertex Node Id")
62  GNO_END if (GNO_PARSE(argc, argv) < 0)
63  {
64  return 1;
65  }
66  /*
67  * options parsed
68  */
69 
70  if (pszVertex == NULL) {
71  GNO_HELP("minspan usage");
72  return 1;
73  }
74  nVertex = atol(pszVertex);
75 
76  printf("Graph read:\n");
77  if ((fd = open(pszGraph, O_RDONLY)) < 0) {
78  perror("open");
79  return 1;
80  }
81  nret = dglRead(&graph, fd);
82  if (nret < 0) {
83  fprintf(stderr, "dglRead error: %s\n", dglStrerror(&graph));
84  return 1;
85  }
86  close(fd);
87  printf("Done.\n");
88 
89  printf("Graph minimum spanning:\n");
90  nret = dglMinimumSpanning(&graph, &graphOut, nVertex, _clipper, NULL);
91  if (nret < 0) {
92  fprintf(stderr, "dglMinimumSpanning error: %s\n",
93  dglStrerror(&graph));
94  return 1;
95  }
96  printf("Done.\n");
97 
98 
99  printf("Graph flatten:\n");
100  nret = dglFlatten(&graphOut);
101  printf("Done.\n");
102 
103  if (dglGet_EdgeCount(&graphOut) > 0) {
104 
105 
106  if (pszGraphOut) {
107  printf("Graph write:\n");
108  if ((fd =
109  open(pszGraphOut, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
110  perror("open");
111  return 1;
112  }
113  dglWrite(&graphOut, fd);
114  if (nret < 0) {
115  fprintf(stderr, "dglWrite error: %s\n",
116  dglStrerror(&graphOut));
117  return 1;
118  }
119  close(fd);
120  printf("Done.\n");
121  }
122  }
123  else {
124  printf("Empty span. No output produced.\n");
125  }
126 
127  dglRelease(&graph);
128  dglRelease(&graphOut);
129  return 0;
130 }