/* 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: is_locally_strongly_connected.cc,v 1.5 2004/10/26 18:57:01 gawrilow Exp $"

#include <Poly.h>
#include <complex_tools.h>
#include <t_graph.h>
#include <connected.h>
#include <argv_options.h>

#include <string>

namespace {
   enum { all, quiet};
}

namespace polymake {
namespace topaz {
  
void is_locally_strongly_connected(std::ostream& os, Poly& p, const argv_option *options)
{
   const HasseDiagram<> HD = p.give("HASSE_DIAGRAM");
   const int dim = HD.dim()-1;
   
   bool loc_str_conn = true;
   
   for (int d=0; d<dim && (loc_str_conn || options[all]); ++d)
      for (Entire<sequence>::const_iterator f=entire(HD.nodes_of_dim(d));
	   !f.at_end(); ++f) {
	 const FacetList& star = star_in_HD(HD,*f);
	 
	 if ( !polymake::graph::is_connected(t_dual_graph(star)) ) {
	    if (!options[quiet])
	       wrap(os) << "is_locally_strongly_connected: star(" << HD.node(*f)
			<< ") is not strongly connected."<< endl;
	    loc_str_conn = false;
	    if (!options[all])
	       break;
	 }
      }
   
   p.take("LOCALLY_STRONGLY_CONNECTED") << loc_str_conn;
}
   
} }

using namespace polymake;

/** @file is_locally_strongly_connected
 *
 *  Check whether a complex is locally strongly connected.
 *
 *  The @c -all flag tells the client to compute all faces with
 *  non strongly connected star.
 *
 *  @synopsis is_manifold <file> [ -all -quiet ]
 *
 *  @reading  HASSE_DIAGRAM
 *  @writing  LOCALLY_STRONGLY_CONNECTED
 */

int main(int argc, const char* argv[])
{
   argv_option options[]={ { "-all" }, { "-quiet" } };
   if ( !extract_options(argc,argv,1,options) || argc!=2 ) {
      cerr << "usage: " << argv[0] << " <file> [ -all -quiet ]" << endl;
      return 1;
   }
   try {
      Poly p(argv[1], ios::in | ios::out);
      topaz::is_locally_strongly_connected(std::cout, p, options);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}


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