GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Sep-2006, 12:12
cpit cpit is offline
Junior Member
 
Join Date: Jan 2006
Posts: 67
cpit is on a distinguished road

combinations of elements on vectors


Hi friends,

I have a vector x={1,2,2} and I want to obtain its combinations

i.e.
y={{1,1,1},{1,1,2},{1,2,1},{1,2,2}}


Here is a sample of the program, we can see the amount of combinations with this...

CPP / C++ / C Code:
#include <iostream>
#include <vector>

using namespace std;

int main(){

vector<int> mroute;

mroute.push_back(1);
mroute.push_back(2);
mroute.push_back(2);

//To find the number of combinations
int ncombs=1;

for(int i=0;i<mroute.size();i++){
        if(mroute[i]>0){
            ncombs*=mroute[i];               
        }
}
cout << "Number of combinations: " <<ncombs<<endl;

//--- to print the combinations! ----------------

system("PAUSE");    
}

Can someone give a suggestion to do it?


regards,

CPIT
Last edited by cpit : 27-Sep-2006 at 12:44. Reason: improved explanation
  #2  
Old 28-Sep-2006, 04:47
MichaelS-R MichaelS-R is offline
Junior Member
 
Join Date: Apr 2006
Location: Berkshire, UK
Posts: 65
MichaelS-R is on a distinguished road

Re: combinations of elements on vectors


Hmmm... bit lost here. How is {1,1,1} a combination of {1,2,2}? What are you using the results for?
__________________
Michael

Dual Opteron 280 (2 x dual core) with 2Gb RAM, 2x36GB system drives, 2T on 3Ware 9500Mi RAID controller. Running Fedora Core 4. Using Anjuta IDE. Developemnt in C++ with MySQL (via mysql++).
  #3  
Old 28-Sep-2006, 05:36
cpit cpit is offline
Junior Member
 
Join Date: Jan 2006
Posts: 67
cpit is on a distinguished road

Re: combinations of elements on vectors


OK Friend,
I will try to explain better...

I´m going use this for to implement all possible routes in a network.

I made a modification on Dijkstra´s algorithm to find all shortest paths between two nodes.

Then, imagine a network with 4nodes and connected like this:

1-2 or be, node1 have a link to connect node2...
1-3
2-4
3-4

After perform the algorithm to find out all shortest paths we have 2 routes from the node 1 to node 4, and 2 routes from the node 2 to 3.

then, we can set a matrix of paths like this:

- 1 1 2
1 - 2 1
1 2 - 1
2 1 1 -

and using the number above the main diagonal, we have 1 1 2 2 1 1

Now I need to perform other calcs on each combination of traffic.

then 1 1 1 1 1 1 means that I will use a set of paths
and 1 1 1 2 1 1 means that I will use other set of paths
and 1 1 2 1 1 1
and 1 1 2 2 1 1



In this simple case we have only 4 combinations, but I want to tread large networks, for example, with more than 100 nodes!!!


Regards,

CPIT
 
 

Recent GIDBlogI?m Home by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting elements of arrays C++ the_crazyman C++ Forum 25 30-May-2008 08:27
Vectors and inserting elements Krandygrl00 C++ Forum 2 07-Jun-2005 14:14
vectors of references mirizar C++ Forum 1 12-Apr-2005 03:02
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 22:26
Declaring a vector of vectors? Lethal411 C++ Forum 2 20-Mar-2004 10:02

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:10.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.