My Program is working fine but problem is this it is too lengthy , i am creating Graph object again and again in each function the i am using it .Can I make one Global function in which i can assign vertices and edges to Graph object and can use it in other functions ,so that my code gets reduced .If yes please help me giving me one example for this i will really thankful to you .
#include <boost/config.hpp>
#include <iostream>
#include <vector>
#include <utility>
#include <string>
#include <boost/graph/adjacency_list. hpp>
#include <boost/graph/graph_utility.h pp>
#include <boost/property_map.hp p>
#include "ed.h"
#include "ve.h"
using namespace boost;
using namespace std;
class Molecule
{
map<int,string> m;
public:
void set_molecule_pr operty(vector<s tring>& s4)
{
for (unsigned int i = 1; i <= s4.size(); i++)
{
m[i]=s4[i-1];
}
}
typedef adjacency_list< boost::vecS, boost::listS, boost::undirect edS, ve::VertexPrope rties, ed::EdgePropert ies> Graph;
int number_of_verti ces()
{
return m.size();
}
void create_chemical _Network(vector <int> s1,vector<int> s2,vector<int> s3)
{
typedef adjacency_list< boost::vecS, boost::listS, boost::undirect edS, ve::VertexPrope rties, ed::EdgePropert ies> Graph;
const int V = m.size()+1;
Graph g(V);
property_map<Gr aph, std::size_t ve::VertexPrope rties::*>::type
id = get(&ve::Vertex Properties::ind ex, g);
property_map<Gr aph, std::string ed::EdgePropert ies::*>::type
name = get(&ed::EdgePr operties::name, g);
boost::graph_tr aits<Graph>::ve rtex_iterator vi, viend;
int vnum = 0;
for (boost::tie(vi, viend) = vertices(g); vi != viend; ++vi)
id[*vi] = vnum++;
for (int i = 0; i <50 ; i++)
{
if(s3[i]==1){
add_edge(vertex (s1[i], g), vertex(s2[i], g), ed::EdgePropert ies("single bond"), g);
}
if(s3[i]==2){
add_edge(vertex (s1[i], g), vertex(s2[i], g), ed::EdgePropert ies("double bond"), g);
}
if(s3[i]==3){
add_edge(vertex (s1[i], g), vertex(s2[i], g), ed::EdgePropert ies("triple_bon d"), g);
}
}
graph_traits<Gr aph>::vertex_it erator i, end;
graph_traits<Gr aph>::out_edge_ iterator ei, edge_end;
for (boost::tie(i,e nd) = vertices(g); i != end; ++i)
{
if(id[*i]!=0)
{
cout << id[*i]<<"("<<m[id[*i]]<<")" << " ";
for (boost::tie(ei, edge_end) = out_edges(*i, g); ei != edge_end; ++ei)
cout << " --" << name[*ei] << "--> " << m[id[target(*ei, g)]] << " ";
cout << endl;
}
}
}
void Get_interaction (vector<int> s1,vector<int> s2,vector<int> s3)
{
typedef adjacency_list< boost::vecS, boost::listS, boost::undirect edS, ve::VertexPrope rties, ed::EdgePropert ies> Graph;
const int V = m.size()+1;
Graph g(V);
property_map<Gr aph, std::size_t ve::VertexPrope rties::*>::type
id = get(&ve::Vertex Properties::ind ex, g);
property_map<Gr aph, std::string ed::EdgePropert ies::*>::type
name = get(&ed::EdgePr operties::name, g);
boost::graph_tr aits<Graph>::ve rtex_iterator vi, viend;
int vnum = 0;
for (boost::tie(vi, viend) = vertices(g); vi != viend; ++vi)
id[*vi] = vnum++;
for (int i = 0; i <50 ; i++)
{
if(s3[i]==1){
add_edge(vertex (s1[i], g), vertex(s2[i], g), ed::EdgePropert ies("single bond"), g);
}
if(s3[i]==2){
add_edge(vertex (s1[i], g), vertex(s2[i], g), ed::EdgePropert ies("double bond"), g);
}
if(s3[i]==3){
add_edge(vertex (s1[i], g), vertex(s2[i], g), ed::EdgePropert ies("triple_bon d"), g);
}
}
print_edges(g, id);
}
void print_verices(v ector<int> s1,vector<int> s2,vector<int> s3)
{
typedef adjacency_list< boost::vecS, boost::listS, boost::undirect edS, ve::VertexPrope rties, ed::EdgePropert ies> Graph;
const int V = m.size()+1;
Graph g(V);
property_map<Gr aph, std::size_t ve::VertexPrope rties::*>::type
id = get(&ve::Vertex Properties::ind ex, g);
property_map<Gr aph, std::string ed::EdgePropert ies::*>::type
name = get(&ed::EdgePr operties::name, g);
boost::graph_tr aits<Graph>::ve rtex_iterator vi, viend;
int vnum = 0;
for (boost::tie(vi, viend) = vertices(g); vi != viend; ++vi)
id[*vi] = vnum++;
for (int i = 0; i <50 ; i++)
{
if(s3[i]==1){
add_edge(vertex (s1[i], g), vertex(s2[i], g), ed::EdgePropert ies("single bond"), g);
}
if(s3[i]==2){
add_edge(vertex (s1[i], g), vertex(s2[i], g), ed::EdgePropert ies("double bond"), g);
}
if(s3[i]==3){
add_edge(vertex (s1[i], g), vertex(s2[i], g), ed::EdgePropert ies("triple_bon d"), g);
}
}
graph_traits<Gr aph>::vertex_it erator i, end;
graph_traits<Gr aph>::out_edge_ iterator ei, edge_end;
for (boost::tie(i,e nd) = vertices(g); i != end; ++i)
{
if(id[*i]!=0)
{
cout << id[*i];
cout << endl;
}
}
}
void Get_Neighbours( vector<int> s1,vector<int> s2,vector<int> s3,int atom)
{
typedef adjacency_list< boost::vecS, boost::listS, boost::undirect edS, ve::VertexPrope rties, ed::EdgePropert ies> Graph;
const int V = m.size()+1;
Graph g(V);
property_map<Gr aph, std::size_t ve::VertexPrope rties::*>::type
id = get(&ve::Vertex Properties::ind ex, g);
property_map<Gr aph, std::string ed::EdgePropert ies::*>::type
name = get(&ed::EdgePr operties::name, g);
boost::graph_tr aits<Graph>::ve rtex_iterator vi, viend;
int vnum = 0;
for (boost::tie(vi, viend) = vertices(g); vi != viend; ++vi)
id[*vi] = vnum++;
for (int i = 0; i <50 ; i++)
{
if(s3[i]==1){
add_edge(vertex (s1[i], g), vertex(s2[i], g), ed::EdgePropert ies("single bond"), g);
}
if(s3[i]==2){
add_edge(vertex (s1[i], g), vertex(s2[i], g), ed::EdgePropert ies("double bond"), g);
}
if(s3[i]==3){
add_edge(vertex (s1[i], g), vertex(s2[i], g), ed::EdgePropert ies("triple_bon d"), g);
}
}
graph_traits<Gr aph>::vertex_it erator i, end;
graph_traits<Gr aph>::out_edge_ iterator ei, edge_end;
for (boost::tie(i,e nd) = vertices(g); i != end; ++i)
{
if(id[*i]==atom)
{
for (boost::tie(ei, edge_end) = out_edges(*i, g); ei != edge_end; ++ei)
cout << id[target(*ei, g)]<< " ";
cout << endl;
}
}
}
void Get_bond(vector <int> s1,vector<int> s2,vector<int> s3,int a ,int b)
{
typedef adjacency_list< boost::vecS, boost::listS, boost::undirect edS, ve::VertexPrope rties, ed::EdgePropert ies> Graph;
const int V = m.size()+1;
Graph g(V);
property_map<Gr aph, std::size_t ve::VertexPrope rties::*>::type
id = get(&ve::Vertex Properties::ind ex, g);
property_map<Gr aph, std::string ed::EdgePropert ies::*>::type
name = get(&ed::EdgePr operties::name, g);
boost::graph_tr aits<Graph>::ve rtex_iterator vi, viend;
int vnum = 0;
for (boost::tie(vi, viend) = vertices(g); vi != viend; ++vi)
id[*vi] = vnum++;
for (int i = 0; i <50 ; i++)
{
if(s3[i]==1){
add_edge(vertex (s1[i], g), vertex(s2[i], g), ed::EdgePropert ies("single bond"), g);
}
if(s3[i]==2){
add_edge(vertex (s1[i], g), vertex(s2[i], g), ed::EdgePropert ies("double bond"), g);
}
if(s3[i]==3){
add_edge(vertex (s1[i], g), vertex(s2[i], g), ed::EdgePropert ies("triple_bon d"), g);
}
}
graph_traits<Gr aph>::vertex_it erator i, end;
graph_traits<Gr aph>::out_edge_ iterator ei, edge_end;
for (boost::tie(i,e nd) = vertices(g); i != end; ++i)
{
if(id[*i]==a)
{
cout << id[*i]<<"("<<m[id[*i]]<<")" << " ";
for (boost::tie(ei, edge_end) = out_edges(*i, g); ei != edge_end; ++ei)
if(id[target(*ei, g)]==b)
cout << " bond " << name[*ei] ;
cout << endl;
}
}
}
bool Is_adjacent(vec tor<int> s1,vector<int> s2,vector<int> s3,int a,int b)
{
typedef adjacency_list< boost::vecS, boost::listS, boost::undirect edS, ve::VertexPrope rties, ed::EdgePropert ies> Graph;
const int V = m.size()+1;
Graph g(V);
property_map<Gr aph, std::size_t ve::VertexPrope rties::*>::type
id = get(&ve::Vertex Properties::ind ex, g);
property_map<Gr aph, std::string ed::EdgePropert ies::*>::type
name = get(&ed::EdgePr operties::name, g);
boost::graph_tr aits<Graph>::ve rtex_iterator vi, viend;
int vnum = 0;
for (boost::tie(vi, viend) = vertices(g); vi != viend; ++vi)
id[*vi] = vnum++;
for (int i = 0; i <50 ; i++)
{
if(s3[i]==1){
add_edge(vertex (s1[i], g), vertex(s2[i], g), ed::EdgePropert ies("single bond"), g);
}
if(s3[i]==2){
add_edge(vertex (s1[i], g), vertex(s2[i], g), ed::EdgePropert ies("double bond"), g);
}
if(s3[i]==3){
add_edge(vertex (s1[i], g), vertex(s2[i], g), ed::EdgePropert ies("triple_bon d"), g);
}
}
graph_traits<Gr aph>::vertex_it erator i, end;
graph_traits<Gr aph>::out_edge_ iterator ei, edge_end;
for (boost::tie(i,e nd) = vertices(g); i != end; ++i)
{
for (boost::tie(ei, edge_end) = out_edges(*i, g); ei != edge_end; ++ei)
{
if(id[source(*ei, g)]==a)
{
if(id[target(*ei, g)]==b)
//cout<<id[source(*ei, g)]<<" "<<"--------"<<id[target(*ei, g)];
return true;
}
}
}
return false;
}
bool Is_Present(vect or<int> s1,vector<int> s2,vector<int> s3,int b)
{
typedef adjacency_list< boost::vecS, boost::listS, boost::undirect edS, ve::VertexPrope rties, ed::EdgePropert ies> Graph;
const int V = m.size()+1;
Graph g(V);
property_map<Gr aph, std::size_t ve::VertexPrope rties::*>::type
id = get(&ve::Vertex Properties::ind ex, g);
property_map<Gr aph, std::string ed::EdgePropert ies::*>::type
name = get(&ed::EdgePr operties::name, g);
boost::graph_tr aits<Graph>::ve rtex_iterator vi, viend;
int vnum = 0;
for (boost::tie(vi, viend) = vertices(g); vi != viend; ++vi)
id[*vi] = vnum++;
for (int i = 0; i <50 ; i++)
{
if(s3[i]==1){
add_edge(vertex (s1[i], g), vertex(s2[i], g), ed::EdgePropert ies("single bond"), g);
}
if(s3[i]==2){
add_edge(vertex (s1[i], g), vertex(s2[i], g), ed::EdgePropert ies("double bond"), g);
}
if(s3[i]==3){
add_edge(vertex (s1[i], g), vertex(s2[i], g), ed::EdgePropert ies("triple_bon d"), g);
}
}
graph_traits<Gr aph>::vertex_it erator i, end;
graph_traits<Gr aph>::out_edge_ iterator ei, edge_end;
for (boost::tie(i,e nd) = vertices(g); i != end; ++i)
{
if(id[*i]==b)
{
return 1;
}
}
return 0;
}
};
int main(int,char* [])
{
using namespace boost;
Molecule mol;
int s11[]={1,1,2,2,3,3,4 ,4,5,5,6,6,6,7, 7,7,8,8,8,8,9,9 ,10,10,11,11,12 ,12,13,14,17,19 ,19,19,20,20,20 ,21,21,21};
int s12[]={13,19,14,20,1 5,21,16,18,17,1 8,16,27,28,18,3 8,39,9,10,22,23 ,11,12,16,17,14 ,24,13,25,15,15 ,26,29,30,31,32 ,33,34,35,36,37 };
int s13[]={1,1,1,1,1,1,2 ,1,1,2,1,1,1,1, 1,1,1,1,1,1,2,1 ,1,2,1,1,2,1,1, 2,1,1,1,1,1,1,1 ,1,1,1};
string s14[]={"O","O","O"," N","N","N","N", "C","C","C","C" ,"C","C","C","C ","C","C","C"," C","C","C","H", "H","H","H","H" ,"H","H","H","H ","H","H","H"," H","H","H","H", "H","H"};
vector <string> s4;
vector <int> s1,s2,s3;
for (unsigned int i = 0; i < sizeof(s11)/sizeof(string); i++)
{
s1.push_back(s1 1[i]);
s2.push_back(s1 2[i]);
s3.push_back(s1 3[i]);
}
for (unsigned int i = 0; i < sizeof(s14)/sizeof(string); i++)
{
s4.push_back(s1 4[i]);
}
mol.set_molecul e_property(s4);
mol.create_chem ical_Network(s1 ,s2,s3);
mol.print_veric es(s1,s2,s3);
mol.Get_interac tion(s1,s2,s3);
mol.Get_Neighbo urs(s1,s2,s3, 6);
mol.Get_bond(s1 ,s2,s3,1,19);
cout<<mol.Is_ad jacent(s1,s2,s3 ,0,23)<<endl;
cout<<mol.Is_Pr esent(s1,s2,s3, 67)<<endl;
cout<<"number_o f_vertices="<<m ol.number_of_ve rtices()<<endl;
return 0;
}