new user here. I am writing this code that creates a network with nodes and uses a random number to create edges between them. I track the entire graph as a vector, each entry is a vector representing a node whose elements are its neighbors. He then uses the depth search to find the number of components that are divided parts of the graph (my account variable). Then I output the node and the number of neighbors to which it is connected to a txt file. The code compiles, but an error message appears on the command line:
ending a call after calling an instance of 'std :: out_of_range' what (): vector :: _ M_range_check
This application asked Runtime to terminate it in an unusual way. Please contact support ...
So ... what does it mean and how can I fix it?
Also, do I need to keep track of how many nodes are in each component, any ideas?
Thanks in advance, here is my code:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <vector>
using namespace std;
void gengraph(int v, float p, vector <vector <int> >& G);
void DFS(vector <vector <int> > G, int v, vector<int>& M);
int main()
{
int a = 1000;
float b = 0.004;
vector <vector <int> > G;
gengraph(a,b,G);
vector <int> M (1000);
int count = 0;
int i;
for (i = 0; i < a; i++)
{
if (M[i]==0)
{
DFS(G, i, M);
count += 1;
}
}
ofstream myfile;
myfile.open ("data.txt");
for (int l=0; l<1000; l++)
{
myfile << "v len(G[v])\n";
}
myfile.close();
}
void gengraph(int v, float p, vector <vector <int> >& G)
{
for (int i = 0; i<1000; i++)
{
for (int j = 0; j<1000; j++)
{
int y = rand();
bool Prob = (y <= p);
if (i == j)
continue;
else
{
if(Prob == true)
{
G.at(i).push_back (j);
G.at(j).push_back (i);
}
}
}
}
}
void DFS(vector <vector <int> >& G, int v, vector<int>& M)
{
M[v]=1;
for(unsigned int j = 0; j < G[v].size(); j++)
{
if (M[j]==0)
{
DFS(G, j, M);
}
}
}
source
share