/* 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: rigid_components_of_patterns.cc,v 1.1 2005/11/10 10:29:11 thilosch Exp $"

#include <Poly.h>
#include <Vector.h>
#include <Matrix.h>
#include <SparseMatrix.h>
#include <SparseVector.h>
#include <Rational.h>
#include <Graph.h>
#include <linalg.h>
#include <PowerSet.h>
#include <Array.h>
#include <max_cliques.h>

namespace polymake { namespace polytope {
   namespace {
      typedef Graph< Vector< Rational > > Framework;
   }

   void rigid_components_of_patterns(Poly& p, const char* rigid_components_section, const char* pattern_section) {
      const Framework F = p.give("FRAMEWORK");
      const int n_nodes = F.nodes();
      const Matrix<Rational> P = p.give(pattern_section);
      Array< PowerSet< int > > RC(P.rows());
      int i = 0;
      const sequence points(0,n_nodes);

      for(Entire< Rows< Matrix< Rational > > >::const_iterator ri = entire(rows(P)); !ri.at_end(); ++ri,++i) {
	 Entire< Subsets_of_k< sequence > >::const_iterator ei = entire(all_subsets_of_k(points,2)); 
	 Graph<> G(n_nodes);
	 for(Entire< Vector< Rational > >::const_iterator ni = entire(*ri); !ni.at_end(); ++ni, ++ei) {
	    if(*ni == 0) {
	       G.edge((*ei).front(), (*ei).back());
	    }
	 }
	 RC[i] = polymake::graph::max_cliques(G);
      }
      p.take(rigid_components_section) << RC;
   }
} }

using namespace polymake;

/** @file rigid_components_of_patterns
 *
 *  Calculate the rigid components of the graphs induced by the given patterns. 
 *
 *  @synopsis rigid_components_of_patterns <file> <rigid_components_section> <pattern_section>
 *
 *  @reading  FRAMEWORK, <pattern_section>
 *  @writing  <rigid_components_section>
 */

int main(int argc, const char *argv[]) {
  if (argc!=4) {
    cerr << "usage: " << argv[0] << " <file> <rigid_components_section> <pattern_section>" << endl;
    return 1;
  }
  try {
     Poly p(argv[1], ios::in | ios::out);

     polytope::rigid_components_of_patterns(p,argv[2],argv[3]);
  }
  catch (const std::exception& e) {
    cerr << e.what() << endl;
    return 1;
  }
  return 0;
}
