-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphsGenerating.cpp
More file actions
214 lines (185 loc) · 6.14 KB
/
GraphsGenerating.cpp
File metadata and controls
214 lines (185 loc) · 6.14 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
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
#include "GraphsGenerating.h"
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
using namespace std;
// Initialize static members
int** GraphsGenerating::incMatrix = nullptr;
int GraphsGenerating::numVertices = 0;
int GraphsGenerating::numEdges = 0;
slistEl** GraphsGenerating::adjList = nullptr;
// Define the directory path for the input files
const char* DIRECTORY_PATH = "C:\\Users\\10122\\CLionProjects\\GraphEfficiency\\resources\\";
/**
* @brief Loads a graph from a file.
*
* This function reads a graph from a file and stores it in both incidence matrix and adjacency list formats.
* The file should contain the number of edges and vertices on the first line, followed by lines containing the start vertex, end vertex, and weight of each edge.
*/
void GraphsGenerating::loadGraphFromFile() {
string fileName;
cin >> fileName;
string fullPath = DIRECTORY_PATH + fileName;
FILE *inputFile = fopen(fullPath.c_str(), "r");
if (!inputFile) {
cout << "Cannot open the file" << endl;
return;
}
fscanf(inputFile, "%d %d", &numEdges, &numVertices);
incMatrix = new int*[numVertices];
for (int i = 0; i < numVertices; ++i) {
incMatrix[i] = new int[numEdges]();
}
adjList = new slistEl*[numVertices]();
int start, end, weight, edgeIdx = 0;
while (fscanf(inputFile, "%d %d %d", &start, &end, &weight) != EOF) {
incMatrix[start][edgeIdx] = weight;
incMatrix[end][edgeIdx] = -weight;
slistEl *p = new slistEl;
p->v = end;
p->weight = weight;
p->next = adjList[start];
adjList[start] = p;
edgeIdx++;
}
fclose(inputFile);
}
/**
* @brief Prints the incidence matrix of the graph.
*
* This function prints the incidence matrix representation of the graph to the console.
*/
void GraphsGenerating::printIncidenceMatrix() {
cout << "Incidence Matrix:" << endl;
for (int i = 0; i < numVertices; i++) {
for (int j = 0; j < numEdges; j++) {
cout << incMatrix[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
/**
* @brief Prints the adjacency list of the graph.
*
* This function prints the adjacency list representation of the graph to the console.
*/
void GraphsGenerating::printAdjacencyList() {
cout << "Adjacency List:" << endl;
for (int i = 0; i < numVertices; i++) {
cout << "A [" << i << "] =";
slistEl *p = adjList[i];
while (p) {
cout << " " << p->v << " (weight: " << p->weight << ");";
p = p->next;
}
cout << endl;
}
}
/**
* @brief Adds an edge to the incidence matrix and adjacency list.
*
* @param start The start vertex of the edge.
* @param end The end vertex of the edge.
* @param weight The weight of the edge.
* @param edgeIndex The index of the edge in the incidence matrix.
*/
void addEdge(int start, int end, int weight, int edgeIndex, int** incMatrix, slistEl** adjList) {
incMatrix[start][edgeIndex] = weight;
incMatrix[end][edgeIndex] = -weight;
slistEl *p = new slistEl;
p->v = end;
p->weight = weight;
p->next = adjList[start];
adjList[start] = p;
slistEl *pReverse = new slistEl;
pReverse->v = start;
pReverse->weight = weight;
pReverse->next = adjList[end];
adjList[end] = pReverse;
}
/**
* @brief Generates a random graph.
*
* This function generates a random graph with a given number of vertices and density.
* The density is a percentage that determines the number of edges in the graph.
* The graph is stored in both incidence matrix and adjacency list formats.
*
* @param vertices The number of vertices in the graph.
* @param density The density of the graph, as a percentage.
*/
void GraphsGenerating::generateRandomGraph(int vertices, int density) {
freeMemory();
numVertices = vertices;
numEdges = (density * (vertices * (vertices - 1)) / 2) / 100;
incMatrix = new int*[numVertices];
for (int i = 0; i < numVertices; ++i) {
incMatrix[i] = new int[numEdges]();
}
adjList = new slistEl*[numVertices]();
srand(time(0));
// Create a spanning tree to ensure the graph is connected
int* verticesList = new int[numVertices];
for (int i = 0; i < numVertices; ++i) {
verticesList[i] = i;
}
// Shuffle the vertices list to create a random spanning tree
for (int i = numVertices - 1; i > 0; --i) {
int j = rand() % (i + 1);
swap(verticesList[i], verticesList[j]);
}
int edgesAdded = 0;
for (int i = 1; i < numVertices; ++i) {
int start = verticesList[i - 1];
int end = verticesList[i];
int weight = rand() % 50 + 1;
addEdge(start, end, weight, edgesAdded++, incMatrix, adjList);
}
// Add additional edges to meet the required density
while (edgesAdded < numEdges) {
int start = rand() % numVertices;
int end = rand() % numVertices;
if (start != end) {
bool alreadyConnected = false;
for (int i = 0; i < edgesAdded; ++i) {
if ((incMatrix[start][i] != 0 && incMatrix[end][i] != 0) ||
(incMatrix[end][i] != 0 && incMatrix[start][i] != 0)) {
alreadyConnected = true;
break;
}
}
if (!alreadyConnected) {
int weight = rand() % 9 + 1;
addEdge(start, end, weight, edgesAdded++, incMatrix, adjList);
}
}
}
delete[] verticesList;
}
/**
* @brief Frees the memory used by the incidence matrix and list.
*
* This function deletes the incidence matrix and list and sets their pointers to nullptr.
*/
void GraphsGenerating::freeMemory() {
if (incMatrix) {
for (int i = 0; i < numVertices; ++i) {
delete[] incMatrix[i];
}
delete[] incMatrix;
incMatrix = nullptr;
}
if (adjList) {
for (int i = 0; i < numVertices; ++i) {
slistEl *p = adjList[i];
while (p) {
slistEl *r = p;
p = p->next;
delete r;
}
}
delete[] adjList;
adjList = nullptr;
}
}