-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGraphGeneration.h
124 lines (105 loc) · 3.09 KB
/
GraphGeneration.h
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
#ifndef GRAPH_GEN_H_
#define GRAPH_GEN_H_
double EuclideanDistance(vector<double> v1, vector<double> v2)
{
double diff_x = v1[0] - v2[0] ;
double diff_y = v1[1] - v2[1] ;
double diff = sqrt(pow(diff_x,2) + pow(diff_y,2)) ;
return diff ;
}
// Connect vertices within specified radius
void RadiusConnect(char * cDir, int graphNum, vector< vector<double> > vertices, double radius, int meanMax, int sigMax)
{
srand(graphNum+1);
vector< vector<double> > edges(pow(vertices.size(),2), vector<double>(4)) ;
ULONG k = 0 ;
for (ULONG i = 0; i < (ULONG)vertices.size(); i++)
{
for (ULONG j = 0; j < (ULONG)vertices.size(); j++)
{
double diff = EuclideanDistance(vertices[i], vertices[j]) ;
if (diff <= radius && i != j)
{
edges[k][0] = (double)i ;
edges[k][1] = (double)j ;
edges[k][2] = diff + (double)(rand() % (meanMax*100))/100.0 ;
edges[k][3] = (double)(rand() % (sigMax*100))/100.0 ; // 0.0 ; *** EDGE COST STANDARD DEVIATION ***
k++ ;
}
}
}
edges.resize(k) ;
// Write edges to txt file
stringstream eFileName ;
eFileName << cDir << "/edges" << graphNum << ".txt" ;
ofstream edgesFile ;
edgesFile.open(eFileName.str().c_str()) ;
for (ULONG i = 0; i < edges.size(); i++)
{
edgesFile << edges[i][0] << "," << edges[i][1] << "," << edges[i][2] << "," << edges[i][3] << "\n" ;
}
edgesFile.close() ;
}
void GenerateVertices(char * cDir, int graphNum, int numVerts, double x, double y, double &radius, vector< vector<double> > &vertices){
srand (graphNum+1);
double vertx, verty;
int xx = x;
int yy = y;
int i = 0 ;
while (i < numVerts){
bool fDbl = false ;
if (i == 0)
{
vertices[i][0] = 0 ;
vertices[i][1] = 0 ;
}
else if (i == numVerts-1)
{
vertices[i][0] = x ;
vertices[i][1] = y ;
}
else
{
vertx = rand() % xx;
verty = rand() % yy;
if ((vertx == 0 && verty == 0) || (vertx == x && verty == y)){
fDbl = true ;
break ;
}
else {
for (int j = 0; j < i; j++){
if (vertx == vertices[j][0] && verty == vertices[j][1]){
fDbl = true ;
break ;
}
}
}
if (!fDbl){ // only add to list if unique
vertices[i][0] = vertx ;
vertices[i][1] = verty ;
}
}
if (!fDbl)
i++ ;
}
// Write vertices to txt file
stringstream vFileName ;
vFileName << cDir << "/vertices" << graphNum << ".txt" ;
ofstream vertsFile ;
vertsFile.open(vFileName.str().c_str()) ;
for (ULONG i = 0; i < vertices.size(); i++)
{
vertsFile << vertices[i][0] << "," << vertices[i][1] << "\n" ;
}
vertsFile.close() ;
radius = sqrt((6.0/PI)*x*y*(log((double)numVerts)/(double)numVerts)) ;
}
void GenerateGraph(char * cDir, int graphNum, int numVerts, double x, double y, int meanMax, int sigMax){
// cout << "Generating random vertices in " << x << " by " << y << "\n" ;
double radius ;
vector< vector< double > > vertices(numVerts, vector<double>(2));
GenerateVertices(cDir, graphNum, numVerts, x, y, radius, vertices) ;
// cout << "Connecting with radius " << radius << endl;
RadiusConnect(cDir, graphNum, vertices, radius, meanMax, sigMax);
}
#endif // GRAPH_GEN_H_