Skip to content

Commit f14f096

Browse files
authored
Merge pull request #31 from BatyLeo/better-topological-order
Improve topological order robustness
2 parents 6b7808f + 6cee708 commit f14f096

File tree

5 files changed

+38
-8
lines changed

5 files changed

+38
-8
lines changed

CITATION.bib

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ @misc{ConstrainedShortestPaths.jl
22
author = {Léo Baty and contributors},
33
title = {ConstrainedShortestPaths.jl},
44
url = {https://github.com/BatyLeo/ConstrainedShortestPaths.jl},
5-
version = {v0.6.3},
6-
year = {2024},
7-
month = {12}
5+
version = {v0.6.4},
6+
year = {2025},
7+
month = {03}
88
}

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ConstrainedShortestPaths"
22
uuid = "b3798467-87dc-4d99-943d-35a1bd39e395"
33
authors = ["Léo Baty and contributors"]
4-
version = "0.6.3"
4+
version = "0.6.4"
55

66
[deps]
77
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"

src/ConstrainedShortestPaths.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ using Graphs:
1212
dst,
1313
edges,
1414
outneighbors,
15+
inneighbors,
1516
induced_subgraph
1617
using PiecewiseLinearFunctions:
1718
PiecewiseLinearFunction, convex_meet, remove_redundant_breakpoints, convex_meet

src/interface.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,15 @@ function CSPInstance(;
7070
cost_function,
7171
forward_functions,
7272
backward_functions=nothing,
73+
topological_ordering=nothing,
7374
)
7475
@assert is_directed(graph) "`graph` must be a directed graph"
7576
@assert !is_cyclic(graph) "`graph` must be acyclic"
76-
useful_vertices = topological_order(graph, origin_vertex, destination_vertex)
77+
useful_vertices = if isnothing(topological_ordering)
78+
topological_order(graph, origin_vertex, destination_vertex)
79+
else
80+
topological_ordering
81+
end
7782
is_useful = falses(nv(graph))
7883
is_useful[useful_vertices] .= true
7984
if isnothing(destination_backward_resource) && isnothing(backward_functions)

src/utils/utils.jl

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@ function remove_dominated!(Mw::AbstractVector{R}, rq::R) where {R}
1313
return nothing
1414
end
1515

16+
function dfs(graph::G, v::T; out=true) where {T,G<:AbstractGraph{T}}
17+
visited = falses(nv(graph))
18+
stack = [v]
19+
while !isempty(stack)
20+
v = pop!(stack)
21+
if visited[v]
22+
continue
23+
end
24+
visited[v] = true
25+
for w in (out ? outneighbors(graph, v) : inneighbors(graph, v))
26+
push!(stack, w)
27+
end
28+
end
29+
return visited
30+
end
31+
1632
# Topological order computing
1733
function scan!(
1834
graph::G, vertex::T, order::Vector{T}, opened::BitVector
@@ -29,11 +45,19 @@ function scan!(
2945
end
3046

3147
function topological_order(graph::G, s::T, t::T) where {T,G<:AbstractGraph{T}}
48+
s_visited = dfs(graph, s; out=true)
49+
t_visited = dfs(graph, t; out=false)
50+
visited = s_visited .& t_visited
51+
3252
order = Int[]
3353
opened = falses(nv(graph))
3454
scan!(graph, s, order, opened)
3555

36-
start = findfirst(x -> (x == t), order) # Can we do smarter than that ?
37-
@assert !isnothing(start)
38-
return order[start:end]
56+
res = [o for o in order if visited[o]]
57+
@assert res[1] == t
58+
return res
59+
60+
# start = findfirst(x -> (x == t), order) # Can we do smarter than that ?
61+
# @assert !isnothing(start)
62+
# return order[start:end]
3963
end

0 commit comments

Comments
 (0)