Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
45 lines (33 sloc) 1.02 KB
# Ilya Grigorik
# Generate a N page web, with each page randomly linked to x others, and plot it
require "rubygems"
require "gsl"
include GSL
# INPUT: link structure matrix, probability of following a link
# OUTPUT: pagerank scores
def pagerank(g, s)
raise if g.size1 != g.size2
i = Matrix.I(g.size1) # identity matrix
p = (1.0/g.size1) * Matrix.ones(g.size1,1) # teleportation vector
t = 1-s # probability of teleportation
# INPUT: number of pages, and number of outgoing links for this page
# OUTPUT: n-dimensional column vector with randomly set links
def random_column(pages, links, index)
v = Vector.alloc(pages)
links.times { |n| v[rand(pages)] = 1 }
v[index] = 1
return (1.0/v.sum) * v
def random_network(pages, links)
g = Matrix.alloc(pages, pages)
pages.times { |n| g.set_col(n,random_column(pages, links-1, n)) }
return g
g = random_network(1000,10)
pr = pagerank(g,0.85)
puts "Pagerank vector: ", pr
Something went wrong with that request. Please try again.