The code basically takes in number of nodes(N) and the number of neighbors(k) as the input. Then in the main function's for loop the node will be created which will have all the variables declared in the structure. Then the main function calls the initialization function.
The initialization function creates a vector g consisting of N-1 node numbers which doesn't have the node creating it.(That is if N=100 and the 1st node has called the initialization function, then vector g will have 2,3,....,100 ,and if node 2 has called the initialization function, then vector g will have 1,3,4,...,100).
After this random_numbers function is called and this function randomly shuffles this vector g and picks the first k elements from it and returns it to the initialization function which in turn returns it to the main function. Then the main function will update the node1.neighbor_list using the vector it has received.
After the code finishes running, the output has to look something like this suppose, N=5, k=3.
node1
{
userID = 1;
neighbor_list = [2,5,4];
}
node2
{
userID = 2;
neighbor_list = [5,4,1];
}
node3 {
userID = 3;
neighbor_list = [2,5,4];
}
node4
{
userID = 4;
neighbor_list = [2,5,1];
}
node5
{
userID = 5;
neighbor_list = [3,2,4];
}
I am able to trace the working till the initialization function returns the vector back to the main function. But I am unable to copy this vector into the neighbor_list vector of the node. Please help!! I have been trying for long at this point.
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
int N;
int k;
vector<int> g;
int j;
int i;
struct nodes
{
int userID;
vector<int> neighbor_list;
};
vector<int> initialization(int j);
vector<int> random_numbers(vector<int> &r);
int main()
{
int f;
vector<int> received;
cout << "Enter the number of nodes: "; //enter some number
cin >> N;
cout << "Enter the number of neighbors: "; // enter a number less than N
cin >> k;
cout << endl;
for (i = 1; i <= N; i++)
{
nodes node[i]; // creates a node
node[i].userID = i;
cout << "creating " << i << " node" << endl;
cout << "Calling the initialization function" << endl;
received = initialization(i);
cout
<< "The vector received back from the initialization function is: ";
for (f = 1; f <= k; f++)
{
cout << received[f - 1] << " ";
}
cout << endl;
for (f = 1; f <= k; f++)
{
node[i].neighbor_list.push_back(received[f - 1]); //error(guessing)
}
cout << "The neighbor_list for node " << i << " is " << endl;
for (f = 1; f <= k; f++)
{
cout << "f: " << f;
cout << node[i].neighbor_list[f - 1] << " ";
}
cout << endl;
cout << endl;
}
return 0;
}
vector<int> initialization(int j) //passing the value of i
{
cout << "Entered the initialization function" << endl;
cout << "The value of the node created passed in is " << j << endl;
int f;
vector<int> g;
vector<int> receiver;
int x;
int n;
int z;
int rand_array[N][N - 1]; //to store the neighbor list of the node[i]
int d;
int a = 0;
for (z = 1; z <= N; z++)
{
if (j != z)
{
rand_array[j - 1][a] = z; //Put all values into array except i
cout << "The value of rand_array[" << j - 1 << "][" << a << "] is "
<< z << endl;
a++;
}
else
{
continue;
}
}
for (f = 1; f <= N - 1; f++)
{
g.push_back(rand_array[j - 1][f - 1]);
}
cout << "vector g after push back from the rand_array is: ";
for (f = 1; f <= N - 1; f++)
{
cout << g[f - 1] << " ";
}
cout << endl;
cout << "Calling the random function" << endl;
receiver = random_numbers(g);
cout << "value of receiver vector from random_numbers function: ";
for (f = 1; f <= k; f++)
{
cout << receiver[f - 1] << " ";
}
cout << endl;
return receiver;
}
vector<int> random_numbers(vector<int> &r)
{
int h;
int f;
cout << "Entered the random_numbers function" << endl;
cout << "The vector passed by the initialization function is: ";
for (f = 1; f <= N - 1; f++)
{
cout << r[f - 1] << " ";
}
cout << endl;
vector<int> sender;
random_shuffle(r.begin(), r.end()); //random shuffle the vector
for (h = 1; h <= k; h++)
{
sender.push_back(r[h - 1]);
}
return sender;
}