-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph2.cpp
953 lines (706 loc) · 27.7 KB
/
graph2.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
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
#include <iostream>
#include <map>
#include <vector>
#include <list>
#include <ctime> // standard C library
#include <cstdlib> // standard C library
#include <fstream>
// forward class declarations
class graphPoint;
class Graph;
class ShortestPathAlgo
{
private:
std::list<unsigned int> *pathList;
int pathCost; // the path cost, or (-1) if no path exists
public:
ShortestPathAlgo();
~ShortestPathAlgo();
// returns a count of the nodes
unsigned int verticies(Graph &G);
// returns the cost of the path (or -1 if no path exists)
int path_size( Graph &G, unsigned int originNode, unsigned int destNode );
// returns a std::list pointer with the path
std::list<unsigned int> *path( Graph &G, unsigned int originNode, unsigned int destNode);
// this helps print the path list
friend std::ostream &operator<< (std::ostream &cout, std::list<unsigned int> *path);
};
//-------------------------------------------------------------------------------------------------------
// A class defining an entire graph, which is comprised of graphPoints with edges to other graphPoints
//-------------------------------------------------------------------------------------------------------
//
class Graph
{
private:
std::map< int, graphPoint* > graphNodes;// a map of all graphPoints (i.e. nodes, vertices) in the graph
unsigned int m_totalNumVerticies; // the total number of vertices (nodes) in this graph
unsigned int m_totalNumEdges; // the total number of edges in this graph
int m_originNode; // the node number of the origin (-1 if not assigned)
public:
Graph();
Graph( char* fileName);
void addNode(unsigned int nodeNumber);
void removeNode(unsigned int nodeNumber);
void setNodeValue(unsigned int nodeNumber, unsigned int cost);
unsigned int getNodeValue(unsigned int nodeNumber);
void addEdge(unsigned int sourceNodeNumber, unsigned int destNodeNumber, unsigned int edgeWeight);
bool hasEdge(unsigned int sourceNodeNumber, unsigned int destNodeNumber);
void deleteEdge(unsigned int sourceNodeNumber, unsigned int destNodeNumber);
void makeOriginNode(unsigned int nodeNumber); // origin nodes have a totalcost of 0 and have been visited
int modifyEdge(unsigned int destNodeNumber, unsigned int weight);
int getEdgeValue(unsigned int sourceNodeNumber, unsigned int destNodeNumber);
int setEdgeValue(unsigned int sourceNodeNumber,unsigned int destNodeNumber, unsigned int weight );
unsigned int getNodeCount(void);
unsigned int getEdgeCount(void);
bool isNodeVisited(unsigned int nodeNumber);
void setNodeVisited(unsigned int nodeNumber);
void doDijkstra( unsigned int originNode, unsigned int destNode, std::list<unsigned int> *pathResult, int &pathCost);
void doPrim( unsigned int originNode);
void printGraph();
};
//-------------------------------------------------------------------------------------------------------
// A class defining a graphPoint, which may become included in a graph
//-------------------------------------------------------------------------------------------------------
//
class graphPoint
{
friend class Graph;
private:
unsigned int m_nodeNumber; // a unique identifier for this node
std::map<unsigned int, unsigned int> m_edges; // a vector of all edges from the node
unsigned int m_viaNode; // the "from node" to this node for the m_totalCost recorded
int m_totalCost; // total path cost for this instance
bool m_visited; // has this instance been visited in the algorthim?
unsigned int m_numEdges; // this number of edges for this instance
public:
graphPoint( unsigned int nodeNumber, int cost, bool visited);
void printGraphPoint();
void createEdge(unsigned int dest_node, unsigned int weight);
int deleteEdge(unsigned int dest_node);
int setEdgeValue(unsigned int sourceNodeNumber,unsigned int NodeNumber, unsigned int weight );
int getEdgeValue(unsigned int sourceNodeNumber );
int modifyEdge(unsigned int dest_node, unsigned int weight);
int getPointCost ();
void setPointCost (int cost);
bool modifyPoint (int cost, bool visited);
void setVisited ();
bool getVisited ();
void cleanNode();
};
//*****************************************************************
//**
//** graphPoint methods
//**
//*****************************************************************
graphPoint::graphPoint( unsigned int nodeNumber, int cost = (-1), bool visited = false )
{
m_totalCost = cost; // the accumulated cost of this node
m_visited = visited; // not visited when created (but can be overriden for the source node)
m_nodeNumber = nodeNumber; // this node's number
m_viaNode = nodeNumber; // the node number that this node was reached from (start with self)
m_numEdges = 0; // there are no edges to start
// std::cout << "Node number " << nodeNumber << " added" << std::endl;
}
bool graphPoint::modifyPoint (int cost, bool visited = false)
{
m_totalCost = cost;
m_visited = visited;
}
void graphPoint::setVisited ()
{
m_visited = true;
}
bool graphPoint::getVisited ()
{
return m_visited;
}
int graphPoint::getPointCost ()
{
return m_totalCost;
}
void graphPoint::setPointCost (int cost)
{
m_totalCost = cost;
}
void graphPoint::cleanNode()
{
m_viaNode = 0;
m_totalCost = -1;
m_visited = false;
}
void graphPoint::printGraphPoint()
{
std::cout << "Graph point #" << m_nodeNumber
<< ((m_totalCost == 0) ? " (ORIGIN)" : "")
<< " has a cost of "
<< m_totalCost
<< " and has" << (m_visited ? "" : " not") << " been visited" << std::endl;
if(m_numEdges) std::cout << "Edge at:" << std::endl;
for(std::map<unsigned int, unsigned int>::iterator it=m_edges.begin(); it != m_edges.end(); ++it)
{
std::cout << "-- to node:" << it->first << " (" << it->second << ")" << std::endl;
}
}
// create a new edge to "dest_node" with a cost of "weight"
void graphPoint::createEdge(unsigned int dest_node, unsigned int weight)
{
std::map<unsigned int, unsigned int>::iterator it;
// replace any duplicate entry with a new weight
if((it = m_edges.find(dest_node)) != m_edges.end())
{
// std::cout << "duplicate edge \"dest node\"found: "
// << it->first << ", replacing " << it->second
// << " with " << weight << std::endl;
it->second = weight;
return;
}
// otherwise, insert a new edge in the set of all edges from this node
else
{
m_edges.insert(std::pair<unsigned int,unsigned int>(dest_node, weight));
m_numEdges++;
}
}
// remove the edge to "dest_node"
int graphPoint::deleteEdge(unsigned int dest_node)
{
int retval = -1;
std::map<unsigned int, unsigned int>::iterator it;
// find and delete the edge. If not found, do nothing
if((it = m_edges.find(dest_node)) != m_edges.end())
{
std::cout << "\"dest node\"found: "
<< it->first << ", erasing edge to it " << it->second
<< std::endl;
m_edges.erase(it);
m_numEdges--;
retval = 0;
}
return retval;
}
// modify the edge to "dest_node"
//
// return -1 if error, 0 if ok.
//
int graphPoint::modifyEdge(unsigned int dest_node, unsigned int weight)
{
int retval = -1;
std::map<unsigned int, unsigned int>::iterator it;
// find and delete the edge. If not found, do nothing
if((it = m_edges.find(dest_node)) != m_edges.end())
{
// std::cout << "\"dest node\"found: "
// << it->first << ", modifying the edge to " << it->second
// << std::endl;
it->second = weight;
return 0;
}
return -1;
}
// get the edge to "dest_node"
//
// return -1 if error, edge weight if ok
//
int graphPoint::getEdgeValue(unsigned int node)
{
int retval = -1;
std::map<unsigned int, unsigned int>::iterator it;
// std::cout << "Finding edge cost from " << m_nodeNumber << " to " << node << std::endl;
// find the edge. If not found, return a cost of -1 (this mean infinity)
if((it = m_edges.find(node)) != m_edges.end())
{
// std::cout << "Found a cost of : " << it->second << std::endl;
retval = (it->second);
}
// std::cout << "done" << std::endl;
return retval;
}
//*****************************************************************
//**
//** Graph methods
//**
//*****************************************************************
//
Graph::Graph()
{
m_totalNumVerticies = 0;
m_totalNumEdges = 0;
}
// add a node to the graph
void Graph::addNode(unsigned int nodeNumber)
{
std::map<int, graphPoint* >::iterator it = graphNodes.find(nodeNumber);
if(it == graphNodes.end())
{
std::cout << "adding node " << nodeNumber << std::endl;
graphNodes[nodeNumber] = new graphPoint(nodeNumber);
m_totalNumVerticies++;
m_originNode = -1;
}
}
// // remove a node to the graph (but only if it exists)
// void Graph::removeNode(unsigned int nodeNumber)
// {
// std::map<int, graphPoint* >::iterator it = graphNodes.find(nodeNumber);
// if( it != graphNodes.end())
// {
// graphNodes.erase(it);
// m_totalNumVerticies--;
// }
// }
void Graph::addEdge(unsigned int sourceNodeNumber, unsigned int destNodeNumber, unsigned int edgeWeight)
{
std::map<int, graphPoint* >::iterator it_s = graphNodes.find(sourceNodeNumber);
std::map<int, graphPoint* >::iterator it_d = graphNodes.find(destNodeNumber);
if( it_s != graphNodes.end())
{
it_s->second->createEdge(destNodeNumber, edgeWeight);
m_totalNumEdges++;
}
}
bool Graph::hasEdge(unsigned int sourceNodeNumber, unsigned int destNodeNumber)
{
return (graphNodes.find(sourceNodeNumber)->second->getEdgeValue(sourceNodeNumber) == destNodeNumber);
}
// void Graph::deleteEdge(unsigned int sourceNodeNumber,unsigned int destNodeNumber)
// {
// std::map<int, graphPoint* >::iterator it = graphNodes.find(sourceNodeNumber);
// if( it != graphNodes.end())
// {
// if(!(it->second->deleteEdge(destNodeNumber))) m_totalNumEdges--;
// }
// }
// make the specified node the origin
void Graph::makeOriginNode(unsigned int nodeNumber)
{
std::map<int, graphPoint* >::iterator it = graphNodes.find(nodeNumber);
// To be complete, we should check if m_originNode is -1 before we set the origin.
// If m_originNode != -1, then the user had already set the origin, and is modifying it.
// In that case, we should remove "orgin-ness" from the originally set origin point
// For now, assume that the origin will not be changed once it's set.
if( it != graphNodes.end())
{
it->second->modifyPoint (0, true);
m_originNode = nodeNumber;
}
}
void Graph::setNodeValue(unsigned int nodeNumber, unsigned int cost)
{
std::map<int, graphPoint* >::iterator it = graphNodes.find(nodeNumber);
if( it != graphNodes.end())
{
it->second->setPointCost (cost);
}
}
// unsigned int Graph::getNodeValue(unsigned int nodeNumber)
// {
// unsigned int retval = -1;
// std::map<int, graphPoint* >::iterator it = graphNodes.find(nodeNumber);
// if( it != graphNodes.end())
// {
// retval = it->second->getPointCost ();
// }
// return retval;
// }
// int Graph::setEdgeValue(unsigned int sourceNodeNumber,unsigned int destNodeNumber, unsigned int weight )
// {
// unsigned int retval = -1;
// std::map<int, graphPoint* >::iterator it = graphNodes.find(sourceNodeNumber);
// if( it != graphNodes.end())
// {
// retval = it->second->modifyEdge (destNodeNumber, weight);
// }
// return retval;
// }
//returns -1 if not found
int Graph::getEdgeValue(unsigned int sourceNodeNumber,unsigned int destNodeNumber)
{
unsigned int retval = -1;
std::map<int, graphPoint* >::iterator it = graphNodes.find(sourceNodeNumber);
if( it != graphNodes.end())
{
retval = it->second->getEdgeValue (destNodeNumber);
}
return retval;
}
bool Graph::isNodeVisited(unsigned int nodeNumber)
{
bool retval = false;
std::map<int, graphPoint* >::iterator it = graphNodes.find(nodeNumber);
if( it != graphNodes.end())
{
retval = it->second->getVisited();
}
return retval;
}
void Graph::setNodeVisited(unsigned int nodeNumber)
{
std::map<int, graphPoint* >::iterator it = graphNodes.find(nodeNumber);
if( it != graphNodes.end())
{
it->second->setVisited();
}
}
unsigned int Graph::getNodeCount()
{
return m_totalNumVerticies;
}
unsigned int Graph::getEdgeCount()
{
return m_totalNumEdges;
}
void Graph::printGraph()
{
unsigned int retval = -1;
for(std::map<int, graphPoint* >::iterator it = graphNodes.begin(); it != graphNodes.end(); ++it)
{
it->second->printGraphPoint();
}
std::cout << "TOTAL NODES: " << getNodeCount() << "\tTOTAL EDGES: " << getEdgeCount() << "\n" << std::endl;
}
Graph::Graph(char *fileName)
{
m_totalNumVerticies = 0;
m_totalNumEdges = 0;
int graphSize;
// read the graph from a file.
// the first int is the size, and followed by tuples of int node1, int node2, int cost
// open the graph file for read only
std::fstream filestream(fileName, std::fstream::in );
filestream >> graphSize;
std::cout << " graph size is " << graphSize << " nodes" << std::endl;
while(true)
{
unsigned int node1, node2, cost;
// collect a edge data
filestream >> node1 >> node2 >> cost;
// only read with the input is valid
if(filestream.eof()) break;
std::cout << "node1: " << node1 << " node2: " << node2 << " cost: " << cost << std::endl;
addNode(node1); // these addNode calls are idempotent
addNode(node2);
addEdge(node1, node2 , cost);
}
}
// a type which defines a conceptual two dimensional vector of ints (or at least can be addressed as such)
typedef typename std::vector< std::vector<int> > mst_result;
typedef typename std::map<unsigned int, unsigned int> edge_type;
// a type which describes a node in the graph
typedef typename std::map< int, graphPoint* > node_type;
const int NODENUM_IDX = 0;
const int EDGEWEIGHT_IDX = 1;
void Graph::doPrim( unsigned int originNode)
{
int debug;
int i;
// initialize the 2d vector of [numNodes][2] ints. (nodenumber, weight) will be stored as we compute the prim solution
mst_result mst_for_graph(m_totalNumVerticies, std::vector<int> (2));
std::cout << "Starting Prim MST algorithm for " << m_totalNumVerticies << " nodes...";
// std::cin >> debug;
// initialize the solution
for(i=0; i<m_totalNumVerticies; i++)
{
mst_for_graph[i][NODENUM_IDX]=(-1);
}
// add the origin node to the solved set
mst_for_graph[0][NODENUM_IDX] = originNode;
mst_for_graph[0][EDGEWEIGHT_IDX] = 0;
setNodeVisited(originNode);
int solution_points_found=1;
// now build the MST while iterating through the graph
for(i=(m_totalNumVerticies-1); i>0; i--)
{
unsigned int lowest_cost_edge_this_iteration = 100; // the lowest cost and node for this iteration
unsigned int lowest_cost_node_this_iteration = (-1);
std::cout << "iterating through the mst solution " << i << " more nodes left in graph" << std::endl;
// std::cin >> debug;
for(int j=0; j<solution_points_found; j++)
{
std::cout << "looking at solution location " << j << " node: " << mst_for_graph[j][NODENUM_IDX] << std::endl;
// std::cin >> debug;
// find the node info for the node being examined (this lookup cannot fail)
node_type::iterator itNode=graphNodes.find(mst_for_graph[j][NODENUM_IDX]);
std::cout << "examining the edges connected to node: " << itNode->second->m_nodeNumber << std::endl;
// std::cin >> debug;
// look through the edges of all the nodes in the solution so far
for(edge_type::iterator itEdge=itNode->second->m_edges.begin(); itEdge != itNode->second->m_edges.end(); ++itEdge)
{
std::cout << "found edge to other node: " << itEdge->first << std::endl;
// std::cin >> debug;
if(isNodeVisited(itEdge->first))
{
std::cout << "***node is already in solution, skipping" << std::endl;
continue;
}
std::cout << "now checking if this is the lowest cost: " << (itEdge->second) << std::endl;
std::cout << "lowest_cost_edge_this_iteration: " << lowest_cost_edge_this_iteration << std::endl;
// now check if its the lowest cost
if((itEdge->second) < lowest_cost_edge_this_iteration)
{
std::cout << "**new lowest code node found: Node: " << itEdge->first << " cost: " << itEdge->second << std::endl;
lowest_cost_edge_this_iteration = itEdge->second;
lowest_cost_node_this_iteration = itEdge->first;
}
}
}
std::cout << "\n==== adding node " << lowest_cost_node_this_iteration << " to solution" << " with a cost of "
<< lowest_cost_edge_this_iteration << std::endl;
// std::cin >> debug;
// add a newly found lowest node to the solution
mst_for_graph[solution_points_found][NODENUM_IDX] = lowest_cost_node_this_iteration;
mst_for_graph[solution_points_found][EDGEWEIGHT_IDX] = lowest_cost_edge_this_iteration;
setNodeVisited(lowest_cost_node_this_iteration);
solution_points_found++;
}
std::cout << "----------- MST path ---------------" << std::endl;
unsigned int mst_total_cost = 0;
for(i=0; i<solution_points_found; i++)
{
std::cout << "Node: " << mst_for_graph[i][NODENUM_IDX] << " \t"
<< " Cost: " << mst_for_graph[i][EDGEWEIGHT_IDX] <<std::endl;
mst_total_cost += mst_for_graph[i][EDGEWEIGHT_IDX];
}
std::cout << "Total MST cost: " << mst_total_cost << std::endl;
}
void Graph::doDijkstra( unsigned int originNode, unsigned int destNode, std::list<unsigned int> *pathList, int &pathCost)
{
bool validRouteFoundToDestination = false;
unsigned int closedNodeNum = originNode;
// std::cout << "---Running shortest path algorithm from node "<<originNode << " to node " << destNode << std::endl;
// initialize the outcome
pathCost = 0;
pathList->clear();
// special case for origin == destination, just return
if(originNode == destNode)
{
return;
}
// before starting, clean the nodes of computed values in case we're re-running the algorythm
for(std::map<int, graphPoint* >::iterator itGraphNode = graphNodes.begin(); itGraphNode != graphNodes.end(); ++itGraphNode)
{
itGraphNode->second->cleanNode();
}
//
// run the shortest path algorithm on the graph passed in
//
makeOriginNode(originNode);
std::vector<unsigned int> openSet; // a set of all "not yet visited" Nodes
while(true)
{
int numNodesAddedToOpenSet = 0;
// std::cout << "New closed node: " << closedNodeNum << std::endl;
//
// add the connected nodes from this node to the open set (Step N+1)
//
std::map<int, graphPoint* >::iterator itGraphNode = graphNodes.find(closedNodeNum);
unsigned int closedNodeCost = itGraphNode->second->m_totalCost;
// std::cout << "The closed node cost is "<< closedNodeCost << std::endl;
// get all the nodes connected to this one and
// adjust the costs and (from node) values of each connected node
// itGraphEdge->first is the connected node number and itGraphEdge->second is the edge cost
for(std::map<unsigned int, unsigned int>::iterator itGraphEdge= itGraphNode->second->m_edges.begin();
itGraphEdge != itGraphNode->second->m_edges.end();
++itGraphEdge)
{
// first, mark this node as visited
itGraphNode->second->setVisited();
// std::cout << "Examining node " << itGraphEdge->first << std::endl;
std::map<int, graphPoint* >::iterator itNextEdgeNode = graphNodes.find(itGraphEdge->first);
if(itNextEdgeNode == graphNodes.end()) continue; // no node actually exists
int i;
for(i=0; i<openSet.size(); i++)
{
if(openSet[i] == itNextEdgeNode->second->m_nodeNumber) break;
}
// add it to the open set if it's not there already and it hasn't already been visited
if(i == openSet.size() && (itNextEdgeNode->second->getVisited() == false))
{
openSet.push_back(itNextEdgeNode->second->m_nodeNumber);
numNodesAddedToOpenSet++;
}
// now see if this is a lower cost path to this connected node
if(itNextEdgeNode->second->getPointCost() == (-1) ||
(closedNodeCost + itGraphEdge->second) < itNextEdgeNode->second->getPointCost())
{
// new lower cost found
itNextEdgeNode->second->setPointCost(static_cast<int>(closedNodeCost + itGraphEdge->second));
itNextEdgeNode->second->m_viaNode = closedNodeNum;
// std::cout << "found new lower cost (" << itNextEdgeNode->second->getPointCost()
// << ") to node " << itGraphEdge->first << " from node "
// << closedNodeNum << std::endl;
}
}
// std::cout << "Openset now has " << openSet.size() << " members " << std::endl;
//if we didn't add any new members to the openset and it's empty, we are done
if ( (openSet.size() == 0) && (numNodesAddedToOpenSet == 0) )
{
// std::cout << "openset is empty" << std::endl;
break;
}
// Now, find the lowest cost member of the open set and make it the new closed set member
unsigned int lowest_cost_node;
unsigned int lowest_cost_index;
for (int i=0; i<openSet.size(); i++)
{
int lowest_cost_val;
std::map<int, graphPoint* >::iterator itGraphNode = graphNodes.find(openSet[i]);
// std::cout << "i: " << i << " openset[i]: " << openSet[i] <<" nodenum: " <<
// itGraphNode->second->m_nodeNumber << " cost: " << itGraphNode->second->m_totalCost
// << std::endl;
if((i==0) || (itGraphNode->second->m_totalCost < lowest_cost_val ))
{
lowest_cost_node = itGraphNode->second->m_nodeNumber;
lowest_cost_val = itGraphNode->second->m_totalCost;
lowest_cost_index = i;
// std::cout << "found new lowest cost node : " << lowest_cost_node << std::endl;
}
}
// this is the node that we'll be evaluating in the next iteration
closedNodeNum = lowest_cost_node;
// remove that node from the open set
// std::cout << "deleting node: " << lowest_cost_node << std::endl;
for(std::vector<unsigned int>::iterator vit = openSet.begin(); vit != openSet.end(); ++vit)
{
// std::cout << "lowest_cost_node is: " << lowest_cost_node << " and found openSet member number " << *vit << std::endl;
if (*vit == lowest_cost_node)
{
// std::cout << "erasing member!" << std::endl;
openSet.erase(vit);
break;
}
}
// std::cout << "Post delete: Openset now has " << openSet.size() << " members " << std::endl;
//if that node is the dest node, we have succeeded and we are done
if (closedNodeNum == destNode)
{
validRouteFoundToDestination = true;
// std::cout << "***Found a route to the destination node!" << std::endl;
break;
}
//else keep on truckin'
}
//
// Now tell the user whether or not we've been able to find a route
// If so, print out the route
// else, well..there's not much we can do except break the bad news
//
if(validRouteFoundToDestination == true)
{
std::vector<unsigned int> routeSet;
// now print out the route
unsigned int routeNode = destNode;
unsigned int lastRouteNode = routeNode;
// reuse the openSet vector to record the final route
pathCost = 0;
while(true)
{
pathList->push_front(routeNode);
// if we've added the orig to the route record, then we're done
if(routeNode == originNode) break;
// std::cout << "route reclaimation looking for node " << routeNode << std::endl;
// std::cout << "route reclaimation found node " <<
// graphNodes.find(routeNode)->second->m_nodeNumber << std::endl;
// remember the "from" node for the cost computation
lastRouteNode = routeNode;
// find the "next" node
routeNode = graphNodes.find(routeNode)->second->m_viaNode;
// std::cout << "route reclaimation found viaNode..." << routeNode << std::endl;
// record this hop in the total cost of the path
// std::cout << "Cost of route from " << routeNode << " to " << lastRouteNode <<
// " is " << getEdgeValue(routeNode, lastRouteNode) << std::endl;
pathCost += getEdgeValue(routeNode, lastRouteNode);
}
}
else
{
// std::cout << "Count not find a route from " << originNode << " to " << destNode << std::endl;
pathList->clear(); // no elements in the list
pathCost = -1;
}
}
ShortestPathAlgo::ShortestPathAlgo() : pathCost(-1)
{
pathList = new std::list<unsigned int>;
}
ShortestPathAlgo::~ShortestPathAlgo()
{
delete pathList;
}
// returns a count of the nodes
unsigned int ShortestPathAlgo::verticies(Graph &G)
{
return G.getNodeCount();
}
// returns the cost of the path (or -1 if no path exists)
int ShortestPathAlgo::path_size( Graph &G, unsigned int originNode, unsigned int destNode )
{
G.doDijkstra(originNode, destNode, pathList, pathCost);
return pathCost;
}
// returns a list with the path
std::list<unsigned int> *ShortestPathAlgo::path( Graph &G, unsigned int originNode, unsigned int destNode)
{
G.doDijkstra(originNode, destNode, pathList, pathCost);
return pathList;
}
std::ostream &operator<< (std::ostream &cout, std::list<unsigned int> *path)
{
unsigned routeLen = path->size();
if(path->size())
{
cout << "==== Shortest Route has " << routeLen << " nodes" << std::endl;
cout << "===== Shortest Route is : ";
// There's a valid route, so iterate through the list and print each member...
for(std::list<unsigned int>::iterator listIt = path->begin(); listIt != path->end(); ++listIt)
{
cout << *listIt << " ";
}
}
else
{
std::cout << "No route found" << std::endl;
}
return cout;
}
//#define USING_KNOWN_GRAPH
int main()
{
#ifdef USING_KNOWN_GRAPH
int originNode = 1;
int destNode = 6;
std::cout << "Using a known graph for testing" << std::endl;
Graph G;
G.addNode(1);
G.addEdge(1, 2, 1);
G.addEdge(1, 3, 3);
G.addNode(2);
G.addEdge(2, 4, 1);
G.addEdge(2, 3, 1);
G.addNode(3);
G.addEdge(3, 1, 1);
G.addEdge(3, 6, 4);
G.addEdge(3, 5, 7);
G.addNode(4);
G.addEdge(4, 6, 5);
G.addNode(5);
G.addNode(6);
G.printGraph();
#else
/* initialize random seed: */
srand (time(NULL));
char print_graph_entry;
Graph G(const_cast<char *>("graph.txt")); // a new graph G
std::cout << "start prim" << std::endl;
G.doPrim(0);
// print it out but only if the user wants to take a look at it
if(print_graph_entry == 'y' )
{
G.printGraph();
}
else
{
std::cout <<"Graph has " << G.getNodeCount() << " nodes and " << G.getEdgeCount() << " indicies" << std::endl;
}
#endif
return(1);
}