-
Notifications
You must be signed in to change notification settings - Fork 0
/
adjacency-matrix.cpp
114 lines (96 loc) · 3.39 KB
/
adjacency-matrix.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-matrix.cpp
// minimum-spanning-trees
//
// Created by Samuel K. Lam on 2/26/17.
// Copyright © 2017 Samuel K. Lam. All rights reserved.
//
#include "adjacency-matrix.hpp"
#include <math.h>
#include <iostream>
#include <iomanip>
#include <stdio.h>
/*
* Generates and returns an adjacency matrix
* @param MAX_ROW : number of rows
* @param MAX_COL : number of columns
*/
template<int MAX_ROW, int MAX_COL>
void MatrixGenerator(float (&matrix)[MAX_ROW][MAX_COL], int n, int dim) {
srand((unsigned)time(NULL));
if (dim == 0) {
// skips the diagonal and copies symmetric value of matrix to other side
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
matrix[i][j] = (float)rand() / RAND_MAX;
matrix[j][i] = matrix[i][j];
}
}
}
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 - 1; i++) {
for (int j = i + 1; j < n; j++) {
// standard Euclidean distance calculation
matrix[i][j] = sqrt(pow(x_coords[i] - x_coords[j], 2) + (pow(y_coords[i] - y_coords[j], 2)));
matrix[j][i] = matrix[i][j];
}
}
}
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 - 1; i++) {
for (int j = i + 1; j < n; j++) {
matrix[i][j] = 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)));
matrix[j][i] = matrix[i][j];
}
}
}
else 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 - 1; i++) {
for (int j = i + 1; j < n; j++) {
matrix[i][j] = 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));
matrix[j][i] = matrix[i][j];
}
}
}
}
/*
* Prints resulting matrix for debugging and testing purpose
* @param matrix : pointer to a 2d matrix
*/
template<int MAX_ROW, int MAX_COL>
void MatrixPrint(float (&matrix)[MAX_ROW][MAX_COL]) {
for (int i = 0; i < MAX_ROW; i++) {
for (int j = 0; j < MAX_COL; j++) {
std::cout << std::setw(10) << std::left << matrix[i][j];
}
std::cout << std::endl;
}
}