Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Robin Becker Newsgroups: comp.lang.python Subject: Re: How to remove item from heap efficiently? Date: Wed, 13 Jan 2016 16:40:29 +0000 Lines: 21 Message-ID: References: <568EEC40.7070807@mail.de> <568FE797.6090808@mail.de> <5692A795.3070904@mail.de> <5695276A.2020101@mail.de> <7A7E26D9-01E5-4D77-92B4-3491D86E1EA8@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de MyWpcdd1NPIXJafNwOyyzQ+dpNsFe7vefZUh3WVDbK7g== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'so?': 0.07; 'subject:How': 0.09; 'assumed': 0.09; 'dict': 0.09; 'mutable': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; 'assume': 0.11; 'jan': 0.11; 'wed,': 0.15; '2016': 0.16; 'accesses': 0.16; 'amortized': 0.16; 'heap': 0.16; 'heap,': 0.16; 'heapq': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'somehow,': 0.16; 'subject:item': 0.16; 'subject:remove': 0.16; 'wrote:': 0.16; 'looked': 0.16; 'element': 0.18; 'fixed.': 0.18; '(the': 0.22; 'amounts': 0.22; 'code,': 0.23; 'code.': 0.23; 'references': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'url:moin': 0.27; '13,': 0.29; 'lot.': 0.29; 'understand,': 0.29; 'url:wiki': 0.30; 'code': 0.30; 'guess': 0.31; 'says': 0.32; 'point': 0.33; 'class': 0.33; 'url:python': 0.33; 'operations.': 0.33; "i'll": 0.33; 'list': 0.34; 'quite': 0.35; 'but': 0.36; 'list,': 0.36; 'there': 0.36; 'url:org': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'seem': 0.37; 'delete': 0.38; 'subject:from': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'times': 0.63; 'received:109': 0.75; 'becker': 0.84; 'coupled': 0.84; 'technically': 0.91; 'average': 0.93 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: 109.174.168.73 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:101631 On 13/01/2016 15:34, srinivas devaki wrote: > On Wed, Jan 13, 2016 at 4:50 PM, Cem Karan wrote: >> >> Is that so? I'll be honest, I never tested its asymptotic performance, I just assumed that he had a dict coupled with a heap somehow, but I never looked into the code. >> > > I have just tested the code, the aymptotic performance is O(log(n)) > for all operations. Infact the code is very simple to understand, > technically the heapdict class is composed of a dict and heap, each element of > heap is a mutable list and dict stores references to that mutable list, > so that a specific element can be deleted in O(log(n)) > is this true? I looked at https://wiki.python.org/moin/TimeComplexity and it says that dict.get which I assume is used for accessing the heapq delete point can be large (the average time is O(1), but amortized over a lot of accesses can be O(n)). Apparently the history of sets/gets can affect individual times quite a lot. I seem to remember there was some kind of hashing attack against python dicts that would use up large amounts of time, but I guess that's now fixed. -- Robin Becker