-
Notifications
You must be signed in to change notification settings - Fork 59
Derangements #150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Derangements #150
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fa90350
Add a function to generate all derangements.
FedericoStra d16ccf7
Add tests for `derangements`
FedericoStra 6897953
Add doctest for `derangements`
FedericoStra 82fc13d
Fix type stability in `multiset_permutations`
FedericoStra 4944526
Remove unnecessary call to `collect`
FedericoStra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
#Permutations | ||
|
||
export | ||
derangements, | ||
levicivita, | ||
multiset_permutations, | ||
nthperm!, | ||
|
@@ -136,6 +137,42 @@ function permutations(a, t::Integer) | |
return Permutations(a, t) | ||
end | ||
|
||
""" | ||
derangements(a) | ||
|
||
Generate all derangements of an indexable object `a` in lexicographic order. | ||
Because the number of derangements can be very large, this function returns an iterator object. | ||
Use `collect(derangements(a))` to get an array of all derangements. | ||
Only works for `a` with defined length. | ||
|
||
# Examples | ||
```jldoctest | ||
julia> derangements("julia") |> collect | ||
44-element Vector{Vector{Char}}: | ||
['u', 'j', 'i', 'a', 'l'] | ||
['u', 'j', 'a', 'l', 'i'] | ||
['u', 'l', 'j', 'a', 'i'] | ||
['u', 'l', 'i', 'a', 'j'] | ||
['u', 'l', 'a', 'j', 'i'] | ||
['u', 'i', 'j', 'a', 'l'] | ||
['u', 'i', 'a', 'j', 'l'] | ||
['u', 'i', 'a', 'l', 'j'] | ||
['u', 'a', 'j', 'l', 'i'] | ||
['u', 'a', 'i', 'j', 'l'] | ||
⋮ | ||
['a', 'j', 'i', 'l', 'u'] | ||
['a', 'l', 'j', 'u', 'i'] | ||
['a', 'l', 'u', 'j', 'i'] | ||
['a', 'l', 'i', 'j', 'u'] | ||
['a', 'l', 'i', 'u', 'j'] | ||
['a', 'i', 'j', 'u', 'l'] | ||
['a', 'i', 'j', 'l', 'u'] | ||
['a', 'i', 'u', 'j', 'l'] | ||
['a', 'i', 'u', 'l', 'j'] | ||
``` | ||
""" | ||
derangements(a) = (d for d in multiset_permutations(a, length(a)) if all(t -> t[1] != t[2], zip(a, d))) | ||
|
||
|
||
function nextpermutation(m, t, state) | ||
perm = [m[state[i]] for i in 1:t] | ||
|
@@ -249,8 +286,8 @@ julia> collect(multiset_permutations([1,1,2], 3)) | |
``` | ||
""" | ||
function multiset_permutations(a, t::Integer) | ||
m = unique(collect(a)) | ||
f = [sum([c == x for c in a]) for x in m] | ||
m = unique(a) | ||
f = [sum(c == x for c in a)::Int for x in m] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I also removed the square brackets to turn the comprehension into a generator expression, which is ever so slightly more efficient, as can be verified by benchmarking. Benchmarkjulia> a = repeat(1:10, 3); With comprehension: julia> @benchmark multiset_permutations($a)
BenchmarkTools.Trial: 10000 samples with 10 evaluations per sample.
Range (min … max): 1.284 μs … 1.236 ms ┊ GC (min … max): 0.00% … 99.62%
Time (median): 1.350 μs ┊ GC (median): 0.00%
Time (mean ± σ): 1.921 μs ± 16.533 μs ┊ GC (mean ± σ): 12.11% ± 1.41%
▆█▇▄▂ ▁ ▁▂ ▁▂▂▁▁▂▃▂▁▂▂▂▁▁▁▁▁ ▁
█████▇▆▅▅▄▅▇▇▇▅▆▄▅▅▇▆▅▅▇██▇█████████████████████████▇▇▇▇▆▅ █
1.28 μs Histogram: log(frequency) by time 3.07 μs <
Memory estimate: 3.39 KiB, allocs estimate: 55. With generator expression: julia> @benchmark multiset_permutations($a)
BenchmarkTools.Trial: 10000 samples with 22 evaluations per sample.
Range (min … max): 943.545 ns … 462.770 μs ┊ GC (min … max): 0.00% … 99.37%
Time (median): 976.227 ns ┊ GC (median): 0.00%
Time (mean ± σ): 1.251 μs ± 5.907 μs ┊ GC (mean ± σ): 13.38% ± 3.54%
▇█▄▁ ▁ ▁ ▂▂▁▂▁▂▁▁▁ ▂
████▇▇████▇▆▅▄▃▁▁▁▁▄▆▆█████████████▇▇▆▆▆▄▄▄▄▁▄▄▄▃▅▄▃▃▁▄▄▁▃▁▁▅ █
944 ns Histogram: log(frequency) by time 2.54 μs <
Memory estimate: 2.45 KiB, allocs estimate: 35. |
||
multiset_permutations(m, f, t) | ||
end | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This call to
collect
is unnecessary becauseunique
already collects the items into a new vector.Benchmark
Benchmark improves to