473,395 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to reduce code length

151 100+
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.hpp>
#include <boost/property_map.hpp>
#include "ed.h"
#include "ve.h"
using namespace boost;
using namespace std;
class Molecule
{
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];
}
}

typedef adjacency_list<boost::vecS, boost::listS, boost::undirectedS, ve::VertexProperties, ed::EdgeProperties> Graph;
int number_of_vertices()
{
return m.size();
}

void create_chemical_Network(vector<int> s1,vector<int> s2,vector<int> s3)
{
typedef adjacency_list<boost::vecS, boost::listS, boost::undirectedS, ve::VertexProperties, ed::EdgeProperties> Graph;
const int V = m.size()+1;
Graph g(V);
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++;
for (int i = 0; i <50 ; 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);
}
}

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;
}
}

}


void Get_interaction(vector<int> s1,vector<int> s2,vector<int> s3)
{
typedef adjacency_list<boost::vecS, boost::listS, boost::undirectedS, ve::VertexProperties, ed::EdgeProperties> Graph;
const int V = m.size()+1;
Graph g(V);
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++;
for (int i = 0; i <50 ; 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);
}
}

print_edges(g, id);

}



void print_verices(vector<int> s1,vector<int> s2,vector<int> s3)
{
typedef adjacency_list<boost::vecS, boost::listS, boost::undirectedS, ve::VertexProperties, ed::EdgeProperties> Graph;
const int V = m.size()+1;
Graph g(V);
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++;
for (int i = 0; i <50 ; 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);
}
}
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];

cout << endl;
}
}
}

void Get_Neighbours(vector<int> s1,vector<int> s2,vector<int> s3,int atom)
{
typedef adjacency_list<boost::vecS, boost::listS, boost::undirectedS, ve::VertexProperties, ed::EdgeProperties> Graph;
const int V = m.size()+1;
Graph g(V);
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++;
for (int i = 0; i <50 ; 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);
}
}


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]==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::undirectedS, ve::VertexProperties, ed::EdgeProperties> Graph;
const int V = m.size()+1;
Graph g(V);
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++;
for (int i = 0; i <50 ; 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);
}
}

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]==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(vector<int> s1,vector<int> s2,vector<int> s3,int a,int b)
{
typedef adjacency_list<boost::vecS, boost::listS, boost::undirectedS, ve::VertexProperties, ed::EdgeProperties> Graph;
const int V = m.size()+1;
Graph g(V);
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++;
for (int i = 0; i <50 ; 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);
}
}

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)
{
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(vector<int> s1,vector<int> s2,vector<int> s3,int b)
{
typedef adjacency_list<boost::vecS, boost::listS, boost::undirectedS, ve::VertexProperties, ed::EdgeProperties> Graph;
const int V = m.size()+1;
Graph g(V);
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++;
for (int i = 0; i <50 ; 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);
}
}
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]==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,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.print_verices(s1,s2,s3);
mol.Get_interaction(s1,s2,s3);
mol.Get_Neighbours(s1,s2,s3, 6);
mol.Get_bond(s1,s2,s3,1,19);
cout<<mol.Is_adjacent(s1,s2,s3,0,23)<<endl;
cout<<mol.Is_Present(s1,s2,s3,67)<<endl;
cout<<"number_of_vertices="<<mol.number_of_vertice s()<<endl;
return 0;
}
Apr 5 '08 #1
2 1565
hsn
237 100+
hello
i don't think that someone can teach you how to make your code shorter..
it depends on you and your skills
if you are a new programmer then don't worry you will learc how to writer better programs with less number of lines......

good luck

hsn
Apr 5 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
Why do you feel this is too lengthy? Think what the Windows kernal folks deal with using 140,000 source files.
Apr 5 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
4
by: Ben | last post by:
Hi all, I'm trying to figure out how how complex map, filter and reduce work based on the following piece of code from http://www-106.ibm.com/developerworks/linux/library/l-prog.html : ...
181
by: Tom Anderson | last post by:
Comrades, During our current discussion of the fate of functional constructs in python, someone brought up Guido's bull on the matter: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 ...
5
by: Marcel Akkerman | last post by:
Hi, Does anyone have a clue how to reduce the number of nodes using XSLT? When outputing all nodes in order I could just use <xsl:for-each select="name"> But what if I, besides sorting and...
8
by: CoolPint | last post by:
Is there any way I can reduce the size of internal buffer to store characters by std::string? After having used a string object to store large strings, the object seems to retain the large...
10
by: klineb | last post by:
Good Day, I have written and utility to convert our DOS COBOL data files to a SQL Server database. Part of the process requires parsing each line into a sql statement and validting the data to...
3
by: mark | last post by:
I have a table of UK companies whose records I want to filter using a map of postcode regions. For the benfit of people outside the UK, our postcodes are a pain to work with because they are not...
3
by: imtmub | last post by:
Hi All, I am facing some problem in the field data(Description field in Item table). In that table one field(Description field)data legth is 255 and type: nvarchar. In some operation this field data...
7
by: cnb | last post by:
This must be because of implementation right? Shouldn't reduce be faster since it iterates once over the list? doesnt sum first construct the list then sum it? ----------------------- reduce...
1
by: vikassawant | last post by:
Hi, I m facing some critical problem for downloading 5mb file from server. Does anyone know? How to reduce the time to download file from server using HTTP connection?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.