Class: PageRank::Dense
Overview
Implementation of PageRank using matrix multiplication
Ruby is not known for its speed, especial for math computations. As such this implementation is not well suited for large graphs, and it is especially not well suited for graphs that have a small edge-to-vertex ratio. The primary purpose of this implementation is to provide a checkpoint against other implementations to verify their validity.
If speed is desired, it would be best to implement a NativeDense class (and optionally NativeSparse) which would perform the algorithm in C.
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #add(source, dest, weight: 1.0) ⇒ nil
-
#initialize(**options) ⇒ Dense
constructor
Initialize with default damping and tolerance.
Methods inherited from Base
Constructor Details
#initialize(**options) ⇒ Dense
Initialize with default damping and tolerance. A maximum number of iterations can also be supplied (default is no maximum, i.e. iterate until tolerance).
22 23 24 25 26 27 28 |
# File 'lib/page_rank/dense.rb', line 22 def initialize(**) super(**) @out_links = [] @key_to_idx = {} @idx_to_key = {} end |
Instance Method Details
#add(source, dest, weight: 1.0) ⇒ nil
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/page_rank/dense.rb', line 33 def add(source, dest, weight: 1.0) return if source == dest source_idx = index(source) dest_idx = index(dest) @out_links[source_idx] ||= [] @out_links[source_idx][dest_idx] ||= 0.0 @out_links[source_idx][dest_idx] += weight nil end |