PageRank in Julia
Using iterators and the linear map data structure to implement PageRank.
function pagerank(A, alpha=0.85, p = fill(1/size(A,2),size(A,2));
=false, args...)
details= 1.0 ./ (A * ones(Float64,size(A,2)))
S = find(isinf,S) # S[i] = Inf if node i has no outlinks
D = 0.0
S[D] = p./norm(p,1)
p = LinearMap{Float64}((y,x) -> begin
L At_mul_B!(y, A, S .* x)
.= alpha .* y .+ (alpha * sum(x[D]) + 1.0 - alpha) .* p
y
yend, size(A,1), size(A,2))
= copy(p)
x = powermethod!(L, x; args...)
res if details x, res, L else x end
end
function powermethod!(A, x, Ax=similar(x);
=15, tol=eps(Float64) * size(A,2),
maxiter=true, verbose=true)
log= eltype(A)
T ./= norm(x,1)
x && println("Running power method, maxiter = $maxiter, tol = $tol")
verbose = Tuple{Int,T,T}[]
history = 0
iter = zero(T)
radius = Inf
err while iter <= maxiter
A_mul_B!(Ax, A, x)
= norm(Ax,1)
radius ./= radius # want |x|_1 = 1
Ax = norm(Ax-x,1)
err && @show iter,err,radius
verbose && push!(history,(iter,err,radius))
log copy!(x, Ax)
< tol && break
err += 1
iter end
= err < tol
isconverged if log radius,x,history,isconverged else radius,x end
end
Vipin Vijayan. Aug 28, 2022 (u: Aug 31, 2022)