Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

1340

1341

1342

1343

1344

1345

1346

1347

1348

1349

1350

1351

1352

1353

1354

1355

1356

1357

1358

1359

1360

1361

1362

1363

1364

1365

1366

1367

1368

1369

1370

1371

1372

1373

1374

1375

1376

1377

1378

1379

1380

1381

1382

1383

1384

1385

1386

1387

1388

1389

1390

1391

1392

1393

1394

1395

1396

1397

1398

1399

1400

1401

1402

1403

1404

1405

1406

r""" 

Suffix Tries and Suffix Trees 

""" 

#***************************************************************************** 

# Copyright (C) 2008 Franco Saliola <saliola@gmail.com> 

# 

# 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 six.moves import range 

from six import iteritems 

 

from sage.structure.sage_object import SageObject 

from sage.graphs.digraph import DiGraph 

from sage.sets.set import Set 

from sage.combinat.words.words import Words 

from sage.rings.integer import Integer 

 

################################################################################ 

# Suffix Tries 

################################################################################ 

 

class SuffixTrie(SageObject): 

def __init__(self, word): 

r""" 

Construct the suffix trie of the word w. 

 

The suffix trie of a finite word w is a data structure representing 

the factors of w. It is a tree whose edges are labelled with 

letters of w, and whose leafs correspond to suffixes of w. 

 

This is a straightforward implementation of Algorithm 1 from [1]. 

It constructs the suffix trie of w[:i] from that of w[:i-1]. 

 

A suffix trie is modelled as a deterministic finite-state automaton 

together with the suffix_link map. The set of states corresponds to 

factors of the word (below we write x' for the state corresponding 

to x); these are always 0, 1, .... The state 0 is the initial 

state, and it corresponds to the empty word. For the purposes of 

the algorithm, there is also an auxiliary state -1. The transition 

function t is defined as:: 

 

t(-1,a) = 0 for all letters a; and 

t(x',a) = y' for all x',y' \in Q such that y = xa, 

 

and the suffix link function is defined as:: 

 

suffix_link(0) = -1; 

suffix_link(x') = y', if x = ay for some letter a. 

 

REFERENCES: 

 

- [1] E. Ukkonen, "On-line construction of suffix trees", 

Algorithmica, 1995, volume 14, number 3, pages 249--260. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import SuffixTrie 

sage: w = Words("cao")("cacao") 

sage: t = SuffixTrie(w); t 

Suffix Trie of the word: cacao 

 

:: 

 

sage: e = Words("ab")() 

sage: t = SuffixTrie(e); t 

Suffix Trie of the word: 

sage: t.process_letter("a"); t 

Suffix Trie of the word: a 

sage: t.process_letter("b"); t 

Suffix Trie of the word: ab 

sage: t.process_letter("a"); t 

Suffix Trie of the word: aba 

 

TESTS:: 

 

sage: from sage.combinat.words.suffix_trees import SuffixTrie 

sage: w = Words("cao")("cacao") 

sage: s = SuffixTrie(w) 

sage: loads(dumps(s)) 

Suffix Trie of the word: cacao 

""" 

# Create the suffix trie for the empty word. 

self._active_state = 0 

self._transition_function = {} 

self._suffix_link = [-1] 

self._alphabet = word.parent().alphabet() 

 

# Process each letter, in order. 

W = word.parent() 

w = W() 

for letter in word: 

self._process_letter(W([letter])) 

 

def _process_letter(self, letter): 

r""" 

Process a letter. That is, modify the current suffix trie producing 

the suffix trie for ``self.word() + letter``. 

 

.. note:: 

 

``letter`` must occur within the alphabet of the word. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import SuffixTrie 

sage: t = SuffixTrie(Word("ababba")) 

sage: t._process_letter(Words("ab")("b")); t 

Suffix Trie of the word: ababbab 

""" 

r = self._active_state 

# While r is not the auxiliary vertex, or 

# there is not transition from r along letter, ... 

while r != -1 and \ 

(r,letter) not in self._transition_function: 

# adjoin a new state s 

s = len(self._suffix_link) 

self._suffix_link.append(None) 

# create a transition from r to s along letter 

self._transition_function[(r,letter)] = s 

if r != self._active_state: 

# update the suffix link 

self._suffix_link[old_s] = s 

old_s = s 

r = self._suffix_link[r] 

# update the suffix link for the last visited state 

if r == -1: 

self._suffix_link[old_s] = 0 

else: 

self._suffix_link[old_s] = self._transition_function[(r,letter)] 

# update the active state 

self._active_state = \ 

self._transition_function[(self._active_state, letter)] 

 

def process_letter(self, letter): 

r""" 

Modify ``self`` to produce the suffix trie for ``self.word() + 

letter``. 

 

.. note:: 

 

``letter`` must occur within the alphabet of the word. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import SuffixTrie 

sage: w = Words("ab")("ababba") 

sage: t = SuffixTrie(w); t 

Suffix Trie of the word: ababba 

sage: t.process_letter("a"); t 

Suffix Trie of the word: ababbaa 

 

TESTS:: 

 

sage: from sage.combinat.words.suffix_trees import SuffixTrie 

sage: w = Words("cao")("cacao") 

sage: t = SuffixTrie(w); t 

Suffix Trie of the word: cacao 

sage: t.process_letter("d") 

Traceback (most recent call last): 

... 

ValueError: d not in alphabet! 

""" 

# Make certain that letter is a word containing one letter. 

letter = Words(self._alphabet)([letter]) 

self._process_letter(letter) 

 

##### 

# The following are not necessary for constructing the suffix trie (just 

# the __init__ and process_letter are needed). They just add additional 

# functionality to the class. 

