Skip to content

Commit

Permalink
added girth
Browse files Browse the repository at this point in the history
  • Loading branch information
RandyRDavila committed Oct 5, 2023
1 parent 2228ddb commit 775d654
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/GraphInvariants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 ##############################

Expand Down Expand Up @@ -118,6 +119,7 @@ export GraphRule

export Order
export Size
export Girth

export MaximumDegree
export MinimumDegree
Expand Down
32 changes: 32 additions & 0 deletions src/basics/from_graphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 775d654

Please sign in to comment.