我编写了这段代码来创建树格式的平面图,其中每个节点都有随机的子节点数,所有级节点都将被连接,所有叶节点将以循环方式连接。生成的图表很好但是在尝试将它们写入文本文件时,它会导致"分段错误(代码转储)",只有当我输入生成的大于5的图形时才会发生它6.我认为这是因为内存边界发生但我尝试清除所有队列,并且在将图形写入文件之前已经清空了所有队列。
#include<time.h>
#include<stdlib.h>
#include<queue>
#include<fstream>
#include<iostream>
using namespace std;
void updateGraph(int x,int n,int s);
void printGraph(bool **gh,int len);
void clearQueue(queue<int> &q );
bool **g = NULL;
int vertexCount = 0;
int main()
{
cout<<"Tree- Planar Graph generation"<<endl;
cout<<"Enter the no of graphs to be generated"<<endl;
int no_graphs = 0;
cin>>no_graphs;
int verticesNo = 0;
srand(time(NULL));
ofstream my_file("planar_tree_graphs.txt",ios :: out);
if(my_file.is_open())
{
my_file << no_graphs;
my_file << "\n";
for(int k=0;k<no_graphs;k++)
{
g = NULL;
verticesNo = 5;
vertexCount = 0;
//verticesNo = rand() % 21;
//verticesNo = verticesNo + 50;
my_file << verticesNo;
my_file << "\n";
int v = 0;
queue<int> leaf;
queue<int> level;
queue<int> levelv;
//cout<<level.empty();
//cout<<levelv.empty();
level.push(0);
level.push(-1);
v++;
vertexCount = 1;
g = new bool*[1];
g[0] = new bool[1];
g[0][0] = false;
while(v < verticesNo && !level.empty())
{
if(level.front() == -1)
{ //cout<<"levelv "<<levelv.empty()<<endl;
while(!levelv.empty())
{
int x = levelv.front();
if(x == -1)
{
levelv.pop();
//cout<<"x-break"<<endl;
break;
}
levelv.pop();
int y = levelv.front();
if(y == -1)
{
levelv.pop();
//cout<<"y-break"<<endl;
break;
}
//cout<<"left"<<endl;
g[x][y] = true;
g[y][x] = true;
}
//cout<<"poped"<<endl;
//cout<<"levelv "<<levelv.empty()<<endl;
level.pop();
//cout<<"level "<<level.empty()<<endl;
}
int vertex = level.front();
level.pop();
while(vertex == -1 && !level.empty())
{
vertex = level.front();
level.pop();
}
if(vertex == -1)
break;
int nadj = 0;
if(v + 10 >= verticesNo)
nadj = rand() % (verticesNo - v + 1);
else
nadj = rand() % 10;
while(v == 1 && nadj == 0)
{
if(v + 10 >= verticesNo)
nadj = rand() % (verticesNo - v + 1);
else
nadj = rand() % 10;
}
//cout<<"goit"<<endl;
if(nadj == 0)
{
leaf.push(vertex);
//cout<<"vertex "<<vertex<<endl;
}
for(int i=v;i<v+nadj;i++)
{
level.push(i);
levelv.push(i);
}
if(level.front() == -1)
{
levelv.push(-1);
level.push(-1);
}
updateGraph(vertex,nadj,v);
//printGraph(g,vertexCount);
v = v + nadj;
}
//cout<<"over"<<endl;
while(!levelv.empty())
{
int x = levelv.front();
levelv.pop();
while(x == -1)
{
x = levelv.front();
levelv.pop();
}
if(levelv.empty())
break;
int y = levelv.front();
while(y == -1)
{
levelv.pop();
y = levelv.front();
}
//cout<<"x "<<x<<" y "<<y<<endl;
g[x][y] = true;
g[y][x] = true;
}
//cout<<"over2"<<endl;
//for cycle
while(!level.empty())
{
int x = level.front();
level.pop();
while(x == -1 && !level.empty())
{
x = level.front();
level.pop();
}
if(x != -1)
{
leaf.push(x);
//cout<<"x "<<x<<endl;
}
}
//cout<<"over3"<<endl;
if(leaf.size() >= 2)
{
//cout<<"inside"<<endl;
int x = leaf.front();
int y = leaf.back();
//cout<<x<<y<<endl;
g[x][y] = true;
g[y][x] = true;
}
//cout<<"over4"<<endl;
while(!leaf.empty())
{
int x = leaf.front();
leaf.pop();
if(leaf.empty())
break;
int y = leaf.front();
leaf.pop();
if(leaf.empty())
break;
//cout<<"x "<<x<<" y "<<y<<endl;
g[x][y] = true;
g[y][x] = true;
}
cout<<leaf.empty();
cout<<level.empty();
cout<<levelv.empty();
for(int m = 0;m<verticesNo;m++)
{
for(int n = 0;n<verticesNo;n++)
{
my_file << g[m][n];
my_file << "\t";
}
my_file << "\n";
}
}
}
my_file.close();
cout<<no_graphs<<" generated successfully!"<<endl;
cout<<"All graphs are stored in a file named- planar_tree_graphs.txt"<<endl;
return 0;
}
void updateGraph(int x,int n,int s)
{
bool **dummy = NULL;
dummy = new bool*[vertexCount];
for(int i=0;i<vertexCount;i++)
dummy[i] = new bool[vertexCount];
for(int i=0;i<vertexCount;i++)
for(int j=0;j<vertexCount;j++)
dummy[i][j] = g[i][j];
//cout<<"dummy"<<endl;
//printGraph(dummy,vertexCount);
g = NULL;
vertexCount = vertexCount + n;
g = new bool*[vertexCount];
for(int i=0;i<vertexCount;i++)
g[i] = new bool[vertexCount];
for(int i=0;i<vertexCount;i++)
for(int j=0;j<vertexCount;j++)
g[i][j] = false;
//cout<<"new initial"<<endl;
//printGraph(g,vertexCount);
for(int i=0;i<vertexCount-n;i++)
for(int j=0;j<vertexCount-n;j++)
g[i][j] = dummy[i][j];
//cout<<"dummy copy"<<endl;
//printGraph(g,vertexCount);
for(int i=s;i<vertexCount;i++)
{
g[x][i] = true;
g[i][x] = true;
}
}
void printGraph(bool **gh,int vertexCount)
{
cout<<"Graph is: "<<endl;
for(int i=0;i<vertexCount;i++)
{
for(int j=0;j<vertexCount;j++)
cout<<gh[i][j]<<" ";
cout<<"\n";
}
}
void clearQueue(queue<int> &q )
{
queue<int> empty;
swap( q, empty );
}