/* 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: t_graph.tcc 4714 2004-06-22 16:23:15Z gawrilow $"

#include <t_graph.h>
#include <complex_tools.h>

namespace polymake { namespace topaz {

typedef Graph<> graph;

// Computes the Graph of a simplicial complex.
template <typename Complex> inline
graph t_graph(const Complex& C) {
   const PowerSet<int> OSK=k_skeleton(C,1);
   const Set<int> V = accumulate(OSK, operations::add());   

   graph G(V.size());
   for (Entire< PowerSet<int> >::const_iterator c_it=entire(OSK);
	!c_it.at_end(); ++c_it)
      if (c_it->size() == 2)  // is edge
	 G.edge(c_it->front(), c_it->back());
   
   return G;
}

template <typename Complex> inline
Graph<nothing,double> t_mixed_graph(const Complex& C, const graph& PG, const graph& DG, const double weight) {
   Graph<nothing,double> MG;
   MG.resize(PG.nodes() + DG.nodes());

   // add primal edges
   for (Entire< graph::edge_container >::const_iterator e=entire(edges(PG));
	!e.at_end(); ++e)
      MG.create_edge(e.from_node(), e.to_node(),1.0);

   // add dual edges
   const int diff = PG.nodes();
   for (Entire< graph::edge_container >::const_iterator e=entire(edges(DG));
	!e.at_end(); ++e)
      MG.create_edge(e.from_node()+diff, e.to_node()+diff,1.0);

   // add mixed edges
   typedef typename Complex::value_type Facet;
   int c=diff;

   for (typename Entire<Complex>::const_iterator c_it=entire(C);
	!c_it.at_end(); ++c_it, ++c) {
      Facet f=*c_it; 
      for (typename Entire<Facet>::iterator v=entire(f);
	   !v.at_end(); ++v)
	 MG.create_edge(c,*v,weight);
   }
   
   return MG;
}

} }


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