-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.h
389 lines (327 loc) · 11.9 KB
/
Graph.h
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
#ifndef ADS_GRAPH_H
#define ADS_GRAPH_H
#include "Comparator.h"
#include "DArray.h"
#include "GraphUtils.h"
#include "LChain.h"
#include <stdio.h>
#include <math.h>
#include <ios>
#include <ostream>
/**
* Graph.h
*
* author: Giacomo Benincasa (me@gbenin.casa)
*/
namespace ADS
{
template<class T>
struct Edge
{
Edge (unsigned int uiDstKey, T cost);
~Edge (void);
bool operator == (const Edge &other) const;
unsigned int uiAdjVertext;
T edgeCost;
};
template<class T>
struct Vertex
{
Vertex (unsigned int uiKey);
~Vertex (void);
unsigned int uiVertexKey;
LChain<Edge<T>*, ObjPointerComparator<Edge<T> > > edges;
};
template<class T>
class Graph
{
public:
/* bDirected is set on true if the graph is directed
* - If bAllowMultipleEdges is set on true, an edge is added
* only if an edge from src to dst does not exist
*/
Graph (bool bDirected=true, bool bAllowMultipleEdges=false);
virtual ~Graph (void);
/* Returns true if the vertex was added, false otherwise */
bool addVertex (unsigned int uiKey);
/* If the graph is directed, the method returns true if the edge was
added, false otherwise.
If the graph is undirected, the method returns true if all the two
edges (src -> dst and dst -> src) were added, false otherwise */
bool addEdge (unsigned int uiSrcKey, unsigned int uiDstKey, T cost);
/* Removes all the vertices and edges from the graph */
void empty (void);
/* Returns the edge density of the graph. The returned value is a
percentage expressed in decimal notation in the range [0, 1] */
double getDensity (void);
/* Returns the edge density of the graph. The returned value is a
percentage expressed in integer notation in the range [0, 100] */
unsigned int getDensityAsInt (void);
/* Returns the total number of edges */
unsigned int getEdgeCount (void);
/* Returns the i-th Vertex */
Vertex<T> * getVertex (unsigned int uiIndex);
/* Returns the total number of vertices */
unsigned int getVertexCount (void);
/* If the graph is not bDirected it returns NULL, otherwise it returns
the graph's underlying undirected graph */
Graph * getUndirectedGraph (void);
/* Returns true if at least one edge from uiSrcKey to uiDstKey
exists, returns false otherwise */
bool hasEdge (unsigned int uiSrcKey, unsigned int uiDstKey);
/* If bStronglyConnected is set on true, the method returns true
if the graph is strongly connected, false otherwise.
If bStronglyConnected is set on false, the method returns false
if the graph is not even weakly connected, true otherwise */
bool isConnected (bool bStronglyConnected=false);
bool isDirected (void);
void dump (std::ostream &out, bool bPrintColumnNames);
private:
/* Performs a depth-first traverse of the graph */
void traverse (Vertex<T> *pVertex, bool *pVisited);
private:
bool _bDirected;
bool _bAllowMultipleEdges;
unsigned int _uiNVertexes;
unsigned int _uiNEdges;
DArray<Vertex<T>* > _vertices;
};
template<class T>
Graph<T>::Graph (bool bDirected, bool bAllowMultipleEdges)
: _vertices (0, 0, true)
{
_bDirected = bDirected;
_bAllowMultipleEdges = bAllowMultipleEdges;
_uiNVertexes = 0;
_uiNEdges = 0;
}
template<class T>
Graph<T>::~Graph()
{
empty();
}
template<class T>
bool Graph<T>::addVertex (unsigned int uiKey)
{
Vertex<T> *pVertex = _vertices.get (uiKey);
if (pVertex == NULL) {
pVertex = new Vertex<T> (uiKey);
_vertices.add (uiKey, pVertex);
_uiNVertexes++;
return true;
}
return false;
}
template<class T>
bool Graph<T>::addEdge (unsigned int uiSrcKey, unsigned int uiDstKey, T cost)
{
/* Make sure that the source and destination vertices exist */
addVertex (uiSrcKey);
addVertex (uiDstKey);
bool bRet = true;
Vertex<T> *pVertex = _vertices.get (uiSrcKey);
if (_bAllowMultipleEdges || !hasEdge (uiSrcKey, uiDstKey)) {
/* if multiple edges are allowed (therefore it is not necessary
to check whether the edge is already contained) or if the
does not yet exist - add it */
pVertex->edges.prepend (new Edge<T> (uiDstKey, cost));
_uiNEdges++;
}
else
bRet = false;
if (!_bDirected) {
pVertex = _vertices.get (uiDstKey);
if (_bAllowMultipleEdges || !hasEdge (uiDstKey, uiSrcKey)) {
/* if multiple edges are allowed (therefore it is not necessary
to check whether the edge is already contained) or if the
does not yet exist - add it */
pVertex->edges.prepend (new Edge<T> (uiSrcKey, cost));
_uiNEdges++;
}
else
bRet = false;
}
return bRet;
}
template<class T>
void Graph<T>::empty()
{
for (unsigned int i = 0; i < _vertices.size(); i++) {
Vertex<T> *pVertex = _vertices.remove (i);
if (pVertex != NULL) {
for (Edge<T> *pEdge = pVertex->edges.pop(); pEdge != NULL;
pEdge = pVertex->edges.pop())
delete pEdge;
}
delete pVertex;
}
_uiNVertexes = 0;
_uiNEdges = 0;
}
template<class T>
double Graph<T>::getDensity()
{
return GraphUtils::getDensity (getVertexCount(), getEdgeCount(),
isDirected());
}
template<class T>
unsigned int Graph<T>::getDensityAsInt()
{
return static_cast<int>(floor ((getDensity() * 100.0) + 0.5));
}
template<class T>
unsigned int Graph<T>::getEdgeCount()
{
return _uiNEdges;
}
template<class T>
Vertex<T> * Graph<T>::getVertex (unsigned int uiIndex)
{
return _vertices.get (uiIndex);
}
template<class T>
unsigned int Graph<T>::getVertexCount()
{
return _uiNVertexes;
}
template<class T>
Graph<T> * Graph<T>::getUndirectedGraph()
{
if (!isDirected())
return NULL;
/* temporarely set pUndirectedGraph->_bDirected to true in order to
avoid pUndirectedGraph->addEdge() to add the dst -> src edge */
Graph<T> *pUndirectedGraph = new Graph<T> (true, _bAllowMultipleEdges);
for (unsigned int uiSrc = 0; uiSrc < _vertices.size(); uiSrc++) {
Vertex<T> *pVertex = _vertices.get (uiSrc);
if (pVertex != NULL) {
for (Edge<T> *pEdge = pVertex->edges.getFirst (true);
pEdge != NULL;
pEdge = pVertex->edges.getNext()) {
unsigned int uiDst = pEdge->uiAdjVertext;
if (!pUndirectedGraph->hasEdge (uiSrc, uiDst)) {
pUndirectedGraph->addEdge (uiSrc, uiDst, pEdge->edgeCost);
}
if (!hasEdge (uiDst, uiSrc) &&
!pUndirectedGraph->hasEdge (uiDst, uiSrc)) {
pUndirectedGraph->addEdge (uiDst, uiSrc, pEdge->edgeCost);
}
}
}
}
pUndirectedGraph->_bDirected = false;
return pUndirectedGraph;
}
template<class T>
bool Graph<T>::hasEdge (unsigned int uiSrcKey, unsigned int uiDstKey)
{
Vertex<T> *pVertex = _vertices.get (uiSrcKey);
if (pVertex == NULL)
return false;
for (Edge<T> *pEdge = pVertex->edges.getFirst (true);
pEdge != NULL;
pEdge = pVertex->edges.getNext())
if (pEdge->uiAdjVertext == uiDstKey)
return true;
return false;
}
template<class T>
bool Graph<T>::isConnected (bool bStronglyConnected)
{
if (!bStronglyConnected && isDirected()) {
/* Get the underlying undirected graph and checks if
that is connected */
Graph *pGraph = getUndirectedGraph();
bool bRet = pGraph->isConnected (bStronglyConnected);
delete pGraph;
return bRet;
}
/* Allocated array initializes all the elements to 0x0 */
bool *pVisited = (bool *) allocateArray (_uiNVertexes, sizeof (bool));
/* For each vertex */
Vertex<T> *pVertex = NULL;
unsigned int uiVertexKey = 0;
for (; uiVertexKey < _vertices.size(); uiVertexKey++) {
if ((pVertex = _vertices.get (uiVertexKey)) != NULL) {
pVisited[uiVertexKey] = true;
traverse (pVertex, pVisited);
/* Checks whether all the vertices were visited */
for (unsigned int i = 0; i < _uiNVertexes; i++)
if ((_vertices.get (i) != NULL) && !pVisited[i]) {
/* the vertex of key "i" exists and it was not visited */
deallocate ((void**)&pVisited);
return false;
}
}
if (!isDirected())
/* if the graph is undirected, having 1 connected node is
a sufficient condition for the graph to be strongly connected
(and therefore not to be weakly connected as well */
break;
/* Reset array of visited nodes */
setArray ((int)false, pVisited, _uiNVertexes, sizeof (bool));
}
deallocate ((void**)&pVisited);
return true;
}
template<class T>
bool Graph<T>::isDirected()
{
return _bDirected;
}
template<class T>
void Graph<T>::traverse (Vertex<T> *pVertex, bool *pVisited)
{
if (pVertex == NULL)
return;
for (Edge<T> *pEdge = pVertex->edges.getFirst (true); pEdge != NULL;
pEdge = pVertex->edges.getNext())
if ((pEdge != NULL) && !pVisited[pEdge->uiAdjVertext]) {
/* check whether pEdge->uiAdjVertexthas already been visited
to avoid loops */
pVisited[pEdge->uiAdjVertext] = true;
traverse (_vertices.get (pEdge->uiAdjVertext), pVisited);
}
}
template<class T>
void Graph<T>::dump (std::ostream &out, bool bPrintColumnNames)
{
if (bPrintColumnNames) {
printf ("[vertex]\tuiAdjVertex (cost)\tuiAdjVertex (cost) ...\n");
}
for (unsigned int i = 0; i < _vertices.size (); i++) {
Vertex<T> *pVertex = _vertices.get (i);
if (pVertex != NULL) {
for (Edge<T> *pEdge = pVertex->edges.getFirst (true);
pEdge != NULL; pEdge = pVertex->edges.getNext()) {
out << pVertex->uiVertexKey << ' ' << pEdge->uiAdjVertext << ' ' << pEdge->edgeCost << std::endl;
}
}
}
}
template<class T>
Edge<T>::Edge (unsigned int uiDstKey, T cost)
{
uiAdjVertext = uiDstKey;
edgeCost = cost;
}
template<class T>
Edge<T>::~Edge()
{
}
template<class T>
bool Edge<T>::operator==(const Edge<T> &other) const
{
return (uiAdjVertext == other.uiAdjVertext);
}
template<class T>
Vertex<T>::Vertex (unsigned int uiKey)
{
uiVertexKey = uiKey;
}
template<class T>
Vertex<T>::~Vertex()
{
}
}
#endif /* ADS_GRAPH_H */