Skip to content

Instantly share code, notes, and snippets.

@n-kats
Created October 24, 2014 12:28
Show Gist options
  • Save n-kats/df1ade3935518c528269 to your computer and use it in GitHub Desktop.
Save n-kats/df1ade3935518c528269 to your computer and use it in GitHub Desktop.
This will calculate genus of Gauss codes
class GaussCode
def initialize(n,germ=Array.new(n).map!.each_with_index{|x,i|rand( 2*n - 2*i - 1 )})
@n = n
@germ = germ
@pairs = []
ar = (0...(2*n)).to_a
n.times do |i|
head = ar.shift
tail = ar[germ[i]]
ar .delete tail
@pairs << [head, tail]
end
end
def size
@n
end
def std_genus
(size + 2 - bounds_num)/2
end
def bounds_num
cir = Circuit.std(size*2)
@pairs.each do |i,j|
cir.surgery(i,j)
# p cir.nexts
end
# p cir.to_s
cir.components + 1
end
def reverse(i)
@pairs[@pairs.flatten.index(i)/2].dup.delete(i).first
end
alias tau reverse
end
class Circuit < Array
class << self
def std(n)
new(n).map!.with_index{|x,i|i}.build_std_next
end
end
def nexts
@next
end
def build_std_next
@next = {}
self.each_with_index do |x,i|
if i == (self.length - 1)
@next[x] = self.first
else
@next[x] = self[i+1]
end
end
self
end
def next_pt(i)
@next[i]
end
def surgery(i,j)
ii = @next[i]
jj = @next[j]
@next[i] = jj
@next[j] = ii
end
def components
return 0 if self.empty?
x = self.dup
y = x.first
cnt = 0
until x.empty?
x.delete y
yy = next_pt(y)
if x.include? yy
y = yy
else
cnt += 1
y = x.first
end
end
cnt
end
def to_s
"#{super} #{@next}"
end
end
=begin
p GaussCode.new(rand(10)+5)
p Circuit.new(10){rand(9)}.class
x = Circuit.std(19)
(y = x.dup).pop
p x, y, x.components
puts '-'*80
5.times do
p (p GaussCode.new(5)).std_genus
end
puts '-'*80
p GaussCode.new(5,Array.new(5,0)).std_genus
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment