-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_adj_matrix.cpp
More file actions
70 lines (57 loc) · 1.53 KB
/
gen_adj_matrix.cpp
File metadata and controls
70 lines (57 loc) · 1.53 KB
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
// gen_adjMatrix.cc
// generates random adjacency matrix of desired size and connectivity
// represents a random graph
// gw
// MSVS: use Win32 Console Application / Empty Project
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <climits>
#include <ctime>
using namespace std;
#define MAX_WEIGHT 20
void gen_adj_matrix (int *, int, int);
void print_matrix (int*, int);
int main (int argc, char *argv[])
{
int numVertices, avgConnectivity;
// setup/initialize
if ((argc != 3) || (atoi(argv[2]) > atoi(argv[1]))) {
cerr << "usage: progName numVertices avgConnectivity\n" << endl;
exit(-1);
}
else {
numVertices = atoi(argv[1]);
avgConnectivity = atoi(argv[2]);
}
int *adjMatrix = new int[numVertices * numVertices];
// generate random graph/matrix
gen_adj_matrix(adjMatrix, numVertices, avgConnectivity);
print_matrix (adjMatrix, numVertices);
delete [] adjMatrix;
return 0;
}
void gen_adj_matrix(int *data, int size, int connectivity)
{
int i,j;
// generate random size x size adjacency matrix of requested connectivity
// uses "infinity" to indicate non-adjacent nodes
srand((unsigned) time(0));
for (i=0; i < size; i++)
for (j=0; j < size; j++)
if (((rand() % size) < connectivity) && i!=j)
data[i*size+j] = 1 + (rand() % MAX_WEIGHT);
else
data[i*size+j] = INT_MAX;
}
void print_matrix (int *data, int size)
{
int i,j;
ofstream outputFile;
outputFile.open ("matrix.out");
for (i=0; i < size; i++) {
for (j=0; j < size; j++)
outputFile << data[i*size+j] << "\t";
}
outputFile.close();
}