-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgraph.cpp
More file actions
314 lines (281 loc) · 9.41 KB
/
graph.cpp
File metadata and controls
314 lines (281 loc) · 9.41 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
// David Oswald
// CSIT 832
// Assignment 2: Graphs.
// Version 0.02: See graph.h for explanation.
#include <stdexcept> // runtime_error()
#include <vector> // vector, vector<>::iterator
#include <map> // map, pair
#include <iostream> // cerr.
// Add vertices, one at a time. Returns false if vertex already existed.
// Throws under the impossible condition of an attempt to add to same
// index twice.
// Also throws if is_full() returns true, or if any memory allocation fails.
template<class VertexT>
bool Graph<VertexT>::add_vertex( VertexT v )
{
using std::map;
using std::vector;
using std::pair;
using std::runtime_error;
// is_full() *should* catch out of memory before we attempt to allocate,
// but we will also trap std::bad_alloc's later on just in case.
if( is_full() )
throw( runtime_error(
"add_vertex(): No room for more vertices."
) ); // This should be difficult but NOT impossible to reach.
// Note: v_to_i.size() will return number of elements, which we can use
// as the index number of the new element.
// Add the vertex to our crossreference lookups.
// First, the vertex to index crossref.
pair<
typename map<
VertexT,
std::vector<int>::size_type
>::iterator,
bool
> v_to_i_ret;
try
{
v_to_i_ret
= v_to_i.insert( pair< VertexT, std::vector<int>::size_type >( v, v_to_i.size() ) );
}
catch( std::bad_alloc& ba )
{
std::cerr << "Bad Allocation caught adding to map: "
<< ba.what() << std::endl;
throw( runtime_error(
"add_vertex(): Failed to allocate memory for new vertex."
) );
}
if( !v_to_i_ret.second )
return false; // The vertex already existed.
// Next, the index to vertex crossref.
try
{
i_to_v.push_back( v );
}
catch( std::bad_alloc& ba )
{
std::cerr << "Bad Allocation caught adding to index: "
<< ba.what() << std::endl;
throw( runtime_error(
"add_vertex(): Failed to allocate memory for new vertex."
) );
}
// Add a column to each existing row of the matrix for this new vertex.
vector<vector<int> >::iterator it;
for( it=matrix.begin(); it != matrix.end(); ++it )
{
try
{
it->push_back( NON_EDGE );
}
catch( std::bad_alloc& ba )
{
std::cerr << "Bad Allocation caught adding to matrix: "
<< ba.what() << std::endl;
throw( runtime_error(
"add_vertex(): Failed to allocate memory for new vertex."
) );
}
}
// Note: v_to_i.size() will now return the number of elements inclusive
// of the new vertex, so we can use that as the number of columns for
// our new row.
// Add a new row to the matrix for this new vertex.
vector<int> row( v_to_i.size(), NON_EDGE ); // Create a new row to insert
matrix.push_back( row );
return true;
}
// Passes by reference a vector containing all the vertex names.
// Returns a vertex count.
template<class VertexT>
int Graph<VertexT>::get_vertices( std::vector<VertexT>& vertices ) const
{
using std::vector;
int size = num_vertices();
typename vector<VertexT>::const_iterator vit;
for( vit = i_to_v.begin(); vit != i_to_v.end(); ++vit )
{
vertices.push_back( *vit );
}
return size;
}
// Returns a vertex count.
template<class VertexT>
int Graph<VertexT>::num_vertices() const
{
using std::runtime_error;
if( i_to_v.size() != v_to_i.size() || v_to_i.size() != matrix.size() )
throw( runtime_error(
"Graph components out of sync: Detected in num_vertices()"
) ); // Should be impossible. Just a sanity check.
return i_to_v.size();
}
// Removes all vertices.
// Clears the matrix.
template<class VertexT>
void Graph<VertexT>::make_empty()
{
using std::runtime_error;
v_to_i.clear();
i_to_v.clear();
matrix.clear();
if( ! v_to_i.empty() || ! i_to_v.empty() || ! matrix.empty() )
throw( runtime_error(
"make_empty(): One or more components of Graph failed to clear"
) ); // Impossible, but never hurts to be careful.
return;
}
// Returns true if the graph is empty.
template<class VertexT>
bool Graph<VertexT>::is_empty() const
{
using std::runtime_error;
if(
i_to_v.empty() != v_to_i.empty() ||
v_to_i.empty() != matrix.empty()
)
throw( runtime_error(
"Graph components out of sync: detected in is_empty()"
) ); // This should be impossible. Just a sanity check.
return matrix.empty(); // Returns true for empty, false otherwise.
}
// Check whether the Graph is full. Because our graph is implemented with
// a vector matrix and maps, running out of room is synonymous with running
// out of heap space (memory). We probably should just be handling that sort
// of situation via exceptions, since memory shortages are exceptional cases.
template<class VertexT>
bool Graph<VertexT>::is_full() const
{
// First, check to see if we can fit another vertex in our maps.
// Also check whether our matrix can hold another row. We subtract one
// from the max_size to accommodate an extra column per existing row.
if(
v_to_i.size() >= v_to_i.max_size() ||
i_to_v.size() >= i_to_v.max_size() ||
matrix.size() >= matrix.max_size() - 1
)
return true;
else
// We should be ok. Return false, indicating there's room for
// another vertex.
return false;
}
// Get the index of a vertex v. Return -1 if not found.
template<class VertexT>
int Graph<VertexT>::index_is( VertexT v ) const
{
using std::map;
typename map<
VertexT,
std::vector<int>::size_type
>::const_iterator idx_it
= v_to_i.find( v );
if( idx_it == v_to_i.end() )
return -1; // Sentinal flag: Vertex doesn't exist!
else
return idx_it->second;
}
// Get the vertex for index i. Throw if vertex not found.
template<class VertexT>
VertexT Graph<VertexT>::vertex_is( std::vector<int>::size_type i ) const
{
using std::map;
using std::runtime_error;
if( i > i_to_v.size() - 1 )
throw( runtime_error(
"vertex_is() called on an out of range index: "
"No vertex by that index."
) );
else
return i_to_v[i];
}
// Adds an edge. Returns false under the condition that either vertex
// doesn't exist. Will blindly modify an existing edge.
template<class VertexT>
bool Graph<VertexT>::add_edge( VertexT va, VertexT vb, int weight )
{
int row = index_is( va );
int col = index_is( vb );
if( row < 0 || col < 0 )
return false;
else {
matrix[row][col] = weight;
// We implement non-directed as auto-two-way-direction.
if( !directed ) matrix[col][row] = weight;
return true;
}
}
// Returns true if an edge exists. False if edge doesn't exist, or
// also if one of the specified vertices doesn't exist.
template<class VertexT>
bool Graph<VertexT>::edge_exists( VertexT va, VertexT vb ) const
{
int row = index_is( va );
int col = index_is( vb );
if( row >= 0 && col >= 0 && matrix[row][col] != NON_EDGE )
return true;
else
return false;
}
// Deletes an edge by setting its position in matrix to zero.
// Fails silently if edge doesn't exist
// Throws if one or more of the vertices doesn't exist.
// While that should reflect a logic error, it's not necessarily
// a fatal issue. Perhaps I could re-implement as a bool return.
template<class VertexT>
void Graph<VertexT>::delete_edge( VertexT va, VertexT vb )
{
using std::runtime_error;
int row = index_is( va );
int col = index_is( vb );
if( row < 0 || col < 0 )
throw( runtime_error(
"delete_edge(): Attempt to delete an edge with invalid vertex."
) );
matrix[row][col] = NON_EDGE;
// non-directed implemented as automatic direct-back (2-way direction).
if( !directed ) matrix[col][row] = NON_EDGE;
return;
}
// Returns the weight of an edge. Throws if a vertex doesn't exist.
// If an edge doesn't exist, returns NON_EDGE, which is defined as 0.
template<class VertexT>
int Graph<VertexT>::get_weight( VertexT va, VertexT vb ) const
{
using std::runtime_error;
int row = index_is( va );
int col = index_is( vb );
if( row < 0 || col < 0 )
throw( runtime_error(
"get_weight(): Can't get a weight of edge with invalid vertex."
) );
else
return matrix[row][col];
}
// Scan all the columns for the Vertex's row in the matrix.
// Any non-zero column is an edge.
template<class VertexT>
void Graph<VertexT>::get_adjacent(
VertexT v,
std::priority_queue< std::pair<VertexT,int>,
std::vector< std::pair<VertexT,int> >,
PairComparator< VertexT > >& pq
) const
{
using std::runtime_error;
using std::pair;
int r_ix = index_is( v );
if( r_ix < 0 )
throw( runtime_error(
"get_adjacent(): Invalid vertix."
) );
for( unsigned c_ix = 0; c_ix < matrix[r_ix].size(); c_ix++ )
if( int weight = matrix[r_ix][c_ix] )
{
VertexT vb = vertex_is( c_ix );
pq.push( pair<VertexT,int>( vb, weight ) );
}
return;
}