Skip to content

Commit

Permalink
Improve cycle reconstruction performance (#173)
Browse files Browse the repository at this point in the history
* Improve perfomance of cycle reconstruction

* update Project.toml

* Update src/extra/cycles.jl

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
mtsch and github-actions[bot] authored Dec 19, 2024
1 parent 8b296a0 commit 6859825
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/extra/cycles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ function OneSkeleton(
return OneSkeleton{T,F,S,typeof(weights)}(filtration, thresh, weights, removed)
end

function Graphs.SimpleGraph(g::OneSkeleton)
return SimpleGraph(edges(g))
end

_birth_or_value::AbstractCell) = birth(σ)
_birth_or_value(σ) = σ
_in::S, g::OneSkeleton{S}) where {S} = !isnothing(σ) && σ g.threshold && σ g.removed
Expand Down Expand Up @@ -97,19 +101,21 @@ end
Find the shortest cycle in `g` that has exactly one edge from `g.removed`.
"""
function _find_cycle(g, dists)
function _find_cycle(skeleton, dists)
# Idea:
# best_weight is the current best length of shortest cycle.
# best_path is the current best candidate for shortest cycle.
# best_simplex completes best_path to a cycle.
# We go through all edges in g.removed and find the shortest path through its
# We go through all edges in skeleton.removed and find the shortest path through its
# endpoints. The shortest among those is the shortest cycle.
graph = SimpleGraph(skeleton)

best_weight = missing
best_path = edgetype(g)[]
best_sx = first(g.removed)
for sx in g.removed
u, v = _linear.(Ref(g), sx)
path = a_star(g, u, v, dists, _heuristic(g.filtration, v, dists))
best_path = edgetype(skeleton)[]
best_sx = first(skeleton.removed)
for sx in skeleton.removed
u, v = _linear.(Ref(skeleton), sx)
path = a_star(graph, u, v, dists, _heuristic(skeleton.filtration, v, dists))
weight = _path_length(dists, path) + dists[u, v]
if !isempty(path) && isless(weight, best_weight)
best_weight = weight
Expand All @@ -123,8 +129,8 @@ function _find_cycle(g, dists)
result = [best_sx]
# Convert the type of best_path from `Edge`s to the simplex type of the filtration.
for e in best_path
u, v = _inv_linear.(Ref(g), (src(e), dst(e)))
push!(result, simplex(g.filtration, Val(1), (u, v)))
u, v = _inv_linear.(Ref(skeleton), (src(e), dst(e)))
push!(result, simplex(skeleton.filtration, Val(1), (u, v)))
end
return result
end
Expand Down

2 comments on commit 6859825

@mtsch
Copy link
Owner Author

@mtsch mtsch commented on 6859825 Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error while trying to register: Version 0.16.13 already exists

Please sign in to comment.