Skip to content
Merged
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
18 changes: 12 additions & 6 deletions src/community/modularity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ julia> barbell = blockdiag(complete_graph(3), complete_graph(3));
julia> add_edge!(barbell, 1, 4);

julia> modularity(barbell, [1, 1, 1, 2, 2, 2])
0.35714285714285715
0.3571428571428571

julia> modularity(barbell, [1, 1, 1, 2, 2, 2], γ=0.5)
0.6071428571428571
Expand Down Expand Up @@ -90,14 +90,20 @@ function modularity(
c2 = c[v]
if c1 == c2
Q += distmx[u, v]
if u == v && !is_directed(g)
#Since we do not look at each end in outer loop
Q += distmx[u, v]
end
end
kout[c1] += distmx[u, v]
kin[c2] += distmx[u, v]
if u == v && !is_directed(g)
#Since we do not look at each end in outer loop
kout[c1] += distmx[u, v]
kin[c2] += distmx[u, v]
end
end
end
Q = Q * m
@inbounds for i in 1:nc
Q -= γ * kin[i] * kout[i]
end
return Q / m^2
Q = Q/m - γ * sum(kin .* kout) / m^2
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine whoever wrote the previous inbounds loop wanted to avoid the allocation. The new format creates a temporary array to store kin .* kout before performing the sum. I guess I personally do not care about this type of micro-optimization and prefer your new code, but mentioning this here for documentation purposes.

julia> f(a,b) = sum(a .* b)
f (generic function with 1 method)

julia> g(a,b) = begin s = 0.0; for i in eachindex(a); s += a[i]*b[i]; end; s end

g (generic function with 1 method)

julia> a = rand(100);

julia> b = rand(100);

julia> @time f(a, b);
  0.027304 seconds (13.31 k allocations: 711.914 KiB, 99.97% compilation time)

julia> @time f(a, b);
  0.000004 seconds (3 allocations: 944 bytes)

julia> @time g(a, b);
  0.007733 seconds (5.17 k allocations: 261.992 KiB, 99.91% compilation time)

julia> @time g(a, b);
  0.000003 seconds (1 allocation: 16 bytes)

and here are a few more options to consider for simpler syntax

julia> h(a,b) = sum(zip(a,b)) do (x,y); x*y; end
h (generic function with 1 method)

julia> k(a,b) = sum(splat(*), (a,b))
k (generic function with 1 method)

julia> @time h(a, b);
  0.018568 seconds (131.63 k allocations: 6.261 MiB, 15.16% gc time, 99.96% compilation time)

julia> @time h(a, b);
  0.000002 seconds (1 allocation: 16 bytes)

julia> @time k(a, b);
  0.028699 seconds (146.38 k allocations: 7.347 MiB, 99.72% compilation time)

julia> @time k(a, b);
  0.000016 seconds (403 allocations: 7.891 KiB)

return Q
end
41 changes: 38 additions & 3 deletions test/community/modularity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
@test isapprox(Q2, 0.6071428571428571, atol=1e-3)
end

# Test with self loop
add_edge!(barbell, 1, 1)
for g in test_generic_graphs(barbell)
Q = @inferred(modularity(g, c))
@test isapprox(Q, 0.3671875, atol=1e-3)
end

# 2. directed test cases
triangle = SimpleDiGraph(3)
add_edge!(triangle, 1, 2)
Expand Down Expand Up @@ -65,6 +72,20 @@
Q = @inferred(modularity(g, c, distmx=d))
@test isapprox(Q, 0.045454545454545456, atol=1e-3)
end
# Add self loop with weight 5
add_edge!(barbell, 1, 1)
d = [
[5 1 1 0 0 0]
[1 0 1 0 0 0]
[1 1 0 1 0 0]
[0 0 1 0 1 1]
[0 0 0 1 0 1]
[0 0 0 1 1 0]
]
for g in test_generic_graphs(barbell)
Q = @inferred(modularity(g, c, distmx=d))
@test isapprox(Q, 0.329861, atol=1e-3)
end

# 3.2. directed and weighted test cases
triangle = SimpleDiGraph(3)
Expand All @@ -73,12 +94,12 @@
add_edge!(triangle, 3, 1)

barbell = blockdiag(triangle, triangle)
add_edge!(barbell, 1, 4) # this edge has a weight of 5
add_edge!(barbell, 3, 4) # this edge has a weight of 5
c = [1, 1, 1, 2, 2, 2]
d = [
[0 1 0 5 0 0]
[0 1 0 0 0 0]
[0 0 1 0 0 0]
[1 0 0 0 0 0]
[1 0 0 5 0 0]
[0 0 0 0 1 0]
[0 0 0 0 0 1]
[0 0 0 1 0 0]
Expand All @@ -87,4 +108,18 @@
Q = @inferred(modularity(g, c, distmx=d))
@test isapprox(Q, 0.1487603305785124, atol=1e-3)
end
# Add self loop with weight 5
add_edge!(barbell, 1, 1)
d = [
[5 1 0 0 0 0]
[0 0 1 0 0 0]
[1 0 0 1 0 0]
[0 0 0 0 1 0]
[0 0 0 0 0 1]
[0 0 0 1 0 0]
]
for g in test_generic_graphs(barbell)
Q = @inferred(modularity(g, c, distmx=d))
@test isapprox(Q, 0.333333333, atol=1e-3)
end
end
Loading