-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphMatrix.cpp
214 lines (176 loc) · 4.91 KB
/
GraphMatrix.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
// Filename: GraphMatrix.cpp
// Author: EVGENI C. DOBRANOV
// Date: 4.19.2016
#include "GraphMatrix.h"
#include <cassert>
// Purpose: Constructor for a GraphMatrix
GraphMatrix::GraphMatrix()
{
width = 0; // Start with no edges
height = 0; // Start with no vertices
widthcap = INITIAL_SIZE; // Initial edge cap
heightcap = INITIAL_SIZE; // Initial vertex cap
matrix = new string*[INITIAL_SIZE]; // Create new "vertical" row
// For every entry, create the columns (how 2D matrices are done in C++)
for (int i = 0; i < INITIAL_SIZE; ++i)
matrix[i] = new string[INITIAL_SIZE];
for (int i = 0; i < INITIAL_SIZE; ++i) // Populate all entries with blanks
{
for (int j = 0; j < INITIAL_SIZE; ++j)
matrix[i][j] = "";
}
}
// Purpose: Destructor for a GraphMatrix
GraphMatrix::~GraphMatrix()
{
for (int i = 0; i < heightcap; ++i)
delete[] matrix[i];
delete[] matrix;
}
// Purpose: Operator overload for () -> Returns the matrix value at indices
string &GraphMatrix::operator()(int a, int b) const
{
assert(a >= 0 && a <= height && b >= 0 && b <= width);
return matrix[a][b];
}
// Purpose: Adds new entry for vertex / edge, and adds a "T" in that position
void GraphMatrix::add(string vertex, string edge)
{
if (height + 1 == heightcap) // Expand the height if necessary
expandheight();
if (width + 1 == widthcap) // Expand the width if necessary
expandwidth();
// Go through rows until the vertex or a blank space is found
int row = 1;
while (matrix[row][0] != "" and matrix[row][0] != vertex)
row++;
// If exit condition was because of a blank, add the vertex to the matrix
if (matrix[row][0] == "")
{
matrix[row][0] = vertex;
height++;
}
// Go through cols until the edge or a blank space is found
int col = 1;
while (matrix[0][col] != "" and matrix[0][col] != edge)
col++;
// If exit condition was because of a blank, add the edge to the matrix
if (matrix[0][col] == "")
{
matrix[0][col] = edge;
width++;
}
matrix[row][col] = "T"; // Mark the intersection with a "T"
}
// Purpose: Expand the height (capacity for the number of vertices)
void GraphMatrix::expandheight()
{
string **temp = new string*[heightcap * 2 + 2]; // New 2D matrix
// Go through all of the rows of the original matrix
for (int row = 0; row < heightcap * 2 + 2; ++row)
{
// Create all of the new columns
temp[row] = new string[widthcap];
if (row <= height) // Add all original "T" values
{
for (int col = 0; col < widthcap; ++col)
temp[row][col] = matrix[row][col];
}
// Add blanks to the remainder
else {
for (int col = 0; col < widthcap; ++col)
temp[row][col] = "";
}
}
// Delete original values and reassign values
for (int row = 0; row < heightcap; ++row)
delete[] matrix[row];
delete[] matrix;
matrix = temp;
heightcap = heightcap * 2 + 2;
}
// Purpose: Expand the width (capacity for the number of edges)
void GraphMatrix::expandwidth()
{
// Go through all of the rows of the original matrix
for (int row = 0; row < heightcap; ++row)
{
// Create all of the new columns
string *temp = new string[widthcap * 2 + 2];
// Copy all column values over
for (int col = 0; col < widthcap * 2 + 2; ++col)
{
if (col <= width)
temp[col] = matrix[row][col];
else
temp[col] = "";
}
delete[] matrix[row]; // Delete current row
matrix[row] = temp; // Update to new row
}
widthcap = widthcap * 2 + 2; // Update width cap
}
// Purpose: List all of the vertices
string* GraphMatrix::lv()
{
string *vertexs = new string[height + 1];
for (int i = 1; i <= height; ++i)
vertexs[i-1] = matrix[i][0];
return vertexs;
}
// Purpose: List all of the edges
string* GraphMatrix::le()
{
string *edges = new string[width + 1];
for (int i = 1; i <= width; ++i)
edges[i-1] = matrix[0][i];
return edges;
}
// Purpose: Search through the rows of the matrix (i.e. - vertices)
void GraphMatrix::rowmajor(string vertex)
{
int row = 1;
while (matrix[row][0] != "" && matrix[row][0] != vertex && row <= height)
row++;
if (matrix[row][0] == vertex)
{
for (int col = 1; col <= width; ++col)
{
if (matrix[row][col] == "T")
cout << matrix[0][col] << endl;
}
}
}
// Purpose: Search through the columns of the matrix (i.e. - edges)
void GraphMatrix::colmajor(string edge)
{
int col = 1;
while (matrix[0][col] != "" && matrix[0][col] != edge && col <= width)
col++;
if (matrix[0][col] == edge)
{
for (int row = 1; row <= height; ++row)
{
if (matrix[row][col] == "T")
cout << matrix[row][0] << endl;
}
}
}
// Purpose: Searches through the rows and attempts to find a vertex or edge
bool GraphMatrix::exists(string vertex, string edge)
{
// If vertex isn't blank, search for the vertex
if (vertex != "") {
for (int row = 1; row <= height; ++row) {
if (matrix[row][0] == vertex)
return true;
}
}
else if (edge != "") {
for (int col = 1; col <= width; ++col) {
if (matrix[0][col] == edge)
return true;
}
}
return false;
}