-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodularDecomposition.py
2351 lines (1859 loc) · 85.1 KB
/
modularDecomposition.py
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
# -*- coding: utf-8 -*-
r"""
Modular Decomposition
This module implements the function for computing the modular decomposition
of undirected graphs.
#*****************************************************************************
# Copyright (C) 2017 Lokesh Jain <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
"""
from sage.graphs.graph import Graph
from collections import deque
PRIME = 0
SERIES = 1
PARALLEL = 2
NORMAL = 3
FOREST = -1
LEFT_SPLIT = 1
RIGHT_SPLIT = 2
BOTH_SPLIT = 3
NO_SPLIT = 0
LEFT_OF_SOURCE = -1
RIGHT_OF_SOURCE = 1
SOURCE = 0
class NodeInfo:
"""
Node class stores information about the node type, node split and index
of the node in the parent tree.
Node type can be PRIME, SERIES, PARALLEL, NORMAL or FOREST. Node split can
be NO_SPLIT, LEFT_SPLIT, RIGHT_SPLIT or BOTH_SPLIT. A node is split in the
refinement phase and the split used is propagated to the ancestors.
- ``node_type`` -- Specifies the type of node
* ``"PARALLEL"`` -- indicates the node is a parallel module
* ``"SERIES"`` -- indicates the node is a series module
* ``"PRIME"`` -- indicates the node is a prime module
* ``"FOREST"`` -- indicates a forest containing trees
* ``"NORMAL"`` -- indicates the node is normal containing a vertex
- ``node_split`` -- Specifies the type of splits which have occurred in
the node and its descendants
* ``"LEFT_SPLIT"`` -- indicates a left split has occurred
* ``"RIGHT_SPLIT"`` -- indicates a right split has occurred
* ``"BOTH_SPLIT"`` -- indicates both left and right split have occurred
* ``"NO_SPLIT"`` -- indicates no split has occurred
- ``index_in_root`` -- specifies the index of the node in the forest
obtained after promotion phase
- ``comp_num`` -- specifies the number given to nodes in a (co)component
before refinement
- ``is_separated`` -- specifies whether a split has occurred with the node
as the root
"""
def __init__(self, node_type):
self.node_type = node_type
self.node_split = NO_SPLIT
self.index_in_root = -1
self.comp_num = -1
self.is_separated = False
def set_node_split(self, node_split):
"""
Add node_split to the node split of self.
LEFT_SPLIT and RIGHT_SPLIT can exist together in self as BOTH_SPLIT.
INPUT:
- ``node_split`` -- node_split to be added to self
"""
if self.node_split == NO_SPLIT:
self.node_split = node_split
elif ((self.node_split == LEFT_SPLIT and
node_split == RIGHT_SPLIT) or
(self.node_split == RIGHT_SPLIT and
node_split == LEFT_SPLIT)):
self.node_split = BOTH_SPLIT
def has_left_split(self):
"""
Return true if self has LEFT_SPLIT
"""
return self.node_split == LEFT_SPLIT or self.node_split == BOTH_SPLIT
def has_right_split(self):
"""
Return true if self has RIGHT_SPLIT
"""
return self.node_split == RIGHT_SPLIT or self.node_split == BOTH_SPLIT
def __str__(self):
if self.node_type == SERIES:
return "SERIES"
elif self.node_type == PARALLEL:
return "PARALLEL"
elif self.node_type == PRIME:
return "PRIME"
elif self.node_type == FOREST:
return "FOREST"
else:
return "NORMAL"
def __repr__(self):
return self.__str__()
def __eq__(self, other):
return self.node_type == other.node_type
def modular_decomposition(graph):
"""
Compute the modular decomposition tree for the input graph.
The tree structure is represented in form of nested lists. A tree node is
a list with two elements. The first element is object of class NodeInfo
and second element is a list which contains other tree nodes.
INPUT:
- ``graph`` -- The graph for which modular decompostion
tree needs to be computed
OUTPUT:
A nested list representing the modular decomposition tree computed
for the graph
EXAMPLES:
The Icosahedral graph is Prime::
sage: from sage.graphs.modular_decomposition import \
modular_decomposition, test_modular_decomposition, print_md_tree
sage: print_md_tree(modular_decomposition(graphs.IcosahedralGraph()))
PRIME
8
5
1
11
7
0
6
9
2
4
10
3
The Octahedral graph is not Prime::
sage: print_md_tree(modular_decomposition(graphs.OctahedralGraph()))
SERIES
PARALLEL
2
3
PARALLEL
1
4
PARALLEL
0
5
Tetrahedral Graph is Series::
sage: print_md_tree(modular_decomposition(graphs.TetrahedralGraph()))
SERIES
3
2
1
0
Modular Decomposition tree containing combination of parallel and series modules::
sage: d = {2:[4,3,5], 1:[4,3,5], 5:[3,2,1,4], 3:[1,2,5], 4:[1,2,5]}
sage: g = Graph(d)
sage: print_md_tree(modular_decomposition(g))
SERIES
5
PARALLEL
3
4
PARALLEL
1
2
TESTS:
Bad Input::
sage: g = DiGraph()
sage: modular_decomposition(g)
Traceback (most recent call last):
...
ValueError: Graph must be undirected
Empty Graph is Prime::
sage: g = Graph()
sage: modular_decomposition(g)
[PRIME, []]
Graph from Marc Tedder implementation of modular decomposition::
sage: d = {1:[5,4,3,24,6,7,8,9,2,10,11,12,13,14,16,17], 2:[1], \
3:[24,9,1], 4:[5,24,9,1], 5:[4,24,9,1], 6:[7,8,9,1], \
7:[6,8,9,1], 8:[6,7,9,1], 9:[6,7,8,5,4,3,1], 10:[1], \
11:[12,1], 12:[11,1], 13:[14,16,17,1], 14:[13,17,1], \
16:[13,17,1], 17:[13,14,16,18,1], 18:[17], 24:[5,4,3,1]}
sage: g = Graph(d)
sage: test_modular_decomposition(modular_decomposition(g), g)
True
Graph from wikipedia link :wikipedia:`Modular_decomposition`::
sage: d2 = {1:[2,3,4], 2:[1,4,5,6,7], 3:[1,4,5,6,7], 4:[1,2,3,5,6,7], \
5:[2,3,4,6,7], 6:[2,3,4,5,8,9,10,11], \
7:[2,3,4,5,8,9,10,11], 8:[6,7,9,10,11], 9:[6,7,8,10,11], \
10:[6,7,8,9], 11:[6,7,8,9]}
sage: g = Graph(d)
sage: test_modular_decomposition(modular_decomposition(g), g)
True
Tetrahedral Graph is Series::
sage: print_md_tree(modular_decomposition(graphs.TetrahedralGraph()))
SERIES
3
2
1
0
Modular Decomposition tree containing combination of parallel and series modules::
sage: d = {2:[4,3,5], 1:[4,3,5], 5:[3,2,1,4], 3:[1,2,5], 4:[1,2,5]}
sage: g = Graph(d)
sage: print_md_tree(modular_decomposition(g))
SERIES
5
PARALLEL
3
4
PARALLEL
1
2
"""
if graph.is_directed():
raise ValueError("Graph must be undirected")
if graph.order() == 0: #Empty Graph
return create_prime_node()
if graph.order() == 1: # Single vertex graph
root = create_normal_node(next(graph.vertex_iterator()))
return root
if not graph.is_connected():
# Parallel case:- The tree contains the MD trees of its connected
# components as subtrees
components = graph.connected_components()
root = create_parallel_node()
for component in components:
root[1].append(modular_decomposition(graph.subgraph(component)))
return root
elif graph.complement().is_connected(): #Prime Graph
root = create_prime_node()
else:
root = create_series_node() #Series Graph
bfs_generator = graph.breadth_first_search(next(graph.vertex_iterator()),
report_distance=True)
prev_level_distance = -1 # used as a demarker for different levels in bfs
prev_level_list = [] # stores the vertices in previous level
# dictionary stores the distance of vertices from the SOURCE
vertex_dist = {}
# dictionary stores the position of vertices w.r.t SOURCE
vertex_status = {}
vertex_status[next(graph.vertex_iterator())] = SOURCE
# Different distances from the source vertex are considered
# as different levels in the algorithm
for (vertex, distance) in bfs_generator:
vertex_dist[vertex] = distance
# Mark the neighbours of source as LEFT_OF_SOURCE as they appear to
# left of source in the forest, other vertices are marked as
# RIGHT_OF_SOURCE
if distance == 1:
vertex_status[vertex] = LEFT_OF_SOURCE
elif distance != 0:
vertex_status[vertex] = RIGHT_OF_SOURCE
if distance != prev_level_distance: # On start of new level in BFS
if prev_level_list:
# MD Tree is computed for each level and added to the forest
root[1].append(modular_decomposition(
graph.subgraph(prev_level_list))
)
prev_level_list = []
prev_level_distance = distance
prev_level_list.append(vertex)
# The last level is left out in the above loop
root[1].append(modular_decomposition(graph.subgraph(prev_level_list)))
# The MD tree for the neighbours of source marked as LEFT_OF_SOURCE
# are placed left of Source in the forest. root[1][1] is the source
# and root[1][0] is the MD tree for the neighbours therefore the
# the first two elements in the list are replaced
root[1][0], root[1][1] = root[1][1], root[1][0]
root[0].node_type = FOREST
clear_node_split_info(root)
number_cocomponents(root, vertex_status)
number_components(root, vertex_status)
refine(graph, root, vertex_dist, vertex_status)
promote_left(root)
promote_right(root)
promote_child(root)
assembly(graph, root, vertex_status, vertex_dist)
if root[0].node_type == FOREST:
return root[1][0]
else:
return root
def number_components(root, vertex_status):
"""
Function to number the components to the right of SOURCE vertex in the
forest input to the assembly phase
INPUT:
- ``root`` -- the forest which contains the components and cocomponents
- ``vertex_status`` -- dictionary which stores the position of vertex
w.r.t SOURCE
EXAMPLES:
sage: from sage.graphs.modular_decomposition import NodeInfo, \
FOREST, SERIES, PARALLEL, LEFT_OF_SOURCE, SOURCE, RIGHT_OF_SOURCE, \
create_normal_node, number_components
sage: forest = [NodeInfo(FOREST), [create_normal_node(2), \
create_normal_node(3), create_normal_node(1), \
[NodeInfo(SERIES), [create_normal_node(4), \
create_normal_node(5)]], [NodeInfo(PARALLEL), \
[create_normal_node(6), create_normal_node(7)]]]]
sage: vertex_status = {2: LEFT_OF_SOURCE, 3: SOURCE, \
1: RIGHT_OF_SOURCE, 4: RIGHT_OF_SOURCE, \
5: RIGHT_OF_SOURCE, 6: RIGHT_OF_SOURCE, \
7: RIGHT_OF_SOURCE}
sage: number_components(forest, vertex_status)
sage: forest[1][-1][1][0][0].comp_num
2
sage: forest[1][-1][1][1][0].comp_num
3
TESTS:
sage: forest = [NodeInfo(FOREST), [create_normal_node(2), \
create_normal_node(3), create_normal_node(1), \
[NodeInfo(SERIES), [create_normal_node(4), \
create_normal_node(5)]], [NodeInfo(PARALLEL), \
[create_normal_node(6), create_normal_node(7)]]]]
sage: vertex_status = {2: LEFT_OF_SOURCE, 3: SOURCE, \
1: RIGHT_OF_SOURCE, 4: RIGHT_OF_SOURCE, \
5: RIGHT_OF_SOURCE, 6: RIGHT_OF_SOURCE, \
7: RIGHT_OF_SOURCE}
sage: number_components(forest, vertex_status)
sage: forest[1][-1][1][0][0].comp_num == 2 and \
forest[1][-1][1][1][0].comp_num == 3
True
sage: forest[1][-2][1][0][0].comp_num == 1 and \
forest[1][-2][1][1][0].comp_num == 1
True
"""
comp_num = 0
flag = False
if not root: #root is empty
return ValueError("Input forest {} is empty".format(root))
for tree in root[1]:
#flag set to True after source vertex is encountered
if tree[0].node_type == NORMAL and \
vertex_status[tree[1][0]] == SOURCE:
flag = True
continue
if not flag: # Cocomponents are skipped
continue
comp_num += recursively_number_cocomponents(tree, comp_num, PARALLEL)
def number_cocomponents(root, vertex_status):
"""
Function to number the cocomponents to the left of SOURCE vertex in the
forest input to the assembly phase
INPUT:
- ``root`` -- the forest which contains the cocomponents and components
- ``vertex_status`` -- dictionary which stores the position of vertex
w.r.t SOURCE
EXAMPLES:
sage: from sage.graphs.modular_decomposition import NodeInfo, \
FOREST, SERIES, PARALLEL, LEFT_OF_SOURCE, SOURCE, RIGHT_OF_SOURCE, \
create_normal_node, number_cocomponents
sage: forest = [NodeInfo(FOREST), [create_normal_node(2), \
[NodeInfo(SERIES), [create_normal_node(4), \
create_normal_node(5)]], [NodeInfo(PARALLEL), \
[create_normal_node(6), create_normal_node(7)]], \
create_normal_node(3), create_normal_node(1)]]
sage: vertex_status = {2: LEFT_OF_SOURCE, 3: SOURCE, \
1: RIGHT_OF_SOURCE, 4: LEFT_OF_SOURCE, \
5: LEFT_OF_SOURCE, 6: LEFT_OF_SOURCE, \
7: LEFT_OF_SOURCE}
sage: number_cocomponents(forest, vertex_status)
sage: forest[1][1][1][0][0].comp_num
1
sage: forest[1][1][1][1][0].comp_num
2
TESTS:
sage: forest = [NodeInfo(FOREST), [create_normal_node(2), \
[NodeInfo(SERIES), [create_normal_node(4), \
create_normal_node(5)]], [NodeInfo(PARALLEL), \
[create_normal_node(6), create_normal_node(7)]], \
create_normal_node(3), create_normal_node(1)]]
sage: vertex_status = {2: LEFT_OF_SOURCE, 3: SOURCE, \
1: RIGHT_OF_SOURCE, 4: LEFT_OF_SOURCE, \
5: LEFT_OF_SOURCE, 6: LEFT_OF_SOURCE, \
7: LEFT_OF_SOURCE}
sage: number_cocomponents(forest, vertex_status)
sage: forest[1][1][1][0][0].comp_num == 1 and \
forest[1][1][1][1][0].comp_num == 2
True
sage: forest[1][2][1][0][0].comp_num == 3 and \
forest[1][2][1][1][0].comp_num == 3
True
"""
cocomp_num = 0
for tree in root[1]:
# Only cocomponents are numbered
if tree[0].node_type == NORMAL and \
vertex_status[tree[1][0]] == SOURCE:
break
cocomp_num += recursively_number_cocomponents(tree, cocomp_num,
SERIES)
def recursively_number_cocomponents(tree, cocomp_num, by_type):
"""
Recursively number the nodes in the (co)components.
If the tree node_type is same as by_type then cocomp_num is incremented
before assigning to the subtree else entire tree is numbered by cocomp_num
INPUT:
- ``tree`` -- the forest which contains the cocomponents and components
- ``cocomp_num`` -- input number to be used as reference for numbering
the (co)components
- ``by_type`` -- type which determines how numbering is done
OUTPUT:
The value incremented to cocomp_num
EXAMPLES:
sage: from sage.graphs.modular_decomposition import NodeInfo, \
FOREST, SERIES, PARALLEL, LEFT_OF_SOURCE, SOURCE, RIGHT_OF_SOURCE, \
create_normal_node, recursively_number_cocomponents
sage: tree = [NodeInfo(SERIES), [create_normal_node(4), \
create_normal_node(5)]]
sage: recursively_number_cocomponents(tree, 1, SERIES)
2
sage: tree[0].comp_num
1
sage: tree[1][0][0].comp_num
1
sage: tree[1][1][0].comp_num
2
TESTS:
sage: from sage.graphs.modular_decomposition import NodeInfo, \
FOREST, SERIES, PARALLEL, LEFT_OF_SOURCE, SOURCE, RIGHT_OF_SOURCE, \
create_normal_node, recursively_number_cocomponents
sage: tree = [NodeInfo(SERIES), [create_normal_node(4), \
create_normal_node(5)]]
sage: recursively_number_cocomponents(tree, 1, SERIES)
2
sage: tree[0].comp_num == 1 and tree[1][0][0].comp_num == 1 and tree[1][1][0].comp_num == 2
True
"""
# inner function
def number_subtree(tree, number):
"""
set the ``comp_num`` for all the nodes in the subtree to ``number``
INPUT:
- ``tree`` -- tree to be numbered
- ``number`` -- number assigned to the tree
"""
tree[0].comp_num = number
if tree[0].node_type != NORMAL:
for subtree in tree[1]:
number_subtree(subtree, number)
orig_cocomp_num = cocomp_num
if tree[0].node_type==by_type:
# if node_type is same as tree's node_type then cocomp_num is
# incremented before assigning to each subtree
tree[0].comp_num = cocomp_num
for subtree in tree[1]:
number_subtree(subtree, cocomp_num)
cocomp_num += 1
else:
# entire tree is numbered by cocomp_num
number_subtree(tree, cocomp_num)
cocomp_num+=1
return cocomp_num - orig_cocomp_num
def assembly(graph, root, vertex_status, vertex_dist):
"""
Assemble the forest obtained after the promotion phase into a modular
decomposition tree.
INPUT:
- ``graph`` -- graph whose MD tree is to be computed
- ``root`` -- Forest which would be assembled into a MD tree
- ``vertex_status`` -- Dictionary which stores the position of
vertex with respect to the source
EXAMPLES:
sage: from sage.graphs.modular_decomposition import NodeInfo, \
FOREST, SERIES, PARALLEL, LEFT_OF_SOURCE, SOURCE, RIGHT_OF_SOURCE, \
create_normal_node, number_cocomponents, number_components, \
assembly
sage: g = Graph()
sage: g.add_vertices([1, 2, 3, 4, 5, 6, 7])
sage: g.add_edge(2, 3)
sage: g.add_edge(4, 3)
sage: g.add_edge(5, 3)
sage: g.add_edge(2, 6)
sage: g.add_edge(2, 7)
sage: g.add_edge(6, 1)
sage: forest = [NodeInfo(FOREST), [create_normal_node(2), \
[NodeInfo(SERIES), [create_normal_node(4), \
create_normal_node(5)]], create_normal_node(3), \
[NodeInfo(PARALLEL), [create_normal_node(6), \
create_normal_node(7)]], create_normal_node(1)]]
sage: vertex_status = {2: LEFT_OF_SOURCE, 3: SOURCE, \
1: RIGHT_OF_SOURCE, 4: LEFT_OF_SOURCE, \
5: LEFT_OF_SOURCE, 6: RIGHT_OF_SOURCE, \
7: RIGHT_OF_SOURCE}
sage: vertex_dist = {2: 1, 4: 1, 5: 1, 3: 0, 6: 2, 7: 2, 1: 3}
sage: forest[1][0][0].comp_num = 1
sage: forest[1][1][0].comp_num = 1
sage: forest[1][1][1][0][0].comp_num = 1
sage: forest[1][1][1][1][0].comp_num = 1
sage: number_components(forest, vertex_status)
sage: assembly(g, forest, vertex_status, vertex_dist)
sage: forest[1]
[[PRIME, [[NORMAL, [2]], [SERIES, [[NORMAL, [4]], [NORMAL, [5]]]], [NORMAL, [3]], [PARALLEL, [[NORMAL, [6]], [NORMAL, [7]]]], [NORMAL, [1]]]]]
sage: g.add_edge(4, 2)
sage: g.add_edge(5, 2)
sage: forest = [NodeInfo(FOREST), [create_normal_node(2), \
[NodeInfo(SERIES), [create_normal_node(4), \
create_normal_node(5)]], create_normal_node(3), \
[NodeInfo(PARALLEL), [create_normal_node(6), \
create_normal_node(7)]], create_normal_node(1)]]
sage: number_cocomponents(forest, vertex_status)
sage: assembly(g, forest, vertex_status, vertex_dist)
sage: forest[1]
[[PRIME, [[NORMAL, [2]], [SERIES, [[NORMAL, [4]], [NORMAL, [5]], [NORMAL, [3]]]], [PARALLEL, [[NORMAL, [6]], [NORMAL, [7]]]], [NORMAL, [1]]]]]
TESTS:
sage: g.add_edge(4, 2)
sage: g.add_edge(5, 2)
sage: forest = [NodeInfo(FOREST), [create_normal_node(2), \
[NodeInfo(SERIES), [create_normal_node(4), \
create_normal_node(5)]], create_normal_node(3), \
[NodeInfo(PARALLEL), [create_normal_node(6), \
create_normal_node(7)]], create_normal_node(1)]]
sage: number_cocomponents(forest, vertex_status)
sage: number_components(forest, vertex_status)
sage: assembly(g, forest, vertex_status, vertex_dist)
sage: forest[1]
[[PRIME, [[NORMAL, [2]], [SERIES, [[NORMAL, [4]], [NORMAL, [5]], [NORMAL, [3]]]], [PARALLEL, [[NORMAL, [6]], [NORMAL, [7]]]], [NORMAL, [1]]]]]
"""
# Maps index to the mu (co)component computed for the tree at the index
mu = {}
# Stores index of tree containing the source vertex in the forest
source_index = -1
# Maps index to list of vertices in the tree at the index in the forest
vertices_in_component = {}
# comp_num of parent should be equal to comp_num of its first child
update_comp_num(root)
for index, component in enumerate(root[1]):
if component[0].node_type == NORMAL and \
vertex_status[component[1][0]] == SOURCE:
source_index = root[1].index(component)
vertices_in_component[index] = get_vertices(component)
component[0].index_in_root = index
# compute mu values for (co)components
for index, component in enumerate(root[1]):
if index < source_index:
mu[index] = compute_mu_for_co_component(graph, index,
source_index, root,
vertices_in_component)
elif index > source_index:
mu[index] = compute_mu_for_component(graph, index,
source_index, root,
vertices_in_component)
mu[source_index] = root[1][source_index]
# stores the leftmost cocomponent included in the module containing
# source_index
left = root[1][source_index]
# stores the rightmost component included in the module containing
# source_index
right = root[1][source_index]
while len(root[1]) != 1:
# source_index is changed everytime a new module is formed therefore
# updated left or right are changed every time module is formed.
# First series module is attempted
result, source_index = check_series(root, left, right,
source_index, mu)
if result:
left = root[1][source_index][1][0]
continue
# If series module cant be formed, parallel is tried
result, source_index = check_parallel(graph, root, left, right,
source_index, mu, vertex_dist,
vertices_in_component)
if result:
right = root[1][source_index][1][-1]
continue
# Finally a prime module is formed if both
# series and parallel can not be created
result, source_index = check_prime(graph, root, left, right,
source_index, mu, vertex_dist,
vertices_in_component)
if result:
if root[1][source_index][1][0][0].index_in_root != -1:
left = root[1][source_index][1][0]
if root[1][source_index][1][-1][0].index_in_root != -1:
right = root[1][source_index][1][-1]
def update_comp_num(root):
"""
Set the comp_num of the parent to the comp_num of its first child
INPUT:
- ``root`` -- root of the tree whose nodes comp_num needs to be updated
"""
if root[0].node_type != NORMAL:
root[0].comp_num = root[1][0][0].comp_num
for child in root[1]:
update_comp_num(child)
def check_prime(graph, root, left, right,
source_index, mu, vertex_dist,
vertices_in_component):
"""
Assemble the forest to create a prime module.
INPUT:
- ``root`` - forest which needs to be assembled
- ``left`` - The leftmost fragment of the last module
- ``right`` - The rightmost fragment of the last module
- ``source_index`` - index of the tree containing the source vertex
- ``mu`` - dictionary which maps the (co)components with their mu values.
OUTPUT:
``[module_formed, source_index]`` where ``module_formed`` is ``True`` if
module is formed else ``False`` and ``source_index`` is the index of the
new module which contains the source vertex
EXAMPLES:
sage: from sage.graphs.modular_decomposition import NodeInfo, \
FOREST, SERIES, PARALLEL, LEFT_OF_SOURCE, SOURCE, RIGHT_OF_SOURCE, \
create_normal_node, number_cocomponents, number_components, \
check_prime, get_vertices, compute_mu_for_co_component, \
compute_mu_for_component
sage: g = Graph()
sage: g.add_vertices([1, 2, 3, 4, 5, 6, 7])
sage: g.add_edge(2, 3)
sage: g.add_edge(4, 3)
sage: g.add_edge(5, 3)
sage: g.add_edge(2, 6)
sage: g.add_edge(2, 7)
sage: g.add_edge(6, 1)
sage: forest = [NodeInfo(FOREST), [create_normal_node(2), \
[NodeInfo(SERIES), [create_normal_node(4), \
create_normal_node(5)]], create_normal_node(3), \
[NodeInfo(PARALLEL), [create_normal_node(6), \
create_normal_node(7)]], create_normal_node(1)]]
sage: vertex_status = {2: LEFT_OF_SOURCE, 3: SOURCE, \
1: RIGHT_OF_SOURCE, 4: LEFT_OF_SOURCE, \
5: LEFT_OF_SOURCE, 6: RIGHT_OF_SOURCE, \
7: RIGHT_OF_SOURCE}
sage: vertex_dist = {2: 1, 4: 1, 5: 1, 3: 0, 6: 2, 7: 2, 1: 3}
sage: source_index = 2
sage: vertices_in_component = {}
sage: mu = {}
sage: left = right = forest[1][2]
sage: for index, component in enumerate(forest[1]):
....: vertices_in_component[index] = get_vertices(component)
....: component[0].index_in_root = index
sage: for index, component in enumerate(forest[1]):
....: if index < source_index:
....: mu[index] = compute_mu_for_co_component(g, index,
....: source_index, forest,
....: vertices_in_component)
....: elif index > source_index:
....: mu[index] = compute_mu_for_component(g, index,
....: source_index, forest,
....: vertices_in_component)
sage: forest[1][0][0].comp_num = 1
sage: forest[1][1][0].comp_num = 1
sage: forest[1][1][1][0][0].comp_num = 1
sage: forest[1][1][1][1][0].comp_num = 1
sage: number_components(forest, vertex_status)
sage: check_prime(g, forest, left, right,
....: source_index, mu, vertex_dist,
....: vertices_in_component)
[True, 0]
sage: forest[1]
[[PRIME, [[NORMAL, [2]], [SERIES, [[NORMAL, [4]], [NORMAL, [5]]]], [NORMAL, [3]], [PARALLEL, [[NORMAL, [6]], [NORMAL, [7]]]], [NORMAL, [1]]]]]
"""
# stores the index of rightmost component included in the prime module
new_right_index = source_index + 1 if source_index + 1 < len(root[1]) \
else source_index
# stores the index of leftmost component included in the prime module
new_left_index = source_index - 1 if source_index - 1 >= 0 \
else source_index
# stores the indices of the cocomponents included in the prime module
# the cocomponents are extracted one by one for adding more components
left_queue = deque()
# stores the indices of the components included in the prime module
# the components are extracted one by one for adding more cocomponents
right_queue = deque()
if new_left_index != source_index:
left_queue.append(new_left_index)
if new_right_index != source_index:
right_queue.append(new_right_index)
while left_queue or right_queue:
if left_queue:
# cocomponent indices extracted from the queue
left_index = left_queue.popleft()
# more components added based on the below condition
while new_right_index < len(root[1]) - 1 and \
root[1][new_right_index][0].index_in_root < \
mu[left_index][0].index_in_root:
new_right_index += 1
right_queue.append(new_right_index)
# cocomponent added while cocomponent at left_index
# has cocomponent to its left with same comp_num
while has_left_cocomponent_fragment(root, left_index):
if left_index >= 1:
left_index -= 1
if new_left_index > left_index:
left_queue.append(left_index)
new_left_index = min(left_index, new_left_index)
if right_queue:
# component indices extracted from the queue
right_index = right_queue.popleft()
# more cocomponents added based on the below condition
while new_left_index > 0 and \
root[1][new_left_index][0].index_in_root > \
mu[right_index][0].index_in_root:
new_left_index -= 1
left_queue.append(new_left_index)
# component is added while component at right_index
# has component to its right with same comp_num
# or has a connected component with vertices at different
# level from the source vertex
while has_right_component_fragment(root, right_index) or \
has_right_layer_neighbor(graph, root,
right_index, vertex_dist,
vertices_in_component):
if has_right_layer_neighbor(graph, root,
right_index, vertex_dist,
vertices_in_component):
new_left_index = 0
new_right_index = len(root[1]) - 1
break
if right_index + 1 < len(root[1]):
right_index += 1
if new_right_index < right_index:
right_queue.append(right_index)
new_right_index = max(right_index, new_right_index)
node = create_prime_node()
# vertices or modules are added in the prime_module
for temp in range(new_left_index, new_right_index + 1):
node[1].append(root[1][temp])
# list elements included in the prime module
# are removed from the forest
root[1][new_left_index:new_right_index + 1] = []
#insert the newly created prime module in the forest
root[1].insert(new_left_index, node)
return [True, new_left_index]
def check_parallel(graph, root, left, right,
source_index, mu, vertex_dist,
vertices_in_component):
"""
Assemble the forest to create a parallel module.
INPUT:
- ``root`` -- forest which needs to be assembled
- ``left`` -- The leftmost fragment of the last module
- ``right`` -- The rightmost fragment of the last module
- ``source_index`` -- index of the tree containing the source vertex
- ``mu`` -- dictionary which maps the (co)components with their mu values.
OUTPUT:
``[module_formed, source_index]`` where ``module_formed`` is ``True`` if
module is formed else ``False`` and ``source_index`` is the index of the
new module which contains the source vertex
EXAMPLES:
sage: from sage.graphs.modular_decomposition import NodeInfo, \
FOREST, SERIES, PARALLEL, LEFT_OF_SOURCE, SOURCE, RIGHT_OF_SOURCE, \
create_normal_node, number_cocomponents, number_components, \
check_parallel, get_vertices, compute_mu_for_co_component, \
compute_mu_for_component
sage: g = Graph()
sage: g.add_vertices([1, 2, 3, 4, 5, 6, 7])
sage: g.add_edge(2, 3)
sage: g.add_edge(4, 3)
sage: g.add_edge(5, 3)
sage: g.add_edge(2, 6)
sage: g.add_edge(2, 7)
sage: g.add_edge(2, 1)
sage: g.add_edge(4, 1)
sage: forest = [NodeInfo(FOREST), [create_normal_node(2), \
[NodeInfo(SERIES), [create_normal_node(4), \
create_normal_node(5)]], create_normal_node(3), \
[NodeInfo(PARALLEL), [create_normal_node(6), \
create_normal_node(7), create_normal_node(1)]]]]
sage: vertex_status = {2: LEFT_OF_SOURCE, 3: SOURCE, \
1: RIGHT_OF_SOURCE, 4: LEFT_OF_SOURCE, \
5: LEFT_OF_SOURCE, 6: RIGHT_OF_SOURCE, \
7: RIGHT_OF_SOURCE}
sage: vertex_dist = {2: 1, 4: 1, 5: 1, 3: 0, 6: 2, 7: 2, 1: 2}
sage: source_index = 2
sage: vertices_in_component = {}
sage: mu = {}
sage: left = right = forest[1][2]
sage: for index, component in enumerate(forest[1]):
....: vertices_in_component[index] = get_vertices(component)
....: component[0].index_in_root = index
sage: for index, component in enumerate(forest[1]):
....: if index < source_index:
....: mu[index] = compute_mu_for_co_component(g, index,
....: source_index, forest,
....: vertices_in_component)
....: elif index > source_index:
....: mu[index] = compute_mu_for_component(g, index,
....: source_index, forest,
....: vertices_in_component)
sage: number_components(forest, vertex_status)
sage: check_parallel(g, forest, left, right,
....: source_index, mu, vertex_dist,
....: vertices_in_component)
[True, 2]
sage: forest[1]
[[NORMAL, [2]], [SERIES, [[NORMAL, [4]], [NORMAL, [5]]]], [PARALLEL, [[NORMAL, [3]], [NORMAL, [6]], [NORMAL, [7]], [NORMAL, [1]]]]]
"""
# stores the index of rightmost component included in the parallel module
new_right_index = source_index
while new_right_index + 1 < len(root[1]):
# component at new_right_index + 1 is added only if it doesn't have
# a component to its right with same comp_num
if has_right_component_fragment(root, new_right_index + 1):
break
# component at new_right_index + 1 is added only if it doesn't have a
# connected component to its right with vertices at different level
# from its vertices
if has_right_layer_neighbor(graph, root, new_right_index + 1,
vertex_dist, vertices_in_component):
break
# stores the index in root of new component to be added in the
# parallel module
i = root[1][new_right_index + 1][0].index_in_root
# condition for adding more components in the parallel module
if mu[i][0].index_in_root >= left[0].index_in_root:
new_right_index += 1
else:
break
# if new_right_index > source_index then only parallel
# module can be formed