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

#include <vector>
#include <list>
#include <diameter.h>
#include <iterators.h>

namespace polymake { namespace graph {

template <typename Graph>
int diameter(const Graph& G)
{
   const int nodes=G.nodes();
   int diam=0;
   std::vector<int> dist(nodes);		// distances from "root" node
   std::list<int> node_queue(nodes);

   for (int r=0; r<nodes; ++r) {
      std::fill(dist.begin(),dist.end(),-1);	// marking all nodes as not visited
      dist[r]=0;

      node_queue.clear();
      node_queue.push_back(r);

      while (!node_queue.empty()) {
	 const int n=node_queue.front();
	 node_queue.pop_front();
	 int d=dist[n];
	 if (d>diam) diam=d;
	 ++d;
	 for (typename Entire<typename Graph::out_edge_list>::const_iterator e=G.out_edges(n).begin();
	      !e.at_end(); ++e) {
	    const int nn=e.to_node();
	    if (dist[nn]<0) {
	       node_queue.push_back(nn);
	       dist[nn]=d;
	    }
	 }
      }
   }
   return diam;
}

} }

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