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

r""" 

Huffman Encoding 

 

This module implements functionalities relating to Huffman encoding and 

decoding. 

 

AUTHOR: 

 

- Nathann Cohen (2010-05): initial version. 

 

 

Classes and functions 

===================== 

""" 

 

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

# Copyright (c) 2010 Nathann Cohen <nathann.cohen@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. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# http://www.gnu.org/licenses/ 

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

from __future__ import print_function 

 

import six 

from sage.structure.sage_object import SageObject 

 

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

# 

# Helper functions 

# 

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

 

def frequency_table(string): 

r""" 

Return the frequency table corresponding to the given string. 

 

INPUT: 

 

- ``string`` -- a string of symbols over some alphabet. 

 

OUTPUT: 

 

- A table of frequency of each unique symbol in ``string``. If ``string`` 

is an empty string, return an empty table. 

 

EXAMPLES: 

 

The frequency table of a non-empty string:: 

 

sage: from sage.coding.source_coding.huffman import frequency_table 

sage: str = "Stop counting my characters!" 

sage: T = sorted(frequency_table(str).items()) 

sage: for symbol, code in T: 

....: print("{} {}".format(symbol, code)) 

3 

! 1 

S 1 

a 2 

c 3 

e 1 

g 1 

h 1 

i 1 

m 1 

n 2 

o 2 

p 1 

r 2 

s 1 

t 3 

u 1 

y 1 

 

The frequency of an empty string:: 

 

sage: frequency_table("") 

{} 

""" 

d = {} 

for s in string: 

d[s] = d.get(s, 0) + 1 

return d 

 

class Huffman(SageObject): 

r""" 

This class implements the basic functionalities of Huffman codes. 

 

It can build a Huffman code from a given string, or from the information 

of a dictionary associating to each key (the elements of the alphabet) a 

weight (most of the time, a probability value or a number of occurrences). 

 

INPUT: 

 

- ``source`` -- can be either 

 

- A string from which the Huffman encoding should be created. 

 

- A dictionary that associates to each symbol of an alphabet a numeric 

value. If we consider the frequency of each alphabetic symbol, then 

``source`` is considered as the frequency table of the alphabet with 

each numeric (non-negative integer) value being the number of 

occurrences of a symbol. The numeric values can also represent weights 

of the symbols. In that case, the numeric values are not necessarily 

integers, but can be real numbers. 

 

In order to construct a Huffman code for an alphabet, we use exactly one of 

the following methods: 

 

#. Let ``source`` be a string of symbols over an alphabet and feed 

``source`` to the constructor of this class. Based on the input string, a 

frequency table is constructed that contains the frequency of each unique 

symbol in ``source``. The alphabet in question is then all the unique 

symbols in ``source``. A significant implication of this is that any 

subsequent string that we want to encode must contain only symbols that 

can be found in ``source``. 

 

#. Let ``source`` be the frequency table of an alphabet. We can feed this 

table to the constructor of this class. The table ``source`` can be a 

table of frequencies or a table of weights. 

 

Examples:: 

 

sage: from sage.coding.source_coding.huffman import Huffman, frequency_table 

sage: h1 = Huffman("There once was a french fry") 

sage: for letter, code in h1.encoding_table().items(): 

....: print("'{}' : {}".format(letter, code)) 

'a' : 0111 

' ' : 00 

'c' : 1010 

'e' : 100 

'f' : 1011 

'h' : 1100 

'o' : 11100 

'n' : 1101 

's' : 11101 

'r' : 010 

'T' : 11110 

'w' : 11111 

'y' : 0110 

 

We can obtain the same result by "training" the Huffman code with the 

following table of frequency:: 

 

sage: ft = frequency_table("There once was a french fry"); ft 

{' ': 5, 

'T': 1, 

'a': 2, 

'c': 2, 

'e': 4, 

'f': 2, 

'h': 2, 

'n': 2, 

'o': 1, 

'r': 3, 

's': 1, 

'w': 1, 

'y': 1} 

sage: h2 = Huffman(ft) 

 

Once ``h1`` has been trained, and hence possesses an encoding table, 

it is possible to obtain the Huffman encoding of any string 

(possibly the same) using this code:: 

 

sage: encoded = h1.encode("There once was a french fry"); encoded 

'11110110010001010000111001101101010000111110111111010001110010110101001101101011000010110100110' 

 

We can decode the above encoded string in the following way:: 

 

sage: h1.decode(encoded) 

'There once was a french fry' 

 

Obviously, if we try to decode a string using a Huffman instance which 

has been trained on a different sample (and hence has a different encoding 

table), we are likely to get some random-looking string:: 

 

sage: h3 = Huffman("There once were two french fries") 

sage: h3.decode(encoded) 

' wehnefetrhft ne ewrowrirTc' 

 

This does not look like our original string. 

 

Instead of using frequency, we can assign weights to each alphabetic 

symbol:: 

 

sage: from sage.coding.source_coding.huffman import Huffman 

sage: T = {"a":45, "b":13, "c":12, "d":16, "e":9, "f":5} 

sage: H = Huffman(T) 

sage: L = ["deaf", "bead", "fab", "bee"] 

sage: E = [] 

sage: for e in L: 

....: E.append(H.encode(e)) 

....: print(E[-1]) 

111110101100 

10111010111 

11000101 

10111011101 

sage: D = [] 

sage: for e in E: 

....: D.append(H.decode(e)) 

....: print(D[-1]) 

deaf 

bead 

fab 

bee 

sage: D == L 

True 

""" 

 

