Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Oscar Benjamin Newsgroups: comp.lang.python Subject: Re: How to remove item from heap efficiently? Date: Thu, 14 Jan 2016 11:53:52 +0000 Lines: 30 Message-ID: References: <568EEC40.7070807@mail.de> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de zVLhAYziNdJuDAdVHhrwUgFq7lgMvRM/atOFTeaiBDRA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'url:pypi': 0.03; 'essentially': 0.04; 'binary': 0.05; 'cc:addr:python-list': 0.09; 'subject:How': 0.09; 'timestamp': 0.09; 'trees': 0.09; 'python.': 0.11; '(say': 0.16; '2016': 0.16; 'cc:name:python list': 0.16; 'heap': 0.16; 'popping': 0.16; 'priority).': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:item': 0.16; 'subject:remove': 0.16; 'wrote:': 0.16; 'element': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; "python's": 0.23; 'header:In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'actual': 0.28; 'guess': 0.31; 'url:python': 0.33; 'third': 0.33; 'structure': 0.34; 'add': 0.34; 'received:google.com': 0.35; 'received:74.125.82': 0.35; 'item': 0.35; 'problem.': 0.35; 'but': 0.36; 'there': 0.36; 'url:org': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'january': 0.38; 'minimum': 0.38; 'google': 0.39; 'data': 0.39; 'does': 0.39; 'subject:from': 0.39; 'where': 0.40; 'url:0': 0.63; 'oldest': 0.66; 'oscar': 0.84; 'min': 0.91; 'have.': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=5YxBNl09pUqH2ilD07iwic7KSFQJ7Dp4Fte6vAAbegs=; b=otZ6MFRh7J3Ht4aA+IR5Q1Nc+X29DWceQ8K1/wxBHiLqa6yDRwlkYa4CdZGaeHTyTX ZsOFjMrSCVTiwKq8SIVPIe0UYZP3gspYaFw3ESA1tDtG7fAoKd1agdm+zgYOo1hO8ETc bQq0OkFPeUCwVJH8yEkMFJxUI2W1upIOnSab0ywaZGfTFS7Ns2ghCoF1SeXTU/ow3mka TKmw7nBDG2iSyrNBRwOmdugmP2RJBCOW3bp1qQSfYxg2mymQdsKiKI4s+7LC4IudUlFo xsNsSQ+qifulBXwecOFW5PU3570SXBBFKM0fGzyIa62xf2xx5ptYMXUln6us4GCLh9zk 16MA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=5YxBNl09pUqH2ilD07iwic7KSFQJ7Dp4Fte6vAAbegs=; b=XZAETlv31kWbXt+ytmMfOjnwPHQ+KLpAgTYJumLUh8md4JyZDK4Zv4k6UTKjXLV1H9 7yJOeYI2Cy6if0eOH7oELPgzJ5s0zTZmkgrVhqX/fNgjP1gNZeSp9PN2sKEJeBKpqNIP JTD8HLKqLdECHM3L6mW7vZFIE0M5Xj46cHaJGPNkKe6n7ycppTZ0HbCsbHxi5F3H8Ren ECO29suwNVHaiQEK6dCnqGOXCFz8DQjd27NR+vPY0STGZokLQYOef0/xljlo+aj9rZ4O VN/NVOtUtmdnsyRO3xpDfx6EAPzUIZiXR1GNj6IkMtbhXrG+L3Z1yQ7f3n9hNUGupUtP h6ZA== X-Gm-Message-State: ALoCoQmfth4m9IKdyCsXkeMfRf/URqynWOfSVlWYr8QPE0Zb5dbDTvNWIzRBa66tGkEjY7xHzE1P4krCVYqMKSZt6e1M0VHo/g== X-Received: by 10.112.166.100 with SMTP id zf4mr983563lbb.58.1452772452883; Thu, 14 Jan 2016 03:54:12 -0800 (PST) In-Reply-To: <568EEC40.7070807@mail.de> 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:101684 On 7 January 2016 at 22:52, Sven R. Kunze wrote: > > suppose, I need items sorted by two criteria (say timestamp and priority). > For that purpose, I use two heaps (heapq module): > > heapA # items sorted by timestamp > heapB # items sorted by priority > > Now my actual problem. When popping an item of heapA (that's the oldest > item), I need to remove the very same item from heapB, regardlessly where it > is in heapB. And vice versa. > > Is there a datastructure or a simple trick to achieve that in an efficient > matter? Essentially you want a data structure that can efficiently do the following: 1) add an arbitrary item 2) remove an arbitrary item 3) get/pop the minimum element So Python's set object does add/remove in O(1) but min in O(N). A heap does add and min in O(log(N)) but remove in O(N). Self-balancing binary trees can do all of add/remove/min in O(log(N)) so I guess that's better than what you currently have. Google shows many third party implementations for Python. Here's one: https://pypi.python.org/pypi/bintrees/2.0.2 -- Oscar