Module: PageRank

Defined in:
lib/page_rank.rb,
lib/page_rank/base.rb,
lib/page_rank/dense.rb,
lib/page_rank/sparse.rb,
lib/page_rank/sparse_native.rb

Overview

A module for supporting Ruby implementations of PageRank. Rather than rely on one single implementation, this module allows for multiple implementations that may be beneficial in different scenarios.

Example

PageRank.calculate(strategy: :dense, damping: 0.8, max_iterations: 100) do
  add('nodeA', 'nodeC', weight: 4.3)
  add('nodeA', 'nodeE', weight: 2.1)
  add('nodeB', 'nodeC', weight: 3.6)
  add('nodeE', 'nodeD', weight: 1.9)
  add('nodeA', 'nodeC', weight: 5.3)
end

Defined Under Namespace

Classes: Base, Dense, Sparse, SparseNative

Class Method Summary collapse

Class Method Details

.calculate(**options, &block) ⇒ Hash<Object, Float>

Convenience method to quickly calculate PageRank. In the calling block, graph edges can be added.

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :strategy (Symbol)

    PageRank strategy to use (either :sparse or :dense)

  • :damping (Float)

    The probability of following the graph vs. randomly choosing a new node

  • :tolerance (Float)

    The desired accuracy of the results

Returns:

  • (Hash<Object, Float>)

    of nodes with rank


36
37
38
39
40
# File 'lib/page_rank.rb', line 36

def self.calculate(**options, &block)
  pr = new(**options)
  pr.instance_exec(&block)
  pr.calculate(**options)
end

.new(strategy: :sparse, **options) ⇒ PageRank::Base

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :strategy (Symbol)

    PageRank strategy to use (either :sparse or :dense)

  • :damping (Float)

    The probability of following the graph vs. randomly choosing a new node

  • :tolerance (Float)

    The desired accuracy of the results

Returns:


29
30
31
# File 'lib/page_rank.rb', line 29

def self.new(strategy: :sparse, **options)
  const_get(strategy.to_s.split('_').map(&:capitalize).join).new(**options)
end