/
graph3.cpp
992 lines (859 loc) · 33.1 KB
/
graph3.cpp
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
#include "node.h"
#include "graph.h"
#include "identity.h"
#include <vector>
#include <list>
#include <iostream>
#include <ctime>
#include <cstdlib>
//---
//Graph function definitions, part III
//---
//Reduce and return identity
Identity Graph::reduceIdent(std::list<Identity>::iterator pid)
{
std::list<Node>::iterator pleftNode, prightNode, pbaseNode;
pleftNode = pid->getLeft();
prightNode = pid->getRight();
pbaseNode = pid->getBase();
std::vector<short> leftWord = pleftNode->getElemName();
std::vector<short> baseWord = pbaseNode->getElemName();
bool letterSwitch = pleftNode->lastLetterIsX(); //x <-> true, y <-> false
bool endPath = false; //detect if right reduction has reached an empty product
while (leftWord.size() != baseWord.size()) //loop over exponents until last exp. of baseWord
{
while (leftWord[leftWord.size() - 1] > 0)
{
//check right path; if exists, move left and right back one product
if (letterSwitch) //for letter x
{
if (!prightNode->isXinEmpty())
{
prightNode = prightNode->getXin();
pleftNode = pleftNode->getXin();
/*
///Test: use search instead of pointers
std::vector<short> searchWord = leftWord;
searchWord[leftWord.size() - 1] = leftWord[leftWord.size() - 1] - 1;
if (searchWord[leftWord.size() - 1] == 0)
searchWord.pop_back();
pleftNode = searchNodes(&searchWord);
///Test
*/
}
else
{
endPath = true;
break;
}
}
else //for letter y
{
if (!prightNode->isYinEmpty())
{
prightNode = prightNode->getYin();
pleftNode = pleftNode->getYin();
/*
///Test: use search instead of pointers
std::vector<short> searchWord = leftWord;
searchWord[leftWord.size() - 1] = leftWord[leftWord.size() - 1] - 1;
if (searchWord[leftWord.size() - 1] == 0)
searchWord.pop_back();
pleftNode = searchNodes(&searchWord);
///Test
*/
}
else
{
endPath = true;
break;
}
}
//decrement last exponent
leftWord[leftWord.size() - 1] = leftWord[leftWord.size() - 1] - 1;
}
//either loop broken prematurely by end of path...
if (endPath)
break;
//...or exponent is now 0, so move to next exponent
leftWord.pop_back();
letterSwitch = !letterSwitch;
}
//if not at end of path, continue reducing
if (!endPath)
{
while (baseWord[baseWord.size() - 1] != leftWord[leftWord.size() - 1])
{
if (letterSwitch)
{
if (!prightNode->isXinEmpty())
{
prightNode = prightNode->getXin();
pleftNode = pleftNode->getXin();
/*
///Test: use search instead of pointers
std::vector<short> searchWord = leftWord;
searchWord[leftWord.size() - 1] = leftWord[leftWord.size() - 1] - 1;
if (searchWord[leftWord.size() - 1] == 0)
searchWord.pop_back();
pleftNode = searchNodes(&searchWord);
///Test
*/
}
else
{
endPath = true;
break;
}
}
else
{
if (!prightNode->isYinEmpty())
{
prightNode = prightNode->getYin();
pleftNode = pleftNode->getYin();
/*
///Test: use search instead of pointers
std::vector<short> searchWord = leftWord;
searchWord[leftWord.size() - 1] = leftWord[leftWord.size() - 1] - 1;
if (searchWord[leftWord.size() - 1] == 0)
searchWord.pop_back();
pleftNode = searchNodes(&searchWord);
///Test
*/
}
else
{
endPath = true;
break;
}
}
leftWord[leftWord.size() - 1] = leftWord[leftWord.size() - 1] - 1;
}
}
//make and return reduced identity;
Identity reducedId(pleftNode, prightNode, pbaseNode);
/*
std::cout << "\nIdentity ";
pid->printIdent();
std::cout << "\nreduced to ";
reducedId.printIdent();
std::cout << "\n";
*/
return reducedId;
}
//Set nodes in path to OPEN
void Graph::preservePath(std::list<Node>::iterator pbaseNode, std::list<Node>::iterator ptargetNode)
{
//make sure iterators are valid
std::list<Node>::iterator pnull = std::list<Node>::iterator(NULL);
if (pbaseNode == pnull || ptargetNode == pnull)
{
//std::cout << "\nNULL ITERATORS PASSED TO preservePath()\n";
return;
}
std::vector<short> baseWord = pbaseNode->getElemName();
std::vector<short> targetWord = ptargetNode->getElemName();
//target word must be longer than or equal length as base word
if (targetWord.size() < baseWord.size())
{
//std::cout << "\nPRESERVE PATH BAD RETURN1\n";
return;
}
//should never touch the identity; this is a special case which is handled in initial step
if (baseWord[0] == 0 || targetWord[0] == 0)
{
//std::cout << "\nPRESERVE PATH BAD RETURN2\n";
return;
}
//if words are identical there's nothing to do
if (baseWord == targetWord)
{
//std::cout << "\nPRESERVE PATH BAD RETURN3\n";
return;
}
//return;
//At this point we know the nodes exist, their words have appropriate sizes, they're not the identity,
//and they're not equal. But we could still have the problem that the base word is not contained
//in the leftmost portion of the target word, so we check for that.
bool baseContained = true;
int i;
for (i = 0; i < baseWord.size() - 1; i++) //up until the last letter, each exponent identical
if (baseWord[i] != targetWord[i])
baseContained = false;
if (baseWord[i] > targetWord[i]) //on the last letter, the exponent in base may be less or equal
baseContained = false;
if (baseContained == false)
{
std::cout << "\nPRESERVE PATH not contained:\n";
pbaseNode->printWord();
std::cout << "\n";
ptargetNode->printWord();
std::cout << "\n";
return;
}
//nodes have been checked
std::list<Node>::iterator psetNode = ptargetNode;
while (targetWord.size() != baseWord.size()) //stop at last exponent of baseWord
{
int i = targetWord.size() - 1; //always index of last exponent in targetWord
int exp = targetWord[i]; //last exponent on targetWord
while (exp > 0)
{
targetWord[i] = exp;
psetNode = searchNodes(&targetWord);
if (psetNode == std::list<Node>::iterator(NULL))
{
std::cout << "\nAttempt to set node via null iterator in preservePath()\n";
return;
}
if (psetNode->getNodeType() != Node::TEMP && psetNode != ptargetNode)
{
//it's okay to hit a preserved node, this means the nodes before this one will also
//be preserved
return;
}
psetNode->setNodeType(Node::OPEN);
exp--;
}
targetWord.pop_back(); //remove exponent just cleared
}
int d = targetWord.size() - 1;
if (targetWord[d] == baseWord[d])
{
if(pbaseNode->isOpen())
psetNode->setNodeType(Node::OPEN);
else psetNode->setNodeType(Node::CLOSED);
return;
}
int exp = targetWord[d]; //last exponent on targetWord
while (exp > baseWord[d])
{
targetWord[d] = exp;
psetNode = searchNodes(&targetWord);
if (psetNode == std::list<Node>::iterator(NULL))
{
std::cout << "\nAttempt to set node via null iterator in preservePath()\n";
return;
}
if (psetNode->getNodeType() != Node::TEMP)
{
//again this is not a problem
return;
}
psetNode->setNodeType(Node::OPEN);
}
if(pbaseNode->isOpen())
psetNode->setNodeType(Node::OPEN);
else psetNode->setNodeType(Node::CLOSED);
return;
}
//Simply deletes all temp nodes
void Graph::deleteTempNodes()
{
std::list<Node>::iterator dummyIter;
for (std::list<Node>::iterator pln = nodeList.begin();
pln != nodeList.end(); pln++)
if (pln->getNodeType() == Node::TEMP)
{
///NOTE: possible problem here if first node in nodeList is TEMP but that should
///never happen since first node will always be the identity
dummyIter = pln;
dummyIter--;
removeNode(pln);
pln = dummyIter;
}
}
//Copy edges from first node to second
void Graph::copyNodeEdges(std::list<Node>::iterator pn1, std::list<Node>::iterator pn2)
{
std::list< std::list<Node>::iterator >::iterator pnlist;
for (pnlist = pn1->getXinBegin(); pnlist != pn1->getXinEnd(); pnlist++)
setXedge((*pnlist), pn2);
for (pnlist = pn1->getYinBegin(); pnlist != pn1->getYinEnd(); pnlist++)
setYedge((*pnlist), pn2);
for (pnlist = pn1->getXoutBegin(); pnlist != pn1->getXoutEnd(); pnlist++)
setXedge(pn2, (*pnlist));
for (pnlist = pn1->getYoutBegin(); pnlist != pn1->getYoutEnd(); pnlist++)
setYedge(pn2, (*pnlist));
}
//Clear dup. nodes pointing to passed in node, return true if such dup.s found
bool Graph::clearDuplicateNodes(std::list<Node>::iterator pn)
{
if (pn->getSizeXin() > 1)
{
//choose the node you want to keep by finding the one with the least letters in its word
std::list<Node>::iterator preservedNode = pn->getXin();
int presSumExp = 0;
std::vector<short> presWord = preservedNode->getElemName();
for (int i = 0; i < presWord.size(); i++)
presSumExp += presWord[i];
std::list< std::list<Node>::iterator >::iterator plnIter;
for (plnIter = pn->getXinBegin(); plnIter != pn->getXinEnd(); plnIter++)
{
std::vector<short> word = (*plnIter)->getElemName();
int sumExponents = 0; //counts number of letters total in the word
for (int i = 0; i < word.size(); i++)
sumExponents += word[i];
//in this case the node you have is the identity, which you definitely want to keep
if (sumExponents == 0)
{
preservedNode = *plnIter;
break;
}
//if word has less letters than the current preserved node, set it as preserved
if (sumExponents < presSumExp)
{
preservedNode = *plnIter;
presSumExp = sumExponents;
}
}
//okay so at this point the node which is to be kept has been chosen. now copy the edges from the
//trash nodes to the preserved node and delete the trash nodes
plnIter = pn->getXinBegin();
while (plnIter != pn->getXinEnd())
{
//skip over the preserved node
if ((*plnIter) == preservedNode)
{
plnIter++;
continue;
}
//copy edges, remove node, set the iterator to the previous node
std::list< std::list<Node>::iterator >::iterator prevNodeIter = plnIter;
prevNodeIter--;
copyNodeEdges(*plnIter, preservedNode);
removeNode(*plnIter);
plnIter = prevNodeIter;
}
return true;
}
//this is symmetrical to the above block
if (pn->getSizeYin() > 1)
{
std::list<Node>::iterator preservedNode = pn->getYin();
int presSumExp = 0;
std::vector<short> presWord = preservedNode->getElemName();
for (int i = 0; i < presWord.size(); i++)
presSumExp += presWord[i];
std::list< std::list<Node>::iterator >::iterator plnIter;
for (plnIter = pn->getYinBegin(); plnIter != pn->getYinEnd(); plnIter++)
{
std::vector<short> word = (*plnIter)->getElemName();
int sumExponents = 0;
for (int i = 0; i < word.size(); i++)
sumExponents += word[i];
if (sumExponents == 0)
{
preservedNode = *plnIter;
break;
}
if (sumExponents < presSumExp)
{
preservedNode = *plnIter;
presSumExp = sumExponents;
}
}
plnIter = pn->getYinBegin();
while (plnIter != pn->getYinEnd())
{
if ((*plnIter) == preservedNode)
{
plnIter++;
continue;
}
std::list< std::list<Node>::iterator >::iterator prevNodeIter = plnIter;
prevNodeIter--;
copyNodeEdges(*plnIter, preservedNode);
removeNode(*plnIter);
plnIter = prevNodeIter;
}
return true;
}
if (pn->getSizeXout() > 1)
{
std::list<Node>::iterator preservedNode = pn->getXout();
int presSumExp = 0;
std::vector<short> presWord = preservedNode->getElemName();
for (int i = 0; i < presWord.size(); i++)
presSumExp += presWord[i];
std::list< std::list<Node>::iterator >::iterator plnIter;
for (plnIter = pn->getXoutBegin(); plnIter != pn->getXoutEnd(); plnIter++)
{
std::vector<short> word = (*plnIter)->getElemName();
int sumExponents = 0;
for (int i = 0; i < word.size(); i++)
sumExponents += word[i];
if (sumExponents == 0)
{
preservedNode = *plnIter;
break;
}
if (sumExponents < presSumExp)
{
preservedNode = *plnIter;
presSumExp = sumExponents;
}
}
plnIter = pn->getXoutBegin();
while (plnIter != pn->getXoutEnd())
{
if ((*plnIter) == preservedNode)
{
plnIter++;
continue;
}
std::list< std::list<Node>::iterator >::iterator prevNodeIter = plnIter;
prevNodeIter--;
copyNodeEdges(*plnIter, preservedNode);
removeNode(*plnIter);
plnIter = prevNodeIter;
}
return true;
}
if (pn->getSizeYout() > 1)
{
std::list<Node>::iterator preservedNode = pn->getYout();
int presSumExp = 0;
std::vector<short> presWord = preservedNode->getElemName();
for (int i = 0; i < presWord.size(); i++)
presSumExp += presWord[i];
std::list< std::list<Node>::iterator >::iterator plnIter;
for (plnIter = pn->getYoutBegin(); plnIter != pn->getYoutEnd(); plnIter++)
{
std::vector<short> word = (*plnIter)->getElemName();
int sumExponents = 0;
for (int i = 0; i < word.size(); i++)
sumExponents += word[i];
if (sumExponents == 0)
{
preservedNode = *plnIter;
break;
}
if (sumExponents < presSumExp)
{
preservedNode = *plnIter;
presSumExp = sumExponents;
}
}
plnIter = pn->getYoutBegin();
while (plnIter != pn->getYoutEnd())
{
if ((*plnIter) == preservedNode)
{
plnIter++;
continue;
}
std::list< std::list<Node>::iterator >::iterator prevNodeIter = plnIter;
prevNodeIter--;
copyNodeEdges(*plnIter, preservedNode);
removeNode(*plnIter);
plnIter = prevNodeIter;
}
return true;
}
//no duplicates found if you get to this point
return false;
}
//Similar to clearDuplicateNodes(); inserts duplicate nodes into list instead of deleting them
void Graph::insertDuplicateNodes(std::list<Node>::iterator pn, std::list< std::list<Node>::iterator >& deleteNodes)
{
//If there are multiple nodes in the xin list then all these nodes are identical as group elements,
//so function will choose one to preserve and insert the rest into the given list.
if (pn->getSizeXin() > 1)
{
//Select the node with the least letters to preserve.
std::list<Node>::iterator preservedNode = pn->getXin();
int presSumExp = 0;
std::vector<short> presWord = preservedNode->getElemName();
for (int i = 0; i < presWord.size(); i++)
presSumExp += presWord[i];
std::list< std::list<Node>::iterator >::iterator plnIter;
for (plnIter = pn->getXinBegin(); plnIter != pn->getXinEnd(); plnIter++)
{
std::vector<short> word = (*plnIter)->getElemName();
int sumExponents = 0; //counts number of letters total in the word
for (int i = 0; i < word.size(); i++)
sumExponents += word[i];
//In this case the node you have is the identity, which you definitely want to keep
if (sumExponents == 0)
{
preservedNode = *plnIter;
break;
}
//If word has less letters than the current preserved node, set it as preserved
if (sumExponents < presSumExp)
{
preservedNode = *plnIter;
presSumExp = sumExponents;
}
}
//At this point the node which is to be preserved has been chosen. Now copy the edges from the
//trash nodes to the preserved node and insert the trash nodes into the list.
plnIter = pn->getXinBegin();
while (plnIter != pn->getXinEnd())
{
//Ignore the preserved node
if ((*plnIter) == preservedNode)
{
plnIter++;
continue;
}
//Copy edges and add node to list
copyNodeEdges(*plnIter, preservedNode);
deleteNodes.push_back(*plnIter);
plnIter++;
}
}
//this is symmetrical to the above block
if (pn->getSizeYin() > 1)
{
std::list<Node>::iterator preservedNode = pn->getYin();
int presSumExp = 0;
std::vector<short> presWord = preservedNode->getElemName();
for (int i = 0; i < presWord.size(); i++)
presSumExp += presWord[i];
std::list< std::list<Node>::iterator >::iterator plnIter;
for (plnIter = pn->getYinBegin(); plnIter != pn->getYinEnd(); plnIter++)
{
std::vector<short> word = (*plnIter)->getElemName();
int sumExponents = 0;
for (int i = 0; i < word.size(); i++)
sumExponents += word[i];
if (sumExponents == 0)
{
preservedNode = *plnIter;
break;
}
if (sumExponents < presSumExp)
{
preservedNode = *plnIter;
presSumExp = sumExponents;
}
}
plnIter = pn->getYinBegin();
while (plnIter != pn->getYinEnd())
{
if ((*plnIter) == preservedNode)
{
plnIter++;
continue;
}
copyNodeEdges(*plnIter, preservedNode);
deleteNodes.push_back(*plnIter);
plnIter++;
}
}
if (pn->getSizeXout() > 1)
{
std::list<Node>::iterator preservedNode = pn->getXout();
int presSumExp = 0;
std::vector<short> presWord = preservedNode->getElemName();
for (int i = 0; i < presWord.size(); i++)
presSumExp += presWord[i];
std::list< std::list<Node>::iterator >::iterator plnIter;
for (plnIter = pn->getXoutBegin(); plnIter != pn->getXoutEnd(); plnIter++)
{
std::vector<short> word = (*plnIter)->getElemName();
int sumExponents = 0;
for (int i = 0; i < word.size(); i++)
sumExponents += word[i];
if (sumExponents == 0)
{
preservedNode = *plnIter;
break;
}
if (sumExponents < presSumExp)
{
preservedNode = *plnIter;
presSumExp = sumExponents;
}
}
plnIter = pn->getXoutBegin();
while (plnIter != pn->getXoutEnd())
{
if ((*plnIter) == preservedNode)
{
plnIter++;
continue;
}
copyNodeEdges(*plnIter, preservedNode);
deleteNodes.push_back(*plnIter);
plnIter++;
}
}
if (pn->getSizeYout() > 1)
{
std::list<Node>::iterator preservedNode = pn->getYout();
int presSumExp = 0;
std::vector<short> presWord = preservedNode->getElemName();
for (int i = 0; i < presWord.size(); i++)
presSumExp += presWord[i];
std::list< std::list<Node>::iterator >::iterator plnIter;
for (plnIter = pn->getYoutBegin(); plnIter != pn->getYoutEnd(); plnIter++)
{
std::vector<short> word = (*plnIter)->getElemName();
int sumExponents = 0;
for (int i = 0; i < word.size(); i++)
sumExponents += word[i];
if (sumExponents == 0)
{
preservedNode = *plnIter;
break;
}
if (sumExponents < presSumExp)
{
preservedNode = *plnIter;
presSumExp = sumExponents;
}
}
plnIter = pn->getYoutBegin();
while (plnIter != pn->getYoutEnd())
{
if ((*plnIter) == preservedNode)
{
plnIter++;
continue;
}
copyNodeEdges(*plnIter, preservedNode);
deleteNodes.push_back(*plnIter);
plnIter++;
}
}
}
//Find node by tracing path to given word or return pnull if not found
std::list<Node>::iterator Graph::searchPath(std::vector<short>* ptargetWord)
{
std::vector<short> targetWord = *ptargetWord;
std::list<Node>::iterator pnull = std::list<Node>::iterator(NULL);
std::list<Node>::iterator pcurrentNode = nodeList.begin(); //start at the identity
///Ensure path begins at the identity
std::vector<short> startWord = pcurrentNode->getElemName();
if (startWord[0] != 0)
{
std::cout << "\nMISPLACED IDENTITY\n";
return pnull;
}
bool letterSwitch = true; //true <-> x, false <-> y; signifies current letter in loop
for (int i = 0; i < targetWord.size(); i++)
{
int letterExp = targetWord[i];
for (int j = 1; j <= letterExp; j++)
{
if (letterSwitch) //if current letter is x
pcurrentNode = pcurrentNode->getXout();
else //if current letter is y
pcurrentNode = pcurrentNode->getYout();
//return pnull if path was broken
if (pcurrentNode == pnull)
return pnull;
}
//switch the letter for the next exponent
letterSwitch = !letterSwitch;
}
//if you got a null iterator the function would have returned by now, so just return
//the node you've found
return pcurrentNode;
}
//Alt. version of buildPath()
std::list< std::list<Node>::iterator >
Graph::buildPath2(std::list<Node>::iterator pbaseNode, std::list<Node>::iterator ptargetNode)
{
std::list< std::list<Node>::iterator > emptyPath;
std::list< std::list<Node>::iterator > path;
path.push_back(pbaseNode);
//make sure iterators are valid
std::list<Node>::iterator pnull = std::list<Node>::iterator(NULL);
if (pbaseNode == pnull || ptargetNode == pnull)
{
std::cout << "\nFALSE BUILD2 RETURN (null iterators)";
return path;
}
std::vector<short> baseWord = pbaseNode->getElemName();
std::vector<short> targetWord = ptargetNode->getElemName();
//target word must be longer than or equal length as base word
if (targetWord.size() < baseWord.size())
return emptyPath;
//should never touch the identity; this is a special case which is handled in initial step
if (baseWord[0] == 0 || targetWord[0] == 0)
return emptyPath;
//if words are identical there's nothing to do
if (baseWord == targetWord)
return path;
//At this point we know the nodes exist, their words have appropriate sizes, they're not the identity,
//and they're not equal. But we could still have the problem that the base word is not contained
//in the leftmost portion of the target word, so we check for that.
bool baseContained = true;
int i;
for (i = 0; i < baseWord.size() - 1; i++) //up until the last letter, each exponent identical
if (baseWord[i] != targetWord[i])
baseContained = false;
if (baseWord[i] > targetWord[i]) //on the last letter, the exponent in base may be less or equal
baseContained = false;
if (baseContained == false)
return emptyPath;
//Now we know that we can build a valid path from the base node to the target node.
//this first part handles the first exponent on baseWord if it doesn't match targetWord
std::list<Node>::iterator pcurrentNode = pbaseNode; //will be updated to point to the farthest node in the path
if (baseWord[i] < targetWord[i])
{
if (i % 2 == 0) //if baseWord[i] is an exponent of x
for (int j = baseWord[i]; j <= targetWord[i]; j++)
{
pcurrentNode = buildXprod(pcurrentNode);
path.push_back(pcurrentNode);
}
else //if baseWord[i] is an exponent of y
for (int j = baseWord[i]; j <= targetWord[i]; j++)
{
pcurrentNode = buildYprod(pcurrentNode);
path.push_back(pcurrentNode);
}
}
//now build the path up to the last exponent of targetWord (handled specially below)
//note: i currently points to the last letter in baseWord, so it must be incremented to start one after that
i++;
for (i = i; i < targetWord.size() - 1; i++)
{
if (i % 2 == 0) //if targetWord[i] is an exponent of x
for (int j = 1; j <= targetWord[i]; j++)
{
pcurrentNode = buildXprod(pcurrentNode);
path.push_back(pcurrentNode);
}
else //if targetWord[i] is an exponent of y
for (int j = 1; j <= targetWord[i]; j++)
{
pcurrentNode = buildYprod(pcurrentNode);
path.push_back(pcurrentNode);
}
}
//now build the last exponent, up to the last product. since using buildXprod, it's safe
//to "build" the targetWord, but we won't do it anyway
if (i % 2 == 0)
{
for (int j = 1; j < targetWord[i]; j++)
{
pcurrentNode = buildXprod(pcurrentNode);
path.push_back(pcurrentNode);
}
//link final
setXedge(pcurrentNode, ptargetNode);
}
else //if targetWord[i] is an exponent of y
{
for (int j = 1; j < targetWord[i]; j++)
{
pcurrentNode = buildYprod(pcurrentNode);
path.push_back(pcurrentNode);
}
//link final
setYedge(pcurrentNode, ptargetNode);
}
//place final node, targetNode, at the end of the path
path.push_back(ptargetNode);
return path;
}
//Alt. version of reduceIdent()
Identity Graph::reduceIdent2(std::list<Identity>::iterator pid, std::list< std::list<Node>::iterator >& pidPath, bool report)
{
std::list<Node>::iterator prightNode;
prightNode = pid->getRight();
//use to trace back through pidPath
std::list< std::list<Node>::iterator >::reverse_iterator pleftPath = pidPath.rbegin();
std::list< std::list<Node>::iterator >::reverse_iterator pleftPathPlus1 = pleftPath;
pleftPathPlus1++;
while (pleftPathPlus1 != pidPath.rend())
{
//look for pleft in pleftPlus1's xout list
if ((*pleftPathPlus1)->isInXout(*pleftPath))
{
if (prightNode->isXinEmpty())
break;
prightNode = prightNode->getXin();
} //look in pleftPlus1's yout list
else if ((*pleftPathPlus1)->isInYout(*pleftPath))
{
if (prightNode->isYinEmpty())
break;
prightNode = prightNode->getYin();
}
else //in this case the path is not valid
{
std::cout << "\nBAD PATH FOR IDENTITY:\n";
pid->printIdent();
Identity nullid;
return nullid;
}
pleftPath++;
pleftPathPlus1++;
}
//make and return reduced identity;
Identity reducedId(*pleftPath, prightNode, pid->getBase());
if (report)
{
std::cout << "\nIdentity ";
pid->printIdent();
std::cout << "\nreduced to ";
reducedId.printIdent();
std::cout << "\n";
}
return reducedId;
}
//Alt. version of preservePath()
void Graph::preservePath2(std::list< std::list<Node>::iterator >& pidPath, std::list<Node>::iterator pendNode)
{
std::list< std::list<Node>::iterator >::iterator pathIter = pidPath.begin();
while (*pathIter != pendNode)
{
std::list<Node>::iterator pn = *pathIter;
if (pn->isOpen())
pn->setNodeType(Node::OPEN);
else pn->setNodeType(Node::CLOSED);
pathIter++;
}
if (pendNode->isOpen())
pendNode->setNodeType(Node::OPEN);
else pendNode->setNodeType(Node::CLOSED);
}
//Generate different kind of identities
void Graph::generateIdentities2(int length, int numIdents)
{
if (length % 2 == 1) //only even lengths - easier to make identities
length++;
if (length <= 2) //these identities were already used in the first genIdents() function
return;
if (numIdents <= 0) //can't generate nonpositively many identities
return;
//This function generates numIdents-many identities using blocks of length-many units, raised to
//the power of the graph's exponent, which naturally makes them equivalent to the identity.
//Rather than forming identity objects it just sets more edges in the graph, and then
//trusts in the user to reduce those edges later.
short block[length]; //used to fill leftWord
std::vector<short> leftWord; //represents the word that reduces to the identity
std::list<Node>::iterator leftNode; //the node representing the word leftWord
std::list<Node>::iterator ident = nodeList.begin(); //the identity of the group
//each pass through this loop generates a new identity
for (int i = 0; i < numIdents; i++)
{
//this loop fills block with random numbers in the range [1, exponent]
for (int j = 0; j < length; j++)
block[j] = (rand() % exponent) + 1;
//this loop fills leftWord with exponent-many copies of block, yielding a word equal to the identity
for (int k = 0; k < exponent; k++)
{
for (int f = 0; f < length; f++)
leftWord.push_back(block[f]);
}
//now the node representing leftWord can be found and used to form new edges in the graph
leftNode = searchPath(&leftWord);
if (leftNode != std::list<Node>::iterator(NULL)) //if word found
{
copyNodeEdges(ident, leftNode);
copyNodeEdges(leftNode, ident);
}
//clear containers for the next pass through the loop
leftWord.clear();
}
//done
}