require "groups"
class Composition_series
  Graph = {}
  Series = []
  SubSeries = []

  def initialize(sets, g)
    @sets = sets
    mk_table
    descend([g])
    subdivide
  end

  def mk_table
    @sets.each do |x|
      Graph[x] = []
      @sets.each do |y|
	if x.incl?(y) and y.normal?(x) and x != y
	  Graph[x].push y
	end
      end
    end
  end

  def each(&b)
    SubSeries.each(&b)
  end

  def descend(series)
    g = series.last
    if Graph[g].empty?
	Series.push series.Set
    else
      Graph[g].each do |h|
	descend(series + [h])
      end
    end
  end

  def subdivide
    Series.each do |x|
      if Series.all{|y| y == x or !y.incl?(x)}
	SubSeries.push x
      end
    end
  end
end


if __FILE__ == $0
  require "permutation"
  require "sets"
  n = Integer(ARGV.shift)
  n = n % 3 + 3
  s = Marshal.get("data/S#{n}.ma")
  ss = Marshal.get("data/S#{n}subgr.ma")

  PermGr.use_setname
  s.each_with_index{|x,i| x.setname i}
  Set.use_setname
  ss.each_with_index{|x,i| x.setname "#{i}(#{x.size})"}
  cs = Composition_series.new(ss, ss.last)
  cs.each do |x|; puts x.collect{|x| x.inspect}.join(" -> "); end
end
