Skip to content

Commit 8128430

Browse files
authored
get_weight with AbstractEdge (#46)
Quick PR to allow `get_weight` to be called with any `AbstractEdge` using the Interface defined in `Graphs`. This fixes my issue #45. (assuming it is an issue and not a conscious design decision.)
1 parent a8da15d commit 8128430

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

docs/src/tutorial.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ julia> get_weight(g, 1, 2)
2828
0.5
2929
```
3030

31+
Or by passing in any `e::AbstractEdge`:
32+
33+
```jldoctest tuto
34+
julia> get_weight(g, Edge(1, 2))
35+
0.5
36+
```
37+
3138
Find the shortest path from vertex 1 to vertex 3, taking weights into account:
3239

3340
```jldoctest tuto

src/abstractsimpleweightedgraph.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ weighttype(::AbstractSimpleWeightedGraph{T,U}) where {T} where {U} = U
2828

2929
"""
3030
get_weight(g, u, v)
31+
get_weight(g, e)
3132
32-
Retrieve the weight of edge `(u, v)`.
33+
Retrieve the weight of edge `(u, v)` or `e`.
3334
"""
3435
get_weight(g::AbstractSimpleWeightedGraph, u::Integer, v::Integer) = weights(g)[u, v]
36+
function get_weight(g::AbstractSimpleWeightedGraph, e::AbstractEdge{<:Integer})
37+
return weights(g)[src(e), dst(e)]
38+
end
3539

3640
## Vertices
3741

src/overrides.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ function Graphs.induced_subgraph(
194194
for e in elist
195195
if has_edge(g, e)
196196
i, j = index_map[src(e)], index_map[dst(e)]
197-
w = get_weight(g, dst(e), src(e))
197+
w = get_weight(g, e)
198198
push!(I, j) # storage is transposed!
199199
push!(J, i)
200200
push!(W, w)

test/simpleweightedgraph.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ using SparseArrays
204204
@test sum(weights(g)) == 2 * ne(g) * 3
205205
@test @inferred(get_weight(g, 1, 2)) == 3
206206

207+
@test @inferred(get_weight(g, Edge(1, 2))) == 3
208+
@test @inferred(get_weight(g, SimpleWeightedEdge(1, 2, 0.5))) == 3
209+
207210
g = SimpleWeightedDiGraph(path_graph(5), 4.0)
208211
@test sum(weights(g)) == ne(g) * 4.0
209212

@@ -294,12 +297,18 @@ using SparseArrays
294297

295298
@test g[1, 2, Val{:weight}()] 5
296299
@test get_weight(g, 1, 2) 5
300+
@test get_weight(g, Edge(1, 2)) 5
301+
@test get_weight(g, SimpleWeightedEdge(1, 2)) 5
297302
if is_directed(G)
298303
@test g[2, 1, Val{:weight}()] 0
299304
@test get_weight(g, 2, 1) 0
305+
@test get_weight(g, Edge(2, 1)) 0
306+
@test get_weight(g, SimpleWeightedEdge(2, 1)) 0
300307
else
301308
@test g[2, 1, Val{:weight}()] 5
302309
@test get_weight(g, 2, 1) 5
310+
@test get_weight(g, Edge(2, 1)) 5
311+
@test get_weight(g, SimpleWeightedEdge(2, 1)) 5
303312
end
304313
m = adjacency_matrix(g)
305314
@test g[2, 1, Val{:weight}()] g.weights[1, 2]

0 commit comments

Comments
 (0)