-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.cpp
More file actions
135 lines (111 loc) · 3.17 KB
/
graph.cpp
File metadata and controls
135 lines (111 loc) · 3.17 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
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
125
126
127
128
129
130
131
132
133
134
135
#include "graph.h"
/*
double Graph::champernowneNorm(int n, double alpha) const {
double sum = 0.0;
for (int k = 1; k <= n; ++k) {
sum += 1.0 / std::pow(k, alpha);
}
return 1.0 / sum;
}
*/
double Graph::champernownePDF(double x) const {
return CHAMPER_A / (M_PI * std::cosh(CHAMPER_A * (x - CHAMPER_M)));
}
std::vector<std::vector<int>> Graph::getAdjacencyMatrix() const {
std::vector<std::vector<int>> matrix(getRows(), std::vector<int>(getCols()));
for (int i = 0; i < getRows(); ++i) {
for (int j = 0; j < getCols(); ++j) {
matrix[i][j] = get(i, j) ? 1 : 0;
}
}
return matrix;
}
std::vector<std::vector<int>> Graph::getWeightMatrix() const {
std::vector<std::vector<int>> matrix(getRows(), std::vector<int>(getCols()));
for (int i = 0; i < getRows(); ++i) {
for (int j = 0; j < getCols(); ++j) {
matrix[i][j] = get(i, j);
}
}
return matrix;
}
void Graph::printAdjacencyMatrix() const {
auto adjMatrix = getAdjacencyMatrix();
for (const auto& row : adjMatrix) {
for (int val : row) {
std::cout << val << " ";
}
std::cout << std::endl;
}
}
void Graph::printWeightMatrix() const {
auto weightMatrix = getWeightMatrix();
for (const auto& row : weightMatrix) {
for (int val : row) {
std::cout << val << " ";
}
std::cout << std::endl;
}
}
std::pair<std::vector<int>, int> Graph::getBFSOrder(int start) const {
validateVertex(start);
std::vector<int> result;
std::vector<bool> visited(getRows(), false);
std::queue<int> q;
int iterations = 0;
q.push(start);
visited[start] = true;
while (!q.empty()) {
int v = q.front();
q.pop();
result.push_back(v);
for (int i = 0; i < getRows(); ++i) {
iterations++;
if (get(v, i) && !visited[i]) {
visited[i] = true;
q.push(i);
}
}
}
return {result, iterations};
}
std::pair<std::vector<int>, int> Graph::getDFSOrder(int start) const {
validateVertex(start);
std::vector<int> result;
std::vector<bool> visited(getRows(), false);
std::stack<int> s;
int iterations = 0;
s.push(start);
while (!s.empty()) {
int v = s.top();
s.pop();
if (!visited[v]) {
visited[v] = true;
result.push_back(v);
for (int i = getRows()-1; i >= 0; --i) {
iterations++;
if (get(v, i) && !visited[i]) {
s.push(i);
}
}
}
}
return {result, iterations};
}
std::pair<int, int> Graph::findMinMaxWeights() const {
int min_weight = INT_MAX;
int max_weight = INT_MIN;
for (int i = 0; i < getRows(); ++i) {
for (int j = 0; j < getCols(); ++j) {
int weight = get(i, j);
if (weight != 0) {
min_weight = std::min(min_weight, weight);
max_weight = std::max(max_weight, weight);
}
}
}
if (min_weight == INT_MAX) {
return {0, 0};
}
return {min_weight, max_weight};
}