# pagerank.py
"""Volume 1: The Page Rank Algorithm.
"""
# Problem 1
def to_matrix(filename, n):
"""Return the nxn adjacency matrix described by datafile.
Parameters:
datafile (str): The name of a .txt file describing a directed graph.
Lines describing edges should have the form '\t\n'.
The file may also include comments.
n (int): The number of nodes in the graph described by datafile
Returns:
A SciPy sparse dok_matrix.
"""
raise NotImplementedError("Problem 1 Incomplete")
# Problem 2
def calculateK(A,N):
"""Compute the matrix K as described in the lab.
Parameters:
A (ndarray): adjacency matrix of an array
N (int): the datasize of the array
Returns:
K (ndarray)
"""
raise NotImplementedError("Problem 2 Incomplete")
# Problem 3
def iter_solve(adj, N=None, d=.85, tol=1E-5):
"""Return the page ranks of the network described by 'adj'.
Iterate through the PageRank algorithm until the error is less than 'tol'.
Parameters:
adj (ndarray): The adjacency matrix of a directed graph.
N (int): Restrict the computation to the first 'N' nodes of the graph.
If N is None (default), use the entire matrix.
d (float): The damping factor, a float between 0 and 1.
tol (float): Stop iterating when the change in approximations to the
solution is less than 'tol'.
Returns:
The approximation to the steady state.
"""
raise NotImplementedError("Problem 3 Incomplete")
# Problem 4
def eig_solve(adj, N=None, d=.85):
"""Return the page ranks of the network described by 'adj'. Use SciPy's
eigenvalue solver to calculate the steady state of the PageRank algorithm
Parameters:
adj (ndarray): The adjacency matrix of a directed graph.
N (int): Restrict the computation to the first 'N' nodes of the graph.
If N is None (default), use the entire matrix.
d (float): The damping factor, a float between 0 and 1.
tol (float): Stop iterating when the change in approximations to the
solution is less than 'tol'.
Returns:
The approximation to the steady state.
"""
raise NotImplementedError("Problem 4 Incomplete")
# Problem 5
def team_rank(filename='ncaa2013.csv'):
"""Use iter_solve() to predict the rankings of the teams in the given
dataset of games. The dataset should have two columns, representing
winning and losing teams. Each row represents a game, with the winner on
the left, loser on the right. Parse this data to create the adjacency
matrix, and feed this into the solver to predict the team ranks.
Parameters:
filename (str): The name of the data file.
Returns:
ranks (list): The ranks of the teams from best to worst.
teams (list): The names of the teams, also from best to worst.
"""
raise NotImplementedError("Problem 5 Incomplete")