/* 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: triangle_free.cc,v 1.6 2004/06/22 16:22:36 gawrilow Exp $"

#include <Poly.h>
#include <Graph.h>
#include <IncidenceMatrix.h>

namespace polymake { namespace graph {

void triangle_free(Poly& p, const char* graph_section, const char* result_section)
{
   const Graph<nothing,nothing,directed> G=p.give(graph_section);
   const int n=G.nodes();
   const IncidenceMatrix<> Adj=convolute(convolute(neighborhood_matrix(G), T(neighborhood_matrix(G))),
					 T(neighborhood_matrix(G)));

   // true iff there is no (directed) circle of length 3
   bool answer=true;
   for (int i=0; i<n; ++i)
      if (Adj(i,i)) {
	 answer=false;
	 break;
      }

   p.take(result_section) << answer;
}

} }

using namespace polymake;

/** @file triangle_free
 *
 *  Determine whether a (possibly directed) graph has triangles or not.
 *
 *  @synopsis triangle_free <file> <graph_section> <triangle_free_section>
 *
 *  @reading  <graph_section>
 *  @writing  <triangle_free_section>
 */

int main(int argc, const char *argv[]) {
   if (argc!=4) {
      cerr << "usage: " << argv[0] << " <file> <graph_section> <triangle_free_section>\n";
      return 1;
   }
   try {
      Poly p(argv[1], ios::in | ios::out);
      graph::triangle_free(p, argv[2], argv[3]);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