##### 

 

def _repr_(self): 

""" 

TESTS:: 

 

sage: from sage.combinat.words.suffix_trees import SuffixTrie 

sage: SuffixTrie(Word("abcba"))._repr_() 

'Suffix Trie of the word: abcba' 

""" 

return 'Suffix Trie of the %s' % repr(self.word()) 

 

def node_to_word(self, state=0): 

r""" 

Returns the word obtained by reading the edge labels from 0 to 

``state``. 

 

INPUT: 

 

- ``state`` - (default: 0) a state 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import SuffixTrie 

sage: w = Words("abc")("abcba") 

sage: t = SuffixTrie(w) 

sage: t.node_to_word(10) 

word: abcba 

sage: t.node_to_word(7) 

word: abcb 

""" 

if state == 0: 

return Words(self._alphabet)() 

# We first invert the transition function 

tf_inv = {b: a for a, b in iteritems(self._transition_function)} 

 

# Starting from the active state, 

# read labels along the unique path to the root. 

(u,letter) = tf_inv[state] 

w = letter 

s = u 

while s != 0: 

(u,letter) = tf_inv[s] 

w = letter * w 

s = u 

return w 

 

def word(self): 

r""" 

Returns the word whose suffix tree this is. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import SuffixTrie 

sage: w = Words("abc")("abcba") 

sage: t = SuffixTrie(w) 

sage: t.word() 

word: abcba 

sage: t.word() == w 

True 

""" 

return self.node_to_word(self._active_state) 

 

def __eq__(self,other): 

r""" 

If self and other have the same transition function, the same 

suffix link, and the same word, then they are equal. 

 

TESTS:: 

 

sage: from sage.combinat.words.suffix_trees import SuffixTrie 

sage: SuffixTrie(Word("cacao")) == SuffixTrie(Word("ababc")) 

False 

sage: W = Words("cao") 

sage: s = SuffixTrie(W("cacao")) 

sage: t = SuffixTrie(W()) 

sage: t.process_letter("c") 

sage: t.process_letter("a") 

sage: t.process_letter("c") 

sage: t.process_letter("a") 

sage: t.process_letter("o") 

sage: t == s 

True 

""" 

if not isinstance(other,SuffixTrie): 

return False 

return self._transition_function == other._transition_function \ 

and self._suffix_link == other._suffix_link \ 

and self.word() == other.word() 

 

def transition_function(self, node, word): 

r""" 

Returns the state reached by beginning at ``node`` and following the 

arrows in the transition graph labelled by the letters of ``word``. 

 

INPUT: 

 

- ``node`` - a node 

- ``word`` - a word 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import SuffixTrie 

sage: w = Words([0,1])([0,1,0,1,1]) 

sage: t = SuffixTrie(w) 

sage: all(t.transition_function(u, letter) == v 

....: for ((u, letter), v) in t._transition_function.items()) 

True 

""" 

if node == -1: 

return self.transition_function(0, word[1:]) 

if word.is_empty(): 

return 0 

if word.length() == 1: 

return self._transition_function[(node,word)] 

else: 

return self.transition_function( \ 

self._transition_function[(node,word[0:1])], word[1:]) 

 

def states(self): 

r""" 

Returns the states of the automaton defined by the suffix trie. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import SuffixTrie 

sage: w = Words([0,1])([0,1,1]) 

sage: t = SuffixTrie(w) 

sage: t.states() 

[0, 1, 2, 3, 4] 

 

:: 

 

sage: u = Words("aco")("cacao") 

sage: s = SuffixTrie(u) 

sage: s.states() 

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] 

""" 

return list(range(len(self._transition_function))) 

 

def suffix_link(self, state): 

r""" 

Evaluates the suffix link map of the suffix trie on ``state``. 

Note that the suffix link map is not defined on -1. 

 

INPUT: 

 

- ``state`` - a state 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import SuffixTrie 

sage: w = Words("cao")("cacao") 

sage: t = SuffixTrie(w) 

sage: list(map(t.suffix_link, range(13))) 

[-1, 0, 3, 0, 5, 1, 7, 2, 9, 10, 11, 12, 0] 

sage: t.suffix_link(0) 

-1 

 

TESTS:: 

 

sage: from sage.combinat.words.suffix_trees import SuffixTrie 

sage: w = Words("cao")("cacao") 

sage: t = SuffixTrie(w) 

sage: t.suffix_link([1]) 

Traceback (most recent call last): 

... 

TypeError: [1] is not an integer 

sage: t.suffix_link(-1) 

Traceback (most recent call last): 

... 

TypeError: suffix link is not defined for -1 

sage: t.suffix_link(17) 

Traceback (most recent call last): 

... 

TypeError: 17 is not a state 

""" 

if not isinstance(state, (int,Integer)): 

raise TypeError("%s is not an integer" % state) 

if state == -1: 

raise TypeError("suffix link is not defined for -1") 

if state not in range(len(self._suffix_link)): 

raise TypeError("%s is not a state" % state) 

return self._suffix_link[state] 

 

def active_state(self): 

r""" 

Returns the active state of the suffix trie. This is the state 

corresponding to the word as a suffix of itself. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import SuffixTrie 

sage: w = Words("cao")("cacao") 

sage: t = SuffixTrie(w) 

sage: t.active_state() 

8 

 

:: 

 

sage: u = Words([0,1])([0,1,1,0,1,0,0,1]) 

sage: s = SuffixTrie(u) 

sage: s.active_state() 

22 

""" 

return self._active_state 

 

def final_states(self): 

