https://hal.archives-ouvertes.fr/hal-03445891
Raw File
sample_example.cpp
/***************************************************************************
 *   Copyright (C) 2007 by Sid Touati   *
 *   Sid-nospamDOTTouati-nospam@inria.fr   *
 *   Copyright INRIA and University of Versailles (France)                 *
 *   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 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   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.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

/*! \file sample_example.cpp
\brief This file contain a C++ example (main application) to show how to use Dilworth decomposition.
Details.
*/
#include "dilworthdecomposition.h"
#include "LEDA/core/string.h"
#include "LEDA/core/h_array.h"

int main(int argc, char *argv[])
{
	graph G;
	LEDA::string filename;
	set<node> MA; //maximal antichain
	node_array<int> C; //indices of chains
	node_list nl;
	h_array<int,node_list*> chain(nil);
	int i,status;
	int size_ma, // size of a maximal antichain
		size_mc; // size of a minimal chain decomposition
	node u;
	if(argc!=2){
		cerr << argv[0] << ": Dilworth decomposition." << endl;
		cerr << "Usage:"<<argv[0] << " graph_filename" << endl;
		cerr << "The filename extension should be .gw for a graph in leda/gw format, or in .gml for a graph in GML format."<< endl;
		return EXIT_FAILURE;
	}

	filename=LEDA::string(argv[1]);
	if (filename.contains(LEDA::string(".gw"))) {
		cout<<"Reading GW" <<filename<<endl;
		status=G.read(filename);
	}
	else if (filename.contains(LEDA::string(".gml"))) {
		cout<<"Reading GML "<< filename<<endl;
		status=G.read_gml(filename);
	}
	else {
		cerr << "Usage:"<<argv[0] << " graph_filename" << endl;
		cerr << "The filename extension should be .gw for a graph in leda/gw format, or in .gml for a graph in GML format."<< endl;
		return EXIT_FAILURE;
	}

	switch(status){
	case 0: 
	case 2: break;
	case 1: cerr<< filename << " does not exist."<<endl; break;
	case 3: cerr<<filename <<" does not contain a graph"<<endl; break;
	default: return EXIT_FAILURE;
	}
	
	size_mc=MINIMAL_CHAIN(G, C);
	cout<<"Minimal Chain Decomposition"<<endl;
	cout<<"---------------------------"<<endl;
	cout<<"There are "<<size_mc<<" chains"<<endl;
	forall_nodes(u,G){
		if ((chain[C[u]])==nil){
			chain[C[u]]=new node_list;
		}
		(chain[C[u]])->append(u);
	}
	for(i=0;i<size_mc;i++){
		cout<<"chain "<<i<<": ";
		forall(u, *chain[i]){
			G.print_node(u);
		}
		cout<<endl;
	}
	
	
	size_ma=MAXIMAL_ANTI_CHAIN(G, MA);
	cout<<"Maximal Antichain"<<endl;
	cout<<"---------------------------"<<endl;
	cout<<"Size of this maximal anctichain : "<<size_ma<<" nodes"<<endl;
	cout<<"Here are all these nodes:"<<endl;
	i=0;
	forall(u, MA){
		cout<<"node "<<i<<": ";
		G.print_node(u);
		cout<<endl;
		i++;
	}
  	return EXIT_SUCCESS;
}
back to top