/* 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: odd_complex.cc,v 1.21 2005/11/14 14:52:12 gawrilow Exp $"

#include <Poly.h>
#include <bipartite.h>
#include <complex_tools.h>

#include <ext/hash_map>

namespace polymake { namespace topaz {

void odd_complex(Poly& p)
{
   const Array< Set<int> > C = p.give("FACETS");
   const bool is_pure = p.give("PURE");
   if (!is_pure)
      throw std::runtime_error("odd_complex: Complex is not PURE.");
   HasseDiagram<> HD;
   if (!(p.lookup("HASSE_DIAGRAM") >> HD))
      HD = hasse_diagram(C,C[0].size()-1,-2);
   
   if (C[0].size()-1 < 2)
      throw std::runtime_error("odd_complex: DIM of complex must be greater 2.");
   
   bool output = false;
   std::list< Set<int> > odd_complex;
   for (Entire<sequence>::iterator f=entire(HD.nodes_of_dim(-3));
	!f.at_end(); ++f) {
      
      Set<int> star_facets;
      for (Entire< HasseDiagram<>::out_neighbor_list >::const_iterator n=entire(HD.out_neighbors(*f));
	   !n.at_end(); ++n) {
	 for (Entire< HasseDiagram<>::out_neighbor_list >::const_iterator nn=entire(HD.out_neighbors(*n));
	      !nn.at_end(); ++nn)
	    star_facets += *nn;
      }
      
      std::list< Set<int> > Link;
      Set<int> V_of_Link;
      for (Entire< Set<int> >::iterator s_it=entire(star_facets);
	   !s_it.at_end(); ++s_it) {
	 const Set<int> l = HD.node(*s_it)-HD.node(*f);
	 Link.push_back(l);
	 V_of_Link += l;
      }
      
      // create hash map for the vertices of Link
      std_ext::hash_map<int, int> vertex_map(V_of_Link.size());
      int count=0;
      for (Entire< Set<int> >::iterator s_it=entire(V_of_Link);
	   !s_it.at_end(); ++s_it, ++count)
	 vertex_map[*s_it] = count;
      
      Graph<> G(V_of_Link.size());
      for (Entire< std::list< Set<int> > >::iterator l_it=entire(Link);
	   !l_it.at_end(); ++l_it)
	 G.edge( vertex_map[ l_it->front() ], vertex_map[ l_it->back() ] );
      
      if (!graph::is_bipartite(G)) {
	 output = true;
	 odd_complex.push_back(HD.node(*f));
      }
   }
   
   if (output)
      p.take("ODD_SUBCOMPLEX") << as_array(odd_complex);
   else
      p.take("ODD_SUBCOMPLEX") << Set<int>();
}
   
} }

using namespace polymake;

/** @file odd_complex
 *
 *  Find all faces of co-dimension -2 which have a non bipartied link.
 *
 *  @synopsis odd_complex <file>
 */

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::odd_complex(p);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}


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