-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw4.cpp
More file actions
269 lines (252 loc) · 7.17 KB
/
hw4.cpp
File metadata and controls
269 lines (252 loc) · 7.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <string>
#include <iostream>
#include <list>
#include <sstream>
#include <fstream>
#include "ArgumentManager.h"
using namespace std;
class graphType
{
protected:
//used to hold the number of vertices
int maxVertices;
//used to hold the current number of vertices
int nVertices;
list<int> *graph;
public:
graphType() {
maxVertices = 0;
nVertices = 0;
}
graphType(int size)
{
maxVertices = size;
nVertices = size;
graph = new list<int>[size];
}
~graphType()
{
delete[] graph;
}
int getnVertices() const
{
return nVertices;
}
bool isEmpty() const
{
return nVertices == 0;
}
void clearGraph()
{
for (int index = 0; index < nVertices; index++)
graph[index].clear();
nVertices = 0;
}
void make_Graph(ifstream &input, const int& size)
{
int vX;
int adj_Vertex;
string read_Line;
if (nVertices != 0)
clearGraph();
nVertices = size;
if (input.fail())
{
cout << "Cannot open input file." << endl;
return;
}
vX = 0;
while (getline(input, read_Line) && !read_Line.empty())
{
stringstream ss(read_Line);
while (ss >> adj_Vertex) {
graph[vX].push_back(adj_Vertex);
}
vX++;
}
}
//function pulled from the book to traverse list
void Traversal(int var, bool found[])
{
found[var] = true;
for (auto& i : graph[var])
{
int testing = i;//returns the label of the vertex
if (!found[testing])
Traversal(testing, found);
}
}
//function pulled from the book to traverse the list depth first
void DTransversal(int& size)
{
bool *found;//points to a created array that will keep track of vertex that has been visited
bool *last_position;
found = new bool[nVertices];
last_position = new bool[nVertices];
for (int i = 0; i < nVertices; i++)
{
found[i] = false;
last_position[i] = false;
}
//Traverse applied to vetices not found
for (int dT = 0; dT < nVertices; dT++)
{
if (!found[dT])
{
Traversal(dT, found);
size++;
}
}
delete[]found;
delete[]last_position;
}
};
//class pulled from the book that implements Prim's algorithm
class MinimumSpannigTree : public graphType
{
private:
double **nweights;
int *numEdges;
double *edgeWeight;
public:
MinimumSpannigTree(int size) : graphType(size)
{
nweights = new double*[size];
for (int i = 0; i < size; i++)
{
nweights[i] = new double[size];
}
numEdges = new int[size];
edgeWeight = new double[size];
}
MinimumSpannigTree() {
nweights = 0;
numEdges = 0;
edgeWeight = 0;
}
//function to set the weights of the numEdges of the tree
void set_E_Weights(ifstream& input)
{
int i = 0;
string line_read;
while (getline(input, line_read) && !line_read.empty())
{
int k = 0;
int edge_weight;
stringstream parse(line_read);
while (parse >> edge_weight)
{
nweights[i][k] = edge_weight;
k++;
}
i++;
}
}
void DepthT(int num_cluster, ofstream& output)//transverses to see which vertex with weights connects
{
int size = 0;
//loop checking to see the amount of clusters isnt equal clusters wanted
while (size != num_cluster) {
size = 0;
DTransversal(size);//
if (size != num_cluster) {
//searching for the highest weight and stores it to max
double max_weight = 0;
for (int i = 0; i < nVertices; i++)
{
for (int j = 0; j < nVertices; j++)
{
if (max_weight < nweights[i][j])
{
max_weight = nweights[i][j];
}
}
}
//for loops determining conditions is the weight is -999
for (int i = 0; i < nVertices; i++)
{
for (int j = 0; j < nVertices; j++)
{
if (max_weight == nweights[i][j])
{
nweights[i][j] = -999;
graph[i].remove(j);
graph[j].remove(i);
}
}
}
}
else {
size = 0;
bool *found = new bool[nVertices], *last = new bool[nVertices];
for (int i = 0; i < nVertices; i++) {
found[i] = false;last[i] = false;
}
//depth traversal for the vertices not found
for (int depth = 0; depth < nVertices; depth++)
{
if (!found[depth])
{
Traversal(depth, found);
//itterate size to keep track of clusters
size++;
for (int i = 0; i < nVertices; i++) {
if (found[i] && !last[i]) {
output << i << " ";
}
}
output << endl;
for (int i = 0; i < nVertices; i++) {
if (!last[i] && found[i]) last[i] = found[i];
}
}
}
delete[] found;
delete[] last;
}
}
}
~MinimumSpannigTree()
{
for (int i = 0; i < nVertices; i++)
delete[] nweights[i];
delete[] nweights;
delete[] numEdges;
delete edgeWeight;
}
};
int main(int argc, char *argv[])
{
ArgumentManager am(argc, argv);
string inputA = am.get("A");
string outputC = am.get("C");
ifstream input(inputA);
ofstream output(outputC);
int total_Size = 0;
string line_read;
while (getline(input, line_read) && !line_read.empty()) {
total_Size++;
}
int matrix_size = 0;
while (getline(input, line_read) && !line_read.empty())
matrix_size++;
if (total_Size != matrix_size)
output.close();
else
{
MinimumSpannigTree graph(total_Size);
input.seekg(0);
graph.make_Graph(input, total_Size);
graph.set_E_Weights(input);
int clusters;
input >> clusters;
if (clusters<1 || clusters > total_Size)
output.close();
else {
graph.DepthT(clusters, output);
input.close();
output.close();
}
}
return 0;
}