-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijkstraheap.cpp
284 lines (236 loc) · 7.53 KB
/
dijkstraheap.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
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <iostream>
#include <fstream>
#include <cassert>
#include <queue> // for priority queue (heap with deletions)
#include <functional>
class Vertex
{
private:
int m_nId;
int m_nGreedyScore;
public:
Vertex()
{
m_nId = -1; m_nGreedyScore = -1;
}
Vertex( int id, int score )
{
m_nId = id; m_nGreedyScore = score;
}
Vertex( const Vertex &v )
{
m_nId = v.getId();
m_nGreedyScore = v.getScore();
}
~Vertex() {};
int getId() const { return m_nId; }
int getScore() const { return m_nGreedyScore; }
void updateScore(int newScore) { m_nGreedyScore = newScore; }
friend bool operator< (Vertex v1, Vertex v2);
friend bool operator> (Vertex v1, Vertex v2);
friend bool operator== (Vertex v1, Vertex v2);
friend bool operator>= (Vertex v1, Vertex v2);
friend bool operator<= (Vertex v1, Vertex v2);
friend std::ostream &operator<<( std::ostream &output, const Vertex &v )
{
output << "(id = "<<v.getId()<<", score = "<<v.getScore()<<")";
return output;
}
};
bool operator< (Vertex v1, Vertex v2)
{
return (v1.getScore() < v2.getScore()) ;
}
bool operator> (Vertex v1, Vertex v2)
{
return (v1.getScore() > v2.getScore()) ;
}
bool operator== (Vertex v1, Vertex v2)
{
return (v1.getScore() == v2.getScore()) ;
}
bool operator>= (Vertex v1, Vertex v2)
{
return (v1.getScore() >= v2.getScore()) ;
}
bool operator<= (Vertex v1, Vertex v2)
{
return (v1.getScore() <= v2.getScore()) ;
}
// Parses a line from the adjacency info text file
std::vector< std::vector<int> > getDataLine( int lineNo )
{
assert( lineNo >= 0 && lineNo <=200 );
std::vector< std::vector<int> > pq;
std::vector<int> p;
std::vector<int> q;
std::string line;
std::ifstream adjfile("dijkstraData.txt");
for(int i=0; i<lineNo; i++)
{
std::getline(adjfile, line);
}
//std::cout<<line<<std::endl;
adjfile.close();
std::stringstream stream(line);
bool firstnum = true;
bool oddpos = true;
int n;
while( stream>>n )
{
if (firstnum)
{
firstnum = false;
continue;
}
if( stream.peek() == ',' || stream.peek() == ' ' )
stream.ignore();
//std::cout<<n<<std::endl;
if(oddpos)
{
p.push_back( n );
oddpos = !oddpos;
}
else
{
q.push_back( n );
oddpos = !oddpos;
}
}
pq.push_back(p);
pq.push_back(q);
return pq;
}
// -1 if element is not in vector. Very stupid. Should not be doing this. Should use heap with delete method!
int findElemInVect( int elem, std::vector<int> vect )
{
int result = -1;
for( int i = 0; i<vect.size(); ++i )
{
if( vect[i] == elem )
{
result = i ;
break;
}
}
return result;
}
int main()
{
//std::priority_queue< Vertex, std::vector<Vertex>, std::greater<Vertex> > minheap;
//minheap.push( Vertex(1, 70) );
//minheap.push( Vertex(2, 23) );
//minheap.push( Vertex(3, 33) );
//
//std::cout<< minheap.top().getScore() <<std::endl; minheap.pop();
//std::cout<< minheap.top().getScore() <<std::endl; minheap.pop();
//std::cout<< minheap.top().getScore() <<std::endl;
//return 0;
// Initialization phase
std::vector<int> X; // maintain list of processed vertexes
X.push_back(1);
std::vector<bool> processed(200, false);
std::vector<int> A; // maintain shortest distance to each processed vertex
A.push_back(0);
std::priority_queue< Vertex, std::vector<Vertex>, std::greater<Vertex> > minheap;
// minheap will store vertexes from V-X
std::vector< std::vector<int> > pq = getDataLine(1);
for (int i = 0; i<pq[0].size(); ++i )
{
//vertexes directly attached to vertex 1
minheap.push( Vertex( pq[0][i], pq[1][i] ) );
processed[ pq[0][i]-1 ] = true;
}
for (int line=0; line<200; ++line)
{
if( processed[line] == false )
{
minheap.push( Vertex(line+1, 1000000) );
}
}
// Main while loop
int XSizeCur = 0;
while( X.size() > XSizeCur && X.size() <= 200 )
{
XSizeCur = X.size();
/*
for( int i=0; i < X.size(); i++ ) // for each v in X
{
std::vector< std::vector<int> > pq = getDataLine( X[i] );
assert( pq[0].size() == pq[1].size() );
for ( int j=0; j < pq[0].size(); ++j )
{
if( processed[ pq[0][j]-1 ] ) // and each w in V-X
continue;
int curDist = A[i] + pq[1][j];
if (curDist < curMin)
{
curMin = curDist;
wstar = pq[0][j];
}
}
}*/
//linear search for min can be replaced by heap operations below:
Vertex topv = minheap.top();
int wstar = topv.getId();
int curMin = topv.getScore();
std::cout<<topv.getId()<<" "<<topv.getScore()<<" "<< wstar <<" " <<curMin<<std::endl;
X.push_back(wstar);
A.push_back(curMin);
minheap.pop();
// recompute keys for vertexes in V-X reachable from wstar
// I'm doing this stupid tmpheap technique because priority queue has no delete method
std::priority_queue< Vertex, std::vector<Vertex>, std::greater<Vertex> > tmpminheap;
std::vector< std::vector<int> > wstarpq = getDataLine( wstar );
/*std::fill( processed.begin(), processed.end(), false );
for( int p = 0; p < wstarpq[0].size(); ++p )
{
processed[wstarpq[0][p]-1] = true; // ones that are true and in minheap need key update
}*/
std::cout<<"pre minheap size = "<<minheap.size()<< std::endl;
while( minheap.size()>0 )
{
Vertex vtmp = minheap.top();
minheap.pop();
int find = findElemInVect(vtmp.getId(), wstarpq[0]);
if( find != -1 )
{
vtmp.updateScore( std::min(vtmp.getScore(), curMin+wstarpq[1][find]) );
}
tmpminheap.push(vtmp);
/* if( processed[ vtmp.getId()-1 ] == false )
{
tmpminheap.push(vtmp);
std::cout<<"inserted element not needing update "<<vtmp<<std::endl;
}
else
{
for( int q = 0; q < wstarpq[0].size(); ++q )
{
if( !isElemInVect( wstarpq[0][q], X ) && (curMin + wstarpq[1][q] < vtmp.getScore()) )
{
vtmp.updateScore( curMin+wstarpq[1][q] );
}
}
tmpminheap.push(vtmp);
std::cout<<"inserted element with update "<<vtmp<<std::endl;
} */
}
std::cout<<"changing minheap"<<std::endl;
minheap = tmpminheap;
std::cout<<"minheap changed"<<std::endl;
std::cout<<"post minheap size = "<<minheap.size()<< std::endl;
std::cout << X.size() <<" "<< A.size()<<" "<<processed.size()<<std::endl;
}
std::cout<<std::endl;
for( int i=0; i<X.size(); i++ )
{
std::cout<<"("<<X[i]<<","<<A[i]<<") ";
}
std::cout<<std::endl;
return 0;
}