[issue35659] Add heapremove() function to heapq

2019-01-04 Thread Wanja Chresta


New submission from Wanja Chresta :

Heap Queues are extremely useful data structures. They can, for example, be 
used to implement Dijkstra's algorithm for finding the shortest paths between 
nodes in a graph in O(edge * log vertices) time instead of (edge * vertices) 
without heaps.

One operation such implementations need, though, is the possibility to modify 
an element in the heap (and thus having to reorder it afterwards) in O(log n) 
time. One can model such an operation by removing a specific element from the 
heap and then adding the modified element.

So far, heapq only allows removing the first element through heappop; this is 
not what we need. Instead, we would want to support a heapremove function that 
removes an arbitrary element in the heap (if it exists) and raises ValueError 
if the value is not present.

list.remove cannot be used, since it needs O(n) time.

heapremove can be easily implemented by using bisect.bisect_left since heap is 
always sorted:

def heapremove(heap, x):
  i = bisect.bisect_left(heap, x)
  if heap[i] == x:
del heap[i]
  else:
raise ValueError

c.f. remove method in 
https://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html

--
components: Library (Lib)
messages: 333024
nosy: Wanja Chresta
priority: normal
severity: normal
status: open
title: Add heapremove() function to heapq
versions: Python 3.8

___
Python tracker 
<https://bugs.python.org/issue35659>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35659] Add heapremove() function to heapq

2019-01-04 Thread Wanja Chresta


Change by Wanja Chresta :


--
type:  -> enhancement

___
Python tracker 
<https://bugs.python.org/issue35659>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35659] Add heapremove() function to heapq

2019-01-04 Thread Wanja Chresta


Wanja Chresta  added the comment:

After thinking about it some more I realised that this doesn't make sense since 
heapq is based on lists and lists have an insertion complexity of O(n). Thus, 
they'll never read the needed O(log n) and heapq is the wrong place.

Never mind.

--
resolution:  -> wont fix
stage:  -> resolved
status: open -> closed

___
Python tracker 
<https://bugs.python.org/issue35659>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com