r""" 

Returns the set of final states of the suffix trie. These are the 

states corresponding to the suffixes of ``self.word()``. They are 

obtained be repeatedly following the suffix link from the active 

state until we reach 0. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import SuffixTrie 

sage: w = Words("cao")("cacao") 

sage: t = SuffixTrie(w) 

sage: t.final_states() == Set([8, 9, 10, 11, 12, 0]) 

True 

""" 

s = self._active_state 

F = [s] 

while s != 0: 

s = self._suffix_link[s] 

F.append(s) 

return Set(F) 

 

def has_suffix(self,word): 

r""" 

Return ``True`` if and only if ``word`` is a suffix of ``self.word()``. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import SuffixTrie 

sage: w = Words("cao")("cacao") 

sage: t = SuffixTrie(w) 

sage: [t.has_suffix(w[i:]) for i in range(w.length()+1)] 

[True, True, True, True, True, True] 

sage: [t.has_suffix(w[:i]) for i in range(w.length()+1)] 

[True, False, False, False, False, True] 

""" 

# Find the state corresponding to word, and 

# check to see if s is a final state. 

s = self.transition_function(0, word) 

q = self._active_state 

if q == s: 

return True 

else: 

while q != 0: 

q = self._suffix_link[q] 

if q == s: 

return True 

return False 

 

def to_digraph(self): 

r""" 

Returns a ``DiGraph`` object of the transition graph of the suffix 

trie. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import SuffixTrie 

sage: w = Words("cao")("cac") 

sage: t = SuffixTrie(w) 

sage: d = t.to_digraph(); d 

Digraph on 6 vertices 

sage: d.adjacency_matrix() 

[0 1 0 1 0 0] 

[0 0 1 0 0 0] 

[0 0 0 0 1 0] 

[0 0 0 0 0 1] 

[0 0 0 0 0 0] 

[0 0 0 0 0 0] 

""" 

dag = {} 

for ((u, letter), v) in iteritems(self._transition_function): 

dag.setdefault(u, {})[v] = letter 

return DiGraph(dag) 

 

def plot(self, layout='tree', tree_root=0, tree_orientation='up', 

vertex_colors=None, edge_labels=True, *args, **kwds): 

r""" 

Returns a Graphics object corresponding to the transition graph of 

the suffix trie. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import SuffixTrie 

sage: SuffixTrie(Word("cacao")).plot() 

Graphics object consisting of 38 graphics primitives 

 

TESTS:: 

 

sage: from sage.combinat.words.suffix_trees import SuffixTrie 

sage: type(SuffixTrie(Word("cacao")).plot()) 

<class 'sage.plot.graphics.Graphics'> 

""" 

tree = self.to_digraph() 

for (u,v,label) in tree.edge_iterator(): 

tree.set_edge_label(u, v, label.string_rep()) 

if vertex_colors is None: 

suffix_nodes = self.final_states() 

non_suffix_nodes = list(set(self.states()) - set(suffix_nodes)) 

vertex_colors = {'#fec7b8':suffix_nodes,'#ffffff':non_suffix_nodes} 

return tree.plot(layout=layout, tree_root=tree_root, 

tree_orientation=tree_orientation, 

vertex_colors=vertex_colors, edge_labels=edge_labels, 

*args, **kwds) 

 

def show(self, *args, **kwds): 

r""" 

Displays the output of ``self.plot()``. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import SuffixTrie 

sage: w = Words("cao")("cac") 

sage: t = SuffixTrie(w) 

sage: t.show() 

""" 

self.plot(*args, **kwds).show() 

return 

 

################################################################################ 

# Suffix Trees 

################################################################################ 

 

class ImplicitSuffixTree(SageObject): 

def __init__(self, word): 

r""" 

Construct the implicit suffix tree of a word w. 

 

The suffix tree of a word w is a compactification of the suffix 

trie for w. The compactification removes all nodes that have 

exactly one incoming edge and exactly one outgoing edge. It 

consists of two components: a tree and a word. Thus, instead of 

labelling the edges by factors of w, we can labelled them by 

indices of the occurrence of the factors in w. 

 

The following is a straightforward implementation of Ukkonen's 

on-line algorithm for constructing the implicit suffix tree [1]. 

It constructs the suffix tree for w[:i] from that of w[:i-1]. 

 

GENERAL IDEA. The suffix tree of w[:i+1] can be obtained from that 

of w[:i] by visiting each node corresponding to a suffix of w[:i] 

and modifying the tree by applying one of two rules (either append 

a new node to the tree, or split an edge into two). The "active 

state" is the node where the algorithm begins and the "suffix link" 

carries us to the next node that needs to be dealt with. 

 

TREE. The tree is modelled as an automaton, which is stored as a 

dictionary of dictionaries: it is keyed by the nodes of the tree, 

and the corresponding dictionary is keyed by pairs `(i,j)` of 

integers representing the word w[i-1:j]. This makes it faster to 

look up a particular transition beginning at a specific node. 

 

STATES/NODES. The states will always be -1, 0, 1, ..., n. The state 

-1 is special and is only used for the purposes of the algorithm. 

All transitions map -1 to 0, so this information is not explicitly 

stored in the transition function. 

 

EXPLICIT/IMPLICIT NODES. By definition, some of the nodes will not 

be states, but merely locations along an edge; these are called 

implicit nodes. A node r (implicit or explicit) is referenced as a 

pair (s,(k,p)) where s is an ancestor of r and w[k-1:p] is the word 

read by transitioning from s to r in the suffix trie. A reference 

pair is canonical if s is the closest ancestor of r. 

 

SUFFIX LINK. The algorithm makes use of a map from (some) nodes to 

other nodes, called the suffix link. This is stored as a 

dictionary. 

 

ACTIVE STATE. We store as ._active_state the active state of the 

tree, the state where the algorithm will begin when processing the 

next letter. 

 

RUNNING TIME. The running time and storage space of the algorithm 

is linear in the length of the word w (whereas for a suffix tree it 

is quadratic). 

 

REFERENCES: 

 

- [1] E. Ukkonen, "On-line construction of suffix trees", 

Algorithmica, 1995, volume 14, number 3, pages 249--260. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import ImplicitSuffixTree 

sage: w = Words("aco")("cacao") 

sage: t = ImplicitSuffixTree(w); t 

Implicit Suffix Tree of the word: cacao 

sage: ababb = Words([0,1])([0,1,0,1,1]) 

sage: s = ImplicitSuffixTree(ababb); s 

Implicit Suffix Tree of the word: 01011 

 

TESTS:: 

 

sage: from sage.combinat.words.suffix_trees import ImplicitSuffixTree 

sage: w = Words("cao")("cacao") 

sage: s = ImplicitSuffixTree(w) 

sage: loads(dumps(s)) 

Implicit Suffix Tree of the word: cacao 

""" 

