Created
February 11, 2025 04:43
-
-
Save tsaito-cyber/d8addef8ae580275e2b676b65c2d9d44 to your computer and use it in GitHub Desktop.
all_topo.rb
This file contains 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
# 位相空間を全て数え上げるプログラム | |
def dedup(arr) | |
arr.map {|c| c.sort}.uniq.sort_by {|x| x.length} | |
end | |
def topo(xs) | |
return [[]] if xs.empty? | |
rest = topo(xs[1..]) | |
dedup(rest.flat_map {|c| [c & xs[0], c | xs[0], c]}) | |
end | |
def combs(xs) | |
return [[]] if xs.empty? | |
rest = combs(xs[1..]) | |
rest.map { |c| [xs[0]] + c } + rest | |
end | |
def all_topo(xs) | |
dedup(combs(combs(xs)).map {|comb| topo([*[[], xs], *comb])}) | |
end | |
p all_topo([1,2,3,4]).length |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment