2

On the Python official docs here, the following is mentioned regarding heaps:

A nice feature of this sort is that you can efficiently insert new items while the sort is going on, provided that the inserted items are not “better” than the last 0’th element you extracted. This is especially useful in simulation contexts, where the tree holds all incoming events, and the “win” condition means the smallest scheduled time. When an event schedules other events for execution, they are scheduled into the future, so they can easily go into the heap

I can only think of the following simple algorithm to implement a scheduler using heap:

# Priority queue using heap
pq = []
# The first element in the tuple represents the time at which the task should run.
task1 = (1, Task(...))
task2 = (2, Task(...))
add_task(pq, task1)
add_task(pq, task2)
# Add a few more root-level tasks
while pq:
    next_task = heapq.heappop()
    next_task.perform()
    for child_task in next_task.get_child_tasks():
        # Add new child tasks if available
        heapq.heappush(pq, child_task)

In this, where does sorting even come into the picture?
And even if the future child tasks have a time for the 'past', still this algorithm would work correctly.
So, why is the author warning about the child events only being scheduled for the future??
And what does this mean:

you can efficiently insert new items while the sort is going on, provided that the inserted items are not “better” than the last 0’th element you extracted.

Anmol Singh Jaggi
  • 8,376
  • 4
  • 36
  • 77
  • It looks like the author's paragraph about sorting has nothing to do with his paragraph about scheduling. The one paragraph about scheduling stands by itself, with no real connection between the discussion of sorting above, or the discussion of large sorts that follows. – Jim Mischel Jun 02 '19 at 18:16

1 Answers1

0

Heap are used as data structure for priority queue, in fact the fundamental in a min heap is that you have the lowest priority on top (or in max heap the higher priority on top). Therefore you can always extract lowest or highest element without search it.

You can always insert new element during the sorting, try to look how the heapSort works. Every time you need to build your heap and then extract the maximum value and put it on the end of the array, after you decrement the heap.length of 1. If you already sorted some numbers: [..., 13, 15, 16] and you insert a new number that is higher of the last element that is extracted (13 = 0’th element) you will get a wrong solution, because you will extract the new number but you won't put it in the right place: [1, 2, 5, 7, 14, 13, 15, 16]. It will be placed before 13 because it swap the element on heap.length position. This is obviously wrong so you can only insert element that are less of the 0’th element.

mini
  • 180
  • 10