#  Copyright (c) 1997-2004
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
#  http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.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: check_iso,v 1.7 2004/12/10 15:47:24 gawrilow Exp $


# the "big" executable from the nauty package
custom $dreadnaut;

CONFIGURE {
   $dreadnaut=find_via_path(qw( dreadnautB dreadnautx dreadnaut ))
   or die "none of the package nauty programs (dreadnaut*) found\n";
}

sub graph2nauty($$;$) {
   my ($pipe, $graph, $offset)=@_;
   $offset=0 unless defined $offset;
   my $n_nodes= @$graph + $offset;
   print $pipe <<".";
n=$n_nodes
g
.
   my $i=$offset;  my $sep="";
   foreach (@$graph) {
      /\{(.*)\}\s*/;
      print $pipe "$sep$i: $1";
      ++$i;
      $sep ||= ";\n";
   }
   print $pipe ".\n";
   if ($offset) {
      print $pipe "f=[0:", $offset-1, "]\n";
   }
}

sub nauty_result($) {
   my $pipe=shift;
   my $result;
   while (<$pipe>) {
      print if $Switches::v;
      if (/h and h' (?:are (\w+)|have different)/) {
         $result= $1 eq "identical";
      }
   }
   return $result+0;
}

# category: Comparing
# args: Graph1, Graph2
# Check the isomorphism of two graph properties using the @c dreadnaut program from the @see external.nauty package.
# If you need to see all the details reported by @c dreadnaut, increase the verbosity level.

user_function check_iso ($$;$$) {
   my ($graph1, $graph2, $offset1, $offset2)=@_;
   my $dr=new Poly::ProgramPipe($dreadnaut);
   graph2nauty($dr, $graph1, $offset1);
   print $dr "c x \@\n";
   graph2nauty($dr, $graph2, $offset2);
   print $dr "x ##\n";
   return nauty_result($dr);
}


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