Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: srinivas devaki Newsgroups: comp.lang.python Subject: Re: _siftup and _siftdown implementation Date: Sat, 6 Feb 2016 20:16:36 +0530 Lines: 68 Message-ID: References: <56b3e902$0$1613$c3e8da3$5496439d@news.astraweb.com> <56B4CD7B.70506@mail.de> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de W0bI03OJ1s5o4aeSj16JlwgsXQnKWlgmU4IpzVjj96FA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'true,': 0.04; 'executed': 0.07; 'great.': 0.07; 'cc:addr:python-list': 0.09; 'here?': 0.09; 'inserted': 0.09; 'files.': 0.13; '+91': 0.15; '2016': 0.16; 'attaching': 0.16; 'cause.': 0.16; 'comparison.': 0.16; 'editor,': 0.16; 'happened?': 0.16; 'heapq': 0.16; 'level)': 0.16; "module's": 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wow,': 0.16; 'wrote:': 0.16; 'element': 0.18; 'student': 0.20; 'versions': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'junior': 0.22; 'level,': 0.22; 'pos': 0.22; 'subject:skip:i 10': 0.22; 'code,': 0.23; 'feb': 0.23; 'header:In-Reply-To:1': 0.24; 'module': 0.25; "doesn't": 0.26; 'fri,': 0.27; 'message- id:@mail.gmail.com': 0.27; 'idea': 0.28; 'initial': 0.28; '0.5': 0.29; 'comparison': 0.29; "i'm": 0.30; 'comments': 0.30; 'code': 0.30; 'table': 0.32; 'related': 0.32; 'received:google.com': 0.35; 'behind': 0.35; 'text': 0.35; 'happened': 0.35; 'something': 0.35; 'level': 0.35; 'comment': 0.35; 'but': 0.36; 'lines': 0.36; 'received:209.85': 0.36; 'cases': 0.36; 'indian': 0.36; 'smaller': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'version': 0.38; 'received:209': 0.38; 'end': 0.39; 'means': 0.39; 'why': 0.39; 'test': 0.39; 'does': 0.39; 'some': 0.40; 'school': 0.62; 'total': 0.62; 'strange': 0.63; 'here': 0.66; 'levels': 0.70; 'children.': 0.76; 'sounds': 0.76; '50%': 0.79; '(3rd': 0.84; 'child,': 0.84; 'ph:': 0.84; 'remained': 0.84; 'decreased': 0.91 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=adXdKWrRvmR5F0hZumWF3kqanRbAcqd2ieA9L4PfOZo=; b=Ynvlhge7rABcCKOgeCTB5+Qvr9+0gXlhsQyiPfKgx81ZvHxwQjprIPI62YEZdMKd/5 LKb7l269nw8fXS4d6HCNCzKjSq8gaoKutgzxrIym953ka3aviwJZmEN73Gb0EEfh8Crc DstkhaRFxa2jMCM52Pd3IzoJQRu6zQfg0WUqXqbTRtIEtezNSE11TZtzmJ3TUQBWLhr4 UW6oeZGL6ltMK6jcWZcUsynYTtxOwVTTeu6loihNS/UKtJRlKCXT9eLQmlBG6kl0UPhP wV3V2gcGLygjU7UkJyjacNW/kMWwTArdxkScG2YYUXOKAFTMYOT7IOXRbT+lhjqmOdP0 rsNA== 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=adXdKWrRvmR5F0hZumWF3kqanRbAcqd2ieA9L4PfOZo=; b=hTYxKQGzwynhrroPPXzQsPSLqFeDWqYvIWcPuknRd+sEfLiXmu0H7xAwjN5NalrI9V Ajevl7FU+uKRiZZgW9fC38NeSB+YNotCE2BXABp3fzTxyTBAZ7vb8zLKA0Ra5BDS2zVw mylaTFdUmUayxwKwouIx+s0miNRZ1oqZGGc6636E9eS89AEJrY/Jfht3xsOTpmuB0pf7 6fC7CbdtjmWKJbTt0j3JIPYC255HH6l0yIC8SPX1OV2UPgTfxRcG8crbOHdyh5uCLUbg 7LLy7hEJy5xi7b+9Q5CxyzH6KFGb4TqU+2plZpH/+4ISVZVBCp1FmWnZiRwRyR/LZLAL XpjA== X-Gm-Message-State: AG10YORS0CLcNCNKBRrihk6uDLTGt6oGDRf0XSCy9XoX4//QlYhkTX3JQulJ56WJA629nTZOHziW3R5TpuO8eA== X-Received: by 10.50.141.193 with SMTP id rq1mr14850296igb.44.1454770036557; Sat, 06 Feb 2016 06:47:16 -0800 (PST) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 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:102586 As the comments in the heapq module says, in most of the cases (probability 0.837 [from knuth vol3]) the the new inserted element whose initial location is end of the array, which means that it will be larger than the `pos` children. So the old version and new version code is same in these cases because in old version the comparison is taking place at line 129, in the new version comparison is taking place inside _siftdown. SO the total number of comparisons are not decreased by those cases the other type case is the swapped element is smaller than `pos` parents, in this case the old version does a comparison and then goto _sifdown but in the new version _siftdown will be executed and then it comes to _siftup, here in 50% of the cases(len(heap last level) == len(heap) // 2) the pos doesn't have a child, so no comparison. ================================================================================================ `pos` | probability | `old version comparisons` | `new version comparisions` ================================================================================================ last level | 0.5 | 1 | 0 last - 1 level | 0.25 | 1 | 1 lower levels | 0.25 | 1 | >=1 ================================================================================================ as the new version doesn't do a comparison if it is in the last level, the optimization is occurring in that place. Which makes the reason behind the heapq module's choice of _siftup code is not at all related to this cause. PS: please copy the table to some text editor, for better visualization. On Fri, Feb 5, 2016 at 11:12 PM, srinivas devaki wrote: > wow, that's great. > you read a comment in the code, and you test it, to only find that it > is indeed true, > sounds ok, but feels great. :) > > Just experimenting a bit, I swaped the lines _siftdown and _siftup and something > strange happened the number of comparisions in both the versions remained same. > I'm attaching the files. > > do you have any idea why this happened? > > On Fri, Feb 5, 2016 at 9:57 PM, Sven R. Kunze wrote: >> >> Can we do better here? >> > I don't know, I have to read TAOP knuth article. > > -- > Regards > Srinivas Devaki > Junior (3rd yr) student at Indian School of Mines,(IIT Dhanbad) > Computer Science and Engineering Department > ph: +91 9491 383 249 > telegram_id: @eightnoteight -- Regards Srinivas Devaki Junior (3rd yr) student at Indian School of Mines,(IIT Dhanbad) Computer Science and Engineering Department ph: +91 9491 383 249 telegram_id: @eightnoteight