Hi,

the exercise asks one to rank websites using a modification of PageRank. It seems that this ranking is also equal to the eigenvector of the transition matrix P that is associated with eigenvalue 1. However, when I try to apply this idea I get a different ranking. I would appreciate any clues what goes wrong in my reasoning/code.

(heavily) Building on the provided solution, here is my code.

“”"

Return list of pages, ordered by rank

“”"

import re

from operator import itemgetter

import numpy as np

infile = ‘web_graph_data.txt’

alphabet = ‘abcdefghijklmnopqrstuvwxyz’

n = 14 # Total number of web pages (nodes)

# Create a matrix Q indicating existence of links

# * Q[i, j] = 1 if there is a link from i to j

# * Q[i, j] = 0 otherwise

Q = np.zeros((n, n), dtype=int)

f = open(infile, ‘r’)

edges = f.readlines()

f.close()

for edge in edges:

from_node, to_node = re.findall(’\w’, edge)

i, j = alphabet.index(from_node), alphabet.index(to_node)

Q[i, j] = 1

# Create the corresponding Markov matrix P

P = np.empty((n, n))

for i in range(n):

P[i, :] = Q[i, :] / Q[i, :].sum()

v, w = np.linalg.eig( P )

ranked_pages = {alphabet[i] : w[0, i] for i in range(n)}

# Print solution, sorted from highest to lowest rank

print(‘Rankings\n ***’)

for name, rank in sorted(ranked_pages.items(), key=itemgetter(1), reverse=1):

print(f’{name}: {rank:.4}’)