-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
227 lines (170 loc) · 4.85 KB
/
main.cpp
File metadata and controls
227 lines (170 loc) · 4.85 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
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include "Graph.h"
#include <string>
#include <sstream>
using namespace std;
using std::pair;
//typedef GraphArc<char, int> Arc;
//typedef GraphNode<char, int> Node;
typedef GraphArc<string, int> Arc;
typedef GraphNode<pair<string, int>, int> Node;
typedef vector<Node*> Path;
typedef vector<pair<string, int>> PairPath;
typedef vector<PairPath> PathMap;
void empty(Node * pNode) {}
void visit( Node * pNode ) {
cout << "Visiting: " << pNode->data().first << endl;
}
void trackback(Node * pNode) {
cout << "Trackback: " << pNode->data().first /*<< ", " << pNode->data().second*/ << endl;
}
void outputPath(Path* p)
{
Path::iterator pathE = p->end();
Path::iterator pathB = p->begin();
pathE--;
//Using path to track back
for (; pathE != pathB; pathE--) {
trackback(*pathE);
}
trackback(*pathB);
}
void outputPathPlus(Path* p)
{
Path::iterator pathE = p->end();
Path::iterator pathB = p->begin();
pathE--;
cout << "=====OPP" << endl;
cout << "Path from " << (*pathE)->data().first << " to " << (*pathB)->data().first << endl;
cout << endl;
int lastCost = 0;
//Using path to track back
for (; pathE != pathB; pathE--) {
cout << "Node: " << (*pathE)->data().first << ", " << (*pathE)->data().second - lastCost << endl;
lastCost = (*pathE)->data().second;
}
cout << "Node: " << (*pathE)->data().first << ", " << (*pathE)->data().second - lastCost << endl;
cout << endl;
cout << "Path total cost: " << (*pathE)->data().second << endl;
cout << "=====" << endl;
}
void outputPathPlusShort(Path* p)
{
Path::iterator pathE = p->end();
Path::iterator pathB = p->begin();
pathE--;
cout << "=====OPPS" << endl;
cout << "[" << (*pathE)->data().first << "-" << (*pathB)->data().first << "]" << " [" << (*pathB)->data().second << "]" << endl;
cout << ">";
int lastCost = 0;
//Using path to track back
for (; pathE != pathB; pathE--) {
cout << (*pathE)->data().first << "(" << (*pathE)->data().second - lastCost << ")->";
lastCost = (*pathE)->data().second;
}
cout << (*pathE)->data().first << "(" << (*pathE)->data().second - lastCost << ")" << endl;
cout << "=====" << endl;
}
void addPathToMap(PathMap& map, Path& p)
{
//create a path of pairs
vector<pair<string, int>> pairpath;
Path::iterator pathE = p.end();
Path::iterator pathB = p.begin();
pathE--;
int lastCost = 0;
//Loop through path and add each node to the pair path
for (; pathE != pathB; pathE--) {
pairpath.push_back(pair<string, int>((*pathE)->data().first, (*pathE)->data().second - lastCost));
lastCost = (*pathE)->data().second;
}
//last node
pairpath.push_back(pair<string, int>((*pathE)->data().first, (*pathE)->data().second - lastCost));
map.push_back(pairpath);
}
void outputPairPath(PairPath *p)
{
cout << "=====" << "PP" << endl;
//each node and it's distance added to a stringstream, add up the total distance
int i = 0;
int m = p->size();
std::stringstream s;
int total = 0;
for (; i < m; i++)
{
total += p->at(i).second;
s << p->at(i).first << "(" << p->at(i).second << ")";
if (i < m - 1)
s << "->";
}
//Path begin to path end, total distance
cout << "[" << p->begin()->first << "-" << (--p->end())->first << "]" << " [" << total << "]" << endl;
//Actual path
cout << s.str() << endl;
cout << "=====" << endl << endl;
}
int main(int argc, char *argv[]) {
cout << "LAB 5 START\n" << "==========" << endl;
//create graph
Graph<pair<string, int>, int> graph( 6 );
//read nodes
pair<string, int> c("", 0);
int i = 0;
ifstream myfile;
myfile.open("dornodes.txt");
while (myfile >> c.first) {
graph.addNode(c, i++);
}
myfile.close();
//read arcs
myfile.open("dorarcs.txt");
int from, to, weight;
while ( myfile >> from >> to >> weight ) {
graph.addDualArc(from, to, weight);
}
myfile.close();
//=====//Breadth-First
// Now traverse the graph.
//graph.breadthFirst( graph.nodeArray()[0], visit );
//graph.breadthFirstPlus(graph.nodeArray()[0], graph.nodeArray()[5], visit);
//=====//UCS
//cout << endl;
//Max the distance in all nodes
Path path;
//graph.UCS(graph.nodeArray()[0], graph.nodeArray()[5], visit, path);
//outputPathPlus(&path);
//Using end node prev chain to track back
//Node* pathNode = graph.nodeArray()[5];
//while (pathNode->getPrev() != NULL)
//{
// trackback(pathNode);
// pathNode = pathNode->getPrev();
//}
//=====//UCS Precomputation
//cout << endl;
PathMap map;
//Iterate through map and calculate paths
//outer, inner, maxNodes
int o, n, m;
m = 5;
for (o = 0; o < m; o++)
{
for (n = o + 1; n <= m; n++)
{
path.clear();
graph.UCS(graph.nodeArray()[o], graph.nodeArray()[n], empty, path);
addPathToMap(map, path);
}
}
//Output path
PathMap::iterator pmI = map.begin();
PathMap::iterator pmE = map.end();
cout << "-----" << endl;
for (; pmI != pmE; pmI++)
{
outputPairPath(&*pmI);
}
system("PAUSE");
}