-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgengraph.cpp
116 lines (109 loc) · 3.53 KB
/
gengraph.cpp
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
/**********************************************************
* This software is part of the graphviz toolset *
* http://www.graphviz.org/ *
* *
* Copyright (c) 1994-2005 AT&T Corp. *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Corp. *
* *
* Information and Software Systems Research *
* AT&T Research, Florham Park NJ *
* *
* This version available from *
* http://dynagraph.org *
**********************************************************/
#include "common/LGraph-cdt.h"
#include "common/StrAttr.h"
#include "common/emitGraph.h"
#include <time.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
using namespace std;
using namespace Dynagraph;
void usage() {
reports[dgr::cmdline] << "gengraph" << endl <<
"\t-v N\tN vertices" << endl <<
"\t-e N\tN edges" << endl <<
"\t-c\tassign random colors (red,yellow,blue) to edges" << endl <<
"\t-w W\tassign random weights [0..W) to edges" << endl;
exit(-1);
}
int main(int narg,char *argh[]) {
// enable basic dynagraph report streams
reports.enable(dgr::error,&cerr);
reports.enable(dgr::cmdline,&cout);
reports.enable(dgr::output,&cout);
int V = 100, E = 100;
bool colors = false;
int maxweight = 0;
for(int i = 1; i<narg; ++i) {
if(argh[i][0]!='-' || argh[i][2]!=0)
usage();
switch(tolower(argh[i][1])) {
case 'v':
if(++i==narg)
usage();
V = atoi(argh[i]);
break;
case 'e':
if(++i==narg)
usage();
E = atoi(argh[i]);
break;
case 'c':
colors = true;
break;
case 'w':
if(++i==narg)
usage();
maxweight = atoi(argh[i]);
break;
default:
usage();
}
}
unsigned seed = (unsigned)time(NULL);
srand(seed);
StrGraph g;
char buf[20];
sprintf(buf,"%d",seed);
gd<StrAttrs>(&g)["seed"] = buf;
vector<StrGraph::Node*> ez;
ez.resize(V);
int v = V;
while(v--) {
char name[10];
sprintf(name,"%d",v);
ez[v] = g.create_node(name);
}
while(E--) {
int t,h;
do
t = rand()%V, h = rand()%V;
while(t==h || g.find_edge(ez[t],ez[h]) || g.find_edge(ez[h],ez[t])); // play to dynagraph's weaknesses
char name[10];
sprintf(name,"%d",E);
StrGraph::Edge *e = g.create_edge(ez[t],ez[h],name).first;
if(colors) {
const char *color=0;
switch(rand()%3) {
case 0: color = "red";
break;
case 1: color = "yellow";
break;
case 2: color = "blue";
break;
}
gd<StrAttrs>(e)["color"] = color;
}
if(maxweight) {
ostringstream os;
os << rand()%maxweight;
gd<StrAttrs>(e)["weight"] = os.str();
}
}
emitGraph(reports[dgr::output],&g);
return 0;
}