-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.h
More file actions
511 lines (436 loc) · 17.5 KB
/
Copy pathGraph.h
File metadata and controls
511 lines (436 loc) · 17.5 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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#ifndef GRAPH_H
#define GRAPH_H
#include <list>
#include <queue>
using namespace std;
template <class NodeType, class ArcType> class GraphArc;
template <class NodeType, class ArcType> class GraphNode;
// ----------------------------------------------------------------
// Name: Graph
// Description: This is the graph class, it contains all the
// nodes.
// ----------------------------------------------------------------
template<class NodeType, class ArcType>
class Graph {
private:
// typedef the classes to make our lives easier.
typedef GraphArc<NodeType, ArcType> Arc;
typedef GraphNode<NodeType, ArcType> Node;
// ----------------------------------------------------------------
// Description: An array of all the nodes in the graph.
// ----------------------------------------------------------------
Node** m_pNodes;
// ----------------------------------------------------------------
// Description: The maximum number of nodes in the graph.
// ----------------------------------------------------------------
int m_maxNodes;
// ----------------------------------------------------------------
// Description: The actual number of nodes in the graph.
// ----------------------------------------------------------------
int m_count;
public:
// Constructor and destructor functions
Graph( int size );
~Graph();
// Accessors
Node** nodeArray() const {
return m_pNodes;
}
// Public member functions.
bool addNode( NodeType data, int index );
void removeNode( int index );
bool addArc(int from, int to, ArcType weight);
bool addDualArc(int from, int to, ArcType weight);
void removeArc( int from, int to );
Arc* getArc( int from, int to );
void prepUCS();
void clearMarks();
void depthFirst( Node* pNode, void (*pProcess)(Node*) );
void breadthFirst(Node* pNode, void(*pProcess)(Node*));
void breadthFirstPlus(Node* pNode, Node* pTarget, void(*pProcess)(Node*));
void UCS(Node* pStart, Node* pTarget, void(*pProcess)(Node*), std::vector<Node*>& path);
};
template<class NodeType, class ArcType>
class NodeSearchCostComparer {
public:
bool operator()(GraphNode<NodeType, ArcType> * n1, GraphNode<NodeType, ArcType> * n2) {
return n1->data().second > n2->data().second;
}
};
// ----------------------------------------------------------------
// Name: Graph
// Description: Constructor, this constructs an empty graph
// Arguments: The maximum number of nodes.
// Return Value: None.
// ----------------------------------------------------------------
template<class NodeType, class ArcType>
Graph<NodeType, ArcType>::Graph( int size ) : m_maxNodes( size ) {
int i;
m_pNodes = new Node * [m_maxNodes];
// go through every index and clear it to null (0)
for( i = 0; i < m_maxNodes; i++ ) {
m_pNodes[i] = 0;
}
// set the node count to 0.
m_count = 0;
}
// ----------------------------------------------------------------
// Name: ~Graph
// Description: destructor, This deletes every node
// Arguments: None.
// Return Value: None.
// ----------------------------------------------------------------
template<class NodeType, class ArcType>
Graph<NodeType, ArcType>::~Graph() {
int index;
for( index = 0; index < m_maxNodes; index++ ) {
if( m_pNodes[index] != 0 ) {
delete m_pNodes[index];
}
}
// Delete the actual array
delete m_pNodes;
}
// ----------------------------------------------------------------
// Name: addNode
// Description: This adds a node at a given index in the graph.
// Arguments: The first parameter is the data to store in the node.
// The second parameter is the index to store the node.
// Return Value: true if successful
// ----------------------------------------------------------------
template<class NodeType, class ArcType>
bool Graph<NodeType, ArcType>::addNode( NodeType data, int index ) {
bool nodeNotPresent = false;
// find out if a node does not exist at that index.
if ( m_pNodes[index] == 0) {
nodeNotPresent = true;
// create a new node, put the data in it, and unmark it.
m_pNodes[index] = new Node;
m_pNodes[index]->setData(data);
m_pNodes[index]->setMarked(false);
// increase the count and return success.
m_count++;
}
return nodeNotPresent;
}
// ----------------------------------------------------------------
// Name: removeNode
// Description: This removes a node from the graph
// Arguments: The index of the node to return.
// Return Value: None.
// ----------------------------------------------------------------
template<class NodeType, class ArcType>
void Graph<NodeType, ArcType>::removeNode( int index ) {
// Only proceed if node does exist.
if( m_pNodes[index] != 0 ) {
// now find every arc that points to the node that
// is being removed and remove it.
int node;
Arc* arc;
// loop through every node
for( node = 0; node < m_maxNodes; node++ ) {
// if the node is valid...
if( m_pNodes[node] != 0 ) {
// see if the node has an arc pointing to the current node.
arc = m_pNodes[node]->getArc( m_pNodes[index] );
}
// if it has an arc pointing to the current node, then
// remove the arc.
if( arc != 0 ) {
removeArc( node, index );
}
}
// now that every arc pointing to the current node has been removed,
// the node can be deleted.
delete m_pNodes[index];
m_pNodes[index] = 0;
m_count--;
}
}
// ----------------------------------------------------------------
// Name: addArc
// Description: Adds an arc from the first index to the
// second index with the specified weight.
// Arguments: The first argument is the originating node index
// The second argument is the ending node index
// The third argument is the weight of the arc
// Return Value: true on success.
// ----------------------------------------------------------------
template<class NodeType, class ArcType>
bool Graph<NodeType, ArcType>::addArc( int from, int to, ArcType weight ) {
bool proceed = true;
// make sure both nodes exist.
if( m_pNodes[from] == 0 || m_pNodes[to] == 0 ) {
proceed = false;
}
// if an arc already exists we should not proceed
if( m_pNodes[from]->getArc( m_pNodes[to] ) != 0 ) {
proceed = false;
}
if (proceed == true) {
// add the arc to the "from" node.
m_pNodes[from]->addArc( m_pNodes[to], weight );
cout << "Adding arc from " << m_pNodes[from]->data().first << " to " << m_pNodes[to]->data().first << " weight " << weight << endl;
}
return proceed;
}
template<class NodeType, class ArcType>
bool Graph<NodeType, ArcType>::addDualArc(int from, int to, ArcType weight) {
bool proceed = true;
// make sure both nodes exist.
if (m_pNodes[from] == 0 || m_pNodes[to] == 0) {
proceed = false;
}
// if an arc already exists we should not proceed
if (m_pNodes[from]->getArc(m_pNodes[to]) != 0) {
proceed = false;
}
if (proceed == true) {
// add the arc to the "from" node.
m_pNodes[from]->addArc(m_pNodes[to], weight);
m_pNodes[to]->addArc(m_pNodes[from], weight);
//cout << "Adding dual arc from " << m_pNodes[from]->data().first << " to " << m_pNodes[to]->data().first << " weight " << weight << endl;
}
return proceed;
}
// ----------------------------------------------------------------
// Name: removeArc
// Description: This removes the arc from the first index to the second index
// Arguments: The first parameter is the originating node index.
// The second parameter is the ending node index.
// Return Value: None.
// ----------------------------------------------------------------
template<class NodeType, class ArcType>
void Graph<NodeType, ArcType>::removeArc( int from, int to ) {
// Make sure that the node exists before trying to remove
// an arc from it.
bool nodeExists = true;
if( m_pNodes[from] == 0 || m_pNodes[to] == 0 ) {
nodeExists = false;
}
if (nodeExists == true) {
// remove the arc.
m_pNodes[from]->removeArc( m_pNodes[to] );
}
}
// ----------------------------------------------------------------
// Name: getArc
// Description: Gets a pointer to an arc from the first index
// to the second index.
// Arguments: The first parameter is the originating node index.
// The second parameter is the ending node index.
// Return Value: pointer to the arc, or 0 if it doesn't exist.
// ----------------------------------------------------------------
template<class NodeType, class ArcType>
// Dev-CPP doesn't like Arc* as the (typedef'd) return type?
GraphArc<NodeType, ArcType>* Graph<NodeType, ArcType>::getArc( int from, int to ) {
Arc* pArc = 0;
// make sure the to and from nodes exist
if( m_pNodes[from] != 0 && m_pNodes[to] != 0 ) {
pArc = m_pNodes[from]->getArc( m_pNodes[to] );
}
return pArc;
}
template<class NodeType, class ArcType>
void Graph<NodeType, ArcType>::prepUCS()
{
cout << "////===== Prepping UCS" << endl;
int index;
for (index = 0; index < m_maxNodes; index++) {
if (m_pNodes[index] != 0) {
m_pNodes[index]->setData(pair<string, int>(m_pNodes[index]->data().first, INT_MAX));
//cout << m_pNodes[index]->data().first << " distance maxed." << endl;
m_pNodes[index]->setMarked(false);
m_pNodes[index]->setPrev(NULL);
//cout << m_pNodes[index]->data().first << " unmarked" << endl;
}
}
//cout << endl;
}
// ----------------------------------------------------------------
// Name: clearMarks
// Description: This clears every mark on every node.
// Arguments: None.
// Return Value: None.
// ----------------------------------------------------------------
template<class NodeType, class ArcType>
void Graph<NodeType, ArcType>::clearMarks() {
int index;
for( index = 0; index < m_maxNodes; index++ ) {
if( m_pNodes[index] != 0 ) {
m_pNodes[index]->setMarked(false);
cout << m_pNodes[i]->data().first << " unmarked." << endl;
}
}
}
// ----------------------------------------------------------------
// Name: depthFirst
// Description: Performs a depth-first traversal on the specified
// node.
// Arguments: The first argument is the starting node
// The second argument is the processing function.
// Return Value: None.
// ----------------------------------------------------------------
template<class NodeType, class ArcType>
void Graph<NodeType, ArcType>::depthFirst( Node* pNode, void (*pProcess)(Node*) ) {
if( pNode != 0 ) {
// process the current node and mark it
pProcess( pNode );
pNode->setMarked(true);
// go through each connecting node
list<Arc>::iterator iter = pNode->arcList().begin();
list<Arc>::iterator endIter = pNode->arcList().end();
for( ; iter != endIter; ++iter) {
// process the linked node if it isn't already marked.
if ( (*iter).node()->marked() == false ) {
depthFirst( (*iter).node(), pProcess);
}
}
}
}
// ----------------------------------------------------------------
// Name: breadthFirst
// Description: Performs a depth-first traversal the starting node
// specified as an input parameter.
// Arguments: The first parameter is the starting node
// The second parameter is the processing function.
// Return Value: None.
// ----------------------------------------------------------------
template<class NodeType, class ArcType>
void Graph<NodeType, ArcType>::breadthFirst( Node* pNode, void (*pProcess)(Node*) ) {
if( pNode != 0 ) {
queue<Node*> nodeQueue;
// place the first node on the queue, and mark it.
nodeQueue.push( pNode );
pNode->setMarked(true);
// loop through the queue while there are nodes in it.
while( nodeQueue.size() != 0 ) {
// process the node at the front of the queue.
pProcess( nodeQueue.front() );
// add all of the child nodes that have not been
// marked into the queue
list<Arc>::const_iterator iter = nodeQueue.front()->arcList().begin();
list<Arc>::const_iterator endIter = nodeQueue.front()->arcList().end();
for( ; iter != endIter; iter++ ) {
if ( (*iter).node()->marked() == false) {
// mark the node and add it to the queue.
(*iter).node()->setMarked(true);
nodeQueue.push( (*iter).node() );
}
}
// dequeue the current node.
nodeQueue.pop();
}
}
}
// ----------------------------------------------------------------
// Name: breadthFirstPlus
// Description: Performs a depth-first traversal from the starting node
// to the target node specified as input parameters.
// Arguments: The first parameter is the starting node
// The second parameter is the target node
// The third parameter is the processing function.
// Return Value: None.
// ----------------------------------------------------------------
template<class NodeType, class ArcType>
void Graph<NodeType, ArcType>::breadthFirstPlus(Node* pNode, Node* pTarget, void(*pProcess)(Node*)) {
if (pNode != 0) {
queue<Node*> nodeQueue;
bool found = false;
// place the first node on the queue, and mark it.
nodeQueue.push(pNode);
pNode->setMarked(true);
// loop through the queue while there are nodes in it.
while (nodeQueue.size() != 0 && !found) {
// process the node at the front of the queue.
pProcess(nodeQueue.front());
// add all of the child nodes that have not been
// marked into the queue
list<Arc>::const_iterator iter = nodeQueue.front()->arcList().begin();
list<Arc>::const_iterator endIter = nodeQueue.front()->arcList().end();
for (; iter != endIter && !found; iter++) {
//if the node is our target set found to true
if ((*iter).node() == pTarget)
{
(*iter).node()->setPrev(nodeQueue.front());
found = 1;
}
//else add it to the queue
else if ((*iter).node()->marked() == false) {
// mark the node and add it to the queue.
(*iter).node()->setMarked(true);
(*iter).node()->setPrev(nodeQueue.front());
nodeQueue.push((*iter).node());
}
}
// dequeue the current node.
nodeQueue.pop();
}
}
}
template<class NodeType, class ArcType>
void Graph<NodeType, ArcType>::UCS(Node* pStart, Node* pTarget, void(*pProcess)(Node*), std::vector<Node*>& path)
{
//init distances and unmark
prepUCS();
pStart->setData(pair<string, int>(pStart->data().first, 0));
cout << "////===== UCS from " << pStart->data().first << " to " << pTarget->data().first << endl;
//make & set up queue
priority_queue<Node*, vector<Node*>, NodeSearchCostComparer<NodeType, ArcType>> pq;
//Start of UCS
pq.push(pStart);
pStart->setMarked(true);
//Priority Queueue loop
while (!pq.empty() && pq.top() != pTarget)
{
//cout << "TOP: " << pq.top()->data().first << endl;
//for each child node
list<Arc>::const_iterator iter = pq.top()->arcList().begin();
list<Arc>::const_iterator endIter = pq.top()->arcList().end();
//Process all children of the top node
for (; iter != endIter; iter++) {
//if the previous node is not top of the queue
if ((*iter).node() != pq.top()->getPrev())
{
//cout << "Checking: " << pq.top()->data().first << " -> " << (*iter).node()->data().first << " ";
//Get total weight of this route
int c = pq.top()->getArc((*iter).node())->weight() + pq.top()->data().second;
//cout << "[" << c << " < " << ((*iter).node()->data().second) << "]" << endl;
//if it's lower than the weight of the current route
if (c < ((*iter).node()->data().second))
{
//cout << "True, " << (*iter).node()->data().first << " weight is now " << c << ", previous is now " << pq.top()->data().first << endl;
//Set the node's internal weight to the arc from previous plus internal weight of previous
(*iter).node()->setData(pair<string, int>((*iter).node()->data().first, c));
//Set previous pointer of the node to the previous node in the new path
(*iter).node()->setPrev(pq.top());
}
else
{
//if ((*iter).node()->getPrev() != 0)
//cout << "False, " << (*iter).node()->data().first << " remaining " << ((*iter).node()->data().second) << ", previous remains " << (*iter).node()->getPrev()->data().first << endl;
//else cout << "False, " << (*iter).node()->data().first << " remaining " << ((*iter).node()->data().second) << ", previous remains NULL" << endl;
}
//if not marked
if ((*iter).node()->marked() == false) {
//add it to the queue and mark
pq.push((*iter).node());
//cout << "Queueing: " << (*iter).node()->data().first << endl;
(*iter).node()->setMarked(true);
}
}
}
//cout << "Popping: " << pq.top()->data().first << endl << endl;
pq.pop();
}
//Add the nodes to path
while (pTarget->getPrev() != NULL)
{
path.push_back(pTarget);
pTarget = pTarget->getPrev();
}
path.push_back(pTarget);
}
#include "GraphNode.h"
#include "GraphArc.h"
#endif