-
Notifications
You must be signed in to change notification settings - Fork 93
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
Added edge-betweenness.jl to centralities #277
Open
jwassmer
wants to merge
17
commits into
JuliaGraphs:master
Choose a base branch
from
jwassmer:edge-betweenness
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+240
−5
Open
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
86a8c4f
Added edge-betweenness.jl to centralities
a6c4560
Apply suggestions from code review
jwassmer c56a445
bc-dict to sparsematrix & new test for weighted g
10b31bc
fixed ebc over random subset of k vertices.
b8d09d3
added test-file for edge-betweenness
50b48c7
Use GenericGraph for testing centrality algorithms (#272)
simonschoelly be10f80
Use GenericGraph for testing cycles algorithms (#274)
simonschoelly 2d79c3a
Use GenericGraph for testing shortestpaths algorithms (#275)
simonschoelly a77bc5f
Use GenericGraph for testing spanningtrees algorithms (#276)
simonschoelly 45788ab
test file check
0d3630d
Merge branch 'JuliaGraphs:master' into edge-betweenness
jwassmer 894b9f7
added ebc to graphs.jl and to runtests.jl
0b52ed8
using AbstractSparseMatrix in graphs.jl
00abd73
added spzeros to graphs.jl
a4016b7
change doctest bc abstractarray not recongnized
a1539e4
update doc for weighted ebc & vs to kwarg
921d837
update ebc test such that vertices is kwarg
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
""" | ||
edge_betweenness_centrality(g, k) | ||
|
||
Compute the [edge betweenness centrality](https://en.wikipedia.org/wiki/Centrality#Betweenness_centrality) of an edge `e`. | ||
It is defined as the sum of the fraction of all-pairs shortest paths that pass through `e` | ||
`` | ||
bc(e) = \\sum_{s, t \\in V} | ||
\\frac{\\sigma_{st}(e)}{\\sigma_{st}} | ||
``. | ||
|
||
where `V`, is the set of nodes, \\frac{\\sigma_{st}} is the number of shortest-paths, and \\frac{\\sigma_{st}(e)} is the number of those paths passing through edge. | ||
|
||
### Optional Arguments | ||
- `normalize=true`: If true, normalize the betweenness values by the | ||
total number of possible distinct paths between all pairs in the graphs. | ||
For an undirected graph, this number is ``\\frac{(|V|-1)(|V|-2)}{2}`` | ||
and for a directed graph, ``{(|V|-1)(|V|-2)}``. | ||
|
||
|
||
### References | ||
- Brandes 2001 & Brandes 2008 | ||
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 you give more details to help us check the algorithm? |
||
|
||
# Examples | ||
```jldoctest | ||
julia> using Graphs | ||
|
||
julia> edge_betweenness_centrality(star_graph(5)) | ||
Dict{Graphs.SimpleGraphs.SimpleEdge{Int64}, Float64} with 4 entries: | ||
Edge 1 => 2 => 0.4 | ||
Edge 1 => 3 => 0.4 | ||
Edge 1 => 4 => 0.4 | ||
Edge 1 => 5 => 0.4 | ||
|
||
julia> edge_betweenness_centrality(path_digraph(6)) | ||
Dict{Graphs.SimpleGraphs.SimpleEdge{Int64}, Float64} with 5 entries: | ||
Edge 4 => 5 => 0.266667 | ||
Edge 1 => 2 => 0.166667 | ||
Edge 3 => 4 => 0.3 | ||
Edge 5 => 6 => 0.166667 | ||
Edge 2 => 3 => 0.266667 | ||
``` | ||
""" | ||
gdalle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
function edge_betweenness_centrality( | ||
g::AbstractGraph, vs=vertices(g), distmx::AbstractMatrix=weights(g); normalize=true | ||
jwassmer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
edge_betweenness = Dict(edges(g) .=> 0.0) | ||
gdalle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for o in vs | ||
state = dijkstra_shortest_paths(g, o, distmx; allpaths=true, trackvertices=true) | ||
_accumulate_edges!(edge_betweenness, state) | ||
end | ||
gdalle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_rescale_e!(edge_betweenness, nv(g), normalize, is_directed(g)) | ||
|
||
return edge_betweenness | ||
end | ||
|
||
function _accumulate_edges!(edge_betweenness::AbstractDict, state::Graphs.AbstractPathState) | ||
σ = state.pathcounts | ||
pred = state.predecessors | ||
seen = state.closest_vertices | ||
δ = Dict(seen .=> 0.0) | ||
|
||
while length(seen) > 0 | ||
w = pop!(seen) | ||
|
||
coeff = (1.0 + δ[w]) / σ[w] | ||
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 you explain the dynamics of |
||
for v in pred[w] | ||
c = σ[v] * coeff | ||
if Edge(v, w) ∉ edge_betweenness.keys | ||
jwassmer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
edge_betweenness[Edge(w, v)] += c | ||
else | ||
edge_betweenness[Edge(v, w)] += c | ||
δ[v] += c | ||
end | ||
end | ||
end | ||
return nothing | ||
end | ||
|
||
function _rescale_e!( | ||
edge_betweenness::AbstractDict, n::Integer, normalize::Bool, directed::Bool=false | ||
) | ||
if normalize | ||
if n <= 1 | ||
scale = nothing # no normalization b=0 for all nodes | ||
else | ||
scale = 1 / (n * (n - 1)) | ||
end | ||
else # rescale by 2 for undirected graphs | ||
if !directed | ||
scale = 0.5 | ||
else | ||
scale = nothing | ||
end | ||
end | ||
if scale !== nothing | ||
for (k, v) in edge_betweenness | ||
edge_betweenness[k] *= scale | ||
end | ||
end | ||
return nothing | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
have you checked how this displays by building the docs?
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.