changing for loop to block parition - c++

I want to change the for-loop to block scheme
I have this for loop that does this:
let say n = 8
and node = 4
n: [1][2][3][4][5][6][7][8]
id: 0 1 2 3 0 1 2 3
id = 0;
while (id < node){
for (i = id + 1; i <= n; i = i + node)
{
//do stuff here
id = i;
}enter code here
id+1;
}//end while
and i want it to do this:
n = 8
node= 4
n: [1][2][3][4][5][6][7][8]
id: 0 0 1 1 2 2 3 3
n = 16
node = 4
n: [1][2][3][4][5][6][7][8] ... [13][14][15][16]
id: 0 0 0 0 1 1 1 1 ... 3 3 3 3
n = 8
node= 2
n: [1][2][3][4][5][6][7][8]
id: 0 0 0 0 1 1 1 1
where each id is assign to the top n show in examples
I have this but it only works for the specific scenario n= 8 & node = 4
...
b = id + 1
for (i = n-(n-(id+b)); i <= (n-(n-(id+b))+1); i+= 1)
...

I perhaps misunderstood your question but this gives the output for id as you described:
for (int i = 0; i < n; i++)
{
id = i / (n / node);
// do stuff
}
I use n starting from 0, but if you start from 1, just adjust the calculation by ofsetting everything to n-1.

Try this code:
int node = 4;
int n = 16;
for (id = 0; id < node; id++) {
for(int j = 0; j < n / node; j++) {
//do some stuff
}
}
Or you can use this code:
int node = 4;
int n = 16;
for (int i = 0; i < node; i++) {
id = i;
for(int j = 0; j < n / node; j++) {
//do some stuff
}
}
They do the same.

Related

How to transform an adjacency matrix into an incidence Matrix

