Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 17 additions & 30 deletions GNNGraphs/src/transform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1051,43 +1051,30 @@ CRC.@non_differentiable remove_self_loops(x...) # TODO this is wrong, since g c
CRC.@non_differentiable dense_zeros_like(x...)

"""
ppr_diffusion(g::GNNGraph{<:COO_T}, alpha =0.85f0) -> GNNGraph
ppr_diffusion(g::GNNGraph{<:COO_T}; alpha = 0.85f0) -> GNNGraph

Calculates the Personalized PageRank (PPR) diffusion based on the edge weight matrix of a GNNGraph and updates the graph with new edge weights derived from the PPR matrix.
References paper: [The pagerank citation ranking: Bringing order to the web](http://ilpubs.stanford.edu:8090/422)
Return a graph with the same edges as `g`, weighted by Personalized PageRank (PPR)
diffusion. For an edge from `src` to `dst`, its new weight is `P[dst, src]`, where
`P = alpha * inv(I - (1 - alpha) * T)` and `alpha` is the teleport probability.

Each column of `T` contains a source node's nonnegative outgoing edge weights
divided by their sum. Unweighted edges have unit weight. A node with zero total
outgoing weight has a zero column.

The function performs the following steps:
1. Constructs a modified adjacency matrix `A` using the graph's edge weights, where `A` is adjusted by `(α - 1) * A + I`, with `α` being the damping factor (`alpha_f32`) and `I` the identity matrix.
2. Normalizes `A` to ensure each column sums to 1, representing transition probabilities.
3. Applies the PPR formula `α * (I + (α - 1) * A)^-1` to compute the diffusion matrix.
4. Updates the original edge weights of the graph based on the PPR diffusion matrix, assigning new weights for each edge from the PPR matrix.

# Arguments
- `g::GNNGraph`: The input graph for which PPR diffusion is to be calculated. It should have edge weights available.
- `alpha_f32::Float32`: The damping factor used in PPR calculation, controlling the teleport probability in the random walk. Defaults to `0.85f0`.

# Returns
- A new `GNNGraph` instance with the same structure as `g` but with updated edge weights according to the PPR diffusion calculation.
Reference: [The PageRank Citation Ranking: Bringing Order to the Web](http://ilpubs.stanford.edu:8090/422).
"""
function ppr_diffusion(g::GNNGraph{<:COO_T}; alpha = 0.85f0)
s, t = edge_index(g)
T = promote_type(Float32, typeof(alpha), eltype(g))
w = get_edge_weight(g)
if isnothing(w)
w = ones(Float32, g.num_edges)
end

N = g.num_nodes

initial_A = sparse(t, s, w, N, N)
scaled_A = (Float32(alpha) - 1) * initial_A

I_sparse = sparse(Diagonal(ones(Float32, N)))
A_sparse = I_sparse + scaled_A

A_dense = Matrix(A_sparse)

PPR = alpha * inv(A_dense)
w = isnothing(w) ? ones(T, g.num_edges) : convert(AbstractVector{T}, w)

# Scale each source before combining parallel edges or summing outgoing weights.
scale = _scatter(max, w, s, g.num_nodes)[s]
A = sparse(t, s, w ./ max.(scale, one(T)), g.num_nodes, g.num_nodes)
out_weight = vec(sum(A; dims = 1))
transition = A / Diagonal(ifelse.(iszero.(out_weight), one(T), out_weight))
PPR = alpha * inv(Matrix(I - (1 - alpha) * transition))

new_w = [PPR[dst, src] for (src, dst) in zip(s, t)]

Expand Down
38 changes: 23 additions & 15 deletions GNNGraphs/test/transform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -890,25 +890,33 @@ end

@testitem "ppr_diffusion" setup=[GraphsTestModule] begin
using .GraphsTestModule
for GRAPH_T in GRAPH_TYPES
if GRAPH_T == :coo
s = [1, 1, 2, 3]
t = [2, 3, 4, 5]
eweights = [0.1, 0.2, 0.3, 0.4]

g = GNNGraph(s, t, eweights)
s = [1, 1, 2, 3, 4]
t = [2, 3, 4, 5, 5]
w = [1, 2, 3, 4, 0]
alpha = 0.25
# Only direct transitions contribute to the PPR entries for these edges.
expected = alpha * (1 - alpha) .* [1/3, 2/3, 1, 1, 0]
for weights in (w, 10 .* w)
g = ppr_diffusion(GNNGraph(s, t, weights); alpha)
@test get_edge_weight(g) ≈ expected
end

g_new = ppr_diffusion(g)
w_new = get_edge_weight(g_new)
# On a complete graph every off-diagonal PPR entry is (1 - alpha) / (n - alpha).
complete = GNNGraph(complete_digraph(8))
@test get_edge_weight(ppr_diffusion(complete)) ≈ fill((1 - 0.85f0) / (8 - 0.85f0), 56)

check_ew = Float32[0.012749999
0.025499998
0.038249996
0.050999995]
# Parallel edges can overflow even before their source's total is computed.
for T in (Float32, Float64)
weights = T[floatmax(T), floatmax(T), floatmax(T), nextfloat(zero(T))]
g = GNNGraph([1, 1, 1, 2], [2, 2, 3, 4], weights)
@test get_edge_weight(ppr_diffusion(g; alpha = T(0.5))) ≈ T[1/6, 1/6, 1/12, 1/4]
end

@test w_new ≈ check_ew
end
function first_weight(w)
g = GNNGraph([1, 1], [2, 3], w)
return first(get_edge_weight(ppr_diffusion(g; alpha)))
end
@test only(gradient(first_weight, [1.0, 2.0])) ≈ alpha * (1 - alpha) / 9 .* [2, -1]
end

@testitem "graph transform ops set is_coalesced=false" setup=[GraphsTestModule] begin
Expand Down