# For constructing the suffix tree. 

self._transition_function = {0:{}} 

self._suffix_link = {0:-1} 

self._active_state = (0,(1,1)) 

self._letters = [] 

for letter in word: 

self._letters.append(letter) 

self._process_letter(letter) 

# _word is not needed for constructing the suffix tree, 

# but it is useful for the other methods. 

self._word = word 

 

def _process_letter(self, letter): 

r""" 

This is the main part of Ukkonen's algorithm. This corresponds to 

the algorithm "update" in [1]. 

 

.. note:: 

 

This function is a helper and does not update ``self._data`` and 

``self._word``. 

 

REFERENCES: 

 

- [1] E. Ukkonen, "On-line construction of suffix trees", 

Algorithmica, 1995, volume 14, number 3, pages 249--260. 

 

TESTS:: 

 

sage: from sage.combinat.words.suffix_trees import ImplicitSuffixTree 

sage: w = Words("aco")("caca") 

sage: t = ImplicitSuffixTree(w); t 

Implicit Suffix Tree of the word: caca 

sage: new_letter = "o" 

sage: t._letters.append("o") 

sage: t._process_letter("o") 

sage: t._word = Words("aco")("cacao") 

sage: t 

Implicit Suffix Tree of the word: cacao 

 

:: 

 

sage: W = Words([0,1]) 

sage: s = ImplicitSuffixTree(W([0,1,0,1])); s 

Implicit Suffix Tree of the word: 0101 

sage: s._letters.append(1) 

sage: s._process_letter(1) 

sage: s._word = W([0,1,0,1,1]) 

sage: s 

Implicit Suffix Tree of the word: 01011 

""" 

(s,(k,i)) = self._active_state 

old_r = 0 

(end_state, r) = self._test_and_split(s,(k,i-1),letter) 

while not end_state: 

# adjoin a new state rr and create a transition from r to rr 

rr = len(self._transition_function) 

self._transition_function[rr] = {} 

self._transition_function[r][(i,None)] = rr 

# update the suffix link, if necessary 

if old_r != 0: 

self._suffix_link[old_r] = r 

old_r = r 

# follow the suffix link to the next state 

(s, k) = self._canonize(self._suffix_link[s], (k,i-1)) 

(end_state, r) = self._test_and_split(s, (k,i-1), letter) 

# update the suffix link, if necessary 

if old_r != 0: 

self._suffix_link[old_r] = s 

# set the active state 

(s,k) = self._canonize(s,(k,i)) 

self._active_state = (s, (k, i+1)) 

return 

 

def _test_and_split(self, s, k_p, letter): 

r""" 

Helper function for _process_letter. Tests to see whether an edge 

needs to be split. Returns ``(True, state)``, where ``state`` is the 

next state to process (either a newly created state or the original s). 

 

TESTS:: 

 

sage: from sage.combinat.words.suffix_trees import ImplicitSuffixTree 

sage: w = Words("aco")("caca") 

sage: t = ImplicitSuffixTree(w) 

sage: t._letters.append(w.parent().alphabet().rank("o")) 

sage: t._test_and_split(0, (4,5), w.parent().alphabet().rank("o")) 

(False, 3) 

""" 

(k, p) = k_p 

if k <= p: 

# find the transition from s that begins with k-th letter 

((kk,pp), ss) = self._find_transition(s, self._letters[k-1]) 

if letter == self._letters[kk + p - k]: 

return (True, s) 

else: 

# replace transition above by transitions 

del self._transition_function[s][(kk,pp)] 

r = len(self._transition_function) 

self._transition_function[r] = {} 

self._transition_function[s][(kk, kk+p-k)] = r 

self._transition_function[r][(kk+p-k+1, pp)] = ss 

return (False, r) 

else: 

transition = self._find_transition(s, letter) 

if transition is None: 

return (False, s) 

else: 

return (True, s) 

 

def _canonize(self, s, k_p): 

r""" 

Given an implicit or explicit reference pair for a node, returns 

the canonical reference pair. 

 

Recall that a node r is referenced as (s, (k,p)), where s is an 

ancestor or r and w[k-1:p] is the word obtained by reading the edge 

labels along the path from s to r. A reference pair is canonical if 

s is the closest ancestor of r. 

 

TESTS:: 

 

sage: from sage.combinat.words.suffix_trees import ImplicitSuffixTree 

sage: t = ImplicitSuffixTree(Word("cacao")) 

sage: t._canonize(0,(3,5)) 

(3, 5) 

sage: t._canonize(0,(2,5)) 

(5, 3) 

""" 

(k, p) = k_p 

if p < k: 

return (s, k) 

else: 

