-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.cpp
224 lines (190 loc) · 5.26 KB
/
graph.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "graph.h"
Graph::Graph()
{
// empty
}
void Graph::addNode(Node *node)
{
m_nodes.push_back(node);
updateAdjacencyMatrix();
m_nodesCount++;
}
void Graph::addEdge(Edge edge)
{
if (hasEdge(edge))
return;
m_edges.push_back(edge);
updateAdjacencyMatrix();
}
int Graph::getNodesCount() const
{
return m_nodesCount;
}
Node *Graph::getNodeAt(QPointF pos)
{
Node n(pos);
for (Node *nn : m_nodes)
if (Node::getDistance(n, *nn) < Node::blockedRadius)
return nn;
return nullptr;
}
std::vector<Node *> Graph::getNodes() const
{
return m_nodes;
}
std::vector<Edge> Graph::getEdges() const
{
return m_edges;
}
bool Graph::hasEdge(Edge edge) const
{
for (const Edge &x : m_edges)
if (edge == x)
return true;
return false;
}
bool Graph::isOriented() const
{
return m_oriented;
}
void Graph::setOrientation(bool oriented)
{
m_oriented = oriented;
}
void Graph::moveNode(Node *node, QPointF pos)
{
if (node == nullptr)
return;
node->setCoord(pos);
}
void Graph::updateAdjacencyMatrix()
{
int size = getNodesCount();
m_adjacencyMatrix = std::vector<std::vector<bool>>(size, std::vector<bool>(size));
for (const Edge &edge : m_edges) {
m_adjacencyMatrix[edge.getFirstNode()->getInfo() - 1][edge.getSecondNode()->getInfo() - 1]
= true;
}
}
void Graph::saveChangesToFile(QString filename)
{
QFile file(filename);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return;
QTextStream stream(&file);
stream << "Nodes count: " << m_nodesCount << "\n\n";
updateAdjacencyMatrix();
stream << "Adjacency matrix:\n";
for (auto &row : m_adjacencyMatrix) {
for (int val : row) {
stream << val << " ";
}
stream << "\n";
}
stream << "\n";
stream << "Paths:\n";
for (auto &path : m_paths) {
size_t i = 0;
for (; i < path.size() - 1; i++) {
stream << path[i] << ", ";
}
stream << path[i] << "\n";
}
stream << "\n";
stream << "Cycles:\n";
for (auto &cycle : m_cycles) {
size_t i = 0;
for (; i < cycle.size() - 1; i++) {
stream << cycle[i] << ", ";
}
stream << cycle[i] << "\n";
}
stream << "\n";
stream << "Adjacency list:\n";
for (size_t i = 0; i < m_adjacencyMatrix.size(); i++) {
stream << i + 1 << ": ";
size_t j = 0;
for (; j < m_adjacencyMatrix.size(); j++) {
if (m_adjacencyMatrix[i][j] == 1)
stream << j + 1 << ", ";
}
stream << "\n";
}
stream << "\n";
file.close();
}
void Graph::insertCycle(std::vector<int> &cycle) {
for (size_t i = 0; i < cycle.size(); i++) {
cycle.pop_back();
std::rotate(cycle.rbegin(), cycle.rbegin() + 1, cycle.rend());
cycle.push_back(cycle[0]);
if (m_cycles.find(cycle) != m_cycles.end())
return;
}
m_cycles.insert(cycle);
}
void Graph::generateRandomNodes(int n)
{
QPoint center = {500, 340};
int circleRadius = 300;
int initialNodesCount = m_nodesCount;
while (m_nodesCount < initialNodesCount + n) {
int angle = QRandomGenerator::global()->bounded(0, 361);
QPoint randPos = {int(center.x() + circleRadius*cos(angle)), int(center.y() + circleRadius*sin(angle))};
if (!getNodeAt(randPos))
addNode(new Node(randPos, m_nodesCount + 1));
}
}
void Graph::generateElementaryPath()
{
int start = 0;
std::vector<int> path = {start + 1};
std::vector<int> indexes(m_nodesCount - 1);
std::iota(indexes.begin(), indexes.end(), 1);
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(indexes.begin(), indexes.end(), g);
int indexFirstNode = start;
for (const auto &indexSecondNode : indexes) {
addEdge(Edge(m_nodes[indexFirstNode], m_nodes[indexSecondNode]));
path.push_back(indexSecondNode + 1);
if (indexSecondNode == m_nodesCount - 1)
break;
indexFirstNode = indexSecondNode;
}
m_paths.insert(path);
}
void Graph::generateElementaryPaths(int count)
{
std::size_t initialSize = m_paths.size();
while (m_paths.size() < initialSize + std::size_t(count))
generateElementaryPath();
}
void Graph::generateElementaryCycle()
{
std::vector<bool> cycleNodes = std::vector<bool>(m_nodesCount);
std::vector<int> indexes(m_nodesCount);
std::iota(indexes.begin(), indexes.end(), 0);
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(indexes.begin(), indexes.end(), g);
int startPos = QRandomGenerator::global()->bounded(1, m_nodesCount);
int start = indexes[startPos];
std::vector<int> cycle = {start + 1};
int indexFirstNode = start;
for (int &indexSecondNode : indexes) {
addEdge(Edge(m_nodes[indexFirstNode], m_nodes[indexSecondNode]));
cycle.push_back(indexSecondNode + 1);
if (indexSecondNode == start)
break;
indexFirstNode = indexSecondNode;
}
insertCycle(cycle);
}
void Graph::generateElementaryCycles(int count)
{
std::size_t initialSize = m_paths.size();
while (m_cycles.size() < initialSize + std::size_t(count)) {
generateElementaryCycle();
}
}