-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.cpp
More file actions
461 lines (436 loc) · 14 KB
/
Copy pathgraph.cpp
File metadata and controls
461 lines (436 loc) · 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
// Created by Amarjot Gill 10/1/2021
#include "graph.h"
const int NEGATIVE_ONE = -1;
const int ZERO = 0;
// O(1)
Graph::Graph(){
m_head = nullptr;
m_numNodes = ZERO;
m_dataFile = "";
}
// O(1)
Graph::Graph(string dataFile){
m_head = nullptr;
m_numNodes = ZERO;
m_dataFile = dataFile;
loadData();
}
// O(N)
Graph::~Graph(){
// case if graph is empty cant delete anything
if(m_head == nullptr){
return;
}
Node* current = m_head;
while(current -> getNext() != nullptr){
current = current -> getNext();
// deletes the node
delete m_head;
m_head = current;
}
delete m_head;
m_head = nullptr;
}
//O(N)
void Graph::loadData(){
int numNodes;
int node, n, e, s, w;
ifstream dataFile;
dataFile.open(m_dataFile);
if (dataFile.is_open()) {
dataFile >> numNodes;
m_numNodes = numNodes;
for (int i = 0; i < numNodes; i++) {
dataFile >> node >> n >> e >> s >> w;
insert(node, n, e, s, w);
}
}
else
//the following statement provides the reason if a file doesn't open
//please note: not all platforms are providing the same message
cerr << "Error: " << strerror(errno) << endl;
}
// O(N^2)
void Graph::insert(int node, int n, int e, int s, int w){
// This is for the case of the graph being empty and this is the first value being added to the graph
if(m_head == nullptr){
m_numNodes++;
Node *newNode = new Node(node);
m_head = newNode;
if(n > NEGATIVE_ONE){
// since head was nullptr for this case none of these
//nodes were created so all within this case will be created
Node *newNode = new Node(n);
m_head -> setNorth(newNode);
m_head -> setNext(newNode);
m_numNodes++;
}if(e > NEGATIVE_ONE){
Node *newNode = new Node(e);
m_head -> setEast(newNode);
insertAtHead(newNode);
}if(s > NEGATIVE_ONE){
Node *newNode = new Node(s);
m_head -> setSouth(newNode);
insertAtHead(newNode);
}if(w > NEGATIVE_ONE){
Node *newNode = new Node(w);
m_head -> setWest(newNode);
insertAtHead(newNode);
}
}else{
// bool to keep track if a node is already created or not
bool inList = false;
Node* temp = m_head;
// while loop to check if node is created already or not
while(temp -> getNext() != nullptr){
temp = temp -> getNext();
if(temp -> getValue() == node){
inList = true;
}
}
// if node wasnt found it is created and added to end of linked list
if(inList == false){
Node* newNode = new Node(node);
temp -> setNext(newNode);
m_numNodes++;
}
// checking for > -1 bc -1 is case of no connection
if(n > NEGATIVE_ONE){
inList = false;
Node* temp = m_head;
Node* insertedNode;
// first while loop to check if node with value n is already made
while(temp -> getNext() != nullptr){
temp = temp -> getNext();
if(temp -> getValue() == node){
// creates a second temp to search through too see if node with value n is in list
Node* secTemp = m_head;
while(secTemp != nullptr){
if(secTemp -> getValue() == n){
// if it is found sets Node's north to node with value n
temp -> setNorth(secTemp);
inList = true;
}
secTemp = secTemp -> getNext();
}
// if node with value n isnt in list then creates and sets main Node north to it
if(inList == false){
Node* newNode = new Node(n);
temp -> setNorth(newNode);
insertedNode = newNode;
}
}
}
// if node N wasnt in list then it gets added to the end of the linked list
if(inList == false){
temp -> setNext(insertedNode);
m_numNodes++;
}
// Other 3 connections work exactly the same as North
}if(e > NEGATIVE_ONE){
inList = false;
Node* temp = m_head;
Node* insertedNode;
while(temp -> getNext() != nullptr){
temp = temp -> getNext();
if(temp -> getValue() == node){
Node* secTemp = m_head;
while(secTemp != nullptr){
if(secTemp -> getValue() == e){
temp -> setEast(secTemp);
inList = true;
}
secTemp = secTemp -> getNext();
}
if(inList == false){
Node* newNode = new Node(e);
temp -> setEast(newNode);
insertedNode = newNode;
}
}
}
if(inList == false){
temp -> setNext(insertedNode);
m_numNodes++;
}
}if(s > NEGATIVE_ONE){
inList = false;
Node* temp = m_head;
Node* insertedNode;
while(temp -> getNext() != nullptr){
temp = temp -> getNext();
if(temp -> getValue() == node){
Node* secTemp = m_head;
while(secTemp != nullptr){
if(secTemp -> getValue() == s){
temp -> setSouth(secTemp);
inList = true;
}
secTemp = secTemp -> getNext();
}
if(inList == false){
Node* newNode = new Node(s);
temp -> setSouth(newNode);
insertedNode = newNode;
}
}
}
if(inList == false){
temp -> setNext(insertedNode);
m_numNodes++;
}
}if(w > NEGATIVE_ONE){
inList = false;
Node* temp = m_head;
Node* insertedNode;
while(temp -> getNext() != nullptr){
temp = temp -> getNext();
if(temp -> getValue() == node){
Node* secTemp = m_head;
while(secTemp != nullptr){
if(secTemp -> getValue() == w){
temp -> setWest(secTemp);
inList = true;
}
secTemp = secTemp -> getNext();
}
if(inList == false){
Node* newNode = new Node(w);
temp -> setWest(newNode);
insertedNode = newNode;
}
}
}
if(inList == false){
if(temp -> getValue() != insertedNode -> getValue()){
temp -> setNext(insertedNode);
m_numNodes++;
}
}
}
}
}
// O(N)
void Graph::insertAtHead(Node * aNode){
if(m_head == nullptr){
m_head = aNode;
}else{
Node* temp = m_head;
while(temp -> getNext() != nullptr){
temp = temp -> getNext();
}
// sets last node to node to be inserted
temp -> setNext(aNode);
m_numNodes++;
}
}
// O(N)
Node * Graph::findNode(int nodeValue){
if(empty()){
return m_head;
}else{
Node* temp = m_head;
while(temp != nullptr){
// once node with value is found its returned
if(temp -> getValue() == nodeValue){
return temp;
}
temp = temp -> getNext();
}
}
return nullptr;
}
//O(M + N)
bool Graph::findPath(int start, int end){
Node* temp = findNode(start);
Node* secTemp = findNode(end);
// error case, start or end node are not in graph
if(temp == nullptr || secTemp == nullptr){
throw std::out_of_range("Out of range error!");
return false;
}
// error case start or end node are negative
if(start < ZERO || end < ZERO){
throw std::out_of_range("Out of range error!");
return false;
}
// error case list is empty
if(empty()){
throw std::out_of_range("Graph is empty");
return false;
}
// case if you are searching for path to itself
if(start == end){
m_path.push(end);
return true;
}else{
Node* temp = m_head;
while(temp != nullptr){
if (temp -> getValue() == start){
// clears results and visted for obj before finding a new path
clearResult();
clearVisited();
return findPath(temp, end);
}else{
temp = temp -> getNext();
}
}
}
return false;
}
//O(M + N)
bool Graph::findPath(Node* aNode, int end){
// if first time visiting pushes node to stack and sets visited
if(aNode -> getVisited() == false){
m_path.push(aNode -> getValue());
aNode -> setVisited(true);
}
if(aNode -> getValue() == end){
return true;
// calling a depth first search starting from north and ending at west to find value
}else if(aNode -> getNorth() != nullptr && aNode -> getNorth() -> getVisited() == false){
return findPath(aNode -> getNorth(), end);
}else if(aNode -> getEast() != nullptr && aNode -> getEast() -> getVisited() == false){
return findPath(aNode -> getEast(),end);
}else if(aNode -> getSouth() != nullptr && aNode -> getSouth() -> getVisited() == false){
return findPath(aNode -> getSouth(), end);
}else if(aNode -> getWest() != nullptr && aNode -> getWest() -> getVisited() == false){
return findPath(aNode -> getWest(), end);
}if (m_path.empty()){
return false;
}else{
m_path.pop();
// prevents a seg fault and infinite loop
if(m_path.empty()){
return false;
}
// backtracking if reached node with no connections
Node* temp = findNode(m_path.top());
return findPath(temp, end);
}
return false;
}
//O(N)
void Graph::dump(){
if(m_head == nullptr){
cout << "END" << endl;
return;
}
// printing in a first in first out style needs a temp stack
stack<int> temp;
while(!m_path.empty()){
temp.push(m_path.top());
m_path.pop();
}
while(!temp.empty()){
cout<< temp.top() << " => ";
temp.pop();
}
cout<<"END "<< endl;
}
//O(N)
void Graph::clearResult(){
// popping stack to clear it
while(!m_path.empty()) {
m_path.pop();
}
}
//O(N)
void Graph::clearVisited(){
// setting all node's visited in list to false
if(m_head == nullptr){
return;
}else{
Node *temp = m_head;
temp -> setVisited(false);
while(temp -> getNext() != nullptr){
temp = temp -> getNext();
temp -> setVisited(false);
}
}
}
//O(N)
void Graph::buildGraph(string file){
// if the graph is populated clear it
if(m_head != nullptr){
clearVisited();
clearResult();
clearGraph();
}
int numNodes;
int node, n, e, s, w;
ifstream dataFile;
dataFile.open(file);
if (dataFile.is_open()) {
dataFile >> numNodes;
m_numNodes = numNodes;
for (int i = 0; i < numNodes; i++) {
dataFile >> node >> n >> e >> s >> w;
insert(node, n, e, s, w);
}
}
else
//the following statement provides the reason if a file doesn't open
//please note: not all platforms are providing the same message
cerr << "Error: " << strerror(errno) << endl;
}
//O(N)
void Graph::clearGraph(){
// Works same way as destructor
if(m_head == nullptr){
return;
}
Node* current = m_head;
while(current -> getNext() != nullptr){
current = current -> getNext();
// deletes the node
delete m_head;
m_head = current;
}
delete m_head;
m_head = nullptr;
m_numNodes = ZERO;
m_dataFile = "";
}
//O(N)
const Graph & Graph::operator=(const Graph & rhs){
// if given an empty Graph to assign
if(rhs.m_head == nullptr){
throw std::out_of_range("Can not assign an empty graph");
return *this;
}
// clear current Graph if its populated
// protects against self assignment
if(m_head != rhs.m_head){
if(m_head != nullptr){
clearResult();
clearVisited();
clearGraph();
}
Node *temp = rhs.m_head;
// Checks if currect node has connections and then calls insert function to insert them
while(temp != nullptr){
int n = NEGATIVE_ONE;
int e = NEGATIVE_ONE;
int w = NEGATIVE_ONE;
int s = NEGATIVE_ONE;
if(temp -> getNorth() != nullptr){
n = temp -> getNorth() -> getValue();
}if(temp -> getEast() != nullptr){
e = temp -> getEast() -> getValue();
}if(temp -> getSouth() != nullptr){
s = temp -> getSouth() -> getValue();
}if(temp -> getWest() != nullptr){
w = temp -> getWest() -> getValue();
}
insert(temp -> getValue(),n,e,s,w);
temp = temp -> getNext();
}
}else{
throw std::out_of_range("Self Assignment dectected");
}
return *this;
}
//O(1)
bool Graph::empty() const // is the list empty?
{ return m_head == nullptr; }