-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrotating_permutation.py
More file actions
1202 lines (905 loc) · 38 KB
/
rotating_permutation.py
File metadata and controls
1202 lines (905 loc) · 38 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
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
993
994
995
996
997
998
999
1000
"""Define the RotatingPermutation class, with fast mutable operations."""
from cycle_utils_provider import CycleUtilsProvider
from cyclic_chained_list import CyclicChainedList
from sage.all import Permutation
from map_permutation import MapPermutation
from typing import Any
class RotatingPermutation(MapPermutation):
"""
A class representing permutation where it is fast to:
- delete (O(log(n))) element,
- check if two indices are in the same cycle (O(log(n))),
- add (O(log(n))) element in its cycles representation,
- and more things useful in MutableLabelledMap.
Note that compared to simple MapPermutation,
RotatingPermutation are more heavy objects; hence they are more demanding when initializing.
If you don't need all the power of RotatingPermutation, consider using the simple MapPermutation.
Another thing: for compatibility reasons between MutableLabelledMap and LabelledMap,
every method that returns a permutation must return MapPermutation.
Hence, don't assume that the permutation you get is a RotatingPermutation;
you should do it yourself.
WARNING: We take as a convention for this class that if i is bigger than the size of self,
then self(i) = i.
"""
def __init__(self, lst: int | Permutation | list[int] | list[tuple[int, ...]]):
"""
This function initiate the rotating permutation, lst can be a Permutation or a list of int or list of tuple representing the cycle of
the permutation or a MapPermutation or an integer representing the size of the permutation(in this case self will represent the identity permutation of size lst).
INPUT:
- ``lst`` -- List[int] | List[Tuples] | int | Permutation | MapPermutation ; a list representing the permutation or a list of tuples representing
the cycle of the permutationor an integer representing the size of the permutation(In This case it will return the identify of size lst)
or a Permutation or a MapPermutation.
EXAMPLES::
sage: from sage.graphs.maps.rotating_permutation import RotatingPermutation
sage: RotatingPermutation([(1,3,4),(7,8,10),(9,6)])
[3, 2, 4, 1, 5, 9, 8, 10, 6, 7]
NOTE:
O(nlog(n)) where n is the size of the permutation
"""
if isinstance(lst, Permutation) or isinstance(lst, MapPermutation):
self.__init__(list(lst))
return
# Important will stay
# Associate to a node its image
self._permCycle = {}
# Size of the permutation
self._n = 0
# Number of cycles in the permutation
self._numCycles = 0
# Number of fixed point in the permutation
self._numberOfFixedPoint = 0
try:
if lst == int(lst) and lst > 0:
# If lst is an integer we just set our permutation to be the
# identity
self._n = int(lst)
self._numCycles = self._n
self.provider = CycleUtilsProvider([])
return
except BaseException:
pass
mx = 0
seen = []
try:
# We're directly using the cycle representation to initialise the
# permutation
if isinstance(lst[0], type((42,))):
for l in lst:
for i in l:
mx = max(i, mx)
if i != int(i) or i <= 0:
raise ValueError(
f"Invalid argument: {i} isn't a strictly positive integer in the list given")
seen = [False for i in range(mx + 1)]
cnt = 0
for l in lst:
k = 0
while k < len(l):
i = l[k]
if seen[i]:
raise ValueError(
f"Invalid argument: {i} appears at least two times in list given it cannot be a permutation.")
seen[i] = True
k += 1
while k < len(l) and l[k] == i:
k += 1
continue
cnt += 1
self._numCycles += mx - cnt
self._numberOfFixedPoint += mx - cnt
for l in lst:
prevNode = None
for i in l:
newNode = CyclicChainedList(i)
self._permCycle[i] = newNode
if prevNode is not None:
prevNode.insertAfter(newNode)
prevNode = newNode
self._numberOfFixedPoint += len(l) == 1
self._numCycles += 1
else:
mx = len(lst)
for i in lst:
if i != int(i) or i <= 0:
raise ValueError(
f"Invalid argument : {i} isn't a strictly positive integer in the list given")
if i > len(lst):
raise ValueError(
f"{i} is bigger than the size of the given list")
seen = [False for i in range(mx + 1)]
for i in lst:
if seen[i]:
raise ValueError(
f"Invalid argument: {i} appears at least two time in the list given it cannot be a permutation..")
seen[i] = True
seen = [False for i in range(mx + 1)]
for i in range(1, mx + 1):
if seen[i]:
continue
prevNode = None
curElement = i
cnt = 0
while not seen[curElement]:
newNode = CyclicChainedList(curElement)
self._permCycle[curElement] = newNode
if prevNode is not None:
prevNode.insertAfter(newNode)
prevNode = newNode
seen[curElement] = True
curElement = lst[curElement - 1]
cnt += 1
self._numCycles += 1
self._numberOfFixedPoint += cnt == 1
except ValueError as e:
raise
except BaseException:
raise ValueError("Invalid argument: The argument given must be Permutation or MapPermutation or a non empty list of integers representing the permutation or a non empty list of tuples representing the cycles of the permutations or a positive integer.")
self._n = mx
self.provider = CycleUtilsProvider(self.to_cycles())
def to_list(self) -> list[int]:
"""
Return the permutation self, as a list of integers.
EXAMPLES::
sage: from sage.graphs.maps.rotating_permutation import RotatingPermutation
sage: RotatingPermutation([(2,3,4),(6,7)]).to_list()
[1, 3, 4, 2, 5, 7, 6]
NOTE:
O(n)
"""
return [self(i) for i in range(1, self.size() + 1)]
# OK
def size(self) -> int:
"""
OUTPUT:
The size of the permutation
EXAMPLES::
sage: from sage.graphs.maps.rotating_permutation import RotatingPermutation
sage: rperm = RotatingPermutation([(1,3,4), (7,8,2,5)])
sage: rperm.size()
8
NOTE:
O(1)
"""
return self._n
# OK
def deleteLastKIndex(self, k: int) -> None:
"""
This function will delete the last k index from self
INPUT:
-``k`` -- int ; the number of node to delete
EXAMPLES::
sage: from sage.graphs.maps.rotating_permutation import RotatingPermutation
sage: rperm = RotatingPermutation([(1,3,4),(7,8,10),(9,6)])
sage: rperm
[3, 2, 4, 1, 5, 9, 8, 10, 6, 7]
sage: rperm.deleteLastKIndex(3)
sage: rperm
[3, 2, 4, 1, 5, 6, 7]
NOTE:
O(klog(n)) where n is the size of self
"""
if k > self.size():
raise ValueError(
f"Cannot delete {k} last element in a RotatingPermutation of size {self.size()}")
for _ in range(k):
self.delete(self._n)
# OK
def delete(self, index: int) -> None:
"""
This will delete index of the corresponding cycle note that after this operation if we note the original
size of self as n, the which contained index will count one less element,
self will be of size n-1 and if n != index the element numbered n will relabeled as index.
For instance if self is the permutation(1, 2, 3)(4, 5) and we delete 2 it will become(1, 3)(4, 2),
If n = 1 an error or index is not a strictly positive integer <= n an error will be raised.
INPUT:
- ``index`` -- int ;
EXAMPLES::
sage: from sage.graphs.maps.rotating_permutation import RotatingPermutation
sage: rperm = RotatingPermutation([(1,3,4),(7,8,10),(9,6)])
sage: rperm
[3, 2, 4, 1, 5, 9, 8, 10, 6, 7]
sage: rperm.delete(10)
sage: rperm
[3, 2, 4, 1, 5, 9, 8, 7, 6]
NOTE:
O(log(n)),index must be an strictly positive integer and self.size() >= 2 otherwise an error will be raised
"""
if self.size() == 1:
raise ValueError(
"Cannot delete an element from a Permutation of size 1")
if index != int(index) or index <= 0 or index > self.size():
raise ValueError(
"{index} isn't a strictly positive integer <= self.size()")
nPrev = self.size()
node = self.getNode(index)
node.remove()
if self.provider.numberInCycle(index) == 2:
self._numberOfFixedPoint += 1
if self.provider.numberInCycle(index) == 1:
self._numberOfFixedPoint -= 1
self._numCycles -= 1
self._n -= 1
self._permCycle.pop(index)
self.provider.swapIndex(nPrev, index)
self.provider.detach(nPrev)
if nPrev != index:
try:
self._permCycle[nPrev].val = index
self._permCycle[index] = self._permCycle[nPrev]
self._permCycle.pop(nPrev)
except BaseException:
pass
# OK
def inverseApply(self, i: int) -> int:
"""
This function apply the inverse self on i, we take as a convention i if i is an integer > self.size(), self.inverseApply(i) = i
INPUT:
- ``i`` -- int
OUTPUT:
j such that self(j) = i
EXAMPLES::
sage: from sage.graphs.maps.rotating_permutation import RotatingPermutation
sage: rperm = RotatingPermutation([(1,3,4),(7,8,10),(9,6)])
sage: rperm.inverseApply(10)
8
NOTE:
O(1)
"""
if i != int(i) or i <= 0:
raise ValueError("{i} isn't a positive integer")
try:
return self._permCycle[i].prev.val
except BaseException:
return i
# OK
def checkTwoInTheSameCycle(self, listIndexes: list[int]) -> bool:
"""
This function will return a boolean indicating if there is two index in listIndexes in the sameCycle
INPUT:
- ``listIndexes`` -- List[int] ; A list of indexes
OUTPUT:
A boolean indicating if two indexes in listIndexes are in the same cycle
EXAMPLES::
sage: from sage.graphs.maps.rotating_permutation import RotatingPermutation
sage: rperm = RotatingPermutation([(1,3,4),(7,8,10),(9,6)])
sage: rperm.checkTwoInTheSameCycle([1,7,9])
False
NOTE:
O(plog(n)) where p = len(listIndexes) and n is the size
"""
return self.provider.checkTwoInTheSameCycle(listIndexes)
# OK
def swapIndex(self, index: int, otherIndex: int) -> None:
"""
This function swap the index role in the permutation
INPUT:
- ``index`` -- int ;<= self.size()
- ``otherIndex`` -- int ; <= self.size()
EXAMPLES::
sage: from sage.graphs.maps.rotating_permutation import RotatingPermutation
sage: rperm = RotatingPermutation([(1,3,4),(7,8,10),(9,6)])
sage: rperm
[3, 2, 4, 1, 5, 9, 8, 10, 6, 7]
sage: rperm.swapIndex(1,10)
sage: rperm
[7, 2, 4, 10, 5, 9, 8, 1, 6, 3]
NOTE:
O(log(n)), where n is the size of self
"""
self.provider.swapIndex(index, otherIndex)
nodeIndex = self.getNode(index)
nodeOther = self.getNode(otherIndex)
self._permCycle[otherIndex] = nodeIndex
self._permCycle[index] = nodeOther
self._permCycle[otherIndex].val = otherIndex
self._permCycle[index].val = index
def cutDelete(self, startIndex: int, endIndex: int) -> None:
"""
This will cut the cycle in two part startIndex...endIndex and the rest , and than will delete startIndex and endIndex
INPUT:
- ``startIndex`` -- int ; on the same cycle as endIndex such that (startIndex,endIndex)=(self.size(),self.size()-1)
- ``endIndex`` -- int ; on the same cycle as startIndex such that (startIndex,endIndex)=(self.size(),self.size()-1)
EXAMPLES::
sage: from sage.graphs.maps.rotating_permutation import RotatingPermutation
sage: rperm = RotatingPermutation([(1,3,4),(2,7,11,8,10),(9,6)])
sage: rperm.pretty_print()
Rotating permutation: [(1, 3, 4), (2, 7, 11, 8, 10), (5,), (6, 9)]
sage: rperm.cutDelete(10,11)
sage: rperm.pretty_print()
Rotating permutation: [(1, 3, 4), (2, 7), (5,), (6, 9), (8,)]
NOTE:
O(log(m))
"""
assert startIndex != endIndex
assert self.sameCycle(startIndex, endIndex)
tempNewIndex = self._n+1
tempNewIndexOther = self._n+2
self.cutAdd(startIndex, endIndex, tempNewIndex, tempNewIndexOther)
self.deleteLastKIndex(4)
# OK
def cutAdd(self, startIndex: int, endIndex: int, newIndexStart: int, newIndexEnd: int) -> None:
"""
This implement a special operation.In a nutshell it cut a cycle and add two index in each cycle,
let denote A = startIndex, B = endIndex, C = newIndexStart, D = newIndexEnd and say the cycle is of the form F -> A -> S -> .. -> T -> B -> R -> ... -> F
than the situation will be the following after a call to this function, A -> S -> ... -> T -> D -> A and F -> C -> B -> R -> ... -> F
INPUT:
- ``startIndex`` -- int ; on same cycle as ``endIndex``
- ``endIndex `` -- int ; on same cycle as ``startIndex``
- ``newIndexStart`` -- int; such that {newIndexEnd, newIndexStart} = {n+1, n+2}
- ``newIndexEnd`` -- int : such that {newIndexEnd, newIndexStart} = {n+1, n+2}
EXAMPLES::
sage: from sage.graphs.maps.rotating_permutation import RotatingPermutation
sage: rperm = RotatingPermutation([(1,3,4),(2,7,11,8,10),(9,6)])
sage: rperm.pretty_print()
Rotating permutation: [(1, 3, 4), (2, 7, 11, 8, 10), (5,), (6, 9)]
sage: rperm.cutAdd(1,4,12,13)
sage: rperm.pretty_print()
Rotating permutation: [(1, 3, 13), (2, 7, 11, 8, 10), (4, 12), (5,), (6, 9)]
NOTE:
O(log(n))
"""
if newIndexEnd == newIndexStart:
raise ValueError(
f"{newIndexEnd} and {newIndexStart} must be different")
if newIndexStart <= self.size() or newIndexStart > self.size() + 2:
raise ValueError(
f"{newIndexStart} must be >{self.size()} and <= {self.size() + 2}")
if newIndexEnd <= self.size() or newIndexEnd > self.size() + 2:
raise ValueError(
f"{newIndexEnd} must be >{self.size()} and <= {self.size() + 2}")
if not self.sameCycle(startIndex, endIndex):
raise ValueError(
f"{newIndexEnd} and {newIndexStart} must be in the same cycle to use cutAdd")
if startIndex == endIndex:
self.addBefore(startIndex)
self.stretch(1)
return
nodeStartIndex = self.getNode(startIndex)
nodeEndIndex = self.getNode(endIndex)
# Updating scalar attribute accordingly
self._numCycles += 1
self._n += 2
nodeNewIndexStart = self.getNode(newIndexStart)
nodeNewIndexEnd = self.getNode(newIndexEnd)
# NodeNewIndexStart processing
comeBeforeEnd = self.inverseApply(endIndex)
tmpNode = self.getNode(self.inverseApply(startIndex))
nodeNewIndexStart.prev = tmpNode
tmpNode.nxt = nodeNewIndexStart
nodeNewIndexStart.nxt = nodeEndIndex
# NodeNewIndexEnd processing
tmpNode = self.getNode(self.inverseApply(endIndex))
nodeNewIndexEnd.prev = tmpNode
tmpNode.nxt = nodeNewIndexEnd
nodeNewIndexEnd.nxt = nodeStartIndex
nodeEndIndex.prev = nodeNewIndexStart
nodeStartIndex.prev = nodeNewIndexEnd
# Updating the provider
self.provider.cut(startIndex,
comeBeforeEnd)
self.provider.addBefore(startIndex, newIndexEnd)
self.provider.addBefore(endIndex, newIndexStart)
# OK
def labelToTheEnd(self, listIndexes: int) -> dict[int, int]:
"""
This is a helper function it just move all of the element in listIndexes to the last indices
INPUT:
- ``listIndexes`` -- List[int]
OUTPUT:
A map giving a correspondence between the old index and the new
if it was changed.
EXAMPLES::
sage: from sage.graphs.maps.rotating_permutation import RotatingPermutation
sage: rperm = RotatingPermutation([(1,3,4),(2,7,11,8,10),(9,6)])
sage: rperm
[3, 7, 4, 1, 5, 9, 11, 10, 6, 2, 8]
sage: rperm.labelToTheEnd([3,2,11])
{2: 9, 3: 10}
sage: rperm
[10, 6, 9, 1, 5, 2, 11, 3, 7, 4, 8]
NOTE:
O(len(listIndexes)*log(n)) where n is the size of the permutation
"""
for index in listIndexes:
if index != int(index) or index <= 0 or index > self.size():
raise ValueError(
f"In labelToTheEnd : {index} isn't a strictly positive integer <= {self.size()}")
indexMap = set()
for index in listIndexes:
indexMap.add(index)
indexCandidate = set()
for j in range(len(indexMap)):
indexCandidate.add(self.size() - j)
for index in list(indexCandidate):
if index in indexMap:
indexCandidate.remove(index)
indexMap.remove(index)
corresOut = {}
for index in list(indexMap):
if index not in indexMap:
continue
corresIndex = indexCandidate.pop()
corresOut[index] = corresIndex
indexMap.remove(index)
self.swapIndex(index, corresIndex)
return corresOut
# OK
def bruteAddCycles(self, cycles: list[tuple[int, ...]]) -> None:
"""
Another helper function that add cyclein cycles, this one assumed is more dangerous than addCycles
cause it assumed that the cycles are well formed thus the term brute
INPUT:
- ``cycles`` -- List[Tuple] ; list of cycles as tuple
EXAMPLES::
sage: from sage.graphs.maps.rotating_permutation import RotatingPermutation
sage: rperm = RotatingPermutation([(1,3,4)])
sage: rperm.pretty_print()
Rotating permutation: [(1, 3, 4), (2,)]
sage: rperm.stretch(3)
sage: rperm.bruteAddCycles([(5,6,2)] )
sage: rperm.pretty_print()
Rotating permutation: [(1, 3, 4), (2, 5, 6), (7,)]
NOTE:
O(len(cycles)*log(n))
"""
for c in cycles:
for i in range(len(c) - 1):
self.addAfterGeneral(c[i], c[i + 1])
# OK
def addCycles(self, cycles: list[tuple[int, ...]]) -> None:
"""
Another helper function it will raise an error if element of the cycles
are not > self.size() and <= self.size()+len(cycles), the cycle must be well formed
INPUT:
-- ``cycles`` -- List[Tuple] ; list of cycles
EXAMPLES::
sage: from sage.graphs.maps.rotating_permutation import RotatingPermutation
sage: rperm = RotatingPermutation([(1,3,4)])
sage: rperm.addCycles([(5,6)])
sage: rperm
[3, 2, 4, 1, 6, 5]
NOTE:
O(len(cycles)*log(n))
"""
testSet = set()
N = 0
for c in cycles:
N += len(c)
for c in cycles:
for e in c:
testSet.add(e)
if e <= self.size() or e <= 0 or e != int(e) or e > self.size() + N:
raise ValueError("{cycles} isn't valid")
if len(testSet) != N:
raise ValueError("{cycles} isn't valid")
self.stretch(N)
self.bruteAddCycles(cycles)
# OK
def isValidIndex(self, index: int) -> bool:
"""
Check if index is a integer > 0 and <=self.size()
otherwise raise an Error
INPUT:
- ``index`` -- int
EXAMPLES::
sage: from sage.graphs.maps.rotating_permutation import RotatingPermutation
sage: rperm = RotatingPermutation([(1,3,4)])
sage: try :
....: rperm.isValidIndex(100)
....: except:
....: pass
....:
NOTE:
O(1)
"""
if index <= 0 or index != int(index) or index > self.size():
raise ValueError(f"{index} isn't valid")
# OK
def addAfterGeneral(self, index: int, otherIndex: int) -> None:
"""
This is a more general version of addAfter it only assumed that otherIndex is a fixed point
and will add it after index in its cycle
INPUT:
- ``index`` -- int
- ``otherIndex`` -- int
EXAMPLES::
sage: from sage.graphs.maps.rotating_permutation import RotatingPermutation
sage: rperm = RotatingPermutation([(1,3,4)])
sage: rperm.pretty_print()
Rotating permutation: [(1, 3, 4), (2,)]
sage: rperm.addAfterGeneral(4,2)
sage: rperm.pretty_print()
Rotating permutation: [(1, 3, 4, 2)]
NOTE:
O(log(n)), where n is the size of the permutation
"""
self.isValidIndex(index)
self.isValidIndex(otherIndex)
if index == otherIndex:
return
if not self.provider.isFixedPoint(otherIndex):
raise ValueError(
f"Can only add after fixed point {otherIndex} isn't one")
self._numberOfFixedPoint -= self.provider.isFixedPoint(index)
self.provider.addAfter(index, otherIndex)
node = self.getNode(index)
newNode = self.getNode(otherIndex)
self._numberOfFixedPoint -= 1
self._numCycles -= 1
node.insertAfter(newNode)
# OK
def addBeforeGeneral(self, index: int, otherIndex: int) -> None:
"""
More general version of addBeforeit only assumed that otherIndex is a fixed point
and will add it before index in its cycle
INPUT:
- ``index`` -- int
- ``otherIndex`` -- int
EXAMPLES::
sage: from sage.graphs.maps.rotating_permutation import RotatingPermutation
sage: rperm = RotatingPermutation([(1,3,4)])
sage: rperm.pretty_print()
Rotating permutation: [(1, 3, 4), (2,)]
sage: rperm.addBeforeGeneral(3,2)
sage: rperm.pretty_print()
Rotating permutation: [(1, 2, 3, 4)]
NOTE:
O(log(n)), where n is the size of the permutation
"""
self.isValidIndex(index)
indexPrev = self.inverseApply(index)
self.addAfterGeneral(indexPrev, otherIndex)
# OK
def mergeDelete(self, index: int, otherIndex: int) -> None:
"""
Assuming that index and otherIndex are not in the same cycle it will do the
following first index and otherIndex will be sent to self.size() self.size()-1 they will be deleted and given
that before we add: U -> ... -> V -> index -> R -> U and F -> ... -> T -> otherIndex -> Q -> F, we will have after
U -> ... -> V -> Q -> F -> ... -> T -> R -> U
INPUT:
- ``index`` -- int
- ``otherIndex`` -- int
EXAMPLES::
sage: from sage.graphs.maps.rotating_permutation import RotatingPermutation
sage: rperm = RotatingPermutation([(1,3,4), (7,8,2,5)])
sage: rperm.pretty_print()
Rotating permutation: [(1, 3, 4), (2, 5, 7, 8), (6,)]
sage: rperm.mergeDelete(3,7)
sage: rperm.pretty_print()
Rotating permutation: [(1, 3, 2, 5, 4), (6,)]
NOTE:
O(log(n))
"""
if self.sameCycle(index, otherIndex):
raise ValueError("Cannot merge delete two index on the sameCycle")
backUpNumberOfFixedPoint = self.number_of_fixed_points()
self.labelToTheEnd([index, otherIndex])
if self.provider.isFixedPoint(
self._n) or self.provider.isFixedPoint(self._n - 1):
self.deleteLastKIndex(2)
return
beforeIndex = self.inverseApply(self._n)
afterIndex = self.apply(self._n - 1)
self.deleteLastKIndex(2)
nodeBefore = self.getNode(beforeIndex)
nodeAfter = self.getNode(afterIndex)
if nodeBefore.nxt == nodeBefore:
if nodeAfter.nxt == nodeAfter:
nodeBefore.nxt = nodeAfter
nodeBefore.prev = nodeAfter
nodeAfter.nxt = nodeBefore
nodeAfter.prev = nodeBefore
else:
tmpNode = nodeAfter.prev
tmpNode.nxt = nodeBefore
nodeBefore.prev = tmpNode
nodeAfter.prev = nodeBefore
nodeBefore.nxt = nodeAfter
else:
if nodeAfter.nxt == nodeAfter:
tmpNode = nodeBefore.nxt
tmpNode.prev = nodeAfter
nodeAfter.nxt = tmpNode
nodeAfter.prev = nodeBefore
nodeBefore.nxt = nodeAfter
else:
tmpNodeBefore = nodeBefore.nxt
tmpNodeAfter = nodeAfter.prev
tmpNodeBefore.prev = tmpNodeAfter
tmpNodeAfter.nxt = tmpNodeBefore
nodeBefore.nxt = nodeAfter
nodeAfter.prev = nodeBefore
self._numCycles -= 1
self._numberOfFixedPoint = backUpNumberOfFixedPoint
self.provider.merge(beforeIndex, afterIndex)
# OK
def getNode(self, index: int) -> CyclicChainedList:
"""
This function will return the node associated to index
and if it doesn't exit it will create one note that if index > self.size()
it will raise an error.
INPUT:
- ``index`` -- int
OUTPUT:
The node associated to index
EXAMPLES::
sage: from sage.graphs.maps.rotating_permutation import RotatingPermutation
sage: rperm = RotatingPermutation([(1,3,4), (7,8,2,5)])
sage: rperm.getNode(3) != rperm.getNode(4)
True
NOTE:
O(1)
"""
if index != int(index) or index <= 0 or index > self.size():
raise ValueError(
"{index} isn't a strictly positive integer <= self.size()")
try:
node = self._permCycle[index]
except BaseException:
node = CyclicChainedList(index)
self._permCycle[index] = node
return node
# OK
def stretch(self, m: int) -> None:
"""
This function will increase the size of the permutation by m,all the new index will
be fixed point
INPUT:
- ``m`` -- int; ``m`` >= 0
EXAMPLES::
sage: from sage.graphs.maps.rotating_permutation import RotatingPermutation
sage: rperm = RotatingPermutation([(1,3,4), (7,8,2,5)])
sage: rperm
[3, 5, 4, 1, 7, 6, 8, 2]
sage: rperm.stretch(5)
sage: rperm
[3, 5, 4, 1, 7, 6, 8, 2, 9, 10, 11, 12, 13]
NOTE:
O(1)
"""
self._n += m
self._numberOfFixedPoint += m
self._numCycles += m
# OK
def addAfter(self, index: int) -> None:
"""
Let denote n=self.size() given that n>=index>=1, this will increase the size of self by one and add
the new element n+1 on the cycle of index after index.You should note that if index>self.size() this will raise an error.
INPUT:
- ``index`` -- int
EXAMPLES::
sage: from sage.graphs.maps.rotating_permutation import RotatingPermutation
sage: rperm = RotatingPermutation([(1,3,4), (7,8,2,5)])
sage: rperm.pretty_print()
Rotating permutation: [(1, 3, 4), (2, 5, 7, 8), (6,)]
sage: rperm.addAfter(6)
sage: rperm.pretty_print()
Rotating permutation: [(1, 3, 4), (2, 5, 7, 8), (6, 9)]
NOTE:
O(log(n)), where n is the size of self
"""
if index != int(index) or index <= 0 or index > self.size():
raise ValueError(
f"{index} isn't a strictly positive integer <= {self.size()}")
self._numberOfFixedPoint -= self.provider.isFixedPoint(index)
nPrev = self.size()
self.stretch(1)
self.provider.addAfter(index, nPrev + 1)
node = self.getNode(index)
newNode = self.getNode(nPrev + 1)
self._numberOfFixedPoint -= 1
self._numCycles -= 1
node.insertAfter(newNode)
# OK
def addBefore(self, index: int) -> None:
"""
Let denote n=self.size() given that n>=index>=1, this will increase the size of self by one and add
the new element n+1 on the cycle of index before index.You should note that if index>self.size() this will raise an error.
INPUT:
- ``index`` -- int
EXAMPLES::
sage: from sage.graphs.maps.rotating_permutation import RotatingPermutation
sage: rperm = RotatingPermutation([(1,3,4), (7,8,2,5)])
sage: rperm.pretty_print()
Rotating permutation: [(1, 3, 4), (2, 5, 7, 8), (6,)]
sage: rperm.addBefore(3)
sage: rperm.pretty_print()
Rotating permutation: [(1, 9, 3, 4), (2, 5, 7, 8), (6,)]
NOTE:
O(log(n)), where n is the size of self
"""
if index != int(index) or index <= 0 or index > self.size():
raise ValueError(
"{index} isn't a strictly positive integer <= self.size()")
node = self.getNode(index)
prevIndex = node.prev.val
self.addAfter(prevIndex)
# OK
def numberInCycle(self, index: int) -> int:
"""
INPUT:
- ``i`` -- int
OUTPUT:
-A integer representing the number of element in the same cycle as index note that
if index > self.size() it will return 1(which is coherent with the convention that self(i) = i)
EXAMPLES::
sage: from sage.graphs.maps.rotating_permutation import RotatingPermutation
sage: rperm = RotatingPermutation([(1,3,4), (7,8,2,5)])
sage: rperm.pretty_print()
Rotating permutation: [(1, 3, 4), (2, 5, 7, 8), (6,)]
sage: rperm.numberInCycle(5)
4
NOTE:
O(log(n))
"""
return self.provider.numberInCycle(index)
# OK
def numberOfCycles(self) -> int:
"""
OUTPUT:
the number of cycle of self
EXAMPLES::
sage: from sage.graphs.maps.rotating_permutation import RotatingPermutation
sage: rperm = RotatingPermutation([(1,3,4), (7,8,2,5)])
sage: rperm.pretty_print()
Rotating permutation: [(1, 3, 4), (2, 5, 7, 8), (6,)]
sage: rperm.numberOfCycles()
3
NOTE:
O(1)
"""
return self._numCycles
# OK
def sameCycle(self, i: int, j: int) -> bool:
"""
INPUT:
-``i`` -- int ; a strictly positive integer
-``j`` -- int ; a strictly positive integer
OUTPUT: