/* Copyright (c) 1997-2006
   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: edge_colored_bounded_graph.cc,v 1.1 2006/01/17 19:02:23 joswig Exp $"

#include <Poly.h>
#include <Array.h>
#include <HasseDiagram.h>

namespace polymake { namespace polytope {

void edge_colored_bounded_graph(Poly& p, const char* edge_colored_bounded_graph_section,
    			                 const char* hasse_diagram_section,
				         const char* far_face_section, const int upper_bound)
{
   const HasseDiagram<> HD=p.give(hasse_diagram_section);
   const int n=HD.nodes();
   Array<int> highest_rank_seen(n,-1);
   const Set<int> far_face=p.give(far_face_section);

   for(int k= upper_bound>=0? upper_bound : HD.dim()-1; k>=2; --k)
      for (Entire<sequence>::const_iterator it=entire(HD.nodes_of_dim(k)); !it.at_end(); ++it) {
	 const int this_idx=*it;
	 const Set<int>& this_face = HD.node(this_idx);
	 if ((this_face*far_face).empty()) {
	    int rank=k;
	    for (Entire<HasseDiagram<>::out_edge_list>::const_iterator e=entire(HD.out_edges(this_idx)); !e.at_end(); ++e)
	       rank=std::max(rank,highest_rank_seen[e.to_node()]);
	    highest_rank_seen[this_idx]=rank;
	 }
      }
      
   Set<int> Vertices=HD.node(HD.top_node()); // must be subsequently numbered starting from 0; otherwise we are in trouble
   const int m=Vertices.size();
   Graph<int,int> EdgeColoredBoundedGraph(m);
   for (int k=0; k<m; ++k)
      EdgeColoredBoundedGraph.node(k)=k;

   for (Entire<sequence>::const_iterator it=entire(HD.nodes_of_dim(1)); !it.at_end(); ++it) {
      const int this_idx=*it;
      const Set<int>& this_edge = HD.node(this_idx);
      if ((this_edge*far_face).empty()) {
	 int rank=1;
	 for (Entire<HasseDiagram<>::out_edge_list>::const_iterator e=entire(HD.out_edges(this_idx)); !e.at_end(); ++e)
	    rank=std::max(rank,highest_rank_seen[e.to_node()]);
	 EdgeColoredBoundedGraph.edge(this_edge.front(),this_edge.back())=rank;
      }
   }
   
   EdgeColoredBoundedGraph.delete_nodes(far_face);
   p.take(edge_colored_bounded_graph_section) << EdgeColoredBoundedGraph;
}

} } 

using namespace polymake;

/** @file edge_colored_bounded_graph
 *
 *  Compute the bounded graph of an unbounded polyhedron and assign to each edge the maximal dimension of a face containing it.
 *  In the case of tight spans there are a priori upper bounds due to Mike Develin.
 *
 *  @synopsis edge_colored_bounded_graph <file> <edge_colored_bounded_graph_section> <hasse_diagram_section> <far_face_section> [<upper_bound>]
 *
 *  @reading  <hasse_diagram_section> <far_face_section>
 *  @writing  <edge_colored_bounded_graph_section>
 */
int main(int argc, const char *argv[]) {
   if (argc < 5 || argc > 6) {
      cerr << "usage: " << argv[0] << " <file> <edge_colored_bounded_graph_section> <hasse_diagram_section> <far_face_section> [<upper_bound>]" << endl;
      return 1;
   }
   try {
      Poly p(argv[1], ios::in | ios::out);
      const int upper_bound= argc<6? -1 : atoi(argv[5]);
      polytope::edge_colored_bounded_graph(p,argv[2],argv[3],argv[4],upper_bound);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
