Skip to content

Commit

Permalink
fix #817
Browse files Browse the repository at this point in the history
finding an optimal nest for a large list > 12 or so starts to take a
noticeable long time - O(n^2) scaling. The bulk of the time was in
`find_all_segment_splits`.

_backtrack is refactored so that permutations function is no longer
needed and we prune most of the search tree.
  • Loading branch information
domluna committed Mar 2, 2024
1 parent 4266dce commit c02a568
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
2 changes: 0 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ version = "1.0.50"

[deps]
CSTParser = "00ebfdb7-1f24-5e51-bd34-a7502290713f"
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
CommonMark = "a80b9123-70ca-4bc0-993e-6e3bcb318db6"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Glob = "c27321d9-0574-5035-807b-f59d2c89b15c"
Expand All @@ -15,7 +14,6 @@ Tokenize = "0796e94c-ce3b-5d07-9a54-7f471281c624"

[compat]
CSTParser = "^3.4.0"
Combinatorics = "1"
CommonMark = "0.5, 0.6, 0.7, 0.8"
DataStructures = "0.17, 0.18"
Glob = "1.3"
Expand Down
1 change: 0 additions & 1 deletion src/JuliaFormatter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ using Pkg.TOML: parsefile
using Glob
import CommonMark: block_modifier
import Base: get, pairs
using Combinatorics: permutations
using CommonMark:
AdmonitionRule,
CodeBlock,
Expand Down
24 changes: 12 additions & 12 deletions src/nest_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,9 @@ function nest_if_over_margin!(
return false
end

function find_all_segment_splits(n::Int, k::Int)
function find_all_segment_splits2(n::Int, k::Int, max_margin::Int)
res = Vector{Int}[]
# n = size(dp, 1)

if n == k
return [fill(1, k)]
Expand All @@ -190,25 +191,23 @@ function find_all_segment_splits(n::Int, k::Int)
push!(res, t)
end
return
elseif current_sum >= n
return
end

start_val = isempty(t) ? 1 : last(t)
max_val = n - current_sum - (k - length(t) - 1)
start_val = 1
max_val = n - k + 1

for i in start_val:min(n, max_val)
_backtrack([t; i], current_sum + i)
end
end
_backtrack(Int[], 0)

all_splits = Vector{Int}[]
for r in res
for c in unique(permutations(r))
push!(all_splits, c)
end
for i in 1:(n - k + 1)
_backtrack([i], i)
end

return all_splits
return res
end

"""
Expand Down Expand Up @@ -285,7 +284,7 @@ function find_optimal_nest_placeholders(
end
end

# @info "" dp placeholder_inds
@info "" dp placeholder_inds

N = size(dp, 1)

Expand All @@ -307,7 +306,7 @@ function find_optimal_nest_placeholders(
ranges
end

all_splits = find_all_segment_splits(N, s)
@time all_splits = find_all_segment_splits(N, s, max_margin)

best_split = UnitRange{Int}[]
min_diff = 1_000_000 # big number!
Expand All @@ -330,6 +329,7 @@ function find_optimal_nest_placeholders(
segments = Tuple{Int,Int}[]
for s in 1:N
segments = find_best_segments(s)
@info "" segments
fits = true
for (i, s) in enumerate(segments)
if i == 1
Expand Down

0 comments on commit c02a568

Please sign in to comment.