File: cr_large_graph.c

package info (click to toggle)
grass 6.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 104,028 kB
  • ctags: 40,409
  • sloc: ansic: 419,980; python: 63,559; tcl: 46,692; cpp: 29,791; sh: 18,564; makefile: 7,000; xml: 3,505; yacc: 561; perl: 559; lex: 480; sed: 70; objc: 7
file content (317 lines) | stat: -rw-r--r-- 7,268 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* LIBDGL -- a Directed Graph Library implementation
 * Copyright (C) 2002 Roberto Micarelli
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/*
 * Source best viewed with tabstop=4
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#include <math.h>

#include "../type.h"
#include "../graph.h"

#include "opt.h"

extern int errno;


#define NROWS 600
#define NCOLS 100
#define FACTOR 10000
#define BIDIRECTIONAL 1

/*
   #define NROWS 600
   #define NCOLS 400
   #define FACTOR 10000
   #define BIDIRECTIONAL 1
 */


static int _add_edge(dglGraph_s * pgraph,
		     dglInt32_t from, dglInt32_t to, dglInt32_t cost,
		     dglInt32_t arc, char chDirection, int fLast)
{
    int nret;

    dglInt32_t xyz_from[3], xyz_to[3], direction[2];

    if (!fLast) {
	/* setup node attributes with the x y coords in the grid by
	   reversing the calculation done the main loop */
	xyz_from[0] = from % NCOLS;
	xyz_from[1] = from / NCOLS;
	xyz_from[2] = 0;

	xyz_to[0] = to % NCOLS;
	xyz_to[1] = to / NCOLS;
	xyz_to[2] = 0;

	/* chDirection says if the edge direction is 'u'=toward the top 'b'=the bot. 'l'=the left 'r'=the right
	   'o'=toward right-bottom 'O'=toward top-left * Account for this in the edge attributes */
	direction[0] = (dglInt32_t) chDirection;
	direction[1] = 0;


	nret =
	    dglAddEdgeX(pgraph, from, to, cost, arc, xyz_from, xyz_to,
			direction, 0);
	if (nret < 0) {
	    fprintf(stderr, "dglAddEdge error: %s\n", dglStrerror(pgraph));
	    return 1;
	}
    }

    if ((arc % 1024) == 0 || fLast) {
#ifndef DGL_STATS
	printf("add arc %07ld - from %07ld - to %07ld - cost %07ld\r",
	       arc, from, to, cost);
#else
	printf
	    ("add arc %07ld - from %07ld - to %07ld - cost %07ld . Clock: tot %09ld nt %09ld o %09ld\r",
	     arc, from, to, cost, pgraph->clkAddEdge / pgraph->cAddEdge,
	     pgraph->clkNodeTree / pgraph->cAddEdge,
	     (pgraph->clkAddEdge - pgraph->clkNodeTree) / pgraph->cAddEdge);
#endif
	fflush(stdout);
    }
    return 0;
}

int main(int argc, char **argv)
{
    dglGraph_s graph;
    int nret, fd;
    int version;

    int irow, icol, itrow, itcol;
    int irowsave, icolsave;

    dglInt32_t from, to, arc, cost;

    /* node attributes */
    dglInt32_t xyz[3];

    /* edge attributes */
    dglInt32_t direction[2];

    dglInt32_t opaqueset[16] = {
	FACTOR, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    };

    /* program options
     */
    char *pszFileout;
    Boolean fInterlaced;
    char *pszVersion;

    GNO_BEGIN			/* short   long                default     variable        help */
	GNO_OPTION("g", "graph", NULL, &pszFileout, "Output Graph file")
	GNO_SWITCH("i", "interlaced", False, &fInterlaced,
		   "Avoid node ids sorting at insertion - default False")
	GNO_OPTION("v", "version", "1", &pszVersion,
		   "Output Graph Version {1,2,3} - default 1")
    GNO_END if (GNO_PARSE(argc, argv) < 0)
    {
	return 1;
    }

    if (pszFileout == NULL) {
	GNO_HELP("Incomplete parameters");
	return 1;
    }

    /*
     * options parsed
     */
    version = atoi(pszVersion);

    /*
     * new API call
     */
    printf("graph initialize...");
    fflush(stdout);
    dglInitialize(&graph,	/* graph context to initialize */
		  version, sizeof(xyz),	/* node attributes size */
		  sizeof(direction),	/* edge attributes size */
		  opaqueset	/* opaque graph parameters */
	);
    printf("done.\n");

#if 0
    dglSet_Options(&graph, DGL_GO_EdgePrioritize_COST);
#endif

    from = 0;
    to = 0;
    arc = 0;


    printf("add horizontal and vertical edges...\n");
    for (irow = 0; irow < NROWS; irow++) {

	if (fInterlaced == True) {
	    irowsave = irow;
	    if (irow % 2)
		irow = NROWS - irow;
	}

	for (icol = 0; icol < NCOLS; icol++) {

	    if (fInterlaced == True) {
		icolsave = icol;
		if (icol % 2)
		    icol = NCOLS - icol;
	    }

	    itcol = icol + 1;
	    itrow = irow + 1;

	    if (itcol < NCOLS) {
		from = irow * NCOLS + icol;
		to = irow * NCOLS + itcol;
		cost = FACTOR;
		arc++;
		if (_add_edge(&graph, from, to, cost, arc, 'r', 0)) {
		    return 1;
		}
#ifdef BIDIRECTIONAL
		arc++;
		if (_add_edge(&graph, to, from, cost, arc, 'l', 0)) {
		    return 1;
		}
#endif
	    }

	    if (itrow < NROWS) {
		from = irow * NCOLS + icol;
		to = itrow * NCOLS + icol;
		cost = FACTOR;
		arc++;
		if (_add_edge(&graph, from, to, cost, arc, 'b', 0)) {
		    return 1;
		}
#ifdef BIDIRECTIONAL
		arc++;
		if (_add_edge(&graph, to, from, cost, arc, 't', 0)) {
		    return 1;
		}
#endif
	    }

	    if (fInterlaced == True)
		icol = icolsave;
	}

	if (fInterlaced == True)
	    irow = irowsave;
    }
    _add_edge(&graph, to, from, cost, arc, 'x', 1);


#if 1
    printf("\nadd oblique edges...\n");
    for (irow = 0; irow < NROWS; irow++) {
	for (icol = 0; icol < NCOLS; icol++) {
	    itcol = icol + 1;
	    itrow = irow + 1;

	    if (itrow < NROWS && itcol < NCOLS) {
		from = irow * NCOLS + icol;
		to = itrow * NCOLS + itcol;
		cost = (dglInt32_t) (sqrt(2.0) * (double)FACTOR);
		arc++;
		if (_add_edge(&graph, from, to, cost, arc, 'o', 0)) {
		    return 1;
		}
#ifdef BIDIRECTIONAL
		arc++;
		if (_add_edge(&graph, to, from, cost, arc, 'O', 0)) {
		    return 1;
		}
#endif
	    }
	}
    }
    _add_edge(&graph, to, from, cost, arc, 'x', 1);
    printf("\ndone.\n");
#endif


#if 0
    {
	dglEdgeTraverser_s t;
	dglInt32_t *pEdge;

	nret =
	    dglEdge_T_Initialize(&graph, &t, dglGet_EdgePrioritizer(&graph));
	/*nret = dglEdge_T_Initialize(& graph, & t, NULL); */
	if (nret < 0) {
	    fprintf(stderr, "\ndglEdge_T_Initialize error: %s\n",
		    dglStrerror(&graph));
	    return 1;
	}
	for (pEdge = dglEdge_T_First(&t); pEdge; pEdge = dglEdge_T_Next(&t)) {
	    printf("edge: id=%ld cost=%ld\n",
		   dglEdgeGet_Id(&graph, pEdge),
		   dglEdgeGet_Cost(&graph, pEdge));
	}
	dglEdge_T_Release(&t);
    }
#endif


    printf("graph flattening...");
    fflush(stdout);
    nret = dglFlatten(&graph);
    if (nret < 0) {
	fprintf(stderr, "\ndglFlatten error: %s\n", dglStrerror(&graph));
	return 1;
    }
    printf("done.\n");


    printf("graph write...");
    fflush(stdout);
    if ((fd = open(pszFileout, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
	perror("open");
	return 1;
    }
    nret = dglWrite(&graph, fd);
    if (nret < 0) {
	fprintf(stderr, "\ndglWrite error: %s\n", dglStrerror(&graph));
	return 1;
    }
    close(fd);
    printf("done.\n");


    printf("graph release...");
    fflush(stdout);
    dglRelease(&graph);
    printf("program finished.\n");
    return 0;
}