def __init__(self, source): 

r""" 

Constructor for Huffman. 

 

See the docstring of this class for full documentation. 

 

EXAMPLES:: 

 

sage: from sage.coding.source_coding.huffman import Huffman 

sage: str = "Sage is my most favorite general purpose computer algebra system" 

sage: h = Huffman(str) 

 

TESTS: 

 

Feeding anything else than a string or a dictionary:: 

 

sage: Huffman(Graph()) 

Traceback (most recent call last): 

... 

ValueError: Input must be either a string or a dictionary. 

""" 

 

# alphabetic symbol to Huffman encoding translation table 

self._character_to_code = [] 

# Huffman binary tree 

self._tree = None 

# index of each alphabetic symbol 

self._index = None 

 

if isinstance(source, six.string_types): 

self._build_code(frequency_table(source)) 

elif isinstance(source, dict): 

self._build_code(source) 

else: 

raise ValueError("Input must be either a string or a dictionary.") 

 

def _build_code_from_tree(self, tree, d, prefix): 

r""" 

Builds the Huffman code corresponding to a given tree and prefix. 

 

INPUT: 

 

- ``tree`` -- integer, or list of size `2` 

 

- ``d`` -- the dictionary to fill 

 

- ``prefix`` (string) -- binary string which is the prefix 

of any element of the tree 

 

EXAMPLES:: 

 

sage: from sage.coding.source_coding.huffman import Huffman 

sage: str = "Sage is my most favorite general purpose computer algebra system" 

sage: h = Huffman(str) 

sage: d = {} 

sage: h._build_code_from_tree(h._tree, d, prefix="") 

""" 

# This is really a recursive construction of a Huffman code. By 

# feeding this class a sufficiently large alphabet, it is possible to 

# exceed the maximum recursion depth and hence result in a RuntimeError. 

try: 

self._build_code_from_tree(tree[0], 

d, 

prefix="".join([prefix, "0"])) 

self._build_code_from_tree(tree[1], 

d, 

prefix="".join([prefix, "1"])) 

except TypeError: 

d[tree] = prefix 

 

def _build_code(self, dic): 

r""" 

Constructs a Huffman code corresponding to an alphabet with the given 

weight table. 

 

INPUT: 

 

- ``dic`` -- a dictionary that associates to each symbol of an alphabet 

a numeric value. If we consider the frequency of each alphabetic 

symbol, then ``dic`` is considered as the frequency table of the 

alphabet with each numeric (non-negative integer) value being the 

number of occurrences of a symbol. The numeric values can also 

represent weights of the symbols. In that case, the numeric values 

are not necessarily integers, but can be real numbers. In general, 

we refer to ``dic`` as a weight table. 

 

EXAMPLES:: 

 

sage: from sage.coding.source_coding.huffman import Huffman, frequency_table 

sage: str = "Sage is my most favorite general purpose computer algebra system" 

sage: h = Huffman(str) 

sage: d = {} 

sage: h._build_code(frequency_table(str)) 

""" 

from heapq import heappush, heappop 

heap = [] 

# Each alphabetic symbol is now represented by an element with 

# weight w and index i. 

for i, (s, w) in enumerate(dic.items()): 

heappush(heap, (w, i)) 

for i in range(1, len(dic)): 

weight_a, node_a = heappop(heap) 

weight_b, node_b = heappop(heap) 

heappush(heap, (weight_a + weight_b, [node_a, node_b])) 

# dictionary of symbol to Huffman encoding 

d = {} 

self._tree = heap[0][1] 

# Build the binary tree of a Huffman code, where the root of the tree 

# is associated with the empty string. 

self._build_code_from_tree(self._tree, d, prefix="") 

self._index = dict((i, s) for i, (s, w) in enumerate(dic.items())) 

self._character_to_code = dict( 

(s, d[i]) for i, (s, w) in enumerate(dic.items())) 

 

def encode(self, string): 

r""" 

Encode the given string based on the current encoding table. 

 

INPUT: 

 

- ``string`` -- a string of symbols over an alphabet. 

 

OUTPUT: 

 

- A Huffman encoding of ``string``. 

 

EXAMPLES: 

 

This is how a string is encoded and then decoded:: 

 

sage: from sage.coding.source_coding.huffman import Huffman 

sage: str = "Sage is my most favorite general purpose computer algebra system" 

sage: h = Huffman(str) 

sage: encoded = h.encode(str); encoded 

'00000110100010101011000011101010011100101010011011011100111101110010110100001011011111000001110101010001010110011010111111011001110100101000111110010011011100101011100000110001100101000101110101111101110110011000101011000111111101101111010010111001110100011' 

sage: h.decode(encoded) 

'Sage is my most favorite general purpose computer algebra system' 

""" 

