Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pairing_heap

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pairing_heap

  • 3.1.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

PairingHeap

Ruby Style Guide

PairingHeap is a pure Ruby priority queue implementation using a pairing heap as the underlying data structure. While a pairing heap is asymptotically less efficient than the Fibonacci heap, it is usually faster in practice. This makes it a popular choice for Prim's MST or Dijkstra's algorithm implementations.

PairingHeap is currently being used as the priority queue data structure in RGL.

Also implementation without priority change support is provided(SimplePairingHeap), while the asymptotical complexity of the methods stay the same, bookkeeping of elements is not needed making, the constant smaller.

Installation

Add this line to your application's Gemfile:

gem 'pairing_heap'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install pairing_heap

Documentation

https://rubydoc.info/gems/pairing_heap

Usage

require 'pairing_heap'

# Simple PairingHeap
simple_heap = PairingHeap::SimplePairingHeap.new
simple_heap.push(:a, 1)
simple_heap.push(:b, 2)
simple_heap.push(:c, 3)
simple_heap.peek # => :a
simple_heap.peek_priority # => 1
simple_heap.pop_with_priority # => [:a, 1]
simple_heap.pop # => :b

# Min priority queue
best_defenses = PairingHeap::MinPriorityQueue.new
best_defenses.push('Chelsea', 24)
best_defenses.push('City', 30)
best_defenses.push('Tottenham', 25)
best_defenses.any? # => true
best_defenses.size # => 3
best_defenses.decrease_key('City', 15)
best_defenses.min # => 'City'
best_defenses.pop # => 'City'
best_defenses.extract_min # => 'Chelsea'
best_defenses.extract_min # => 'Tottenham'
best_defenses.any? # => false

# Max priority queue
best_teams = PairingHeap::MaxPriorityQueue.new
best_teams.push('City', 56)
best_teams.push('United', 46)
best_teams.push('Leicester', 46)
best_teams.increase_key('Leicester', 47)
best_teams.max # => 'City'
best_teams.pop # => 'City'
best_teams.extract_max # => 'Leicester'

# Custom comparator(it defaults to :<=.to_proc)
compare_by_length = PairingHeap::PairingHeap.new { |l, r| l.length <= r.length }
compare_by_length.push(:a, '11')
compare_by_length.push(:b, '1')
compare_by_length.push(:c, '11')
compare_by_length.change_priority(:c, '')
compare_by_length.peek # => :c
compare_by_length.pop # => :c
compare_by_length.pop # => :b
compare_by_length.pop # => :a

# SafeChangePriortyQueue
queue = PairingHeap::SafeChangePriorityQueue.new
queue.push(:a, 1)
queue.push(:b, 2)
queue.change_priority(:a, 3) # This works and does not throw an exception
queue.peek # => :b

See also test/performance_dijkstra.rb for a Dijkstra algorithm implementation.

Changes from lazy_priority_queue

This API is a drop-in replacement of lazy_priority_queue with the following differences:

  • Custom comparator provided to constructur, compares weights, not internal nodes
  • change_priority returns self instead of the first argument
  • enqueue returns self instead of the first argument
  • Queue classes are in the PairingHeap namespace, so require 'pairing_heap does not load MinPriorityQueue to the global scope
  • top_condition constructor argument is removed

Time Complexity

OperationTime complexityAmortized time complexity
enqueueO(1)O(1)
peekO(1)O(1)
dequeueO(n)O(log n)
* change_priorityO(1)o(log n)
* deleteO(n)O(log n)
^ mergeO(1)O(1)

* Not available in SimplePairingHeap

^ Only available in SimplePairingHeap

Benchmarks

I picked the three fastest pure Ruby priority queue implementations I was aware of for the comparison:

  • lazy_priority_queue that uses a lazy binomial heap. This is probably the most popular option. It was used in RGL until PairingHeap replaced it.
  • Pure Ruby implementation of Fibonacci Heap from priority-queue (link to source)
  • rb_heap that uses a binary heap. Note however that this implementation does not support change_priority operation.

All tests except for the third one were executed by benchmark-ips with parameters time = 60 and warmup = 15, on an Intel(R) Core(TM) i7-10700K CPU @ 3.80GHz.

Stress test without changing priority test(N = 1000) source code

Original performance test from lazy_priority_queue

