Here is my new stl-like implementation of an unweighted graph. Could you please tell me what member functions should I include to my library? Thanks in advance.
file: unweighted_graph.hpp
#include <algorithm>
#include <stdexcept>
#include <vector>
#include <map>
#include <set>
using namespace std;
#ifndef __UNWEIGHTED_GRAPH_HPP
#define __UNWEIGHTED_GRAPH_HPP
namespace lemon {
template< class T, bool dir >
class unw_graph {
private:
map< T, set< T > > adj;
int vcnt;
int ecnt;
public:
typedef typename map< T, set< T > >::iterator vertex_iterator;
typedef typename map< T, set< T > >::const_iterator vertex_const_iterator;
typedef typename set< T >::iterator link_iterator;
typedef typename set< T >::const_iterator link_const_iterator;
typedef pair< T, T > link_type;
typedef size_t size_type;
vertex_iterator begin() { return adj.begin(); }
vertex_const_iterator begin() const { return adj.begin(); }
vertex_iterator end() { return adj.end(); }
vertex_const_iterator end() const { return adj.end(); }
unw_graph( ) : adj( map< T, set< T > >() ), vcnt( 0 ), ecnt( 0 ) {}
void insert_vertex( const T& init ) {
if( adj.find( init )!=adj.end() ) return;
else {
adj.insert( make_pair( init, set< T >() ) );
vcnt++;
}
}
void insert_link( const pair< T, T > &e ) {
insert_vertex( e.first );
insert_vertex( e.second );
if( adj[ e.first ].find( e.second ) == adj[ e.first ].end() ) {
adj[ e.first ].insert( e.second );
ecnt++;
}
if( !dir ) {
if( adj[ e.second ].find( e.first ) == adj[ e.second ].end() ) {
adj[ e.second ].insert( e.first );
}
}
}
unw_graph( const vector< pair< T, T > > &edge_list ) : adj( map< T, set< T > >() ), vcnt( 0 ), ecnt( 0 ) {
typename vector< pair< T, T > >::iterator it;
for( it=edge_list.begin(); it!=edge_list.end(); ++it ) {
insert_link( *it );
}
}
void erase_link( const pair< T, T > &e ) {
if( adj.find( e.first ) == adj.end() || adj.find( e.second ) == adj.end() ) return;
typename set< T >::iterator it=adj[ e.first ].find( e.second );
if( it!=adj[ e.first ].end() ) {
adj[ e.first ].erase( it );
ecnt--;
}
if( !dir ) {
it=adj[ e.second ].find( e.first );
if( it!=adj[ e.second ].end() ) {
adj[ e.second ].erase( it );
}
}
}
typename map< T, set< T > >::iterator find_vertex( const T& vr ) {
return adj.find( vr );
}
typename map< T, set< T > >::const_iterator find_vertex( const T& vr ) const {
return adj.find( vr );
}
bool connection( const pair< T, T > &e ) {
if( adj.find( e.first ) == adj.end() || adj.find( e.second ) == adj.end() ) return false;
else return adj[ e.first ].find( e.second ) != adj[ e.first ].end();
}
set< T >& operator[] ( T idx ) {
return adj[ idx ];
}
const set< T >& operator[] ( T idx ) const {
return adj[ idx ];
}
size_t vertices() const { return vcnt; }
size_t links() const { return ecnt; }
bool directed() const { return dir; }
};
}
#endif
file: test1.cpp
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include "unweighted_graph.hpp"
using namespace std;
int main() {
freopen( "file.in", "r", stdin );
lemon::unw_graph< string, false > g;
int Ec;
cin >> Ec;
while( Ec-- ) {
string a,b;
cin >> a >> b;
g.insert_link( make_pair( a, b ) );
}
cout << g.vertices() << " " << g.links() << endl;
lemon::unw_graph< string, false >::vertex_iterator i;
for( i=g.begin(); i!=g.end(); ++i ) {
cout << i->first << ": ";
lemon::unw_graph< string, false >::link_iterator j;
for( j=i->second.begin(); j!=i->second.end(); ++j ) {
cout << *j << " ";
}
cout << endl;
}
g.erase_link( make_pair( "athens", "lamia" ) );
cout << g.vertices() << " " << g.links() << endl;
for( i=g.begin(); i!=g.end(); ++i ) {
cout << i->first << ": ";
lemon::unw_graph< string, false >::link_iterator j;
for( j=i->second.begin(); j!=i->second.end(); ++j ) {
cout << *j << " ";
}
cout << endl;
}
return 0;
}
file: test2.cpp
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include "unweighted_graph.hpp"
using namespace std;
int main() {
freopen( "file2.in", "r", stdin );
lemon::unw_graph< int, true > g;
int N,M;
cin >> N >> M;
for( int i=0; i<N; ++i ) {
g.insert_vertex( i+1 );
}
while( M-- ) {
int from,to;
cin >> from >> to;
g.insert_link( make_pair( from, to ) );
}
for( int i=1; i<=N; ++i ) {
cout << i << ": ";
lemon::unw_graph< int, false >::link_iterator j;
for( j=g[ i ].begin(); j!=g[ i ].end(); ++j ) {
cout << *j << " ";
}
cout << endl;
}
return 0;
}