-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.h
257 lines (209 loc) · 7.62 KB
/
Graph.h
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
//
// Created by luise on 5/3/2020.
//
#ifndef ALGORITHMS_GRAPH_H
#define ALGORITHMS_GRAPH_H
#include "Node.h"
#include "Edge.h"
#include <fstream>
#include <iostream>
#include "LinkedList.h"
using std::cout;
using std::endl;
using std::string;
using std::ofstream;
template<class T>
class Graph{
private:
LinkedList< LinkedList<T> > graph;
int size = 0;
public:
Graph();
void add(T);
int graphSize();
LinkedList<T> getAllNodes(const string&);
LinkedList<T> &checkListOfVertexNodes(const string &);
bool checkNetworkConnections(const string&,const string&, const LinkedList<T>&);
void printGraph();
void printMatrix();
void outputGraph_ToFile(const string&);
void outputMatrix_ToFile(const string&);
};
template <class T>
Graph<T>::Graph() = default;
template<class T>
void Graph<T>::add(T newNode){
if(newNode.getVertexNode_B() != ""){
LinkedList<T>* vertexLinkedList = &checkListOfVertexNodes(newNode.getVertexNode_A());
if(vertexLinkedList != nullptr){
bool dataExists = checkNetworkConnections(newNode.getVertexNode_A(), newNode.getVertexNode_B(), *vertexLinkedList);
if(!dataExists){
vertexLinkedList->append(newNode); // adds connection to Node
size++;
}
}
else{
LinkedList<T> newLinkedLinkedList;
newLinkedLinkedList.append(newNode);
size++;
graph.append(newLinkedLinkedList); // add this LinkedLinkedList to graph
}
}
}
template<class T>
int Graph<T>::graphSize(){
return this->size;
}
template<class T>
LinkedList<T> & Graph<T>::checkListOfVertexNodes(const string &vertexValue) {
Node< LinkedList<T> >* currentLinkedList = graph.getLinkedList_head();
while(currentLinkedList != nullptr){
if(currentLinkedList->getData()->getLinkedList_head()->getData()->getVertexNode_A() == vertexValue){
return *(currentLinkedList->getData());
}
currentLinkedList = currentLinkedList->getNextNode();
}
LinkedList<T> *temp_val = nullptr;
return *temp_val;
}
template<class T>
bool Graph<T>::checkNetworkConnections(const string& vertex_A, const string& vertex_B, const LinkedList<T>& aNetworkLinkedList){
auto tempIterator = aNetworkLinkedList;
tempIterator.resetIterator();
while(tempIterator.iteratorIsValid()){
if(tempIterator.getIterator().getVertexNode_A() == vertex_B){
return true;
}
tempIterator.iterateForward();
}
return false;
}
template<class T>
LinkedList<T> Graph<T>::getAllNodes(const string& aVertex){
Node< LinkedList<T> >* currentLinkedList = graph.getLinkedList_head();
LinkedList<T> graphVertex = LinkedList<T>();
currentLinkedList->getData()->resetIterator();
while(currentLinkedList != nullptr){
currentLinkedList->getData()->resetIterator();
while(currentLinkedList->getData()->iteratorIsValid()){
if(currentLinkedList->getData()->getIterator().getVertexNode_A() == aVertex){
graphVertex.append(currentLinkedList->getData()->getIterator());
}
currentLinkedList->getData()->iterateForward();
}
currentLinkedList = currentLinkedList->getNextNode();
}
return graphVertex;
}
template<class T>
void Graph<T>::printGraph(){
Node< LinkedList<T> >* currentLinkedList = graph.getLinkedList_head();
cout << "----------------------------- Format: Vertex (Weight) ------------------------------------" << endl << endl;
while(currentLinkedList != nullptr){
cout << currentLinkedList->getData()->getLinkedList_head()->getData()->getVertexNode_A() << " --> ";
currentLinkedList->getData()->print();
currentLinkedList = currentLinkedList->getNextNode();
if(currentLinkedList != nullptr){
cout << endl;
}
}
cout << "------------------------------------------------------------------------------------------- " << endl;
}
template<class T>
void Graph<T>::printMatrix(){
cout << "--> FORMAT : MATRIX" << endl << endl;
Node< LinkedList<T> >* tempLinkedList = graph.getLinkedList_head();
int counter = 1;
cout << " " << std::setw(3) << std::left << "";
while(tempLinkedList != nullptr){
cout << std::setw(4) << std::left << tempLinkedList->getData()->getLinkedList_head()->getData()->getVertexNode_A() ;
tempLinkedList = tempLinkedList->getNextNode();
if(tempLinkedList != nullptr){
counter++;
}
}
cout << endl;
cout << "------";
for(int i = 0; i < counter; i++){
cout << "----";
}
cout << endl;
Node< LinkedList<T> >* currentLinkedList = graph.getLinkedList_head();
while(currentLinkedList != nullptr){
cout << std::setw(4) << std::left << currentLinkedList->getData()->getLinkedList_head()->getData()->getVertexNode_A() << "| ";
currentLinkedList->getData()->printMatrix(counter);
currentLinkedList = currentLinkedList->getNextNode();
if(currentLinkedList != nullptr){
cout << endl;
}
}
cout << "------";
for(int i = 0; i < counter; i++){
cout << "----";
}
cout << endl;
}
template<class T>
void Graph<T>::outputGraph_ToFile(const string& outputFile){
fstream fout;
fout.open(outputFile.c_str(), std::ios::app);
if (!fout.is_open()) {
cout << "'" << outputFile << "' could not be opened. Please check input files." << endl;
exit(-1);
}
Node< LinkedList<T> >* currentLinkedList = graph.getLinkedList_head();
fout << "\n--> FORMAT : DIRECTED GRAPH" << endl << endl;
while(currentLinkedList != nullptr){
fout << currentLinkedList->getData()->getLinkedList_head()->getData()->getVertexNode_A() << " --> ";
currentLinkedList->getData()->printGraph_ToFile(fout);
currentLinkedList = currentLinkedList->getNextNode();
if(currentLinkedList != nullptr){
fout << endl;
}
}
fout.close();
fout.clear();
}
template<class T>
void Graph<T>::outputMatrix_ToFile(const string& outputFile){
fstream fout;
fout.open(outputFile.c_str(), std::ios::app);
if (!fout.is_open()) {
cout << "'" << outputFile << "' could not be opened. Please check input files." << endl;
exit(-1);
}
fout << "\n--> FORMAT : MATRIX" << endl << endl;
Node< LinkedList<T> >* tempLinkedList = graph.getLinkedList_head();
int counter = 1;
fout << " " << std::setw(3) << std::left << "";
while(tempLinkedList != nullptr){
fout << std::setw(4) << std::left << tempLinkedList->getData()->getLinkedList_head()->getData()->getVertexNode_A();
tempLinkedList = tempLinkedList->getNextNode();
if(tempLinkedList != nullptr){
counter++;
}
}
fout << endl;
fout << "------";
for(int i = 0; i < counter; i++){
fout << "----";
}
fout << endl;
Node< LinkedList<T> >* currentLinkedList = graph.getLinkedList_head();
while(currentLinkedList != nullptr){
fout << std::setw(4) << std::left << currentLinkedList->getData()->getLinkedList_head()->getData()->getVertexNode_A() << "| ";
currentLinkedList->getData()->printMatrix_ToFile(fout,counter);
currentLinkedList = currentLinkedList->getNextNode();
if(currentLinkedList != nullptr){
fout << endl;
}
}
fout << "------";
for(int i = 0; i < counter; i++){
fout << "----";
}
fout << endl;
fout.close();
fout.clear();
}
#endif //ALGORITHMS_GRAPH_H