((kk,pp), ss) = self._find_transition(s, self._letters[k-1]) 

while pp is not None and pp - kk <= p - k: 

k = k + pp - kk + 1 

s = ss 

if k <= p: 

((kk,pp), ss) = self._find_transition(s, self._letters[k-1]) 

return (s, k) 

 

def _find_transition(self, state, letter): 

r""" 

Returns the transition from state that begins with letter. Returns 

``None`` if no such transition exists. 

 

The transitions are stored as a dictionary of dictionaries: keyed 

by the nodes, with the corresponding dictionary keyed by pairs 

`(i,j)` of integers representing the word w[i-1:j]. 

 

._transition_function = {..., node: {(i,j): target_node, ...} } 

 

TESTS:: 

 

sage: from sage.combinat.words.suffix_trees import ImplicitSuffixTree 

sage: t = ImplicitSuffixTree(Word("cacao")) 

sage: t._find_transition(-1, "c") 

((0, 0), 0) 

sage: t._find_transition(0, "a") 

((2, 2), 5) 

sage: t._find_transition(0, "c") 

((1, 2), 3) 

sage: t._find_transition(5, "c") 

((3, None), 2) 

sage: t._find_transition(5, "a") 

 

:: 

 

sage: t = ImplicitSuffixTree(Word([0,1,0,1,1])) 

sage: t._find_transition(3, 1) 

((5, None), 4) 

""" 

if state == -1: 

return ((0, 0), 0) 

else: 

if state in self._transition_function: 

for ((k,p),s) in iteritems(self._transition_function[state]): 

if self._letters[k-1] == letter: 

return ((k,p), s) 

return None 

 

##### 

# The following are not necessary for constructing the implicit suffix 

# tree; they add additional functionality to the class. 

##### 

 

##### 

# Visualization 

##### 

 

def _repr_(self): 

r""" 

TESTS:: 

 

sage: from sage.combinat.words.suffix_trees import ImplicitSuffixTree 

sage: ImplicitSuffixTree(Word("abcba"))._repr_() 

'Implicit Suffix Tree of the word: abcba' 

""" 

return 'Implicit Suffix Tree of the %s' % repr(self.word()) 

 

def word(self): 

r""" 

Returns the word whose implicit suffix tree this is. 

 

TESTS:: 

 

sage: from sage.combinat.words.suffix_trees import ImplicitSuffixTree 

sage: ImplicitSuffixTree(Word([0,1,0,1,0])).word() == Word([0,1,0,1,0]) 

True 

""" 

return self._word 

 

def transition_function_dictionary(self): 

r""" 

Returns the transition function as a dictionary of dictionaries. 

The format is consistent with the input format for ``DiGraph``. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import ImplicitSuffixTree 

sage: W = Words("aco") 

sage: t = ImplicitSuffixTree(W("cac")) 

sage: t.transition_function_dictionary() 

{0: {1: (0, None), 2: (1, None)}} 

 

:: 

 

sage: W = Words([0,1]) 

sage: t = ImplicitSuffixTree(W([0,1,0])) 

sage: t.transition_function_dictionary() 

{0: {1: (0, None), 2: (1, None)}} 

""" 

d = {} 

for (u,v,(i,j)) in self.edge_iterator(): 

d.setdefault(u, {})[v] = (i,j) 

return d 

 

def to_digraph(self, word_labels=False): 

r""" 

Returns a ``DiGraph`` object of the transition graph of the suffix tree. 

 

INPUT: 

 

- ``word_labels`` - boolean (default: ``False``) if ``False``, labels 

the edges by pairs `(i, j)`; if ``True``, labels the edges by 

``word[i:j]``. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import ImplicitSuffixTree 

sage: W = Words([0,1,2]) 

sage: t = ImplicitSuffixTree(W([0,1,0,1,2])) 

sage: t.to_digraph() 

Digraph on 8 vertices 

""" 

if self._letters == []: 

d = {0:{}} 

return DiGraph(d) 

d = self.transition_function_dictionary() 

for u in d: 

for (v, (i, j)) in iteritems(d[u]): 

if word_labels: 

d[u][v] = self._word[i:j] 

elif j is None: 

d[u][v] = (i,len(self._letters)) 

return DiGraph(d) 

 

def plot(self, word_labels=False, layout='tree', tree_root=0, 

tree_orientation='up', vertex_colors=None, edge_labels=True, 

*args, **kwds): 

r""" 

Returns a Graphics object corresponding to the transition graph of 

the suffix tree. 

 

INPUT: 

 

- ``word_labels`` - boolean (default: ``False``) if ``False``, labels 

the edges by pairs `(i, j)`; if ``True``, labels the edges by 

``word[i:j]``. 

- ``layout`` - (default: ``'tree'``) 

- ``tree_root`` - (default: 0) 

- ``tree_orientation`` - (default: ``'up'``) 

- ``vertex_colors`` - (default: ``None``) 

- ``edge_labels`` - (default: ``True``) 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import ImplicitSuffixTree 

sage: ImplicitSuffixTree(Word('cacao')).plot(word_labels=True) 

Graphics object consisting of 23 graphics primitives 

sage: ImplicitSuffixTree(Word('cacao')).plot(word_labels=False) 

Graphics object consisting of 23 graphics primitives 

 

TESTS:: 

 

sage: from sage.combinat.words.suffix_trees import ImplicitSuffixTree 

sage: type(ImplicitSuffixTree(Word('cacao')).plot(word_labels=True)) 

<class 'sage.plot.graphics.Graphics'> 

sage: type(ImplicitSuffixTree(Word('cacao')).plot(word_labels=False)) 

<class 'sage.plot.graphics.Graphics'> 

""" 

tree = self.to_digraph(word_labels=word_labels) 