I'm trying to transform the adjacency matrix into an incidence matrix of an undirected graph. For edges :
(1, 2), (1,5), (1,6), (2,3), (2,5), (3,4), (3,5), (4,5), (5,6)
Adj matrix is :
0 1 0 0 1 1
1 0 1 0 1 0
0 1 0 1 1 0
0 0 1 0 1 0
1 1 1 1 0 1
1 0 0 0 1 0
and I expect the result for the incidence matrix to be
0 1 0 0 1 1 0 0 0
1 0 1 0 1 0 0 0 0
0 1 0 1 1 0 0 0 0
0 0 1 0 1 0 0 0 0
1 1 1 1 0 1 0 0 0
1 0 0 0 1 0 0 0 0
but, my program returns this :
1 0 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0
0 1 0 1 1 0 0 0 0
0 0 0 1 0 1 0 0 0
1 0 1 0 1 1 0 0 0
0 0 0 0 0 0 0 0 0
My source code :
graph constructor
Graph(int vertices, int edges)
{
this->vertices = vertices;
this->edges = edges;
edge = std::vector<Graph::Edge*>(edges);
for (int i = 0; i < edges; i++)
{
edge[i] = new Edge(this);
}
}
Graph* g = new Graph(numberOfVertices, numberOfEdges);
g->edge[0]->src = 1;
g->edge[0]->dest = 2;
g->edge[1]->src = 1;
g->edge[1]->dest = 5;
g->edge[2]->src = 1;
g->edge[2]->dest = 6;
g->edge[3]->src = 2;
g->edge[3]->dest = 3;
g->edge[4]->src = 2;
g->edge[4]->dest = 5;
g->edge[5]->src = 3;
g->edge[5]->dest = 4;
g->edge[6]->src = 3;
g->edge[6]->dest = 5;
g->edge[7]->src = 4;
g->edge[7]->dest = 5;
g->edge[8]->src = 5;
g->edge[8]->dest = 6;
for (i = 0; i < numberOfEdges; i++)
{
adjacency_matrix[g->edge[i]->src][g->edge[i]->dest] = 1;
adjacency_matrix[g->edge[i]->dest][g->edge[i]->src] = 1;
}
std::cout << "Adjacency matrix : " << std::endl;
for (i = 1; i <= numberOfVertices; i++)
{
for (j = 1; j <= numberOfVertices; j++)
{
std::cout<<adjacency_matrix[i][j]<<" ";
}
std::cout << std::endl;
}
// Incidence Matrix
int counter = 0;
for( int i = 1; i <= numberOfEdges; i++){
for(int j = i + 1; j < numberOfVertices; j++ ){
if(adjacency_matrix[i][j] == 1){
incidence_matrix[i][counter] = 1;
incidence_matrix[j][counter] = 1;
++counter;
}
}
}
for( int i = 1; i <= numberOfVertices; i++){
for(int j = 1; j <= numberOfEdges; j++){
std::cout<<incidence_matrix[i][j]<<" ";
}
std::cout<<std::endl;
}
The ideas in the code are correct. But the indexing in the array is wrong.
Indexing should start at 0. Note: this also applies when setting up the adjacency matrix.
The numbers you use to name the vertices/nodes where originally 1,2,3,4,5,6. I propose to call them 0,1,2,3,4,5. Your original edge (1,2) then becomes (0,1). But if we consistently rename all the vertices everywhere we end up with the same graph. The advantage of this new naming convention is that we can use these names directly as indices in the C++ data structures you are using. (Provided we use the corresponding integer value and not consider these names to be strings.)
The specification of the Graph becomes
Graph* g = new Graph(numberOfVertices, numberOfEdges);
g->edge[0]->src = 0;
g->edge[0]->dest = 1;
g->edge[1]->src = 0;
g->edge[1]->dest = 4;
g->edge[2]->src = 0;
g->edge[2]->dest = 5;
g->edge[3]->src = 1;
g->edge[3]->dest = 2;
g->edge[4]->src = 1;
g->edge[4]->dest = 4;
g->edge[5]->src = 2;
g->edge[5]->dest = 3;
g->edge[6]->src = 2;
g->edge[6]->dest = 4;
g->edge[7]->src = 3;
g->edge[7]->dest = 4;
g->edge[8]->src = 4;
g->edge[8]->dest = 5;
So printing the adjacency matrix becomes:
std::cout << "Adjacency matrix : " << std::endl;
for (i = 0; i < numberOfVertices; i++)
{
for (j = 0; j < numberOfVertices; j++)
{
std::cout<<adjacency_matrix[i][j]<<" ";
}
std::cout << std::endl;
}
and the calculation of the incidence matrix becomes:
// Incidence Matrix
int counter = 0;
for( int i = 0; i < numberOfVertices; i++){ //numberOfVertices!!
for(int j = i + 1; j < numberOfVertices; j++ ){
if(adjacency_matrix[i][j] == 1){
incidence_matrix[i][counter] = 1;
incidence_matrix[j][counter] = 1;
++counter;
}
}
}
for( int i = 0; i < numberOfVertices; i++){
for(int j = 0; j < numberOfEdges; j++){
std::cout<<incidence_matrix[i][j]<<" ";
}
std::cout<<std::endl;
}
Note that the order of the edges is determined now by the order in which you traverse the adjacency matrix.

Incorrect output in Pascal's triangle program in C++

I was creating a Pascal's triangle program in C++, but the output displayed is not as expected.
Output Expected
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Output got
1
1 1
1 2 1
1 3 3 1
1 2 2 2 1
1 6 6 6 6 1
Till i = 4, output displayed is correct, but after that I couldn't figure out how it goes wrong. Hers is the source code to get reviewed
int main()
{ int num, a[37680], t = 0, b = 2, l;
cout<<"Enter the number of rows: ";
cin>>num;
for (int i = 1; i <= num; i++)
{
for (int j = 1; j <= (num - i); j++)
{
cout<<" ";
}
for (int k = 1; k <= i; k++)
{
l = k;
if (k == 1 || k == i)
{
a[t] = 1;
cout<<a[t]<<" ";
t+=1;
}
else
{
a[t] = a[t - b] + a[t - b - 1];
cout<<a[t]<<" ";
t+=1;
if ( l = (i - 1) )
{
b+=1;
}
}
}
cout<<endl;
}
return 0;}
Equality checking in c++ is done using == and not =, so:
if(l=(i-1))
Should be:
if(l==(i-1))

Optimized (memory-wise) implementation of full graph in C++

