Result Test: Please write code in Python import numpy as np from problem3 import
ID: 3606765 • Letter: R
Question
Result Test:
Please write code in Python
import numpy as np from problem3 import randomwalk Problem 4:Solving sink-node problme in PageRank In this problem, we implement the pagerank algorithm which can solve the sink node problem You could test the correctness of your code by typing nosetests test4.py in the terminal. 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 C30 represents the probability of moving from node i to node . If node i is a sink node, StJi-1/n Input: A: adjacency matrix, a Cn by n) numpy matrix of binary values. If there is a link from node i to node , A IJ-1. Otherwise AS 1-·lf there is no link. Output S: transition matrix, a Cn by n) numpy matrix of float values. Si] represents the probability of moving from node i to node j The values in each column of matrix S should sum to 1.Explanation / Answer
function B = maxproduct(A,n)
[r,c] = size(A);
if n>r && n>c
B = []; % cannot be solved
return
end
global L;
L = zeros(1,4); % [product, home-row, home-col, direction]
for i=1:r
for j=1:c-n+1
check(A(i,j:j+n-1),[i,j,1]); % row, right case
end
end
for i=1:r-n+1
for j=1:c
check(A(i:i+n-1,j),[i,j,2]); % column, down case
end
end
for i=1:r-n+1
for j=1:c-n+1
S=A(i:i+n-1,j:j+n-1);
check(diag(S),[i,j,3]); % diagonal, down case
check(diag(flip(S,2)),[i,j,4]); % reverse diagonal, down case
end
end
i=L(2); j=L(3); % reconstruct coordinates
switch L(4)
case 1, B = [ones(n,1)*i,(j:j+n-1)'];
case 2, B = [(i:i+n-1)',ones(n,1)*j];
case 3, B = [(i:i+n-1)',(j:j+n-1)'];
case 4, B = [(i:i+n-1)',(j+n-1:-1:j)'];
end
end
function check(V,d)
global L;
p = prod(V);
if p>L(1) % if new product larger than any previous
L = [p,d]; % then update product, home and direction
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.