Skip to content

Commit

Permalink
[Port] New rich club metric (#29)
Browse files Browse the repository at this point in the history
added rich club metric with tests and support for directed graphs

Co-authored-by: Victor <[email protected]>
Co-authored-by: vboussange <[email protected]>
  • Loading branch information
3 people authored Nov 8, 2021
1 parent 4b184cc commit 1421e94
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/src/community.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Pages = [
"community/label_propagation.jl",
"community/modularity.jl",
"community/assortativity.jl"
"community/rich_club.jl"
]
Private = false
```
3 changes: 2 additions & 1 deletion src/Graphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ barabasi_albert!, static_fitness_model, static_scale_free, kronecker, dorogovtse
#community
modularity, core_periphery_deg,
local_clustering,local_clustering_coefficient, global_clustering_coefficient, triangles,
label_propagation, maximal_cliques, clique_percolation, assortativity,
label_propagation, maximal_cliques, clique_percolation, assortativity,rich_club,

#generators
complete_graph, star_graph, path_graph, wheel_graph, cycle_graph,
Expand Down Expand Up @@ -256,6 +256,7 @@ include("community/clustering.jl")
include("community/cliques.jl")
include("community/clique_percolation.jl")
include("community/assortativity.jl")
include("community/rich_club.jl")
include("spanningtrees/boruvka.jl")
include("spanningtrees/kruskal.jl")
include("spanningtrees/prim.jl")
Expand Down
27 changes: 27 additions & 0 deletions src/community/rich_club.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
rich_club(g, k)
Return the non-normalised [rich-club coefficient](https://en.wikipedia.org/wiki/Rich-club_coefficient) of graph `g`,
with degree cut-off `k`.
```jldoctest
julia> using LightGraphs
julia> g = star_graph(5)
julia> rich_club(g, 1)
0.4
```
"""
function rich_club(g::AbstractGraph{T}, k::Int) where T
E = zero(T)
for e in edges(g)
if (outdegree(g, src(e)) >= k) && (indegree(g, dst(e)) >= k )
E +=1
end
end
N = count(degree(g) .>= k)
if is_directed(g)
return E / (N*(N-1))
else
return 2*E / (N*(N-1))
end
end
17 changes: 17 additions & 0 deletions test/community/rich_club.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

@testset "Rich club coefficient" begin
@testset "Small graphs" for _n in 5:10
@test @inferred rich_club(star_graph(_n), 1) 2 / _n
@test @inferred rich_club(DiGraph(star_graph(_n)), 1) 2 / _n
end
@testset "Directed ($seed)" for seed in [1, 2, 3], (n, ne) in [(14, 18), (10, 22), (7, 16)]
g = erdos_renyi(n, ne; is_directed=true, seed=seed)
_r = rich_club(g, 1)
@test @inferred rich_club(g, 1) > 0.
end
@testset "Undirected ($seed)" for seed in [1, 2, 3], (n, ne) in [(14, 18), (10, 22), (7, 16)]
g = erdos_renyi(n, ne; is_directed=false, seed=seed)
_r = rich_club(g, 1)
@test @inferred rich_club(g, 1) > 0.
end
end
5 changes: 3 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ using Statistics: mean

const testdir = dirname(@__FILE__)

testgraphs(g) = is_directed(g) ? [g, DiGraph{UInt8}(g), DiGraph{Int16}(g)] : [g, Graph{UInt8}(g), Graph{Int16}(g)]
testgraphs(g) = is_directed(g) ? [g, DiGraph{UInt8}(g), DiGraph{Int16}(g)] : [g, Graph{UInt8}(g), Graph{Int16}(g)]
testgraphs(gs...) = vcat((testgraphs(g) for g in gs)...)
testdigraphs = testgraphs

# some operations will create a large graph from two smaller graphs. We
# might error out on very small eltypes.
testlargegraphs(g) = is_directed(g) ? [g, DiGraph{UInt16}(g), DiGraph{Int32}(g)] : [g, Graph{UInt16}(g), Graph{Int32}(g)]
testlargegraphs(g) = is_directed(g) ? [g, DiGraph{UInt16}(g), DiGraph{Int32}(g)] : [g, Graph{UInt16}(g), Graph{Int32}(g)]
testlargegraphs(gs...) = vcat((testlargegraphs(g) for g in gs)...)

tests = [
Expand Down Expand Up @@ -60,6 +60,7 @@ tests = [
"community/clustering",
"community/clique_percolation",
"community/assortativity",
"community/rich_club",
"centrality/betweenness",
"centrality/closeness",
"centrality/degree",
Expand Down

0 comments on commit 1421e94

Please sign in to comment.