-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphalterations.h
600 lines (526 loc) · 16 KB
/
graphalterations.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
#ifndef GRAPH_ALTERATIONS
#define GRAPH_ALTERATIONS 0
#include <vector>
#include <string>
#include <algorithm>
#include "graph.h"
#define SIZE_LIMIT 75
/*
NOTE: The 'void* arg' is used to pass custom arguments to functions. It
can be either hardcoded into parts of the algorithm or used when library
is used in conjuction with programs other than algernon. Algernon ALWAYS
passes a NULL to 'arg' if it is auto generating arguments.
For values of a,b,c, or d that must be boolean, if any number other than
0 or 1 is used, the function should leave the graph unchanged. This will
for better pattern recognition.
If any value is unreasonable, the graph should reamin unchanged. This will
allow the program to discard bad choices.
*/
void RemoveSpike (m3sgraph &g, m3sgraph &ng,
long long unsigned int a,
long long unsigned int b,
long long unsigned int c,
long long unsigned int d, void* arg)
{
/*
a - Length of Spikes removed
b - Remove all Spikes 1 for no, 2 for yes
c - Number of Spikes To remove(if b is 1)
d - Unused
*/
ng = g;
//Testing to make sure the inputs are sane
if(b > 2)
return;
if(c > 5)
return;
if(a > 3)
return;
if(b == 2 && c > 1)
return;
if(d > 1)
return;
ng.PopulateNeighborlist();
std::vector<NeighborData*>* neighbors = ng.GetNeighborListing();
int spikeArray[a];
int indexOfLastVertex, j;
bool completeSpike = false;
for(int i = 0; i < ng.GetSize(); i++)
{
//this loop goes through all the vertices and the if statement
// selects all of the pendants.
if((*neighbors)[i]->neighbors == 1)
{
indexOfLastVertex = i;
//This loop traverses the graph as long as the degree is two, until
// it reaches the spike length a.
for(j = 0; j < a - 1; j++)
{
spikeArray[j] = indexOfLastVertex;
//This if, elseif is needed so the program doesn't go back and forth between 2 vertices
// if the order of the elements in the list isn't consistent.
if((*neighbors)[indexOfLastVertex]->list[0] != indexOfLastVertex && (*neighbors)[(*neighbors)[indexOfLastVertex]->list[0]]->neighbors == 2)
{
indexOfLastVertex = (*neighbors)[indexOfLastVertex]->list[0];
}
else if((*neighbors)[(*neighbors)[indexOfLastVertex]->list[1]]->neighbors == 2)
{
indexOfLastVertex = (*neighbors)[indexOfLastVertex]->list[1];
}
else
{
continue;
}
//if this if statement is entered, then a spike has been found
// and is going to be deleted
if(j == a-2)
{
for(int k = 0; k < a-1; k++)
{
ng.RemoveVertex(spikeArray[k]);
}
}
}
}
}
//for 1:n, where n is spikelength
//check if degree of neighbor is 2
//if vertex was a spike of length n, then delete all of the nodes traversed
// store the vertices in an array length n
//
//do this for every vertex.
return;
}
void SpikeHighest (m3sgraph &g, m3sgraph &ng,
long long unsigned int a,
long long unsigned int b,
long long unsigned int c,
long long unsigned int d, void* arg)
{
/*
a - rank (i.e. 1st highest, 2nd highest,...)
b - number of such vertices to spike (next highest if no more a)
c - number of spikes per target - capped at size of graph or 5 wichever is higher
d - length of spike (i.e. p1, p2,p3) - capped at 5 i.e.
*/
ng = g;
//Testing to make sure the inputs are sane
//and limiting the length of spikes to 5
//JK - replacing all of the 5s with 3s
if(a>g.GetSize()||b>g.GetSize()||d>3)
return;
if(g.GetSize()>=3 && c > g.GetSize())
return;
if(g.GetSize()<3 && c > 3)
return;
std::vector<long long unsigned int> seq = g.GetDegSeq();
if(seq.size()==0)
return;
std::vector<long long unsigned int> seq2 = seq;
std::sort(seq2.begin(), seq2.end()); //lowest value on lowest index
long long unsigned int targetdeg = seq2[seq2.size()-1];
int count = 0; //vertices spiked - MAX b
int dchanges = 1;
int index;
for(index = seq2.size()-1; index>=0 && dchanges<a; index--)
{
if(seq2[index]!=targetdeg)
{
targetdeg = seq2[index];
dchanges++;
}
}
if(index!=seq2.size()-1)//index was changed
index++; //this compensates for the last index-- done before exiting
/*NOTE: The following are not adjusted so that
identical graphs are not created. If any argument
values are adjusted inside to function we risk
generating identical graphs and afffecting data.
i.e. if instead of exactly b, what ever is available
is used, there will be successful runs with highers b
values resulting in the same graphs.*/
if(dchanges<a)
return; //a is too high for this graph
if(b-1 > index)
return; //b is too high for this graph
int spikeindex;
while(count<b)
{
for(int i=0; i<seq.size()&&count<b; i++)
{
if(seq[i]==targetdeg)//snce new vertices are padded indices are still valid
{
for(long long unsigned int spikes = 0; spikes < c; spikes++)
{
spikeindex = g.GetSize() + (count*c*d) + spikes*d;
ng.AddVertex();
//std::cout << "-" << ng.GetG6() <<std::endl;
ng.SetEdge(spikeindex,i,true);
//std::cout << "--" << ng.GetG6() <<std::endl;
for(unsigned int len = 1; len < d; len++)
{
ng.AddVertex();
ng.SetEdge(spikeindex+len,spikeindex+len-1,true);
}
}
count++;
}
}
targetdeg--; //go to next highest degree
}
if(ng.GetSize()>SIZE_LIMIT)
ng = g;
return;
}
void ClonesToHighest (m3sgraph &g, m3sgraph &ng,
long long unsigned int a,
long long unsigned int b,
long long unsigned int c,
long long unsigned int d, void* arg)
{
//NOTE: It is simple and faster to copy SpikeHighest and just
//add to it. This implmentation is only a test.
//Similar to spiking except attaches clones of the orginal graph
//to the end of the spikes, on the vertex that corolates to the
//current spiked vertex
/*
a - rank (i.e. 1st highest, 2nd highest,...)
b - number of such vertices to spike (next highest if no more a)
c - number of spikes per target - capped at size of graph or 5 wichever is higher
d - length of spike (i.e. p1, p2,p3) - capped at 5 i.e.
*/
ng = g;
//JK - replacing all of the 5s with 3s
if(a>g.GetSize()||b>g.GetSize()||d>3)
return;
if(g.GetSize()>=3 && c > g.GetSize())
return;
if(g.GetSize()<3 && c > 3)
return;
std::vector<long long unsigned int> seq = g.GetDegSeq();
if(seq.size()==0)
return;
std::vector<long long unsigned int> seq2 = seq;
std::sort(seq2.begin(), seq2.end()); //lowest value on lowest index
long long unsigned int targetdeg = seq2[seq2.size()-1];
int count = 0; //vertices spiked - MAX b
int dchanges = 1;
int index;
for(index=seq2.size()-1; index>=0&&dchanges<a;index--)
{
if(seq2[index]!=targetdeg)
{
targetdeg = seq2[index];
dchanges++;
}
}
if(index!=seq2.size()-1)//index was changed
index++; //this compensates for the last index-- done before exiting
if(dchanges<a)
return; //a is too high for this graph
if(b-1 > index)
return; //b is too high for this graph
int spikeindex;
long long unsigned int gsize = g.GetSize();
while(count<b)
{
for(int i=0; i<seq.size()&&count<b; i++)
{
if(seq[i]==targetdeg)//since new vertices are padded indices are still valid
{
for(long long unsigned int spikes = 0; spikes < c; spikes++)
{
spikeindex = g.GetSize() + (count*c*(d+gsize-1)) + spikes*(d+gsize-1);
ng.AddVertex();
//std::cout << "-" << ng.GetG6() <<std::endl;
ng.SetEdge(spikeindex,i,true);
//std::cout << "--" << ng.GetG6() <<std::endl;
for(unsigned int len = 1; len < d; len++)
{
ng.AddVertex();
ng.SetEdge(spikeindex+len,spikeindex+len-1,true);
}
long long unsigned int tip = ng.GetSize()-1; //tip of spike
//////////////////////////////
bool isShort = false; //spike is short
if(tip == g.GetSize()+count*(d+g.GetSize()-1))
{
ng.SetEdge(i,tip,false);
isShort = true;
}
else
{
ng.SetEdge(tip-1,tip,false);
}
/////////////////////////////
for(long long unsigned int v = 0; v < gsize-1; v++)
{
ng.AddVertex();
}
/////////////////////////////
if(isShort)
{
ng.SetEdge(i,i+g.GetSize()+count*(d+g.GetSize()-1),true);
}
else
{
ng.SetEdge(tip-1,tip+i,true);
}
/////////////////////////////
for(long long unsigned int v = 0; v < gsize; v++)
{
for(long long unsigned int u = 0; u < v; u++)
{
if(g.GetEdge(v,u))
{
//ng.SetEdge(tip+((v+i)%gsize),tip+((i+u)%gsize),true);
ng.SetEdge(tip+v,tip+u,true);
}
}
}
}
count++;
}
}
targetdeg--; //go to next highest degree
}
if(ng.GetSize()>SIZE_LIMIT)
ng = g;
return;
}
void CliqueHighest (m3sgraph &g, m3sgraph &ng,
long long unsigned int a,
long long unsigned int b,
long long unsigned int c,
long long unsigned int d, void* arg)
{
/*
a - rank (i.e. 1st highest, 2nd highest,...)
b - number of such vertices (in addition to first) to form a clique with (next highest if no more a)
c - maximum number of cliques to form (currently unused)
d - ?????.
*/
ng = g;
if(a>g.GetSize()||b>g.GetSize()-1||d>1)
return; //imposible rank or impossible # of vertices to include or
//d is invalid so anything other than default value 1 is avoided
if(c>1)//currently unused
return;
std::vector<long long unsigned int> seq = g.GetDegSeq();
if(seq.size()==0)
return;
std::vector<long long unsigned int> seq2 = seq;
std::sort(seq2.begin(), seq2.end()); //lowest value on lowest index
long long unsigned int targetdeg = seq2[seq2.size()-1];
int count = 0; //vertices spiked - MAX b
int rank = 1; //first highest
int index;
for(index=seq2.size()-1; index>=0 && rank<a; index--)
{
if(seq2[index]!=targetdeg)
{
targetdeg = seq2[index];
rank++;
}
}
if(index!=seq2.size()-1)//index was changed
index++; //this compensates for the last index-- done before exiting
/*NOTE: The following are not adjusted so that
identical graphs are not created. If any argument
values are adjusted inside to function we risk
generating identical graphs and afffecting data.
i.e. if instead of exactly b, what ever is available
is used, there will be successful runs with highers b
values resulting in the same graphs.*/
if(rank<a)
return; //a is too high for this graph
if(b > index)
return; //b is too high for this graph
//index is the index of target deg in seq2
bool exists = false;
std::vector<int> targetlist;
for(int j=seq.size()-1; j>=0; j--) //adding first vertex
{
if(seq[j]==seq2[index])
{
targetlist.push_back(j);
index--;
j=-1;
}
}
while(targetlist.size()<=b)
{
for(int j=seq.size()-1; j>=0; j--)
{
if((seq[j]==seq2[index]))
{
for(int k=targetlist.size()-1; k>=0 && !exists; k--)
{
if(targetlist[k]==j)
exists = true;
}
if(!exists)
{
targetlist.push_back(j);
index--;
if(targetlist.size()==b+1)
j=-1;
}
exists = false;
}
}
}
for(int i=targetlist.size()-1; i>0; i--)
{
for(int j=i-1;j>=0;j--)
{
ng.SetEdge(i,j,true);
}
}
if(ng.GetSize()>SIZE_LIMIT)
ng = g;
return;
}
//********************************************
struct AlterationDef
{
std::string name;
void (*ptr) (m3sgraph &g, m3sgraph &ng,
long long unsigned int a,
long long unsigned int b,
long long unsigned int c,
long long unsigned int d, void* arg);
};
class AlterationLib
{
private:
std::vector<AlterationDef> list;
public:
AlterationLib();
int GetListSize();
std::string GetAlterationName(int index);
void DoAlteration(std::string name,
m3sgraph &graph,m3sgraph &ng,
long long unsigned int a, long long unsigned int b,
long long unsigned int c, long long unsigned int d,
void* ptr = NULL);
void DoAlteration(int s,
m3sgraph &graph,m3sgraph &ng,
long long unsigned int a, long long unsigned int b,
long long unsigned int c, long long unsigned int d,
void* ptr = NULL);
m3sgraph DoAlteration(std::string name,
m3sgraph &graph,
long long unsigned int a, long long unsigned int b,
long long unsigned int c, long long unsigned int d,
void* ptr = NULL);
m3sgraph DoAlteration(int s,
m3sgraph &graph,
long long unsigned int a, long long unsigned int b,
long long unsigned int c, long long unsigned int d,
void* ptr = NULL);
std::string DoAlteration(std::string name,
std::string g6,
long long unsigned int a, long long unsigned int b,
long long unsigned int c, long long unsigned int d,
void* ptr = NULL);
std::string DoAlteration(int s,
std::string g6,
long long unsigned int a, long long unsigned int b,
long long unsigned int c, long long unsigned int d,
void* ptr = NULL);
};
void AlterationLib::DoAlteration(std::string name,
m3sgraph &graph,m3sgraph &ng,
long long unsigned int a, long long unsigned int b,
long long unsigned int c, long long unsigned int d,
void* ptr)
{
for(int i=list.size()-1; i>=0; i--)
{
if(list[i].name==name)
{
list[i].ptr(graph,ng,a,b,c,d,ptr);
return;
}
}
}
void AlterationLib::DoAlteration(int s,
m3sgraph &graph,m3sgraph &ng,
long long unsigned int a, long long unsigned int b,
long long unsigned int c, long long unsigned int d,
void* ptr)
{
list[s].ptr(graph,ng,a,b,c,d,ptr);
}
m3sgraph AlterationLib::DoAlteration(std::string name,
m3sgraph &graph,
long long unsigned int a, long long unsigned int b,
long long unsigned int c, long long unsigned int d,
void* ptr)
{
m3sgraph ng;
DoAlteration(name,graph,ng,a,b,c,d,ptr);
return ng;
}
m3sgraph AlterationLib::DoAlteration(int s,
m3sgraph &graph,
long long unsigned int a, long long unsigned int b,
long long unsigned int c, long long unsigned int d,
void* ptr)
{
m3sgraph ng;
DoAlteration(s,graph,ng,a,b,c,d,ptr);
return ng;
}
std::string AlterationLib::DoAlteration(std::string name,
std::string g6,
long long unsigned int a, long long unsigned int b,
long long unsigned int c, long long unsigned int d,
void* ptr )
{
m3sgraph graph,ng;
graph.SetGraph(g6.c_str());
DoAlteration(name,graph,ng,a,b,c,d,ptr);
return ng.GetG6();
}
std::string AlterationLib::DoAlteration(int s,
std::string g6,
long long unsigned int a, long long unsigned int b,
long long unsigned int c, long long unsigned int d,
void* ptr)
{
m3sgraph graph,ng;
graph.SetGraph(g6.c_str());
DoAlteration(s,graph,ng,a,b,c,d,ptr);
return ng.GetG6();
}
int AlterationLib::GetListSize()
{
return list.size();
}
std::string AlterationLib::GetAlterationName(int index)
{
return list[index].name;
}
//********************************************
// Add pointer to the library
// at the end of the constructor below
//********************************************
AlterationLib::AlterationLib()
{
AlterationDef tmp;
tmp.name = "SpikeHighest";
tmp.ptr = SpikeHighest;
list.push_back(tmp);
tmp.name = "ClonesToHighest";
tmp.ptr = ClonesToHighest;
list.push_back(tmp);
tmp.name = "CliqueHighest";
tmp.ptr = CliqueHighest;
list.push_back(tmp);
tmp.name = "RemoveSpike";
tmp.ptr = RemoveSpike;
list.push_back(tmp);
}
#endif