if self._character_to_code: 

return "".join((self._character_to_code[x] for x in string)) 

 

def decode(self, string): 

r""" 

Decode the given string using the current encoding table. 

 

INPUT: 

 

- ``string`` -- a string of Huffman encodings. 

 

OUTPUT: 

 

- The Huffman decoding of ``string``. 

 

EXAMPLES: 

 

This is how a string is encoded and then decoded:: 

 

sage: from sage.coding.source_coding.huffman import Huffman 

sage: str = "Sage is my most favorite general purpose computer algebra system" 

sage: h = Huffman(str) 

sage: encoded = h.encode(str); encoded 

'00000110100010101011000011101010011100101010011011011100111101110010110100001011011111000001110101010001010110011010111111011001110100101000111110010011011100101011100000110001100101000101110101111101110110011000101011000111111101101111010010111001110100011' 

sage: h.decode(encoded) 

'Sage is my most favorite general purpose computer algebra system' 

 

TESTS: 

 

Of course, the string one tries to decode has to be a binary one. If 

not, an exception is raised:: 

 

sage: h.decode('I clearly am not a binary string') 

Traceback (most recent call last): 

... 

ValueError: Input must be a binary string. 

""" 

# This traverses the whole Huffman binary tree in order to work out 

# the symbol represented by a stream of binaries. This method of 

# decoding is really slow. A faster method is needed. 

# TODO: faster decoding implementation 

chars = [] 

tree = self._tree 

index = self._index 

for i in string: 

if i == "0": 

tree = tree[0] 

elif i == "1": 

tree = tree[1] 

else: 

raise ValueError("Input must be a binary string.") 

if not isinstance(tree, list): 

chars.append(index[tree]) 

tree = self._tree 

return "".join(chars) 

 

def encoding_table(self): 

r""" 

Returns the current encoding table. 

 

INPUT: 

 

- None. 

 

OUTPUT: 

 

- A dictionary associating an alphabetic symbol to a Huffman encoding. 

 

EXAMPLES:: 

 

sage: from sage.coding.source_coding.huffman import Huffman 

sage: str = "Sage is my most favorite general purpose computer algebra system" 

sage: h = Huffman(str) 

sage: T = sorted(h.encoding_table().items()) 

sage: for symbol, code in T: 

....: print("{} {}".format(symbol, code)) 

101 

S 00000 

a 1101 

b 110001 

c 110000 

e 010 

f 110010 

g 0001 

i 10000 

l 10011 

m 0011 

n 110011 

o 0110 

p 0010 

r 1111 

s 1110 

t 0111 

u 10001 

v 00001 

y 10010 

""" 

return self._character_to_code.copy() 

 

def tree(self): 

r""" 

Returns the Huffman tree corresponding to the current encoding. 

 

INPUT: 

 

- None. 

 

OUTPUT: 

 

- The binary tree representing a Huffman code. 

 

EXAMPLES:: 

 

sage: from sage.coding.source_coding.huffman import Huffman 

sage: str = "Sage is my most favorite general purpose computer algebra system" 

sage: h = Huffman(str) 

sage: T = h.tree(); T 

Digraph on 39 vertices 

sage: T.show(figsize=[20,20]) 

<BLANKLINE> 

""" 

from sage.graphs.digraph import DiGraph 

g = DiGraph() 

g.add_edges(self._generate_edges(self._tree)) 

return g 

 

def _generate_edges(self, tree, parent="", bit=""): 

""" 

Generate the edges of the given Huffman tree. 

 

INPUT: 

 

- ``tree`` -- a Huffman binary tree. 

 

- ``parent`` -- (default: empty string) a parent vertex with exactly 

two children. 

 

- ``bit`` -- (default: empty string) the bit signifying either the 

left or right branch. The bit "0" denotes the left branch and "1" 

denotes the right branch. 

 

OUTPUT: 

 

- An edge list of the Huffman binary tree. 

 

EXAMPLES:: 

 

sage: from sage.coding.source_coding.huffman import Huffman 

sage: H = Huffman("Sage") 

sage: T = H.tree() 

sage: T.edges(labels=None) 

[('0', 'S: 01'), ('0', 'a: 00'), ('1', 'e: 10'), ('1', 'g: 11'), ('root', '0'), ('root', '1')] 

""" 

if parent == "": 

u = "root" 

else: 

u = parent 

s = "".join([parent, bit]) 

try: 

left = self._generate_edges(tree[0], parent=s, bit="0") 

right = self._generate_edges(tree[1], parent=s, bit="1") 

L = [(u, s)] if s != "" else [] 

return left + right + L 

except TypeError: 

return [(u, "".join([self.decode(s), ": ", s]))]