I need to implement Watts-Strogatz algorithm and I'm running into some problems with creating a full graph. The way I'm implemeting it, it takes up so much memory and I need to work on big systems so that is a problem.
I'm creating a matrix called lattice for my n nodes with their n - 1 = k neighbours. For eg. let's say n = 7. My lattice will look like:
1 6 2 5 3 4
2 0 3 6 4 5
3 1 4 0 5 6
4 2 5 1 6 0
5 3 6 2 0 1
6 4 0 3 1 2
0 5 1 4 2 3
And now the code to create it.
This is main.cpp:
#include "lattice.h"
#include <vector>
int main() {
/*
* initial parameters
*/
int n = 7; //number of agents MUST BE ODD
int k = 6; //number of agents with whom we wire; N - 1 for full graph
// NEEDS TO BE EVEN
int** lattice = new int* [n];
/*
* creating ring lattice
*/
for (int i = 0; i < n; i++) {
lattice[i] = new int [k];
}
createRingLattice (n, k, lattice);
delete[](lattice);
std::cout << std::endl;
return 0;
}
And this is the function createRingLattice:
void createRingLattice (int n, int k, int** lattice) {
/*
* setting up ring lattice
*/
//table of the nearest neighbours
//next iN previous iP
int* iP = new int [n];
int* iN = new int [n];
for (int i = 0; i < n; i++) {
iP[i] = i - 1;
iN[i] = i + 1;
}
//boundary conditions
iP[0] = n - 1;
iN[n - 1] = 0;
for (int i = 0; i < n; i++) {
int countP = 0;
int countN = 0;
for (int j = 0; j < k; j++) {
if (j % 2 == 0) {
if (i + countN > n - 1) {
lattice[i][j] = iN[i + countN - n];
} else {
lattice[i][j] = iN[i + countN];
}
countN++;
}
if (j % 2 == 1 ) {
if (i - countP < 0) {
lattice[i][j] = iP[n + i - countP];
} else {
lattice[i][j] = iP[i - countP];
}
countP++;
}
}
}
delete[](iN);
delete[](iP);
}
First question:
Is there a way to implement this with much less memory usage?
Second question:
I've seen people implementing graphs with adjacency list (this one for example) but I'm wondering if it's acctually more optimized than my implemantation? It does use pointers as well and I'm not the expert so it's hard for me to determine whether it's better when it comes to memory usage.
Note: I'm not concerned about speed at the moment.

Dry Run of Node with maximum child sum in a generic tree

I am not getting at the point where ans = 15 and sum = 15. Now 15 does not have any child so it will not go into the for loop. Then will it directly return ans(15) to 1? What will happen next?
Or please explain me the whole dry run part. Input: 5 3 1 2 3 1 15 2 4 5 1 6 0 0 0 0
TreeNode<int>* maxSumNode(TreeNode<int> *root){
/* Don't write main().
* Don't read input, it is passed as function argument.
* Return output and don't print it.
* Taking input and printing output is handled automatically.
*/
//small calculation
TreeNode<int>* ans = root;
int sum = root -> data;
for(int i = 0; i < root -> children.size(); i++)
{
sum = sum + root -> children[i] -> data;
}
for(int i = 0; i < root -> children.size(); i++)
{
TreeNode<int> *x = maxSumNode(root -> children[i]);
int xSum = x -> data;
for(int i = 0; i < x ->children.size(); i++)
{
xSum = xSum + x ->children[i] -> data;
}
if(xSum > sum)
{
ans = x;
sum = xSum;
}
}
return ans;
}

my prim function isnt working quite right any help appreciated

im supposed to haveit start from any given location which it does but it just prints the same set over and over. any help would be appreciated
void graph::primms(int x)
{
int mincost = 0;
int min = infinite;
int oldindex;
int index;
bool mark[num+1];
int a,v;
for(int i = 1; i<num+1; i++)
{
mark[i] = false;
}
mark[x] = true;
for(int x = 1; x<num; x++){
for(a =1; a<num+1; a++)
{
for(v=1; v<num+1; v++)
{
if(!mark[v] && !mark[a] && garr[a][v] < min && garr[a][v] > 0)
{
min = garr[a][v];
index = v;
oldindex = a;
}
}
}
mark[v] = true;
cout << oldindex <<" -> "<< index<<endl;
mincost += min;
}
cout<<" total cost is"<<mincost;
}
example of my output:
2 -> 3
2 -> 3
2 -> 3
total cost is 3
this is the graph i am working with:
0 1 2 3 4
1 0 0 0 1
2 0 0 1 3
3 0 1 0 3
4 1 3 3 0