diff --git a/src/GraphInvariants.jl b/src/GraphInvariants.jl index 1486f45..dc0c254 100644 --- a/src/GraphInvariants.jl +++ b/src/GraphInvariants.jl @@ -28,6 +28,7 @@ struct AverageDegree <: AbstractCardinality end struct Radius <: AbstractCardinality end struct Diameter <: AbstractCardinality end +struct Girth <: AbstractCardinality end ##################### Graph Optimal Sets and Cardinalities ############################## @@ -118,6 +119,7 @@ export GraphRule export Order export Size +export Girth export MaximumDegree export MinimumDegree diff --git a/src/basics/from_graphs.jl b/src/basics/from_graphs.jl index 732001f..71c7944 100644 --- a/src/basics/from_graphs.jl +++ b/src/basics/from_graphs.jl @@ -88,4 +88,36 @@ function compute( g::AbstractGraph{T} ) where T <: Integer return radius(g) +end + +function compute( + ::Type{Girth}, + g::AbstractGraph{T}, +) where T <: Integer + + shortest_cycle = Inf + + for v in vertices(g) + dist = fill(Inf, nv(g)) + parent = zeros(Int, nv(g)) + dist[v] = 0 + q = [v] + + while !isempty(q) + current = popfirst!(q) + for neighbor in neighbors(g, current) + if dist[neighbor] == Inf + push!(q, neighbor) + dist[neighbor] = dist[current] + 1 + parent[neighbor] = current + elseif parent[current] != neighbor + # We've found a cycle + cycle_length = dist[current] + dist[neighbor] + 1 + shortest_cycle = min(shortest_cycle, cycle_length) + end + end + end + end + + return isinf(shortest_cycle) ? 0 : shortest_cycle end \ No newline at end of file