forked from Smorodov/Multitarget-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmygraph.cpp
More file actions
393 lines (335 loc) · 7.66 KB
/
mygraph.cpp
File metadata and controls
393 lines (335 loc) · 7.66 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
// $Id: mygraph.cpp,v 1.6 2005/03/16 09:51:26 rdmp1c Exp $
#include "mygraph.h"
#ifdef __GNUC__
#if __GNUC__ == 3
#include <iterator>
#endif
#endif
#include <sstream>
#include <cstring>
void MyGraph::load_edge_info_handler (edge e, GML_pair* list)
{
if (list)
{
// Iterate over the list of GML_pair values
struct GML_pair *p = list;
while (p)
{
switch (p->kind)
{
case GML_STRING:
store_edge_string (e, p->key, p->value.str);
break;
case GML_INT:
store_edge_integer (e, p->key, p->value.integer);
break;
case GML_DOUBLE:
store_edge_double (e, p->key, p->value.floating);
break;
default:
break;
}
p = p->next;
}
}
}
void MyGraph::store_edge_double (edge /*e*/, char * /*key*/, double /*value*/)
{
}
void MyGraph::store_edge_integer (edge e, char *key, int value)
{
if (strcmp (key, "weight") == 0)
{
weight[e] = value;
}
}
void MyGraph::store_edge_string (edge e, char *key, char *value)
{
if (strcmp (key, "label") == 0)
{
if (labels_as_weights)
{
// Treat edge label as a weight
std::istringstream iss(value);
iss >> weight[e];
}
else
{
// Store edge label as a label
edge_label[e] = value;
}
}
}
void MyGraph::load_node_info_handler(node n, GML_pair* list )
{
if (list)
{
// Iterate over the list of GML_pair values
struct GML_pair *p = list;
while (p)
{
switch (p->kind)
{
case GML_STRING:
store_node_string (n, p->key, p->value.str);
break;
case GML_INT:
store_node_integer (n, p->key, p->value.integer);
break;
case GML_DOUBLE:
store_node_double (n, p->key, p->value.floating);
break;
default:
break;
}
p = p->next;
}
}
}
void MyGraph::store_node_double (node /*n*/, char * /*key*/, double /*value*/)
{
}
void MyGraph::store_node_integer (node /*n*/, char * /*key*/, int /*value*/)
{
}
void MyGraph::store_node_string (node n, char *key, char *value)
{
if (strcmp (key, "label") == 0)
{
label[n] = value;
}
}
//------------------------------------------------------------------------------
void MyGraph::save_edge_info_handler(std::ostream *os, edge e) const
{
graph::save_edge_info_handler (os, e);
*os << "weight " << weight[e] << std::endl;
*os << "label \"" << edge_label[e] << "\"" << std::endl;
// Line width 1 pt
*os << "graphics [" << std::endl;
*os << "width 1.0" << std::endl;
*os << "]" << std::endl;
// Use standard Postscript font
*os << "LabelGraphics [" << std::endl;
*os << "type \"text\"" << std::endl;
*os << "font \"Helvetica\"" << std::endl;
*os << "]" << std::endl;
}
//------------------------------------------------------------------------------
void MyGraph::save_node_info_handler(std::ostream *os, node n) const
{
graph::save_node_info_handler (os, n);
if (label[n] != "")
*os << "label \"" << label[n] << "\"" << std::endl;
// Use standard Postscript font
*os << "LabelGraphics [" << std::endl;
*os << "type \"text\"" << std::endl;
*os << "font \"Helvetica\"" << std::endl;
*os << "]" << std::endl;
}
//------------------------------------------------------------------------------
void MyGraph::save_dot (char *fname, bool weights)
{
std::ofstream f (fname);
save_dot (f, weights);
f.close ();
}
//------------------------------------------------------------------------------
void MyGraph::save_dot(std::ostream &f, bool weights)
{
node_map <int> index;
graph::node_iterator nit = nodes_begin();
graph::node_iterator nend = nodes_end();
int count = 0;
while (nit != nend)
{
index[*nit] = count++;
nit++;
}
if (is_directed())
f << "digraph";
else
f << "graph";
f << " G {" << std::endl;
// Try and make the graph look nice
f << " node [width=.2,height=.2,fontsize=10];" << std::endl;
f << " edge [fontsize=10,len=2];" << std::endl;
// Write node labels
nit = nodes_begin();
while (nit != nend)
{
f << " " << index[*nit] << " [label=\"" << label[*nit] << "\"";
if (node_colour[*nit] != "white")
{
f << ", color=" << node_colour[*nit] << ", style=filled";
}
f << "];" << std::endl;
nit++;
}
// Write edges
graph::edge_iterator it = edges_begin();
graph::edge_iterator end = edges_end();
while (it != end)
{
f << " " << index[it->source()];
if (is_directed())
f << " -> ";
else
f << " -- ";
f << index[it->target()];
f << " [";
if (weights)
{
f << "label=\"" << weight[*it] << "\", ";
}
else
{
std::string s = edge_label[*it];
if (s != "")
f << "label=\"" << s << "\", ";
}
f << " color=" << edge_colour[*it] << "];" << std::endl;
it++;
}
f << "}" << std::endl;
}
//------------------------------------------------------------------------------
bool MyGraph::edge_exists (node n1, node n2)
{
bool result = false;
if (is_undirected ())
{
// Visit all edges adjacent to n1 and ask whether any
// is connect to n2
node::adj_edges_iterator eit = n1.adj_edges_begin();
node::adj_edges_iterator eend = n1.adj_edges_end();
node found = n1;
while ((found == n1) && (eit != eend))
{
if (n1.opposite (*eit) == n2)
found = n2;
else
eit++;
}
if (found == n2)
{
result = true;
}
}
else
{
// Visit all edges that have n1 as their source and ask
// whether any is connected to n2
node::out_edges_iterator eit = n1.out_edges_begin();
node::out_edges_iterator eend = n1.out_edges_end();
node found = n1;
while ((found == n1) && (eit != eend))
{
if (n1.opposite (*eit) == n2)
found = n2;
else
eit++;
}
if (found == n2)
{
result = true;
}
}
return result;
}
//------------------------------------------------------------------------------
void MyGraph::delete_edge (node n1, node n2)
{
edge e;
bool exists = false;
if (is_undirected ())
{
// Visit all edges adjacent to n1 and ask whether any
// is connect to n2
node::adj_edges_iterator eit = n1.adj_edges_begin();
node::adj_edges_iterator eend = n1.adj_edges_end();
node found = n1;
while ((found == n1) && (eit != eend))
{
if (n1.opposite (*eit) == n2)
{
found = n2;
e = *eit;
}
else
eit++;
}
exists = (found == n2);
}
else
{
// Visit all edges that have n1 as their source and ask
// whether any is connected to n2
node::out_edges_iterator eit = n1.out_edges_begin();
node::out_edges_iterator eend = n1.out_edges_end();
node found = n1;
while ((found == n1) && (eit != eend))
{
if (n1.opposite (*eit) == n2)
{
found = n2;
e = *eit;
}
else
eit++;
}
exists = (found == n2);
}
if (exists)
del_edge(e);
}
double MyGraph::node_cliqueishness (node &n)
{
double c = 1.0;
int numneighbours = n.degree();
int possconnections = numneighbours * (numneighbours - 1) / 2;
int actualconnections = 0;
if (possconnections > 0)
{
// Build list of all nodes adjacent to n (n's neighbours)
nodes_t neighbours;
node::adj_nodes_iterator nit = n.adj_nodes_begin ();
node::adj_nodes_iterator nend = n.adj_nodes_end ();
while (nit != nend)
{
node ne = (*nit);
neighbours.push_back (ne);
nit++;
}
// Count number of edges between neighbours
nodes_t::iterator i = neighbours.begin();
nodes_t::iterator iend = neighbours.end();
while (i != iend)
{
nodes_t::iterator j = i;
j++;
while (j != iend)
{
if (edge_exists (*i, *j))
actualconnections++;
j++;
}
i++;
}
c = (double) actualconnections / (double) possconnections;
}
return c;
}
double MyGraph::cliqueishness ()
{
double sum = 0.0;
graph::node_iterator nit = nodes_begin();
graph::node_iterator nend = nodes_end();
while (nit != nend)
{
node n = (*nit);
sum += node_cliqueishness (n);
nit++;
}
return ( sum / (double)number_of_nodes() );
}