Skip to content

Commit 1381a78

Browse files
Implement laplacian_lambda_max (#240)
* Add laplacian_lambda_max function * Add laplacian_lambda_max tests * Add export laplacian_lambda_max * Fix style * Fix docstring Co-authored-by: Carlo Lucibello <[email protected]> * Fix indentation Co-authored-by: Carlo Lucibello <[email protected]> * Add spaces Co-authored-by: Carlo Lucibello <[email protected]> Add spaces Co-authored-by: Carlo Lucibello <[email protected]> Add spaces Co-authored-by: Carlo Lucibello <[email protected]> Add spaces Co-authored-by: Carlo Lucibello <[email protected]> Add spaces Co-authored-by: Carlo Lucibello <[email protected]> Co-authored-by: Carlo Lucibello <[email protected]>
1 parent 156cfae commit 1381a78

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

src/GNNGraphs/GNNGraphs.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export adjacency_list,
3535
is_bidirected,
3636
normalized_laplacian,
3737
scaled_laplacian,
38+
laplacian_lambda_max,
3839
# from Graphs
3940
adjacency_matrix,
4041
degree,

src/GNNGraphs/query.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,25 @@ function khop_adj(g::GNNGraph,k::Int, T::DataType=eltype(g); dir=:out, weighted=
414414
return (adjacency_matrix(g, T; dir, weighted))^k
415415
end
416416

417+
"""
418+
laplacian_lambda_max(g::GNNGraph, T=Float32; add_self_loops=false, dir=:out)
419+
420+
Return the largest eigenvalue of the normalized symmetric Laplacian of the graph `g`.
421+
422+
If the graph is batched from multiple graphs, return the list of the largest eigenvalue for each graph.
423+
"""
424+
function laplacian_lambda_max(g::GNNGraph,T::DataType=Float32;
425+
add_self_loops::Bool=false, dir::Symbol=:out)
426+
if g.num_graphs==1
427+
return _eigmax(normalized_laplacian(g, T; add_self_loops, dir))
428+
else
429+
eigenvalues=zeros(g.num_graphs)
430+
for i in 1:g.num_graphs
431+
eigenvalues[i] = _eigmax(normalized_laplacian(getgraph(g, i), T; add_self_loops, dir))
432+
end
433+
return eigenvalues
434+
end
435+
end
417436

418437
@non_differentiable edge_index(x...)
419438
@non_differentiable adjacency_list(x...)

test/GNNGraphs/query.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,19 @@
132132
@test L D - A
133133
end
134134

135+
@testset "laplacian_lambda_max" begin
136+
s = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
137+
t = [2, 3, 4, 5, 1, 5, 1, 2, 3, 4]
138+
g = GNNGraph(s,t)
139+
@test laplacian_lambda_max(g) Float32(1.809017)
140+
data1 = [g for i in 1:5]
141+
gall1 = Flux.batch(data1)
142+
@test laplacian_lambda_max(gall1) [Float32(1.809017) for i in 1:5]
143+
data2 = [rand_graph(10,20) for i in 1:3]
144+
gall2 = Flux.batch(data2)
145+
@test length(laplacian_lambda_max(gall2)) == 3
146+
end
147+
135148
@testset "adjacency_matrix" begin
136149
a = sprand(5, 5, 0.5)
137150
abin = map(x -> x > 0 ? 1 : 0, a)

0 commit comments

Comments
 (0)