def pagerank(A, alpha=0.95):
    ''' 
        The final PageRank algorithm, which solves both the sink node problem and sink region problem.
        Given an adjacency matrix A, compute the pagerank score of all the nodes in the network. 
        Input: 
                A: adjacency matrix, a numpy matrix of binary values. If there is a link from node i to node j, A[j][i] =1. Otherwise A[j][i]=0 if there is no link.
                alpha: a float scalar value, which is the probability of choosing option 1 (randomly follow a link on the page)
        Output: 
                x: the ranking scores, a numpy vector of float values, such as np.array([.3, .5, .7])
        Hint: you could solve this problem using two lines of code. You could re-use the functions that you have implemented in problem 3 and 4.
    '''

    # Initialize the score vector
    num_nodes = A.shape[0]  # get the number of nodes (n)
    x_0 = np.ones(num_nodes) / num_nodes

    #########################################
    ## INSERT YOUR CODE HERE
    # compute the transition matrix G from adjacency matrix A
    G = compute_G(A, alpha)

    # random walk for multiple steps until convergence
    x = random_walk(G, x_0)[0]

    #########################################
    return x
示例#2
0
def pagerank_v2(A):
    ''' 
        A simplified version of PageRank algorithm, which solves the sink node problem.
        Given an adjacency matrix A, compute the pagerank score of all the nodes in the network. 
        Input: 
                A: adjacency matrix, a numpy matrix of binary values. If there is a link from node i to node j, A[j][i] =1. Otherwise A[j][i]=0 if there is no link.
        Output: 
                x: the ranking scores, a numpy vector of float values, such as np.array([.3, .5, .2])
    '''

    # Initialize the score vector
    num_nodes = A.shape[0]
    x_0 = np.ones(num_nodes) / num_nodes

    #########################################
    ## INSERT YOUR CODE HERE
    # compute the transition matrix S from adjacency matrix A
    S = compute_S(A)

    # random walk
    x, _ = random_walk(S, x_0)

    #########################################

    return x
示例#3
0
def pagerank_v2(A):
    ''' 
        A simplified version of PageRank algorithm, which solves the sink node problem.
        Given an adjacency matrix A, compute the pagerank score of all the nodes in the network. 
        Input: 
                A: adjacency matrix, a numpy matrix of binary values. If there is a link from node i to node j, A[j][i] =1. Otherwise A[j][i]=0 if there is no link.
        Output: 
                x: the ranking scores, a numpy vector of float values, such as np.array([[.3], [.5], [.7]])
    '''

    # Initialize the score vector with all one values
    num_nodes, _ = A.shape 
    x_0 =  np.asmatrix(np.ones((num_nodes,1))) 

    # compute the transition matrix from adjacency matrix
    S = compute_S(A)
    # random walk
    x, n_steps = random_walk(S,x_0)

    return x
示例#4
0
def pagerank(A, alpha=0.95):
    '''
        The final PageRank algorithm, which solves both the sink node problem and sink region problem.
        Given an adjacency matrix A, compute the pagerank score of all the nodes in the network.
        Input:
                A: adjacency matrix, a numpy matrix of binary values. If there is a link from node i to node j, A[j][i] =1. Otherwise A[j][i]=0 if there is no link.
                alpha: a float scalar value, which is the probability of choosing option 1 (randomly follow a link on the page)
        Output:
                x: the ranking scores, a numpy vector of float values, such as np.array([[.3], [.5], [.7]])
    '''

    # Initialize the score vector with all one values
    num_nodes, _ = A.shape  # get the number of nodes (n)
    x_0 = np.ones((num_nodes, 1))  # create an all-one vector of shape (n by 1)

    # compute the transition matrix from adjacency matrix
    G = compute_G(A, alpha)

    # random walk
    x, n_steps = random_walk(G, x_0)
    return x