Permalink
Browse files

pagerank examples: simple, n-web, zipf web

  • Loading branch information...
0 parents commit 8325e0dba44fd7594220b3be21dac8a7b3851164 @igrigorik committed May 2, 2009
Showing with 137 additions and 0 deletions.
  1. +44 −0 pagerank-gsl.rb
  2. +45 −0 pagerank-n-web.rb
  3. +48 −0 pagerank-zipf-web.rb
@@ -0,0 +1,44 @@
+# Ilya Grigorik
+#
+# Bruteforce implementation of PageRank algorithm, works for small(ish) graphs
+# of up to couple of thousand pages.
+#
+
+require "rubygems"
+require "gsl"
+
+include GSL
+
+# INPUT: link structure matrix
+# OUTPUT: pagerank scores
+def pagerank(g)
+ 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
+
+ s = 0.85 # probability of following a link
+ t = 1-s # probability of teleportation
+
+ t*((i-s*g).invert)*p
+end
+
+# page 1 -> page 2 (0.33)
+# page 2 -> page 3 (0.33)
+# page 3 -> page 1 (0.33)
+puts "Circular: ", pagerank(Matrix[[0,0,1], [0,0,1], [1,0,0]])
+
+# page 1 -> page 3 (0.05) # page 1 & page 2 have minimal postrank values
+# page 2 -> page 3 (0.05) # min value = min(t * Teleport Vector)
+# page 3 -> page 3 (0.09) # for uniform teleport vector, min = t * (1/N)
+puts "Star: ", pagerank(Matrix[[0,0,0], [0,0,0], [1,1,1]])
+
+# page 1 -> page 2, page 3 (0.05)
+# page 2 -> page 3 (0.07)
+# page 3 -> page 3, page 3 (0.87)
+puts "Converge: ", pagerank(Matrix[[0,0,0], [0.5,0,0], [0.5,1,1]])
+
+# page 1 -> page 1, page 2 (0.18)
+# page 2 -> page 1, page 3 (0.13)
+# page 3 -> page 3 (0.69)
+puts "Linked:", pagerank(Matrix[[0.5,0.5,0], [0.5,0,0], [0,0.5,1.0]])
@@ -0,0 +1,45 @@
+# 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
+
+ (t*((i-s*g).invert)*p).column(0)
+end
+
+# 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
+end
+
+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
+end
+
+g = random_network(1000,10)
+pr = pagerank(g,0.85)
+
+puts "Pagerank vector: ", pr
+
+pr.histogram(50).graph()
@@ -0,0 +1,48 @@
+# Ilya Grigorik
+#
+# Generate a N page web, where number of links follows a power-law distribution
+#
+
+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
+
+ (t*((i-s*g).invert)*p).column(0)
+end
+
+# 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
+end
+
+def random_network(pages, links)
+ g = Matrix.alloc(pages, pages)
+ rng = GSL::Rng.alloc
+ data = rng.exponential(links, pages)
+
+ pages.times { |n| g.set_col(n, random_column(pages, data[n].round, n)) }
+
+ return g
+end
+
+g = random_network(500,10)
+pr = pagerank(g,0.85)
+
+puts "Pagerank vector: ", pr
+
+pr.histogram(50).graph()

0 comments on commit 8325e0d

Please sign in to comment.