/* 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.
*/

#ident "$Project: polymake $$Id: lawler.cc,v 1.10 2004/10/26 18:57:01 gawrilow Exp $"

#include <Poly.h>
#include <Array.h>
#include <PowerSet.h>
#include <FacetList.h>
#ifdef POLY_TIMING
#  include <stopwatch.h>
#endif

namespace polymake {
namespace topaz {
  
void lawler(Poly& p)
{
   const Array< Set<int> > F = p.give("FACETS");
   int n_vertices;
   p.give("N_VERTICES") >> n_vertices;
   
#ifdef POLY_TIMING
   stopwatch timer;
   timer.start();
#endif
   sequence Vertices(0,n_vertices);
   
   // C stores the candidates for minimal non-faces
   // insert every single element in the complement of the first facet
   
   FacetList C=all_subsets_of_1(Vertices-F[0]);
   
   // incrementally insert the facets into the complex (skip the first facet)
   Entire< Array< Set<int> > >::const_iterator f=entire(F);
   for (++f; !f.at_end(); ++f) {
      Set<int> V = Vertices-*f;    // compute the complement of the current facet
      FacetList Cnew(n_vertices); 
      // for every element in C
      for (Entire<FacetList>::const_iterator cit=entire(C); !cit.at_end(); ++cit) {
	 
	 // and every vertex in the complement of the facet
	 for (Entire< Set<int> >::const_iterator vit=entire(V); !vit.at_end(); ++vit)
	    Cnew.replaceMin(*cit + *vit);
      }
      C = Cnew;
   }
   
#ifdef POLY_TIMING
   timer.stop();
   cout << "facets:         " << F.size() << endl;
   cout << "min. non-faces: " << C.size() << endl;
   cout << "time:           " << timer << " seconds" << endl;
#endif
   
   p.take("MINIMAL_NON_FACES") << C;
}
   
} }

using namespace polymake;

/** @file lawler
 *
 *  Produces the minimal (with respect to inclusion) non-faces of a simplicial complex.
 *  Only the facets of the complex are used as input, i.e., no hasse diagram is computed.
 *
 *  This implements an old algorithm described by Lawler:
 *  "Covering problems: duality relations and a new method of solution"
 *  SIAM J. Appl. Math., Vol. 14, No. 5, 1966
 *
 *  See also Chapter 2 of "Hypergraphs", C. Berge, North-Holland, Amsterdam, 1989
 *
 *  8/23/2002
 *
 *  @synopsis  lawler <file>
 *
 *  @reading  FACETS, N_VERTICES
 *  @writing  MINIMAL_NON_FACES
 */


int main(int argc, const char* argv[])
{
   if (argc!=2) {
      cerr << "usage: " << argv[0] << " <file>" << endl;
      return 1;
   }

   try {
      Poly p(argv[1], ios::in | ios::out);
      topaz::lawler(p);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}


// Local Variables:
// mode:C++
// c-basic-offset:3
// End:
