-
Notifications
You must be signed in to change notification settings - Fork 0
/
adjacency_list.cpp
114 lines (98 loc) · 3.26 KB
/
adjacency_list.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
//
// adjacency_list.cpp
// minimum-spanning-trees
//
// Created by Samuel K. Lam on 3/19/17.
// Copyright © 2017 Samuel K. Lam. All rights reserved.
//
#include "adjacency_list.hpp"
std::vector< std::list<node> > adj_list(int dim, int n) {
std::vector< std::list<node> > adjlist;
std::list<node> clean;
clean.clear();
srand((unsigned)time(NULL));
for (int i = 0; i < n; i++)
{
adjlist.push_back(clean);
}
if(dim == 0)
{
for (int i = 0; i < n; i ++)
{
adjlist.push_back(clean);
for (int j = i + 1; j < n; j++)
{
node ins;
ins.index = j;
ins.weight = (float)rand() / RAND_MAX;
adjlist[i].push_back(ins);
adjlist[j].push_back(ins);
}
}
}
else if(dim == 2)
{
float x_coords[n];
float y_coords[n];
// initialize x_coords and y_coords
// note that x[0], y[0] denotes the coords for the 0th vertex
for (int a = 0; a < n; a++) {
x_coords[a] = (float)rand() / RAND_MAX;
y_coords[a] = (float)rand() / RAND_MAX;
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
node ins;
ins.index = j;
// standard Euclidean distance calculation
ins.weight = sqrt(pow(x_coords[i] - x_coords[j], 2) + (pow(y_coords[i] - y_coords[j], 2)));
adjlist[i].push_back(ins);
adjlist[j].push_back(ins);
}
}
}
else if (dim == 3)
{
float x_coords[n];
float y_coords[n];
float z_coords[n];
// initialize coordinates
for (int a = 0; a < n; a++) {
x_coords[a] = (float)rand() / RAND_MAX;
y_coords[a] = (float)rand() / RAND_MAX;
z_coords[a] = (float)rand() / RAND_MAX;
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
node ins;
ins.index = j;
ins.weight = sqrt(pow(x_coords[i] - x_coords[j], 2) + (pow(y_coords[i] - y_coords[j], 2)) + (pow(z_coords[i] - z_coords[j], 2)));
adjlist[i].push_back(ins);
adjlist[j].push_back(ins);
}
}
}
if (dim == 4) {
float x_coords[n];
float y_coords[n];
float z_coords[n];
float v_coords[n];
// initialize coordinates
for (int a = 0; a < n; a++) {
x_coords[a] = (float)rand() / RAND_MAX;
y_coords[a] = (float)rand() / RAND_MAX;
z_coords[a] = (float)rand() / RAND_MAX;
v_coords[a] = (float)rand() / RAND_MAX;
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
node ins;
ins.index = j;
ins.weight = sqrt(pow(x_coords[i] - x_coords[j], 2) + pow(y_coords[i] - y_coords[j], 2) + pow(z_coords[i] - z_coords[j], 2) + pow(v_coords[i] - v_coords[j], 2));
adjlist[i].push_back(ins);
adjlist[j].push_back(ins);
}
}
}
return adjlist;
}