-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBCVertexGraph.m
378 lines (249 loc) · 8 KB
/
BCVertexGraph.m
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
//
// BCVertexGraph.m
// Xynk
//
// Created by Tom Houpt on 13/11/8.
//
//
#import "BCVertexGraph.h"
@implementation BCVertex
-(BOOL)isEqualToVertex:(BCVertex *)otherVertex; {
// NOTE: to make this really flexible,
// should we check if otherVertex is same class as self?
return [self isEqualToNumber:otherVertex];
}
@end
@implementation BCEdge
@synthesize vertex1;
@synthesize vertex2;
-(id)initWithStartVertex:(BCVertex *)s andEndVertex:(BCVertex *)e; {
self = [super init];
if (self) {
vertex1 = s;
vertex2 = e;
}
return self;
}
-(BOOL)isEqualToEdge:(BCEdge *)otherEdge; {
return
(
[self containsVertex:[otherEdge vertex1]] && [self containsVertex:[otherEdge vertex2]]
);
}
-(BOOL)containsVertex:(BCVertex *)v; {
if ([[self vertex1] isEqualToVertex:v] || [[self vertex2] isEqualToVertex:v]) { return YES; }
return NO;
}
-(NSString *)stringValue; {
return [NSString stringWithFormat:@"[%@,%@]", [vertex1 stringValue], [vertex2 stringValue]];
}
@end
@implementation BCVertexGraph
/** to get clusters of non-different groups
1. add vertices to graph
2. make graph complete
3. remove significant edges
4. get completeSubGraphs
5. assign same letter to each vertex in each subgraph
*/
@synthesize vertices;
@synthesize edges;
-(id)init; {
self = [super init];
if (self) {
vertices = [[NSMutableSet alloc] init];
edges = [[NSMutableSet alloc] init];
}
return self;
}
-(void)addVertex:(BCVertex *)v; {
for (BCVertex *vertex in vertices) {
if ([vertex isEqualToNumber:v]) {
return;
}
}
[vertices addObject:v];
}
-(void)addVertices:(NSSet *)newVertices; {
for (BCVertex *v in newVertices) {
[self addVertex:v];
}
}
-(void)removeVertex:(BCVertex *)v; {
for (BCVertex *vertex in vertices) {
if ([vertex isEqualToVertex:v]) {
[vertices removeObject:vertex];
for (BCEdge *edge in edges) {
if ([edge containsVertex:vertex]) {
[edges removeObject:edge];
}
}
}
}
}
-(void)removeVertices:(NSSet *)subVertices; {
for (BCVertex *v in subVertices) {
[self removeVertex:v];
}
}
-(void)addEdge:(BCEdge *)e; {
for (BCEdge *edge in edges) {
if ([edge isEqualToEdge:e]) {
return;
}
}
[edges addObject:e];
[vertices addObject: [e vertex1]];
[vertices addObject: [e vertex2]];
}
-(void)addEdges:(NSSet *)newEdges; {
for (BCEdge *e in newEdges) {
[self addEdge:e];
}
}
-(void)removeEdge:(BCEdge *)e; {
for (BCEdge *edge in edges) {
if ([edge isEqualToEdge:e]) {
[edges removeObject:edge];
}
}
}
-(void)removeEdges:(NSSet *)subsetEdges; {
for (BCEdge *e in subsetEdges) {
[self removeEdge:e];
}
}
-(void)makeVerticesOfCount:(NSInteger)count; {
// discard any current vertices
vertices = nil;
vertices = [[NSMutableSet alloc] init];
// discard any current edges
edges = nil;
edges = [[NSMutableSet alloc] init];
for (NSInteger i=0; i<count;i++) {
BCVertex *v = [[BCVertex alloc ] initWithInteger:i];
[self addVertex:v];
}
}
// add all possible edges between the current vertices to the graph
// (deletes and replaces any pre-existing edges)
-(void)makeCompleteEdges; {
// discard any current edges
edges = nil;
edges = [[NSMutableSet alloc] init];
// iterate through all the vertices, connecting them all
// we're lazy, so we'll create redundant edges [a,b] and [b,a]
// the addEdge: method will prevent multiple edges from being added
for (BCVertex *v1 in vertices) {
for (BCVertex *v2 in vertices) {
BCEdge *e = [[BCEdge alloc] initWithStartVertex:v1 andEndVertex:v2];
[self addEdge:e];
}
}
}
-(void)removeUnconnectedVertices; {
// remove all vertices that are not start or end points of edges in the graph
for (BCVertex *v in vertices) {
BOOL vertexUsedByEdge = NO;
for (BCEdge *e in edges) {
if ( [e containsVertex:v] ) {
vertexUsedByEdge = YES;
break;
}
}
if (!vertexUsedByEdge) {
[vertices removeObject:v]; // removeVertex will also check
}
}
}
-(NSUInteger)size; {
return [edges count];
}
#if 0
#error Unimplmented Methods ifdef-ed out to quiet warnings
// add all edges & vertices from other graph (if not already in self)
-(BCVertexGraph *)unionWithGraph:(BCVertexGraph *)otherGraph; {
}
// remove edges from self that are present in otherGraph
-(BCVertexGraph *)removeGraph:(BCVertexGraph *)otherGraph; {
}
// find edges and vertices that appear in both self and otherGraph
-(BCVertexGraph *)intersectWithGraph:(BCVertexGraph *)otherGraph; {
}
-(NSSet *)neighborsOfVertex:(BCVertex *)v; {
NSMutableSet *neighborhood = [[NSMutableSet alloc] init];
for (BCEdge *e in edges) {
if ([e containsVertex:v]){
BCVertex *neighbor;
if (![v isEqualToVertex:[e vertex1]]) {
neighbor = [e vertex1];
}
else {
neighbor = [e vertex1];
}
BOOL vertexInSet = NO;
for (BCVertex *n in neighborhood) {
if ([neighbor isEqualToVertex:n]) {
vertexInSet = YES;
}
}
if (!vertexInSet) {
[neighborhood addObject:neighbor];
}
}
}
return neighborhood;
}
/** given the current edges, return an array of BCVertexGraphs
which is the set of cliques or subgraphs, each of which is maximally complete
(i.e. each vertex in the subgraph is connected to all other vertices in the subgraph)
note that isolated vertices should be returned as a completeGraph
This is the problem of listing all maximal cliques:
http://en.wikipedia.org/wiki/Clique_problem#Listing_all_maximal_cliques
Sounds like the Bron-Kerbosch algorithm is the best choice, especially when dealing with a small number of vertices
http://en.wikipedia.org/wiki/Bron–Kerbosch_algorithm
I think this would be easier to code in lisp...
*/
-(NSArray *)maximalCliques; {
/* setting R and X to be the empty set and P to be the vertex set of the graph.
BronKerbosch1(R,P,X):
if P and X are both empty:
report R as a maximal clique
for each vertex v in P:
BronKerbosch1(R ⋃ {v}, P ⋂ N(v), X ⋂ N(v))
P := P \ {v}
X := X ⋃ {v}
*/
}
#endif
-(NSString *)verticesString; {
NSString *stringValue = @"{ ";
if (0 == [vertices count]) {
stringValue = [stringValue stringByAppendingString:@"∅ "];
}
else {
for (BCVertex *v in vertices) {
stringValue = [stringValue stringByAppendingString:[v stringValue] ];
stringValue = [stringValue stringByAppendingString:@", "];
}
// NOTE: truncate final string by 2 characters (@", ")
}
stringValue = [stringValue stringByAppendingString:@"}"];
return stringValue;
}
-(NSString *)edgesString; {
NSString *stringValue = @"{ ";
if (0 == [edges count]) {
stringValue = [stringValue stringByAppendingString:@"∅ "];
}
else {
for (BCEdge *e in edges) {
stringValue = [stringValue stringByAppendingString:[e stringValue] ];
stringValue = [stringValue stringByAppendingString:@", "];
}
// NOTE: truncate final string by 2 characters (@", ")
}
stringValue = [stringValue stringByAppendingString:@"}"];
return stringValue;
}
@end