@@ -17,8 +17,7 @@ This module provides an implementation of the heap queue algorithm, also known
17
17
as the priority queue algorithm.
18
18
19
19
Min-heaps are binary trees for which every parent node has a value less than
20
- or equal to any of its children.
21
- We refer to this condition as the heap invariant.
20
+ or equal to any of its children. We refer to this condition as the heap invariant.
22
21
23
22
For min-heaps, this implementation uses lists for which
24
23
``heap[k] <= heap[2*k+1] `` and ``heap[k] <= heap[2*k+2] `` for all *k * for which
@@ -170,7 +169,7 @@ The module also offers three general purpose functions based on heaps.
170
169
*reverse * is a boolean value. If set to ``True ``, then the input elements
171
170
are merged as if each comparison were reversed. To achieve behavior similar
172
171
to ``sorted(itertools.chain(*iterables), reverse=True) ``, all iterables must
173
- be sorted from largest to smallest.
172
+ be sorted from largest to smallest, like for example, a max-heap .
174
173
175
174
.. versionchanged :: 3.5
176
175
Added the optional *key * and *reverse * parameters.
@@ -306,23 +305,16 @@ entry as removed and add a new entry with the revised priority::
306
305
Theory
307
306
------
308
307
309
- Heaps are arrays for which ``a[k] <= a[2*k+1] `` and ``a[k] <= a[2*k+2] `` for all
310
- *k *, counting elements from 0. For the sake of comparison, non-existing
308
+ Min-heaps are arrays for which ``a[k] <= a[2*k+1] `` and ``a[k] <= a[2*k+2] `` for
309
+ all *k *, counting elements from 0. For the sake of comparison, non-existing
311
310
elements are considered to be infinite. The interesting property of a heap is
312
- that ``a[0] `` is always its smallest element.
311
+ that ``a[0] `` is always its smallest element. Max-heaps are the reverse.
313
312
314
313
The strange invariant above is meant to be an efficient memory representation
315
314
for a tournament. The numbers below are *k *, not ``a[k] ``::
316
315
317
- 0
318
-
319
- 1 2
320
-
321
- 3 4 5 6
322
-
323
- 7 8 9 10 11 12 13 14
324
-
325
- 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
316
+ .. figure :: heapq-binary-tree.png
317
+ :alt: Example (min-heap) binary tree.
326
318
327
319
In the tree above, each cell *k * is topping ``2*k+1 `` and ``2*k+2 ``. In a usual
328
320
binary tournament we see in sports, each cell is the winner over the two cells
@@ -346,19 +338,18 @@ last 0'th element you extracted. This is especially useful in simulation
346
338
contexts, where the tree holds all incoming events, and the "win" condition
347
339
means the smallest scheduled time. When an event schedules other events for
348
340
execution, they are scheduled into the future, so they can easily go into the
349
- heap. So, a heap is a good structure for implementing schedulers (this is what
350
- I used for my MIDI sequencer :-).
341
+ heap. So, a heap is a suitable structure for implementing schedulers.
351
342
352
343
Various structures for implementing schedulers have been extensively studied,
353
344
and heaps are good for this, as they are reasonably speedy, the speed is almost
354
345
constant, and the worst case is not much different than the average case.
355
346
However, there are other representations which are more efficient overall, yet
356
347
the worst cases might be terrible.
357
348
358
- Heaps are also very useful in big disk sorts. You most probably all know that a
359
- big sort implies producing "runs" (which are pre-sorted sequences, whose size is
349
+ Heaps are also very useful in big disk sorts. A
350
+ big sort implies producing "runs" (pre-sorted sequences, whose size is
360
351
usually related to the amount of CPU memory), followed by a merging passes for
361
- these runs, which merging is often very cleverly organised [ # ]_ . It is very
352
+ these runs, which merging is often very cleverly organised. It is very
362
353
important that the initial sort produces the longest runs possible. Tournaments
363
354
are a good way to achieve that. If, using all the memory available to hold a
364
355
tournament, you replace and percolate items that happen to fit the current run,
@@ -370,20 +361,4 @@ in the current tournament (because the value "wins" over the last output value),
370
361
it cannot fit in the heap, so the size of the heap decreases. The freed memory
371
362
could be cleverly reused immediately for progressively building a second heap,
372
363
which grows at exactly the same rate the first heap is melting. When the first
373
- heap completely vanishes, you switch heaps and start a new run. Clever and
374
- quite effective!
375
-
376
- In a word, heaps are useful memory structures to know. I use them in a few
377
- applications, and I think it is good to keep a 'heap' module around. :-)
378
-
379
- .. rubric :: Footnotes
380
-
381
- .. [# ] The disk balancing algorithms which are current, nowadays, are more annoying
382
- than clever, and this is a consequence of the seeking capabilities of the disks.
383
- On devices which cannot seek, like big tape drives, the story was quite
384
- different, and one had to be very clever to ensure (far in advance) that each
385
- tape movement will be the most effective possible (that is, will best
386
- participate at "progressing" the merge). Some tapes were even able to read
387
- backwards, and this was also used to avoid the rewinding time. Believe me, real
388
- good tape sorts were quite spectacular to watch! From all times, sorting has
389
- always been a Great Art! :-)
364
+ heap completely vanishes, you switch heaps and start a new run.
0 commit comments