| | |
Problem in using the Graph object in different function
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 8
Reputation:
Solved Threads: 0
I am creating a graph using boost library, I am making the network using create_Network function()
i am adding the vertices to this graph by creating the object g of class Graph.
I am trying to use this object g in the another function get_neighbours()
but it is not giving any result.Should I make it global or should use constructor so that values stored in the graph(vertices) object(g), can be used in any function.
#include <boost/config.hpp>
#include <iostream>
#include <vector>
#include <utility>
#include <string>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_utility.hpp>
#include <boost/property_map.hpp>
#include "ed.h"
#include "ve.h"
using namespace boost;
using namespace std;
class Molecule
{
typedef adjacency_list<boost::vecS, boost::listS, boost::undirectedS, ve::VertexProperties, ed::EdgeProperties> Graph;
map<int,string> m;
public:
void set_molecule_property(vector<string>& s4)
{
for (unsigned int i = 1; i <= s4.size(); i++)
{
m[i]=s4[i-1];
}
}
void create_chemical_Network(vector<int> s1,vector<int> s2,vector<int> s3)
{
const int V = m.size()+1;
Graph g(V);
for (int i = 0; i <V ; i++)
{
if(s3[i]==1){
add_edge(vertex(s1[i], g), vertex(s2[i], g), ed::EdgeProperties("single bond"), g);
}
if(s3[i]==2){
add_edge(vertex(s1[i], g), vertex(s2[i], g), ed::EdgeProperties("double bond"), g);
}
if(s3[i]==3){
add_edge(vertex(s1[i], g), vertex(s2[i], g), ed::EdgeProperties("triple_bond"), g);
}
}
}
void get_neighbours()
{
//Graph g(m.size()+1);
Graph g;
property_map<Graph, std::size_t ve::VertexProperties:
>::type
id = get(&ve::VertexProperties::index, g);
property_map<Graph, std::string ed::EdgeProperties:
>::type
name = get(&ed::EdgeProperties::name, g);
boost::graph_traits<Graph>::vertex_iterator vi, viend;
int vnum = 0;
for (boost::tie(vi,viend) = vertices(g); vi != viend; ++vi)
id[*vi] = vnum++;
graph_traits<Graph>::vertex_iterator i, end;
graph_traits<Graph>::out_edge_iterator ei, edge_end;
for (boost::tie(i,end) = 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;
}
}
print_edges(g, id);
cout<<endl;
print_vertices(g,id);
}
};
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,1 0,11,11,12,12,13,14,17,19,19,19,20,20,20,21,21,21} ;
int s12[]={13,19,14,20,15,21,16,18,17,18,16,27,28,18,38,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(s11[i]);
s2.push_back(s12[i]);
s3.push_back(s13[i]);
}
for (unsigned int i = 0; i < sizeof(s14)/sizeof(string); i++)
{
s4.push_back(s14[i]);
}
mol.set_molecule_property(s4);
mol.create_chemical_Network(s1,s2,s3);
mol.get_neighbours();
return 0;
}
Please Help me out of this pblm.I will be really thankful to you.
i am adding the vertices to this graph by creating the object g of class Graph.
I am trying to use this object g in the another function get_neighbours()
but it is not giving any result.Should I make it global or should use constructor so that values stored in the graph(vertices) object(g), can be used in any function.
#include <boost/config.hpp>
#include <iostream>
#include <vector>
#include <utility>
#include <string>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_utility.hpp>
#include <boost/property_map.hpp>
#include "ed.h"
#include "ve.h"
using namespace boost;
using namespace std;
class Molecule
{
typedef adjacency_list<boost::vecS, boost::listS, boost::undirectedS, ve::VertexProperties, ed::EdgeProperties> Graph;
map<int,string> m;
public:
void set_molecule_property(vector<string>& s4)
{
for (unsigned int i = 1; i <= s4.size(); i++)
{
m[i]=s4[i-1];
}
}
void create_chemical_Network(vector<int> s1,vector<int> s2,vector<int> s3)
{
const int V = m.size()+1;
Graph g(V);
for (int i = 0; i <V ; i++)
{
if(s3[i]==1){
add_edge(vertex(s1[i], g), vertex(s2[i], g), ed::EdgeProperties("single bond"), g);
}
if(s3[i]==2){
add_edge(vertex(s1[i], g), vertex(s2[i], g), ed::EdgeProperties("double bond"), g);
}
if(s3[i]==3){
add_edge(vertex(s1[i], g), vertex(s2[i], g), ed::EdgeProperties("triple_bond"), g);
}
}
}
void get_neighbours()
{
//Graph g(m.size()+1);
Graph g;
property_map<Graph, std::size_t ve::VertexProperties:
>::typeid = get(&ve::VertexProperties::index, g);
property_map<Graph, std::string ed::EdgeProperties:
>::typename = get(&ed::EdgeProperties::name, g);
boost::graph_traits<Graph>::vertex_iterator vi, viend;
int vnum = 0;
for (boost::tie(vi,viend) = vertices(g); vi != viend; ++vi)
id[*vi] = vnum++;
graph_traits<Graph>::vertex_iterator i, end;
graph_traits<Graph>::out_edge_iterator ei, edge_end;
for (boost::tie(i,end) = 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;
}
}
print_edges(g, id);
cout<<endl;
print_vertices(g,id);
}
};
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,1 0,11,11,12,12,13,14,17,19,19,19,20,20,20,21,21,21} ;
int s12[]={13,19,14,20,15,21,16,18,17,18,16,27,28,18,38,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(s11[i]);
s2.push_back(s12[i]);
s3.push_back(s13[i]);
}
for (unsigned int i = 0; i < sizeof(s14)/sizeof(string); i++)
{
s4.push_back(s14[i]);
}
mol.set_molecule_property(s4);
mol.create_chemical_Network(s1,s2,s3);
mol.get_neighbours();
return 0;
}
Please Help me out of this pblm.I will be really thankful to you.
I'm assuming that the code as you see it in your IDE is nicely indented, and not riddled with smilies, right?
http://www.daniweb.com/forums/announcement8-3.html
http://www.daniweb.com/forums/announcement8-3.html
![]() |
Similar Threads
- Need advice on function structuring... (C++)
- Variable scope problem (C++)
- Graphics In Pixel: Part II B (C)
- using(STL)function object (bind2nd) with a user defined function object (C++)
Other Threads in the C++ Forum
- Previous Thread: c++
- Next Thread: c++ compilation problem: line to short
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






