|
| 1 | +""" |
| 2 | + graph_property(graph::AbstractGraph, property_specification::GraphProperty{T}, [options = nothing])::Union{Nothing,Some{<:T}} |
| 3 | +
|
| 4 | +Get the graph property specified by `property_specification` of the graph `graph`. |
| 5 | +
|
| 6 | +Only some properties are implemented currently. |
| 7 | +
|
| 8 | +A `nothing` return value may be returned in some cases, such as when a time limit specified in `options` was reached. |
| 9 | +
|
| 10 | +Third-party packages may add methods. Only add three-argument methods, and only if you own the third argument, `options`. |
| 11 | +""" |
| 12 | +function graph_property end |
| 13 | + |
| 14 | +function graph_property(graph::AbstractGraph, prop_spec::GraphProperty) |
| 15 | + graph_property(graph, prop_spec, nothing) |
| 16 | +end |
| 17 | + |
| 18 | +function graph_property(graph::AbstractGraph, ::GraphProperties.NumberOfVertices, ::Nothing) |
| 19 | + Some(nv(graph)) |
| 20 | +end |
| 21 | + |
| 22 | +function graph_property(graph::AbstractGraph, ::GraphProperties.DegreeSequence, ::Nothing) |
| 23 | + if is_directed(graph) |
| 24 | + throw(ArgumentError("expected undirected graph")) |
| 25 | + end |
| 26 | + Some(sort(degree(graph))) |
| 27 | +end |
| 28 | + |
| 29 | +function graph_property(graph::AbstractGraph, ::GraphProperties.NumberOfEdges, ::Nothing) |
| 30 | + if is_directed(graph) |
| 31 | + throw(ArgumentError("expected undirected graph")) |
| 32 | + end |
| 33 | + Some(ne(graph)) |
| 34 | +end |
| 35 | + |
| 36 | +function graph_property(graph::AbstractGraph, ::GraphProperties.NumberOfArcs, ::Nothing) |
| 37 | + if !is_directed(graph) |
| 38 | + throw(ArgumentError("expected directed graph")) |
| 39 | + end |
| 40 | + Some(ne(graph)) |
| 41 | +end |
| 42 | + |
| 43 | +function graph_property( |
| 44 | + graph::AbstractGraph, ::GraphProperties.NumberOfConnectedComponents, ::Nothing |
| 45 | +) |
| 46 | + if is_directed(graph) |
| 47 | + throw(ArgumentError("expected undirected graph")) |
| 48 | + end |
| 49 | + # TODO: performance: avoid allocating the components |
| 50 | + Some(length(connected_components(graph))) |
| 51 | +end |
| 52 | + |
| 53 | +function graph_property( |
| 54 | + graph::AbstractGraph, ::GraphProperties.NumberOfWeaklyConnectedComponents, ::Nothing |
| 55 | +) |
| 56 | + if !is_directed(graph) |
| 57 | + throw(ArgumentError("expected directed graph")) |
| 58 | + end |
| 59 | + # TODO: performance: avoid allocating the components |
| 60 | + Some(length(weakly_connected_components(graph))) |
| 61 | +end |
| 62 | + |
| 63 | +function graph_property( |
| 64 | + graph::AbstractGraph, ::GraphProperties.NumberOfStronglyConnectedComponents, ::Nothing |
| 65 | +) |
| 66 | + if !is_directed(graph) |
| 67 | + throw(ArgumentError("expected directed graph")) |
| 68 | + end |
| 69 | + # TODO: performance: avoid allocating the components |
| 70 | + Some(length(strongly_connected_components(graph))) |
| 71 | +end |
0 commit comments