-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Port] New s_metric method #28
base: master
Are you sure you want to change the base?
Changes from all commits
b36ecfa
0ce7f46
7f53bae
857421a
4acfd1f
7edd949
c01ad23
007de78
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
""" | ||
s_metric(g; norm=true) | ||
|
||
Return the normalised s-metric of `g`. | ||
|
||
The s-metric is defined as the sum of the product of degrees between pair of vertices | ||
for every edge in `g`. [Ref](https://arxiv.org/abs/cond-mat/0501169) | ||
In directed graphs, the paired values are the out-degree of source vertices | ||
and the in-degree of destination vertices. | ||
It is normalised by the maximum s_metric obtained from the family of graph | ||
with similar degree distribution. s_max is computed from an approximation | ||
formula as in https://journals.aps.org/pre/pdf/10.1103/PhysRevE.75.046102 | ||
If `norm=false`, no normalisation is performed. | ||
|
||
# Examples | ||
```jldoctest | ||
julia> using LightGraphs | ||
|
||
julia> s_metric(star_graph(4)) | ||
0.6 | ||
``` | ||
""" | ||
|
||
function s_metric(g::AbstractGraph{T}; norm=true) where T | ||
s = zero(T) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe |
||
for e in edges(g) | ||
s += outdegree(g, src(e)) * indegree(g, dst(e)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need some special handling for self-loops here? |
||
end | ||
if norm | ||
sm = sum(degree(g).^3)/2 | ||
return s/sm | ||
else | ||
return s | ||
end | ||
end |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,22 @@ | ||||||||||
using Random, Statistics | ||||||||||
|
||||||||||
@testset "S-metric" begin | ||||||||||
@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) | ||||||||||
sm = s_metric(g, norm=false) | ||||||||||
sm2 = sum([outdegree(g, src(d)) * indegree(g, dst(d)) for d in edges(g)]) | ||||||||||
@test @inferred sm ≈ sm2 | ||||||||||
sm = s_metric(g, norm=true) | ||||||||||
Comment on lines
+8
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
The inferred macro is used to check if a function is type stable, i.e. the compiler can infer its resulting argument. What you are testing here is, if |
||||||||||
sm2 /= sum(degree(g).^3)/2 | ||||||||||
@test @inferred sm ≈ sm2 | ||||||||||
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) | ||||||||||
sm = s_metric(g, norm=false) | ||||||||||
sm2 = sum([degree(g, src(d)) * degree(g, dst(d)) for d in edges(g)]) | ||||||||||
@test @inferred sm ≈ sm2 | ||||||||||
sm = s_metric(g, norm=true) | ||||||||||
sm2 /= sum(degree(g).^3)/2 | ||||||||||
@test @inferred sm ≈ sm2 | ||||||||||
end | ||||||||||
end | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we have some tests here for graphs different from random graphs? I am especially thinking about graphs with self-loops and isolated vertices. Also, it would be good to test with graphs of eltype different than |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.