A stress test of 1,000,000 operations: starting with 1,000 pushes/0 pops, following 999 pushes/1 pop, and so on till 0 pushes/1000 pops.

ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin23]
LibraryIterationsSecondsIterations per second
pairing_heap (SimplePairingHeap)2662.2494270.419
pairing_heap (PairingHeap)1761.6248060.276(1.52x slower)
rb_heap1663.6565020.251(1.67x slower)
lazy_priority_queue763.3391920.111(3.79x slower)
Fibonacci569.0107370.073(5.77x slower)
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) +YJIT [x86_64-darwin23]
LibraryIterationsSecondsIterations per second
pairing_heap (SimplePairingHeap)3960.7256890.642
rb_heap3160.3703480.514(1.25x slower)
pairing_heap (PairingHeap)2562.2695770.402(1.6x slower)
lazy_priority_queue962.1447100.145(4.43x slower)
Fibonacci865.0643850.123(5.22x slower)
jruby 9.4.5.0 (3.1.4) 2023-11-02 1abae2700f OpenJDK 64-Bit Server VM 21+35-2513 on 21+35-2513 +indy +jit [x86_64-darwin]
LibraryIterationsSecondsIterations per second
pairing_heap (SimplePairingHeap)4360.7346610.709
rb_heap3461.6772280.552(1.28x slower)
pairing_heap (PairingHeap)2861.2843820.458(1.55x slower)
Fibonacci2261.3968970.359(1.97x slower)
lazy_priority_queue2061.8414630.324(2.19x slower)
truffleruby 23.1.2, like ruby 3.2.2, Oracle GraalVM JVM [x86_64-darwin]
LibraryIterationsSecondsIterations per second
pairing_heap (SimplePairingHeap)20260.2256393.388
rb_heap14060.1908812.357(1.44x slower)
pairing_heap (PairingHeap)10060.3736101.692(2x slower)
lazy_priority_queue3161.1792440.510(6.65x slower)
Fibonacci1164.4134190.171(19.81x slower)

Stress test with changing priority(N = 1000) source code

A stress test of 1,501,500 operations: starting with 1,000 pushes/1000 change_priorities/0 pops, following 999 pushes/999 change_priorities/1 pop, and so on till 0 pushes/0 change_priorities/1000 pops.

ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin23]
LibraryIterationsSecondsIterations per second
pairing_heap1560.8178780.247
lazy_priority_queue763.990376s0.109(2.26x slower)
Fibonacci570.148980s0.071(3.47x slower)
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) +YJIT [x86_64-darwin23]
LibraryIterationsSecondsIterations per second
pairing_heap2262.4292640.353
lazy_priority_queue963.4648180.142(2.49x slower)
Fibonacci867.9088120.118(2.99x slower)
jruby 9.4.5.0 (3.1.4) 2023-11-02 1abae2700f OpenJDK 64-Bit Server VM 21+35-2513 on 21+35-2513 +indy +jit [x86_64-darwin]
LibraryIterationsSecondsIterations per second
pairing_heap2761.7095170.438
Fibonacci2061.4956360.326(1.34x slower)
lazy_priority_queue1963.4016010.309(1.46x slower)
truffleruby 23.1.2, like ruby 3.2.2, Oracle GraalVM JVM [x86_64-darwin]
LibraryIterationsSecondsIterations per second
pairing_heap9360.1257501.577
lazy_priority_queue2662.372660s0.419(3.77x slower)
Fibonacci1162.620172s0.177(8.92x slower)

Stress test with changing priority or push/pop(test ignored in summary) source code

Start with 500 pushes, then:

  • If queue supports changing priority 500 change_priority calls, then 500 pops
  • If does not support changing priority 500 push calls, then 1000 pops
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin23]
LibraryIterations per second
pairing_heap (PairingHeap)748.9
lazy_priority_queue388.6(1.93x slower)
pairing_heap (SimplePairingHeap)336.2(2.23x slower)
Fibonacci225.9(3.31x slower)
rb_heap215.2(3.48x slower)
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) +YJIT [x86_64-darwin23]
LibraryIterations per second
pairing_heap1276
pairing_heap(SimplePairingHeap)625.6(2.04x slower)
lazy_priority_queue533.36(2.39x slower)
Fibonacci495.519(2.57x slower)
rb_heap453.323(2.81x slower)
jruby 9.4.5.0 (3.1.4) 2023-11-02 1abae2700f OpenJDK 64-Bit Server VM 21+35-2513 on 21+35-2513 +indy +jit [x86_64-darwin]
LibraryIterations per second
pairing_heap(PairingHeap)1377
Fibonacci1088(1.27x slower)
lazy_priority_queue953.935(1.44x slower)
pairing_heap(SimplePairingHeap)621.35(2.22x slower)
rb_heap595.11(2.31x slower)
truffleruby 23.1.2, like ruby 3.2.2, Oracle GraalVM JVM [x86_64-darwin]
LibraryIterations per second
pairing_heap(PairingHeap)12712
pairing_heap(SimplePairingHeap)9447(1.35x slower)
rb_heap4009(3.17x slower)
Fibonacci2793(4.55x slower)
lazy_priority_queue1188(10.7x slower)