if word_labels: 

for (u,v,label) in tree.edge_iterator(): 

tree.set_edge_label(u, v, label.string_rep()) 

if vertex_colors is None: 

vertex_colors = {'#fec7b8':tree.vertices()} 

return tree.plot(layout=layout, tree_root=tree_root, 

tree_orientation=tree_orientation, 

vertex_colors=vertex_colors, edge_labels=edge_labels, 

*args, **kwds) 

 

def show(self, word_labels=None, *args, **kwds): 

r""" 

Displays the output of ``self.plot()``. 

 

INPUT: 

 

- ``word_labels`` - (default: ``None``) if ``False``, labels the 

edges by pairs `(i, j)`; if ``True``, labels the edges by 

``word[i:j]``. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import ImplicitSuffixTree 

sage: w = Words("cao")("cacao") 

sage: t = ImplicitSuffixTree(w) 

sage: t.show(word_labels=True) 

sage: t.show(word_labels=False) 

""" 

self.plot(word_labels=word_labels, *args, **kwds).show() 

return 

 

 

##### 

# Various methods 

##### 

 

def __eq__(self,other): 

r""" 

If self and other have the same transition function and the 

same word, then they are equal. 

 

TESTS:: 

 

sage: from sage.combinat.words.suffix_trees import ImplicitSuffixTree 

sage: w = Words([0,1,2])([0,1,0,1,2]) 

sage: u = Words([0,1,2])(iter([0,1,0,1,2]))[:5] 

sage: ImplicitSuffixTree(w) == ImplicitSuffixTree(u) 

True 

""" 

if not isinstance(other,ImplicitSuffixTree): 

return False 

return self._transition_function == other._transition_function \ 

and self._letters == other._letters 

 

def transition_function(self, word, node=0): 

r""" 

Returns the node obtained by starting from ``node`` and following the 

edges labelled by the letters of ``word``. Returns ``("explicit", 

end_node)`` if we end at ``end_node``, or ``("implicit", edge, d)`` 

if we end `d` spots along an edge. 

 

INPUT: 

 

- ``word`` - a word 

- ``node`` - (default: 0) starting node 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import ImplicitSuffixTree 

sage: W = Words([0,1,2]) 

sage: t = ImplicitSuffixTree(W([0,1,0,1,2])) 

sage: t.transition_function(W([0,1,0])) 

('implicit', (3, 1), 1) 

sage: t.transition_function(W([0,1,2])) 

('explicit', 4) 

sage: t.transition_function(W([0,1,2]), 5) 

('explicit', 2) 

sage: t.transition_function(W([0,1]), 5) 

('implicit', (5, 2), 2) 

""" 

if word.is_empty(): 

return "explicit", node 

((k,p),s) = self._find_transition(node, word[0]) 

if p is None: 

# test that word is a prefix of self._letters[k-1:] 

if word == self._word[k-1:(k-1)+word.length()]: 

if word.length() == len(self._letters) - k + 1: 

return "explicit", s 

else: 

edge = (node,s) 

return "implicit", edge, word.length() 

else: 

# find longest common prefix 

m = min(p-k+1,word.length()) 

i = 0 

while i < m and self._word[k-1+i] == word[i]: 

i += 1 

if i == p-k+1: 

return self.transition_function(word[p-k+1:],s) 

else: 

edge = (node,s) 

return "implicit", edge, i 

return "explicit", node 

 

def states(self): 

r""" 

Returns the states (explicit nodes) of the suffix tree. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import ImplicitSuffixTree 

sage: W = Words([0,1,2]) 

sage: t = ImplicitSuffixTree(W([0,1,0,1,2])) 

sage: t.states() 

[0, 1, 2, 3, 4, 5, 6, 7] 

""" 

return list(range(len(self._transition_function))) 

 

def suffix_link(self, state): 

r""" 

Evaluates the suffix link map of the implicit suffix tree on ``state``. 

Note that the suffix link is not defined for all states. 

 

The suffix link of a state `x'` that corresponds to the suffix `x` is 

defined to be -1 is `x'` is the root (0) and `y'` otherwise, where `y'` 

is the state corresponding to the suffix ``x[1:]``. 

 

INPUT: 

 

- ``state`` - a state 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import ImplicitSuffixTree 

sage: W = Words([0,1,2]) 

sage: t = ImplicitSuffixTree(W([0,1,0,1,2])) 

sage: t.suffix_link(3) 

5 

sage: t.suffix_link(5) 

0 

sage: t.suffix_link(0) 

-1 

sage: t.suffix_link(-1) 

Traceback (most recent call last): 

... 

TypeError: there is no suffix link from -1 

""" 

if state in self._suffix_link: 

return self._suffix_link[state] 

else: 

raise TypeError("there is no suffix link from %s" % state) 

 

def active_state(self): 

r""" 

Returns the active state of the suffix tree. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import ImplicitSuffixTree 

sage: W = Words([0,1,2]) 

sage: t = ImplicitSuffixTree(W([0,1,0,1,2])) 

sage: t.active_state() 

(0, (6, 6)) 

""" 

return self._active_state 

 

def process_letter(self, letter): 

r""" 

Modifies the current implicit suffix tree producing the implicit 

suffix tree for ``self.word() + letter``. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import ImplicitSuffixTree 

sage: w = Words("aco")("cacao") 

sage: t = ImplicitSuffixTree(w[:-1]); t 

Implicit Suffix Tree of the word: caca 

sage: t.process_letter(w[-1]); t 

Implicit Suffix Tree of the word: cacao 

 

:: 

 

sage: W = Words([0,1]) 

sage: s = ImplicitSuffixTree(W([0,1,0,1])); s 

Implicit Suffix Tree of the word: 0101 

sage: s.process_letter(W([1])[0]); s 

Implicit Suffix Tree of the word: 01011 

""" 

