#  Copyright (c) 1997-2009
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
#  http://www.polymake.de
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#-------------------------------------------------------------------------------
#  $Project: polymake $$Id: grow_complex 9298 2009-09-02 23:10:56Z gawrilow $

# Start with the complete graph on $n vertices and randomly insert $d-faces until the ($d-1)-homology is trivial.
# Output integer homology for each intermediate complex.

die "usage: polymake --script topaz::grow_complex n d\n" unless @ARGV==2;
random_growth(@ARGV);

sub randomize(@) {
   my @set=@_;
   my $n=@set;
   my @list=();
   for (my $i=$n; $i>0; --$i) {
      my $k=int(rand($i));
      push @list, $set[$k];
      splice @set, $k, 1;
   }
   return @list;
}

sub derefify {
   map { "{@$_}\n" } @_;
}

sub random_growth {
  my ($n,$d)=@_;
  my @set=(0..$n-1);
  my @complete_graph=derefify(all_subsets_of_k(2,@set));
  my @triangles=derefify(randomize(all_subsets_of_k($d,@set)));

  my $k=0;
  while (1) {
    my @faces=(@complete_graph,@triangles[0..$k]);
    my $complex=new SimplicialComplex("simplicial_complex", INPUT_FACES=>\@faces);
    print "$k\n", @{$complex->HOMOLOGY}, "\n";
    ++$k;
    last if $complex->HOMOLOGY->[$d-2] eq "({} 0)\n";
  }
  print "time to complete: ", $k/scalar(@triangles), "\n";
}

sub coupon_collector($$) {
  my ($n,$d)=@_;
  my @set=(0..$n-1);
  my @complete_graph=derefify(all_subsets_of_k(2,@set));
  my @triangles=derefify(all_subsets_of_k($d,@set));
  my @faces=@complete_graph;

  my $k=0;
  while (1) {
    my $i=int(rand(scalar(@triangles)));
    push @faces, $triangles[$i];
    my $complex=new SimplicialComplex("simplicial_complex", INPUT_FACES=>\@faces);
    print "$k\n", @{$complex->HOMOLOGY}, "\n";
    ++$k;
    last if $complex->HOMOLOGY->[$d-2] eq "({} 0)\n";
  }
  print "time to complete: ", $k/scalar(@triangles), "\n";
}

1

# Local Variables:
# mode: perl
# c-basic-offset:3
# End:
 

