/* Copyright (c) 1997-2005
   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: induced_subgraph.cc,v 1.3 2005/11/11 10:14:36 gawrilow Exp $"

#include <Poly.h>
#include <Graph.h>
#include <IndexedSubgraph.h>
#include <argv_options.h>

namespace {
  enum { nol };
}

namespace polymake { namespace graph {

void induced_subgraph(Poly& p, const char *graph_section, const char *subgraph_nodes_section, const char *subgraph_section, const argv_option* options)
{
   Graph<int> G;
   p.give(graph_section) >> ignore_node_attrs(G);
   const Set<int> sub_nodes = p.give(subgraph_nodes_section);
   const int n_nodes = G.nodes();

   // label nodes
   if (!options[nol])
      std::copy(entire(sequence(0,n_nodes)), nodes(G).begin());

   if (options[nol])
      p.take(subgraph_section) << ignore_node_attrs(induced_subgraph(G,sub_nodes));
   else
      p.take(subgraph_section) << induced_subgraph(G,sub_nodes);
}

} }

using namespace polymake;

/** @file induced_subgraph
 *
 *  Compute the subgraph induced by a given set of nodes.
 *
 *  @synopsis induced_subgraph <file> <graph_section> <subgraph_nodes_section> <subgraph_section> [ -nol ]
 *
 *  @reading  <graph_section> <subgraph_nodes_section>
 *  @writing  <subgraph_section>
 *
 *  @author Niko Witte <witte@math.tu-berlin.de>
 */

int main(int argc, const char *argv[]) {
   argv_option options[]={ { "-nol" } };
   if (!extract_options(argc,argv,1,options) || argc != 5) {
      cerr << "usage: " << argv[0] << " <file> <graph_section> <subgraph_nodes_section> <subgraph_section> [ -nol ]" << endl;
      return 1;
   }
   try { 
      Poly p(argv[1], ios::in | ios::out);
      graph::induced_subgraph(p, argv[2], argv[3], argv[4], options);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