self._word = self._word * self._word._parent([letter]) 

self._letters.append(letter) 

self._process_letter(letter) 

 

def to_explicit_suffix_tree(self): 

r""" 

Converts self to an explicit suffix tree. It is obtained by 

processing an end of string letter as if it were a regular 

letter, except that no new leaf nodes are created (thus, the only 

thing that happens is that some implicit nodes become explicit). 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import ImplicitSuffixTree 

sage: w = Words("aco")("cacao") 

sage: t = ImplicitSuffixTree(w) 

sage: t.to_explicit_suffix_tree() 

 

:: 

 

sage: W = Words([0,1]) 

sage: s = ImplicitSuffixTree(W([0,1,0,1,1])) 

sage: s.to_explicit_suffix_tree() 

""" 

# append a new unique symbol to the word and process the new letter 

end_of_string = object() 

self._letters.append(end_of_string) 

(s,(k,i)) = self._active_state 

old_r = 0 

(end_state, r) = self._test_and_split(s,(k,i-1), end_of_string) 

while not end_state: 

(s, k) = self._canonize(self._suffix_link[s], (k,i-1)) 

(end_state, r) = self._test_and_split(s, (k,i-1), end_of_string) 

# remove the end of string symbol from the word 

self._letters.pop() 

return 

 

def edge_iterator(self): 

r""" 

Returns an iterator over the edges of the suffix tree. The 

edge from `u` to `v` labelled by `(i,j)` is returned as the tuple 

`(u,v,(i,j))`. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import ImplicitSuffixTree 

sage: sorted( ImplicitSuffixTree(Word("aaaaa")).edge_iterator() ) 

[(0, 1, (0, None))] 

sage: sorted( ImplicitSuffixTree(Word([0,1,0,1])).edge_iterator() ) 

[(0, 1, (0, None)), (0, 2, (1, None))] 

sage: sorted( ImplicitSuffixTree(Word()).edge_iterator() ) 

[] 

""" 

queue = [0] 

while queue: 

v=queue.pop() 

for ((i,j),u) in iteritems(self._transition_function[v]): 

yield (v,u,(i-1,j)) 

queue.append(u) 

 

def number_of_factors(self,n=None): 

r""" 

Count the number of distinct factors of ``self.word()``. 

 

INPUT: 

 

- ``n`` - an integer, or ``None``. 

 

OUTPUT: 

 

- If ``n`` is an integer, returns the number of distinct factors 

of length ``n``. If ``n`` is ``None``, returns the total number of 

distinct factors. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import ImplicitSuffixTree 

sage: t = ImplicitSuffixTree(Word([1,2,1,3,1,2,1])) 

sage: t.number_of_factors() 

22 

sage: t.number_of_factors(1) 

3 

sage: t.number_of_factors(9) 

0 

sage: t.number_of_factors(0) 

1 

 

:: 

 

sage: t = ImplicitSuffixTree(Word("cacao")) 

sage: t.number_of_factors() 

13 

sage: list(map(t.number_of_factors, range(10))) 

[1, 3, 3, 3, 2, 1, 0, 0, 0, 0] 

 

:: 

 

sage: t = ImplicitSuffixTree(Word("c"*1000)) 

sage: t.number_of_factors() 

1001 

sage: t.number_of_factors(17) 

1 

sage: t.number_of_factors(0) 

1 

 

:: 

 

sage: ImplicitSuffixTree(Word()).number_of_factors() 

1 

 

:: 

 

sage: blueberry = ImplicitSuffixTree(Word("blueberry")) 

sage: blueberry.number_of_factors() 

43 

sage: list(map(blueberry.number_of_factors, range(10))) 

[1, 6, 8, 7, 6, 5, 4, 3, 2, 1] 

""" 

if n is None: 

length_word = self.word().length() 

num_factors = 1 # empty word 

for (u,v,(i,j)) in self.edge_iterator(): 

if j is None: 

num_factors += length_word - i 

else: 

num_factors += j - i 

elif isinstance(n, (int,Integer)): 

length_word = self.word().length() 

num_factors = 0 

queue = [(0, 0)] 

while queue: 

(v,l) = queue.pop() 

if l == n: 

num_factors += 1 

if l < n: 

if self._transition_function[v] != {}: 

for ((i,j),u) in iteritems(self._transition_function[v]): 

if j is None: 

j = self.word().length() 

if j - i >= n - l: 

num_factors += 1 

else: 

queue.append((u,l+j-i+1)) 

else: 

raise TypeError("not an integer or None: %s" %s) 

return num_factors 

 

def factor_iterator(self,n=None): 

r""" 

Generate distinct factors of ``self``. 

 

INPUT: 

 

- ``n`` - an integer, or ``None``. 

 

OUTPUT: 

 

- If ``n`` is an integer, returns an iterator over all distinct 

factors of length ``n``. If ``n`` is ``None``, returns an iterator 

generating all distinct factors. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import ImplicitSuffixTree 

sage: sorted( ImplicitSuffixTree(Word("cacao")).factor_iterator() ) 

[word: , word: a, word: ac, word: aca, word: acao, word: ao, word: c, word: ca, word: cac, word: caca, word: cacao, word: cao, word: o] 

sage: sorted( ImplicitSuffixTree(Word("cacao")).factor_iterator(1) ) 

[word: a, word: c, word: o] 

sage: sorted( ImplicitSuffixTree(Word("cacao")).factor_iterator(2) ) 

[word: ac, word: ao, word: ca] 

sage: sorted( ImplicitSuffixTree(Word([0,0,0])).factor_iterator() ) 

[word: , word: 0, word: 00, word: 000] 

sage: sorted( ImplicitSuffixTree(Word([0,0,0])).factor_iterator(2) ) 

[word: 00] 

sage: sorted( ImplicitSuffixTree(Word([0,0,0])).factor_iterator(0) ) 

[word: ] 

sage: sorted( ImplicitSuffixTree(Word()).factor_iterator() ) 

[word: ] 

sage: sorted( ImplicitSuffixTree(Word()).factor_iterator(2) ) 

[] 

""" 

