Skip to content

Commit

Permalink
Compute longest paths from terminals
Browse files Browse the repository at this point in the history
  • Loading branch information
lukem12345 committed Aug 25, 2024
1 parent 7daf15b commit 303962d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/DiagrammaticEquations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module DiagrammaticEquations
using Catlab

export
DerivOp, append_dot, normalize_unicode, infer_states, infer_types!,
DerivOp, append_dot, normalize_unicode, infer_states, infer_terminals, infer_types!,
# Deca
op1_res_rules_1D, op2_res_rules_1D, op1_res_rules_2D, op2_res_rules_2D,
op1_inf_rules_1D, op2_inf_rules_1D, op1_inf_rules_2D, op2_inf_rules_2D,
Expand Down
7 changes: 4 additions & 3 deletions src/graph_traversal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ number_of_ops(d::SummationDecapode) = nparts(d, :Op1) + nparts(d, :Op2) + nparts

start_nodes(d::SummationDecapode) = vcat(infer_states(d), incident(d, :Literal, :type))

Check warning on line 30 in src/graph_traversal.jl

View check run for this annotation

Codecov / codecov/patch

src/graph_traversal.jl#L30

Added line #L30 was not covered by tests


# TODO: This could be Catlab'd. Hypergraph category? Migration to a DWD?
""" function hyper_edge_list(d::SummationDecapode)
Expand All @@ -55,11 +54,13 @@ Taking the maximum of the non-infinite short paths from state variables induces
https://en.wikipedia.org/wiki/Floyd–Warshall_algorithm
"""
function floyd_warshall(d::SummationDecapode)
# Define weights.
w(e) = (length(e.dom) == 1 && e.name [:∂ₜ,:dt]) ? -Inf : -1
# Init dists
V = nparts(d, :Var)
dist = fill(Inf, (V, V))
foreach(hyper_edge_list(d)) do e
dist[(e.dom), e.cod] .= 1
dist[(e.dom), e.cod] .= w(e)
end
for v in 1:V
dist[v,v] = 0
Expand All @@ -86,7 +87,7 @@ The vector returned by this function maps each vertex to the order that it would
function topological_sort_verts(d::SummationDecapode)
m = floyd_warshall(d)
map(parts(d,:Var)) do v
maximum(filter(!isinf, m[start_nodes(d),v]))
minimum(filter(!isinf, m[v,infer_terminals(d)]))
end
end

Expand Down
6 changes: 6 additions & 0 deletions test/graph_traversal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,10 @@ end
end
result = topological_sort_edges(op_combo)
@test is_correct_length(op_combo, result)

sum_with_single_dependency = @decapode begin
F == A + f(A) + h(g(A))
end
result = topological_sort_edges(sum_with_single_dependency)
@test is_correct_length(sum_with_single_dependency, result)
end

0 comments on commit 303962d

Please sign in to comment.