-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
1463 lines (1257 loc) · 47.7 KB
/
Copy pathmain.cpp
File metadata and controls
1463 lines (1257 loc) · 47.7 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
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
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <ctime>
#include <algorithm>
#include <limits>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include<iostream>
#include<cmath>
#include <string>
#include <fstream>
#include <iomanip>
#include<vector>
#include <sstream>
#include <cmath>
#include <stdlib.h> //needed for atof -> convert string to double
#include "main.h"
#include <iterator>
#include <windows.h>
using namespace std;
// Constructor for LinkedNode Class:
// LinkedNode Class, used for data representation in LinkedList.
// Sets Distance and Neighbor equal to parameter.
// Sets next equal to NULL.
LinkedNode::LinkedNode(double distance, Node* neighbor)
{
this->distance = distance;
this->neighbor = neighbor;
this->next = NULL;
}
// Utility Function for LinkedNode Class:
// Returns doubel distance variable in LinkedNode Class.
double LinkedNode::getDistance()
{
return this->distance;
}
// Utility Function for LinkedNode Class:
// Returns Node Pointer Neighbor in LinkedNode Class
Node* LinkedNode::getNeighbor()
{
return this->neighbor;
}
// Utility Function for LinkedNode Class:
// Returns Next Node Pointer in LinkedNode Class
LinkedNode* LinkedNode::getNext()
{
return this->next;
}
// Utility Function for LinkedNode Class:
// Sets Next Node Pointer in LinkedNode Class
void LinkedNode::setNext(LinkedNode* next)
{
this->next = next;
}
// Constructor for Node Class:
// Used for data representation in Vector of Nodes for File Input.
// Sets Longitude, Latitude, name, neighbors, and distances equal to the parameters.
//
Node::Node(double lon, double lat, string name, vector <Node*> neighbors, vector <double> distances)
{
this->lon=lon;
this->lat=lat;
this->name = name;
this->neighbors=neighbors;
this->distances=distances;
}
// Utility Function for Node Class:
// Returns string name variable in Node Class
string Node::getName(){
return this->name;
}
// Utility Function for Node Class:
// Returns double longitude variable in Node Class
double Node::getLon()
{
return this->lon;
}
// Utility Function for Node Class:
// Returns double latitigude variable in Node Class
double Node::getLat()
{
return this->lat;
}
// Utility Function for Node Class:
// Returns Vector of Node Pointers in Node Class.
vector <Node*> Node::getNeighbors()
{
return this->neighbors;
}
// Utility Function for Node Class:
// Pushes Node* to end of Vector of Node Pointers representing Neighbors in Node Class.
void Node::pushNeighbors(Node* neighbor)
{
this->neighbors.push_back(neighbor);
}
// Utility Function for Node Class:
// Returns Vector of doubles representing distances of Neighbors in Node Class.
vector <double> Node::getDistances()
{
return this->distances;
}
// Utility Function for Node Class:
// Pushes double distance to end of Vector of doubles representing distances of Neighbors in Node Class.
void Node::pushDistances(double distance)
{
this->distances.push_back(distance);
}
// File I/O Function for Main:
// Parses Tokens from File Input, separates
// File lines by "," delimiter.
vector<string> createTokens(string s)
{
// std::cout<<s<<endl;
vector <string> lineVector;
string temp="";
// Loops through string s, checking if each character in the string is the delimiter.
// If the character is not the delimiter, adds the character to the buffer string "temp".
// If the character is the delimiter, pushes the buffer string to the lineVector Vector and erases buffer string.
for (int i=0; i<s.length();i++)
{
// Character to be inspected.
char c = s[i];
// std::cout<<c<<" "<<endl;
// if(c==' ')
// continue;
//
// Checks if character is delimiter.
if(c != ',')
{
temp+=c;
//std::cout<<temp<<endl;
}
else
{
// if(temp != " " && !temp.empty()){
// std::cout<<"THISIS"<<temp<<endl;
lineVector.push_back(temp);
// }
//overload operator
//I commented out because I think its easier/more efficent without the commas
/*string jk;
stringstream ss;
ss << c;
ss >> jk;
lineVector.push_back(jk);*/
temp = "";
}
}
return lineVector;
}
// Utility Function for Main class:
// Checks to see if a string matches the name of a node in the locations vector.
// Changes the value of index to the index where a match is found
bool locationNameExists(string checkCase, vector <Node*> locations, int & index)
{
for(int i=0; i<locations.size(); i++)
{
if(checkCase==locations[i]->getName())
{
index = i;
}
return true;
}
return false;
}
// Utility Function for Main class:
// Checks to see if a string matches the name of a node in the locations vector
bool locationNameExists(string checkCase, vector <Node*> locations)
{
for(int i=0; i<locations.size(); i++)
{
if(checkCase==locations[i]->getName())
{
return true;
}
}
return false;
}
// Structure for Adjacency List Node.
// Represents Adjacency List Node with int destination for index, double weight for
// distance between Node and Destination, and Node Pointer for connected Node.
struct AdjListNode
{
int dest;
double weight;
struct AdjListNode* next;
};
// Structure for Adjacency List
// Represents an Array of LinkedLists, with a head Node Pointer AdjListNode.
struct AdjList
{
struct AdjListNode *head; // pointer to head node of list
};
// Structure for Graph
// Represents an Array of Adjacency Lists.
// Size of array will be V (number of vertices in graph)
struct Graph
{
int V;
struct AdjList* array;
};
// Utility Function for Adjacency List Node Class:
// Creates a new Adjacency List Node, allocating memory for size of AdjListNode.
// Sets new nodes destination, weight and next pointer equal to parameters.
struct AdjListNode* newAdjListNode(int dest, double weight)
{
struct AdjListNode* newNode =
(struct AdjListNode*) malloc(sizeof(struct AdjListNode));
newNode->dest = dest;
newNode->weight = weight;
newNode->next = NULL;
return newNode;
}
// Utility Function for Graph Structure:
// Creates Graph, allocating memeory for it, based on number of Vertexes V.
struct Graph* createGraph(int V)
{
struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
graph->V = V;
// Create an array of adjacency lists. Size of array will be V
graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList));
// Initialize each adjacency list as empty by making head as NULL
for (int i = 0; i < V; ++i){
graph->array[i].head = NULL;
}
return graph;
}
// Utility Function for Graph Structure:
// Addas an Edge, an Adjacency List Node to the Graph's array of Adjacency Lists.
// Adjacency List Node represents the physical distance between two Nodes within the Graph.
// Ex: The distance between two buildings is represented as an Edge.
void addEdge(struct Graph* graph, int src, int dest, double weight)
{
// Add an edge from src to dest. A new node is added to the adjacency
// list of src. The node is added at the begining
struct AdjListNode* newNode = newAdjListNode(dest, weight);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
// Since graph is undirected, add an edge from dest to src also
newNode = newAdjListNode(src, weight);
newNode->next = graph->array[dest].head;
graph->array[dest].head = newNode;
}
// Constructur for MinHeapNode Structure.
// Declares parameters v and dist, used to represent vector and distance.
struct MinHeapNode
{
int v;
double dist;
};
// Constructor for MinHeap Structure
// Declares size, capacity, and position pointer.
struct MinHeap
{
int size; // Number of heap nodes present currently
int capacity; // Capacity of min heap
int *pos; // This is needed for decreaseKey()
struct MinHeapNode **array;
};
// Utility Function for MinHeapNode Structure
// Creates a new Min Heap Node, allocating memeory for it and initializing the v and dist.
struct MinHeapNode* newMinHeapNode(int v, double dist)
{
struct MinHeapNode* minHeapNode =
(struct MinHeapNode*) malloc(sizeof(struct MinHeapNode));
minHeapNode->v = v;
minHeapNode->dist = dist;
return minHeapNode;
}
// Utility Function for MinHeap Structure
// Creates the MinHeap, allocating memory for it and initializing the position, size,
// capacity, and array for MinHeapNodes.
struct MinHeap* createMinHeap(int capacity)
{
struct MinHeap* minHeap =
(struct MinHeap*) malloc(sizeof(struct MinHeap));
minHeap->pos = (int *)malloc(capacity * sizeof(int));
minHeap->size = 0;
minHeap->capacity = capacity;
minHeap->array =
(struct MinHeapNode**) malloc(capacity * sizeof(struct MinHeapNode*));
return minHeap;
}
// Utility Function for MinHeapNodes
// Swaps two nodes with each other in each position. Used in the Heapify Function.
void swapMinHeapNode(struct MinHeapNode** a, struct MinHeapNode** b)
{
struct MinHeapNode* t = *a;
*a = *b;
*b = t;
}
// Utility Function for MinHeap:
// A standard function to heapify at given index/idx. Keeps the smallest MinHeapNode on top of the MinHeap.
// This function also updates position of nodes when they are swapped.
// Position is needed for decreaseKey()
void minHeapify(struct MinHeap* minHeap, int idx)
{
int smallest, left, right;
smallest = idx;
left = 2 * idx + 1;
right = 2 * idx + 2;
if (left < minHeap->size &&
minHeap->array[left]->dist < minHeap->array[smallest]->dist )
smallest = left;
if (right < minHeap->size &&
minHeap->array[right]->dist < minHeap->array[smallest]->dist )
smallest = right;
if (smallest != idx)
{
// The nodes to be swapped in min heap
MinHeapNode *smallestNode = minHeap->array[smallest];
MinHeapNode *idxNode = minHeap->array[idx];
// Swap positions
minHeap->pos[smallestNode->v] = idx;
minHeap->pos[idxNode->v] = smallest;
// Swap nodes
swapMinHeapNode(&minHeap->array[smallest], &minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}
// Utility Function for MinHeap:
// Checks if the given minHeap is empty or not. Returns int value.
int isEmpty(struct MinHeap* minHeap)
{
return minHeap->size == 0;
}
// Utility Function for MinHeap:
// Standard function to extract minimum node from heap
struct MinHeapNode* extractMin(struct MinHeap* minHeap)
{
// Returns NULL if nothing is in MinHeap.
if (isEmpty(minHeap))
{
return NULL;
}
// Store the root node
struct MinHeapNode* root = minHeap->array[0];
// Replace root node with last node
struct MinHeapNode* lastNode = minHeap->array[minHeap->size - 1];
minHeap->array[0] = lastNode;
// Update position of last node
minHeap->pos[root->v] = minHeap->size-1;
minHeap->pos[lastNode->v] = 0;
// Reduce heap size and heapify root
--minHeap->size;
minHeapify(minHeap, 0);
return root;
}
// Utility Function for MinHeap:
// Function decreases dist value of a given vertex v. This function
// uses pos[] of min heap to get the current index of node in min heap
void decreaseKey(struct MinHeap* minHeap, int v, double dist)
{
// Gets index of v in MinHeap Array.
int i = minHeap->pos[v];
// Get the node and update its dist value
minHeap->array[i]->dist = dist;
// Travel up while the complete tree is not heapified.
// This is a O(Logn) loop
while (i && minHeap->array[i]->dist < minHeap->array[(i - 1) / 2]->dist)
{
// Swap this node with its parent
minHeap->pos[minHeap->array[i]->v] = (i-1)/2;
minHeap->pos[minHeap->array[(i-1)/2]->v] = i;
swapMinHeapNode(&minHeap->array[i], &minHeap->array[(i - 1) / 2]);
// move to parent index
i = (i - 1) / 2;
}
}
// Utility Function for MinHeap:
// Checks if a given vertex
// 'v' is in min heap or not
bool isInMinHeap(struct MinHeap *minHeap, int v)
{
if (minHeap->pos[v] < minHeap->size)
{
return true;
}
return false;
}
// Utility Function for dijstra's Algorithm:
// Recursive Function that prints the nodes in order from source to destination.
void printPath(int parent[], int src, int destination, map<int, string> hashmap)
{
// Base Case
if(destination==src) return;
// Recursive Check
printPath(parent, src, parent[destination], hashmap);
cout<<hashmap.at(destination)<<"\t\t";
destination=parent[destination];
}
// Utility Function for Dijstra's Algorithm:
// Used to print the array of answers for the Dijstra's Algorithm. Gives source, destination,
// distance in kilometers and steps. Calls Recursive Function printPath to list order of nodes
// from source to destination. Uses hashmap to convert indexes obtained from Dijkstra's Algorithm
// and converts them into building names.
void printArr(double dist[], int src, int parent[], int destination, map<int, string> hashmap, string transp)
{
cout<<"Source: "<<hashmap.at(src)<< endl<<"Destination: "<<hashmap.at(destination)<< endl << "Distance from source: "<<dist[destination]<< " km";
cout << fixed << showpoint;
cout << setprecision(3);
if(transp == "Walk"){
double steps = dist[destination]*1320;
cout<<"\t=\t"<<steps<<" steps";
cout<<endl;
double time = dist[destination]/0.1166667;
cout<<endl;
cout<<"Time to destination (walking): "<<time<< " minute(s)";
}
if(transp =="Bike"){
cout<<endl;
double time = dist[destination]/0.3333333;
cout<<endl;
cout<<"Time to destination (biking): "<<time<<" minute(s)";
}
if(transp =="Skateboard"){
cout<<endl;
double time = dist[destination]/0.2;
cout<<endl;
cout<<"Time to destination (skateboarding): "<<time<<" minute(s)";
}
if(transp=="Long-board"){
cout<<endl;
double time = dist[destination]/0.25;
cout<<endl;
cout<<"Time to destination (longboarding): "<<time<<" minute(s)";
}
cout << endl;
cout << "\nPath from " << hashmap.at(src) << endl;
printPath(parent, src, destination, hashmap);
}
// Dijstra's Algorithm:
// The main function that calulates distances of shortest paths from source node to destination node
// vertices. It is a O(ELogV) function. Calls the printArr function to print list of Nodes.
double dijkstra(struct Graph* graph, int src,int dest, map<int, string> hashmap, string transp, bool food)
{
//starting
if(food == true){
int V = graph->V;// Get the number of vertices in graph
double dist[V]; // dist values used to pick minimum weight edge in cut
int destinIndex= dest;
int parent [V];
// minHeap represents set E
struct MinHeap* minHeap = createMinHeap(V);
// Initialize min heap with all vertices. dist value of all vertices
for (int v = 0; v < V; ++v)
{
dist[v] = std::numeric_limits<double>::max();
minHeap->array[v] = newMinHeapNode(v, dist[v]);
minHeap->pos[v] = v;
}
// Make dist value of src vertex as 0 so that it is extracted first
minHeap->array[src] = newMinHeapNode(src, dist[src]);
minHeap->pos[src] = src;
dist[src] = 0;
parent[src]=-1;
decreaseKey(minHeap, src, dist[src]);
// Initially size of min heap is equal to V
minHeap->size = V;
// In the following loop, min heap contains all nodes
// whose shortest distance is not yet finalized.
while (!isEmpty(minHeap))
{
// Extract the vertex with minimum distance value
struct MinHeapNode* minHeapNode = extractMin(minHeap);
int u = minHeapNode->v; // Store the extracted vertex number
// Traverse through all adjacent vertices of u (the extracted
// vertex) and update their distance values
struct AdjListNode* pCrawl = graph->array[u].head;
while (pCrawl != NULL)
{
int v = pCrawl->dest;
// If shortest distance to v is not finalized yet, and distance to v
// through u is less than its previously calculated distance
if (isInMinHeap(minHeap, v) && dist[u] != std::numeric_limits<double>::max() &&
pCrawl->weight + dist[u] < dist[v])
{
parent[v]=u;
dist[v] = dist[u] + pCrawl->weight;
// update distance value in min heap also
decreaseKey(minHeap, v, dist[v]);
}
pCrawl = pCrawl->next;
}
}
double x = dist[dest];
return x;
}else{
int V = graph->V;// Get the number of vertices in graph
double dist[V]; // dist values used to pick minimum weight edge in cut
int destinIndex= dest;
int parent [V];
// minHeap represents set E
struct MinHeap* minHeap = createMinHeap(V);
// Initialize min heap with all vertices. dist value of all vertices
for (int v = 0; v < V; ++v)
{
dist[v] = std::numeric_limits<double>::max();
minHeap->array[v] = newMinHeapNode(v, dist[v]);
minHeap->pos[v] = v;
}
// Make dist value of src vertex as 0 so that it is extracted first
minHeap->array[src] = newMinHeapNode(src, dist[src]);
minHeap->pos[src] = src;
dist[src] = 0;
parent[src]=-1;
decreaseKey(minHeap, src, dist[src]);
// Initially size of min heap is equal to V
minHeap->size = V;
// In the following loop, min heap contains all nodes
// whose shortest distance is not yet finalized.
while (!isEmpty(minHeap))
{
// Extract the vertex with minimum distance value
struct MinHeapNode* minHeapNode = extractMin(minHeap);
int u = minHeapNode->v; // Store the extracted vertex number
// Traverse through all adjacent vertices of u (the extracted
// vertex) and update their distance values
struct AdjListNode* pCrawl = graph->array[u].head;
while (pCrawl != NULL)
{
int v = pCrawl->dest;
// If shortest distance to v is not finalized yet, and distance to v
// through u is less than its previously calculated distance
if (isInMinHeap(minHeap, v) && dist[u] != std::numeric_limits<double>::max() &&
pCrawl->weight + dist[u] < dist[v])
{
parent[v]=u;
dist[v] = dist[u] + pCrawl->weight;
// update distance value in min heap also
decreaseKey(minHeap, v, dist[v]);
}
pCrawl = pCrawl->next;
}
}
// print the calculated shortest distances
printArr(dist, src, parent, dest, hashmap, transp);
return 0;
}
}
int main()
{
cout << "\n\n\n\n\n\n\n\n\n\n\n\n";
cout << "\n\n\n\n\n\n\n\n\n\n\n\n";
cout << "\n\n\n\n\n\n\n\n\n\n\n\n";
cout << "\n\n\n\n\n\n\n\n\n\n\n\n";
cout << "\n\n\n\n\n\n\n\n\n\n\n\n";
string ufMaps1=" _ _ ______ __ __ _____ _____ ";
string ufMaps2=" | | | | ____| | \\/ | /\\ | __ \\ / ____|";
string ufMaps3=" | | | | |__ | \\ / | / \\ | |__) | (___ ";
string ufMaps4=" | | | | __| | |\\/| | / /\\ \\ | ___/ \\___ \\ ";
string ufMaps5=" | |__| | | | | | |/ ____ \\| | ____) |";
string ufMaps6=" \\____/|_| |_| |_/_/ \\_\\_| |_____/ ";
string gator1 =" _.---._ .---.";
string gator2 =" __...---' .---. `---'-. `.";
string gator3 =" ~ -~ -.-''__.--' _.'( | )`. `. `._ :";
string gator4 ="-.~~ .'__-'_ .--'' ._`---'_.-. `. `-`.";
string gator5 =" ~- ~ ~ -_ -~ ~ -.._ _ _ _ ..-_ `. `-._``--.._";
string gator6 =" ~~-~ ~-_ _~ ~-~ ~ -~ _~~_-~ -._ `-. -. `-._``--.._.--''. ~ -~_";
string gator7 =" ~~ -~_-~ _~- _~~ _~-_~ ~-_~~ ~-.___ -._ `-.__ `. `. ~ -_~";
string gator8 =" ~~ _~- ~~- -_~ ~- ~ - _~~- _~~ ~---...__ _ ._ .` `. ~-_~";
string gator9 =" ~ ~- _~~- _-_~ ~-_ ~-~ ~_-~ _~- ~_~-_~ ~--.....--~ -~_ ~";
string gator10=" ~ ~ - ~ ~ ~~ - ~~- ~~- ~- ~ -~ ~ ~ -~~- ~- ~-~";
int x=0;
while ( ufMaps1[x] != '\0')
{
cout << ufMaps1[x];
if(ufMaps1[x] != ' ')
Sleep(7);
fflush(stdout);
x++;
};
cout<< endl;
x= 0;
while ( ufMaps2[x] != '\0')
{
cout << ufMaps2[x];
if(ufMaps2[x] != ' ')
Sleep(7);
fflush(stdout);
x++;
};
cout<< endl;
x= 0;
while ( ufMaps3[x] != '\0')
{
cout << ufMaps3[x];
if(ufMaps3[x] != ' ')
Sleep(7);
fflush(stdout);
x++;
};
cout<< endl;
x= 0;
while ( ufMaps4[x] != '\0')
{
cout << ufMaps4[x];
if(ufMaps4[x] != ' ')
Sleep(7);
fflush(stdout);
x++;
};
cout<< endl;
x= 0;
while ( ufMaps5[x] != '\0')
{
cout << ufMaps5[x];
if(ufMaps5[x] != ' ')
Sleep(7);
fflush(stdout);
x++;
};
cout<< endl;
x= 0;
while ( ufMaps6[x] != '\0')
{
cout << ufMaps6[x];
if(ufMaps6[x] != ' ')
Sleep(7);
fflush(stdout);
x++;
};
cout << "\n\n\n";
cout <<"\n\n\n";
x= 0;
while ( gator1[x] != '\0')
{
cout << gator1[x];
if(gator1[x] != ' ')
Sleep(4);
fflush(stdout);
x++;
};
cout << endl;
x= 0;
while ( gator2[x] != '\0')
{
cout << gator2[x];
Sleep(4);
fflush(stdout);
x++;
};
cout << endl;
x= 0;
while ( gator3[x] != '\0')
{
cout << gator3[x];
Sleep(4);
fflush(stdout);
x++;
};
cout << endl;
x= 0;
while ( gator4[x] != '\0')
{
cout << gator4[x];
Sleep(4);
fflush(stdout);
x++;
};
cout << endl;
x= 0;
while ( gator5[x] != '\0')
{
cout << gator5[x];
Sleep(4);
fflush(stdout);
x++;
};
cout << endl;
x= 0;
while ( gator6[x] != '\0')
{
cout << gator6[x];
Sleep(4);
fflush(stdout);
x++;
};
cout << endl;
x= 0;
while ( gator7[x] != '\0')
{
cout << gator7[x];
Sleep(4);
fflush(stdout);
x++;
};
cout << endl;
x= 0;
while ( gator8[x] != '\0')
{
cout << gator8[x];
Sleep(4);
fflush(stdout);
x++;
};
cout << endl;
x= 0;
while ( gator9[x] != '\0')
{
cout << gator9[x];
Sleep(4);
fflush(stdout);
x++;
};
cout << endl;
x= 0;
while ( gator10[x] != '\0')
{
cout << gator10[x];
Sleep(4);
fflush(stdout);
x++;
};
cout << endl;
cout << "\n\n\n";
//Vector of node locations: Holds data to be printed in methods to check database.
vector <Node*> locations;
//Temporary Vector.
vector <string> temp;
//Vector of Tokens, taken from file string.
vector <string> token;
//used to figure out which nodes are neighbors
vector <vector <int> > adjMatrix;
vector <LinkedNode*> adjList;
vector <Node*> neighborsPassToNode;
vector <double> distancesPassToNode;
int lines = 0;
// Calls name of File.
string filename = "map.txt";
// Input File of Building names and longitude and latitude coordinates.
// Data is separated with commas.
ifstream infile(filename);
// infile.open(filename);
string str;
// Checks if file is inputted successfully.
if (!infile)
{
cout << "Error in accessing file.";
return 0;
}
// Loops through map.txt, creating vector of tokens.
while(getline(infile,str))
{
temp = createTokens(str);
lines++;
for(int i=0; i<temp.size();i++)
{
//if (temp[i] != ","){
token.push_back(temp[i]);
// }
}
}
//tests token vector
/*for(auto p: token){
std::cout<<p<<endl;
}*/
//iterates through the token list and creates a Node for each one specified
// Variables to represent data in tokens.
string name;
double lon;
double lat;
int iter = 0;
//each line correlates to a new node so each loop creates a node
for(int i = 1; i <= lines; i++)
{
name = token[iter];
iter++;
//converts string to double
lon = atof(token[iter].c_str());
iter++;
//converts string to double
lat= atof(token[iter].c_str());
if(i!=lines)
{
iter++;
}
// Adds new Node to the Vector of locations, represeting the building.
locations.push_back(new Node(lat, lon, name, neighborsPassToNode, distancesPassToNode));
}
//prints out to test the Node locations vector
/*for(int i= 0; i<locations.size(); i++){
cout<< locations[i]->getlon();
cout<< locations[i]->getY();
cout<< locations[i]->getName();
}*/
// File Name for adjacency matrix for map.txt.
// 0 represents building is itself.
// 1 represents building is a neighbor.
// -1 represents building is not a neighbor.
string adjacencyFileName = "adj.txt";
//clears token vector for reuse
token.clear();
ifstream afile(adjacencyFileName);
// Loops through file, getting tokens based on the comma delimiter.
while(getline(afile,str))
{
temp = createTokens(str);
for(int i=0; i<temp.size();i++)
{
//if (temp[i] != ","){
token.push_back(temp[i]);
// }
}
}
vector <int> nodeAdj;
iter = 0;
//iterates through the token list, knowing each line should have a number of 1s/0s/-1s equal to the locations.size()
//which is equal to the number of lines in map.txt
//pushes the 1D vector that corresponds to a node into the 2D vector that holds adjcacency for all nodes
for(int i = 1; i <= lines; i++)
{
for(int j = 0; j<lines; j++)
{
nodeAdj.push_back(atoi(token[iter].c_str()));
iter++;
}
adjMatrix.push_back(nodeAdj);
nodeAdj.clear();
}
//tests adjMatrix
/*for(int x=0;x<adjMatrix.size();x++)
{
for(int y=0;y<adjMatrix[x].size();y++)
{
cout<<adjMatrix[x][y];
}
cout<<endl;
}*/
// Creates the Vector of Neighbors and corresponding Distances for each location Node.
for(int i = 0; i < lines; i++)
{
for(int j = 0; j<lines; j++)
{
if(adjMatrix[i][j]==1)
{
locations[i]->pushNeighbors(locations[j]);
locations[i]->pushDistances(getDist(locations[i]->getLat(), locations[i]->getLon(), locations[j]->getLat(), locations[j]->getLon()));
}
}
}
//creates the adjacency list by making a node for each neighbor at a locations index
LinkedNode* emptyLink;
LinkedNode* tempLink;
for(int i = 0; i < locations.size(); i++)
{
for (int j = 0; j < locations[i]->getNeighbors().size(); j++)
{
//if it is the first neighbor, create the head of the linked list in the adjList
if(j==0)
{
adjList.push_back(new LinkedNode(locations[i]->getDistances()[j], locations[i]->getNeighbors()[j]));
tempLink = adjList[i];
//cout << "\n Pushed " << locations[i]->getNeighbors()[j]->getName() << " to adjList at the index for " << locations[i]->getName();
}
//if adjList has already been pushed the head at this index, set the next node to as the j neighbor
else
{
tempLink->setNext(new LinkedNode(locations[i]->getDistances()[j], locations[i]->getNeighbors()[j]));
tempLink=tempLink->getNext();
//cout << "\n Added " << locations[i]->getNeighbors()[j]->getName() << " to adjList at the index for " << locations[i]->getName();
}
}
}
//tests adjList
/*for(int i = 0; i < adjList.size(); i++){
tempLink = adjList[i];
cout << locations[i]->getName() << endl;
cout << " Neighbor: " << tempLink->getNeighbor()->getName() << endl;
cout << " Distance: " << tempLink->getDistance() << endl;
while(tempLink->getNext()!=NULL)
{