コード例 #1
0
def compute_S(A):
    '''
        compute the transition matrix S from addjacency matrix A, which solves sink node problem by filling the all-zero columns in A.
        S[j][i] represents the probability of moving from node i to node j.
        If node i is a sink node, S[j][i] = 1/n.
        Input:
                A: adjacency matrix, a (n by n) 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:
                S: transition matrix, a (n by n) numpy matrix of float values.  S[j][i] represents the probability of moving from node i to node j.
    The values in each column of matrix S should sum to 1.
    '''
    #########################################
    ## INSERT YOUR CODE HERE
    n = len(A)
    S = compute_P(A)
    for i in range(n):
        is_sink = 1
        for j in range(n):
            if S[j, i]!=0:
                is_sink = 0
        if is_sink == 1:
            for k in range(n):
                S[k, i] = 1./n
    #########################################
    return S
コード例 #2
0
def compute_S(A):
    '''
        compute the transition matrix S from addjacency matrix A, which solves sink node problem by filling the all-zero columns in A.
        S[j][i] represents the probability of moving from node i to node j.
        If node i is a sink node, S[j][i] = 1/n.
        Input: 
                A: adjacency matrix, a (n by n) 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: 
                S: transition matrix, a (n by n) numpy matrix of float values.  S[j][i] represents the probability of moving from node i to node j.
    The values in each column of matrix S should sum to 1.
    '''
    #########################################
    ## INSERT YOUR CODE HERE
    
    S_init = A.copy(); # Need to copy to avoid modifying A
    
    num_rows = np.shape(S_init)[0];
    num_cols = np.shape(S_init)[1];
    S_prob = 1. / num_cols;
    
    for j in range(num_cols):                      # Iterate over all columns (across row)
        if (np.count_nonzero(S_init[: , j]) == 0):  # If no non-zero values in ith column,
            for i in range(num_rows):                # Iterate over all rows (down column)
                S_init[i,j] = S_prob;                 # Reassign 0 => (1 / num_cols) (equal probability)

    S = compute_P(S_init); # We can now use compute_P on correct matrix (infinite loop in sink-node problem is analguous to division by zero that occurs in compute_P)
                
    #########################################
    
    return S;
コード例 #3
0
ファイル: problem4.py プロジェクト: rahul-pande/ds501
def compute_S(A):
    '''
        compute the transition matrix S from addjacency matrix A, which solves sink node problem by filling the all-zero columns in A.
        S[j][i] represents the probability of moving from node i to node j.
        If node i is a sink node, S[j][i] = 1/n.
        Input:
                A: adjacency matrix, a (n by n) 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:
                S: transition matrix, a (n by n) numpy matrix of float values.  S[j][i] represents the probability of moving from node i to node j.
    The values in each column of matrix S should sum to 1.
    '''
    #########################################
    ## INSERT YOUR CODE HERE

    shape = set(np.shape(A))
    assert len(shape) == 1
    n = shape.pop()

    P = compute_P(A)
    colsums = P.sum(axis=0)
    sink_nodes = np.asarray(np.isnan(colsums))
    sink_nodes = sink_nodes.reshape((n, ))

    from copy import deepcopy
    S = deepcopy(P)

    if sink_nodes.any():
        S[:, sink_nodes] = 1 / n

    #########################################
    return S
コード例 #4
0
ファイル: problem4.py プロジェクト: vanand23/WPI_Projects
def compute_S(A):
    '''
        compute the transition matrix S from addjacency matrix A, which solves sink node problem by filling the all-zero columns in A.
        S[j][i] represents the probability of moving from node i to node j.
        If node i is a sink node, S[j][i] = 1/n.
        Input: 
                A: adjacency matrix, a (n by n) 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: 
                S: transition matrix, a (n by n) numpy matrix of float values.  S[j][i] represents the probability of moving from node i to node j.
                   The values in each column of matrix S should sum to 1.
    '''
    #########################################
    ## INSERT YOUR CODE HERE

    n = A.shape[0]

    sumCol = np.sum(A, axis=0)
    for col in range(n):
       if(sumCol[col]==0):
            A[:, col] = 1/n

    P = compute_P(A)
    S=P

    #########################################
    return S
コード例 #5
0
def compute_S(A):
    '''
        compute the transition matrix S from adjacency matrix A, which solves sink node problem by filling the all-zero columns in A.
        S[j][i] represents the probability of moving from node i to node j.
        If node i is a sink node, S[j][i] = 1/n.
        Input: 
                A: adjacency matrix, a (n by n) 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: 
                S: transition matrix, a (n by n) numpy matrix of float values.  S[j][i] represents the probability of moving from node i to node j.
                   The values in each column of matrix S should sum to 1.
    '''
    #########################################
    ## INSERT YOUR CODE HERE
    num_nodes = A.shape[0]
    A[:, A.sum(axis=0) == 0] = np.ones((num_nodes, 1))
    S = compute_P(A)

    #########################################
    return S
    ''' TEST: Now you can test the correctness of your code above by typing `nosetests -v test4.py:test_compute_S' in the terminal.  '''
コード例 #6
0
def compute_S(A):
    '''
        compute the transition matrix S from addjacency matrix A, which solves sink node problem by filling the all-zero columns in A.
        S[j][i] represents the probability of moving from node i to node j.
        If node i is a sink node, S[j][i] = 1/n.
        Input: 
                A: adjacency matrix, a (n by n) 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: 
                S: transition matrix, a (n by n) numpy matrix of float values.  S[j][i] represents the probability of moving from node i to node j.
    The values in each column of matrix S should sum to 1.
    '''
    P = compute_P(A)
    numrows = P.shape[0]
    numcols = P.shape[1]
    columnsum = np.sum(A, axis=0)
    for n in range(columnsum.shape[1]):
        if columnsum[0, n] == 0:
            for k in range(numrows):
                P[k, n] = 1 / numcols
    S = np.asmatrix(P)
    return S
コード例 #7
0
def compute_S(A):
    '''
        compute the transition matrix S from addjacency matrix A, which solves sink node problem by filling the all-zero columns in A.
        S[j][i] represents the probability of moving from node i to node j.
        If node i is a sink node, S[j][i] = 1/n.
        Input:
                A: adjacency matrix, a (n by n) 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:
                S: transition matrix, a (n by n) numpy matrix of float values.  S[j][i] represents the probability of moving from node i to node j.
    The values in each column of matrix S should sum to 1.
    '''
    #########################################
    ## INSERT YOUR CODE HERE
    A = compute_P(A)
    for x in range(A.shape[1]):
        n = 0.0
        for i in (A[:, x]):
            n = n + i
        if n == 0:
            m = float(A.shape[0])
            A[:, x] = 1 / m
    S = A
    #########################################
    return S