# Every factor is a prefix of a suffix, so we do a depth 

# first search of the implicit suffix tree of the word. 

if n is None: 

queue = [(0, self._word.parent()())] 

while queue: 

(v,w) = queue.pop() 

yield w 

if self._transition_function[v] != {}: 

for ((i,j),u) in iteritems(self._transition_function[v]): 

if j is None: 

j = self.word().length() 

for k in range(i,j): 

yield w * self.word()[i-1:k] 

queue.append((u,w*self.word()[i-1:j])) 

elif isinstance(n, (int,Integer)): 

queue = [(0, self._word.parent()())] 

while queue: 

(v,w) = queue.pop() 

length_w = w.length() 

if length_w == n: 

yield w 

if length_w < n: 

if self._transition_function[v] != {}: 

for ((i,j),u) in iteritems(self._transition_function[v]): 

if j is None: 

j = self.word().length() 

if j - i >= n - length_w: 

yield w*self.word()[i-1:i-1+n-length_w] 

else: 

queue.append((u,w*self.word()[i-1:j])) 

else: 

raise TypeError("not an integer or None: %s" %s) 

 

 

def LZ_decomposition(self): 

r""" 

Return a list of index of the begining of the block of the Lempel-Ziv 

decomposition of ``self.word`` 

 

The *Lempel-Ziv decomposition* is the factorisation `u_1...u_k` of a 

word `w=x_1...x_n` such that `u_i` is the longest prefix of `u_i...u_k` 

that has an occurence starting before `u_i` or a letter if this prefix 

is empty. 

 

OUTPUT: 

 

Return a list ``iB`` of index such that the blocks of the decomposition 

are ``self.word()[iB[k]:iB[k+1]]`` 

 

EXAMPLES:: 

 

sage: w = Word('abababb') 

sage: T = w.suffix_tree() 

sage: T.LZ_decomposition() 

[0, 1, 2, 6, 7] 

sage: w = Word('abaababacabba') 

sage: T = w.suffix_tree() 

sage: T.LZ_decomposition() 

[0, 1, 2, 3, 6, 8, 9, 11, 13] 

sage: w = Word([0, 0, 0, 1, 1, 0, 1]) 

sage: T = w.suffix_tree() 

sage: T.LZ_decomposition() 

[0, 1, 3, 4, 5, 7] 

sage: w = Word('0000100101') 

sage: T = w.suffix_tree() 

sage: T.LZ_decomposition() 

[0, 1, 4, 5, 9, 10] 

""" 

iB = [0] 

i = 0 

w = self.word() 

while i < len(w): 

l = 0 

((x, y), successor) = self._find_transition(0, w[i]) 

x = x-1 

while x < i+l: 

if y == None: 

l = len(w)-i 

else: 

l += y-x 

if i+l >= len(w): 

l = len(w)-i 

break 

((x, y), successor) = self._find_transition(successor, w[i+l]) 

x = x-1 

i += max(1, l) 

iB.append(i) 

return iB 

 

##### 

# Miscellaneous methods 

##### 

 

def uncompactify(self): 

r""" 

Returns the tree obtained from self by splitting edges so that they 

are labelled by exactly one letter. The resulting tree is 

isomorphic to the suffix trie. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import ImplicitSuffixTree, SuffixTrie 

sage: abbab = Words("ab")("abbab") 

sage: s = SuffixTrie(abbab) 

sage: t = ImplicitSuffixTree(abbab) 

sage: t.uncompactify().is_isomorphic(s.to_digraph()) 

True 

""" 

tree = self.to_digraph(word_labels=True) 

newtree = DiGraph() 

newtree.add_vertices(range(tree.order())) 

new_node = tree.order() + 1 

for (u,v,label) in tree.edge_iterator(): 

if len(label) == 1: 

newtree.add_edge(u,v) 

else: 

newtree.add_edge(u,new_node,label[0]); 

for w in label[1:-1]: 

newtree.add_edge(new_node,new_node+1,w) 

new_node += 1 

newtree.add_edge(new_node,v,label[-1]) 

new_node += 1 

return newtree 

 

def trie_type_dict(self): 

r""" 

Returns a dictionary in a format compatible with that of the suffix 

trie transition function. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.suffix_trees import ImplicitSuffixTree, SuffixTrie 

sage: W = Words("ab") 

sage: t = ImplicitSuffixTree(W("aba")) 

sage: d = t.trie_type_dict() 

sage: len(d) 

5 

sage: d # random 

{(4, word: b): 5, (0, word: a): 4, (0, word: b): 3, (5, word: a): 1, (3, word: a): 2} 

""" 

d = {} 

new_node = len(self._transition_function) 

for (u, dd) in iteritems(self._transition_function): 

for (sl, v) in iteritems(dd): 

w = self._word[sl[0]-1:sl[1]] 

if w.length() == 1: 

d[u,w] = v 

else: 

d[u,w[0:1]] = new_node 

for i in range(1,w.length()-1): 

d[new_node, w[i:i+1]] = new_node + 1 

new_node += 1 

d[new_node,w[-1:]] = v 

new_node += 1 

return d