diff --git a/src/overrides.jl b/src/overrides.jl index d767bf6..f943672 100644 --- a/src/overrides.jl +++ b/src/overrides.jl @@ -55,12 +55,10 @@ Apply the page rank algorithm on a weighted graph. """ function Graphs.pagerank(g::SimpleWeightedDiGraph, α=0.85, n::Integer=100, ϵ=1.0e-6) A = weights(g) - S = vec(sum(A; dims=1)) - S = 1 ./ S + S = 1 ./ vec(sum(A; dims=2)) # inverse of outdegree S[findall(S .== Inf)] .= 0.0 - M = A' # need a separate line due to bug #17456 in julia # scaling the adjmat to stochastic adjacency matrix - M = (Diagonal(S) * M)' + M = (Diagonal(S) * A)' N = Int(nv(g)) # solution vector x = fill(1.0 / N, N) diff --git a/test/overrides.jl b/test/overrides.jl index ecf475c..8e5c1c4 100644 --- a/test/overrides.jl +++ b/test/overrides.jl @@ -76,7 +76,7 @@ @test adjacency_matrix(g; dir=:out) == adjacency_matrix(g; dir=:in)' @test !issymmetric(laplacian_matrix(g; dir=:out)) @test laplacian_matrix(g, Float64; dir=:out) ≈ g5_l - @test @inferred(pagerank(g))[3] ≈ 0.2266 atol = 0.001 + @test @inferred(pagerank(g)) ≈ [0.119, 0.186, 0.311, 0.383] atol = 0.001 # checked with networkx @test length(@inferred(pagerank(g))) == nv(g) @test_throws ErrorException pagerank(g, 2) @test_throws ErrorException pagerank(g, 0.85, 2) @@ -85,4 +85,11 @@ @test g[2:3] == SimpleWeightedDiGraph{eltype(g5),weighttype(g5)}(gc) @test weights(g[2:3])[1, 2] == 2 end + + let + A = [0 1 1; 1 0 0; 0 1 0] + g = SimpleDiGraph(A) + gw = SimpleWeightedDiGraph(A) + @test pagerank(g) ≈ pagerank(gw) + end end