473,698 Members | 2,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to reduce code length

151 New Member
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;
}
Apr 5 '08 #1
2 1580
hsn
237 New Member
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 Recognized Expert Moderator Expert
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
12588
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 "help", "copyright", "credits" or "license" for more information. >>> d1 = {'a':1} >>> d2 = {'b':2} >>> d3 = {'c':3}
4
5610
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 : bigmuls = lambda xs,ys: filter(lambda (x,y):x*y > 25, combine(xs,ys)) combine = lambda xs,ys: map(None, xs*len(ys), dupelms(ys,len(xs))) dupelms = lambda lst,n: reduce(lambda s,t:s+t, map(lambda l,n=n: *n, lst))
181
8801
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 He says he's going to dispose of map, filter, reduce and lambda. He's going to give us product, any and all, though, which is nice of him.
5
3553
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 grouping, I also output conditionally? <xsl:for-each
8
2991
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 buffer as I found out calling by capacity(). What if I do not need all that buffer space again want to reduce the size to a smaller one? I tried resize() passing a smaller size than the
10
2730
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 keep the integrity of the database. We are parsing roughl 81 files and range in size 1 kb to 65 MB files (Average of 400,000 lines in the larger files). I have written this utility with VB.NET 2003 and when I parse all of the files I run out...
3
2936
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 a standard length, and the part which identifies the region isn't a standard length either. The first letters of the postcode denotes a general region, and then sub-regions are denoted by the following numbers and letters.
3
3844
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 transfer to another table(Ex; table2) field. That table2 field length is 100. For some reason i can;t change the structure of the table. So allways i will get error data type mismatch or related error to data type. When i update table1 (Description...
7
3503
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 with named function: 37.9864357062 reduce with nested, named function: 39.4710288598 reduce with lambda: 39.2463927678 sum comprehension: 25.9530121845
1
2780
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
9164
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8898
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8870
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6524
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.