Simple Dijkstra's algorithm implementation source code

Heaps that support change_priority operation use it. Heaps that do not support it use dijkstra implementation that do not rely on change_priority instead and do additional pops and pushes instead(see Dijkstra-NoDec from Priority Queues and Dijkstra’s Algorithm).

ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin23]
LibraryIterationsSecondsIterations per second
pairing_heap (SimplePairingHeap)4160.1003160.682
pairing_heap (PairingHeap)3861.0036070.623(1.09x slower)
rb_heap3061.4862160.488(1.40x slower)
lazy_priority_queue2360.2517640.382(1.79x slower)
Fibonacci1361.1586220.213(3.21x slower)
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) +YJIT [x86_64-darwin23]
LibraryIterationsSecondsIterations per second
pairing_heap (SimplePairingHeap)6560.8058821.070
pairing_heap (PairingHeap)6060.7908420.987(1.08x slower)
rb_heap5460.9176790.887(1.21x slower)
lazy_priority_queue3060.7127860.495(2.16x slower)
Fibonacci2461.9007150.388(2.76x slower)
jruby 9.4.5.0 (3.1.4) 2023-11-02 1abae2700f OpenJDK 64-Bit Server VM 21+35-2513 on 21+35-2513 +indy +jit [x86_64-darwin]
LibraryIterationsSecondsIterations per second
rb_heap7060.0779281.168
pairing_heap (SimplePairingHeap)6660.4209351.094(1.07x slower)
pairing_heap (PairingHeap)6460.1144671.066(1.1x slower)
Fibonacci5460.4269710.898(1.30x slower)
lazy_priority_queue4960.6369630.809(1.44x slower)
truffleruby 23.1.2, like ruby 3.2.2, Oracle GraalVM JVM [x86_64-darwin]
LibraryIterationsSecondsIterations per second
pairing_heap (SimplePairingHeap)28860.1022784.936
pairing_heap (PairingHeap)23260.1590573.936(1.25x slower)
rb_heap22760.0824823.881(1.27x slower)
Fibonacci10160.0766911.721(2.87x slower)
lazy_priority_queue6660.7715691.1(4.49x slower)

Summary

Change priority required
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin23]
LibrarySlower geometric mean
pairing_heap1
lazy_priority_queue2.1x slower
Fibonacci3.38x slower
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) +YJIT [x86_64-darwin23]
LibrarySlower geometric mean
pairing_heap1
lazy_priority_queue2.55x slower
Fibonacci2.74x slower
jruby 9.4.5.0 (3.1.4) 2023-11-02 1abae2700f OpenJDK 64-Bit Server VM 21+35-2513 on 21+35-2513 +indy +jit [x86_64-darwin]
LibrarySlower geometric mean
pairing_heap1
Fibonacci1.267x slower
lazy_priority_queue1.396x slower
truffleruby 23.1.2, like ruby 3.2.2, Oracle GraalVM JVM [x86_64-darwin]
LibrarySlower geometric mean
pairing_heap1
lazy_priority_queue3.54x slower
Fibonacci5.86x slower
Change priority not required
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin23]
LibrarySlower geometric mean
pairing_heap (SimplePairingHeap)1
pairing_heap (PairingHeap)1.29x slower
rb_heap1.53x slower
lazy_priority_queue2.6x slower
Fibonacci4.29x slower
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) +YJIT [x86_64-darwin23]
LibrarySlower geometric mean
pairing_heap (SimplePairingHeap)1
rb_heap1.227x slower
pairing_heap (PairingHeap)1.316x slower
lazy_priority_queue3.094x slower
Fibonacci3.79x slower
jruby 9.4.5.0 (3.1.4) 2023-11-02 1abae2700f OpenJDK 64-Bit Server VM 21+35-2513 on 21+35-2513 +indy +jit [x86_64-darwin]
LibrarySlower geometric mean
pairing_heap (SimplePairingHeap)1.033x slower
rb_heap1.133x slower
pairing_heap (PairingHeap)1.302x slower
Fibonacci1.602x slower
lazy_priority_queue1.777x slower
truffleruby 23.1.2, like ruby 3.2.2, Oracle GraalVM JVM [x86_64-darwin]
LibrarySlower geometric mean
pairing_heap (SimplePairingHeap)1
rb_heap1.35x slower
pairing_heap (PairingHeap)1.58x slower
lazy_priority_queue5.46x slower
Fibonacci7.54x slower

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/mhib/pairing_heap.

License

The gem is available as open source under the terms of the MIT License.

FAQs

Package last updated on 11 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc