Skip to content

Commit e4ad574

Browse files
authored
Merge pull request #1171 from HanKruiger/master
Added generation of tetrix (3d sierpinski) graphs.
2 parents 1ef4d51 + 265eeb6 commit e4ad574

File tree

2 files changed

+98
-2
lines changed

2 files changed

+98
-2
lines changed

cmd/tools/graph_generator.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,93 @@ void makeSierpinski(int depth, edgefn ef)
332332
free(graph);
333333
}
334334

335+
static void
336+
constructTetrix(int v1, int v2, int v3, int v4, int depth, vtx_data* graph)
337+
{
338+
static int last_used_node_name = 4;
339+
int v5, v6, v7, v8, v9, v10;
340+
341+
int nedges;
342+
343+
if (depth > 0) {
344+
v5 = ++last_used_node_name;
345+
v6 = ++last_used_node_name;
346+
v7 = ++last_used_node_name;
347+
v8 = ++last_used_node_name;
348+
v9 = ++last_used_node_name;
349+
v10 = ++last_used_node_name;
350+
constructTetrix(v1, v5, v6, v8, depth - 1, graph);
351+
constructTetrix(v2, v6, v7, v9, depth - 1, graph);
352+
constructTetrix(v3, v5, v7, v10, depth - 1, graph);
353+
constructTetrix(v4, v8, v9, v10, depth - 1, graph);
354+
return;
355+
}
356+
// depth==0, Construct graph:
357+
nedges = graph[v1].nedges;
358+
graph[v1].edges[nedges++] = v2;
359+
graph[v1].edges[nedges++] = v3;
360+
graph[v1].edges[nedges++] = v4;
361+
graph[v1].nedges = nedges;
362+
363+
nedges = graph[v2].nedges;
364+
graph[v2].edges[nedges++] = v1;
365+
graph[v2].edges[nedges++] = v3;
366+
graph[v2].edges[nedges++] = v4;
367+
graph[v2].nedges = nedges;
368+
369+
nedges = graph[v3].nedges;
370+
graph[v3].edges[nedges++] = v1;
371+
graph[v3].edges[nedges++] = v2;
372+
graph[v3].edges[nedges++] = v4;
373+
graph[v3].nedges = nedges;
374+
375+
nedges = graph[v4].nedges;
376+
graph[v4].edges[nedges++] = v1;
377+
graph[v4].edges[nedges++] = v2;
378+
graph[v4].edges[nedges++] = v3;
379+
graph[v4].nedges = nedges;
380+
381+
return;
382+
383+
}
384+
385+
void makeTetrix(int depth, edgefn ef)
386+
{
387+
vtx_data* graph;
388+
int* edges;
389+
int n;
390+
int nedges;
391+
int i, j;
392+
393+
depth--;
394+
n = 4 + 2 * (((int) (pow(4.0, (double) depth) + 0.5) - 1));
395+
396+
nedges = (int) (pow(6.0, depth + 1.0) + 0.5);
397+
398+
graph = N_NEW(n + 1, vtx_data);
399+
edges = N_NEW(6 * n, int);
400+
401+
for (i = 1; i <= n; i++) {
402+
graph[i].edges = edges;
403+
edges += 6;
404+
graph[i].nedges = 0;
405+
}
406+
407+
constructTetrix(1, 2, 3, 4, depth, graph);
408+
409+
for (i = 1; i <= n; i++) {
410+
int nghbr;
411+
// write the neighbors of the node i
412+
for (j = 0; j < graph[i].nedges; j++) {
413+
nghbr = graph[i].edges[j];
414+
if (i < nghbr) ef( i, nghbr);
415+
}
416+
}
417+
418+
free(graph[1].edges);
419+
free(graph);
420+
}
421+
335422
void makeHypercube(int dim, edgefn ef)
336423
{
337424
int i, j, n;

cmd/tools/gvgen.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
typedef enum { unknown, grid, circle, complete, completeb,
3636
path, tree, torus, cylinder, mobius, randomg, randomt, ball,
37-
sierpinski, hypercube, star, wheel, trimesh
37+
sierpinski, tetrix, hypercube, star, wheel, trimesh
3838
} GraphType;
3939

4040
typedef struct {
@@ -92,6 +92,7 @@ static char *Usage = "Usage: %s [-dv?] [options]\n\
9292
-R<n> : random rooted tree on <n> vertices\n\
9393
-s<x> : star\n\
9494
-S<x> : sierpinski\n\
95+
-X<x> : tetrix (3d sierpinski)\n\
9596
-t<x> : binary tree \n\
9697
-t<x>,<n> : n-ary tree \n\
9798
-T<x,y> : torus \n\
@@ -286,7 +287,7 @@ static char* setFold(char *s, opts_t* opts)
286287
return next;
287288
}
288289

289-
static char *optList = ":i:M:m:n:N:c:C:dg:G:h:k:b:B:o:p:r:R:s:S:t:T:vw:";
290+
static char *optList = ":i:M:m:n:N:c:C:dg:G:h:k:b:B:o:p:r:R:s:S:X:t:T:vw:";
290291

291292
static GraphType init(int argc, char *argv[], opts_t* opts)
292293
{
@@ -377,6 +378,11 @@ static GraphType init(int argc, char *argv[], opts_t* opts)
377378
if (setOne(optarg, opts))
378379
errexit(c);
379380
break;
381+
case 'X':
382+
graphType = tetrix;
383+
if (setOne(optarg, opts))
384+
errexit(c);
385+
break;
380386
case 's':
381387
graphType = star;
382388
if (setOne(optarg, opts))
@@ -509,6 +515,9 @@ int main(int argc, char *argv[])
509515
case sierpinski:
510516
makeSierpinski(opts.graphSize1, ef);
511517
break;
518+
case tetrix:
519+
makeTetrix(opts.graphSize1, ef);
520+
break;
512521
case complete:
513522
makeComplete(opts.graphSize1, ef);
514523
break;

0 commit comments

Comments
 (0)