I'm quite new to graphs and want to figure out my segmentation fault problem. My output is already correct but for some reason, as numShapes approach a higher number ~40, a segmentation fault error occurs.
Program Details: The circuit has two sides and you must determine if each shape can fit into side one, if it can't, determine if it can fit into side two, if it can't again, output none. No shape (with their specific X & Y Spacings) must be overlapping.
Thank you.
Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <vector>
using namespace std;
int numShapes, XSpacing,YSpacing;
vector<int> Dimension(2,0);
string delimiter(string temp);
void main_pr();
bool bounded(struct Graph* graph,vector<int> coordinates, int N);
struct Edge{
int src, dest, part;
int Xll, Yll;
int Xur, Yur;
};
struct Graph{
int V, E;
struct Edge* edge;
};
struct Graph* createGraph(int V, int E){
struct Graph* graph =
(struct Graph*) malloc( sizeof(struct Graph) );
graph->V = V;
graph->E = E;
graph->edge =
(struct Edge*) malloc( graph->E * sizeof( struct Edge ) );
return graph;
}
int find(int parent[], int i){
if (parent[i] == -1)
return i;
return find(parent, parent[i]);
}
// A utility function to do union of two subsets
void Union(int parent[], int x, int y){
int xset = find(parent, x);
int yset = find(parent, y);
if(xset!=yset){
parent[xset] = yset;
}
}
struct Graph* graph;
struct Graph* graph2;
int main(){
int check = 0;
char _temp;
string temp;
string temp1,temp2;
for(int j=0;j<4;j++){
while(_temp != '='){
cin>>_temp;
}
_temp=0;
cin>>temp;
switch(j){
case 0:
numShapes = stoi(temp);
if(numShapes == 0){
cout<<"Program terminating...\n";
return 0;
}
case 1:
XSpacing = stoi(temp);
case 2:
YSpacing = stoi(temp);
case 3:
if(j==3)
for(int i=0;i<temp.size();i++){
if(temp[i] == 'x')
check = 1;
if(check == 0)
temp1.push_back(temp[i]);
else{
if(temp[i] != 'x')
temp2.push_back(temp[i]);
}
}
}
}
Dimension[0]=stoi(temp1);
Dimension[1]=stoi(temp2);
main_pr();
return 0;
}
void main_pr(){
string temp;
vector<int> coordinates(4,0);
graph = createGraph(numShapes, numShapes);
graph2 = createGraph(numShapes, numShapes);
int a = 1;
int b = 1;
int check = 0;
for(int i=0; i<numShapes; i++){
for(int j=0; j<4; j++){
cin>>temp;
switch(j){
case 0:
coordinates[0] = stoi(delimiter(temp));
break;
case 1:
coordinates[1] = stoi(delimiter(temp));
break;
case 2:
coordinates[2] = stoi(delimiter(temp));
break;
case 3:
coordinates[3] = stoi(temp);
break;
}
}
if(i == 0){
graph->V = 2;
graph->E = 2;
graph->edge[0].src = 0;
graph->edge[0].dest = 1;
graph->edge[0].Xll = coordinates[0];
graph->edge[0].Yll = coordinates[1];
graph->edge[0].Xur = coordinates[2];
graph->edge[0].Yur = coordinates[3];
cout<<"Side 1\n";
}
else if(bounded(graph,coordinates,a) == true){
graph->V = a+2;
graph->E = a+2;
graph->edge[a].src = a;
graph->edge[a].dest = a+1;
graph->edge[a].Xll = coordinates[0];
graph->edge[a].Yll = coordinates[1];
graph->edge[a].Xur = coordinates[2];
graph->edge[a].Yur = coordinates[3];
a++;
cout<<"Side 1\n";
}
else if(check == 0){
graph2->V = 2;
graph2->E = 2;
graph2->edge[0].src = 0;
graph2->edge[0].dest = 1;
graph2->edge[0].Xll = coordinates[0];
graph2->edge[0].Yll = coordinates[1];
graph2->edge[0].Xur = coordinates[2];
graph2->edge[0].Yur = coordinates[3];
cout<<"Side 2\n";
check = 1;
}
else if(bounded(graph2,coordinates,b) == true){
graph->V = b+2;
graph->E = b+2;
graph2->edge[b].src = b;
graph2->edge[b].dest = b+1;
graph2->edge[b].Xll = coordinates[0];
graph2->edge[b].Yll = coordinates[1];
graph2->edge[b].Xur = coordinates[2];
graph2->edge[b].Yur = coordinates[3];
b++;
cout<<"Side 2\n";
}
else{
cout<<"None\n";
}
}
}
bool bounded(struct Graph* graph,vector<int> coordinates, int N){
int *parent = (int*) malloc( graph->V * sizeof(int) );
int x = XSpacing - 1;
int y = YSpacing - 1;
int const Length = Dimension[0];
int const Height = Dimension[1];
int Xll = coordinates[0];
int Yll = coordinates[1];
int Xur = coordinates[2];
int Yur = coordinates[3];
memset(parent, -1, sizeof(int) * graph->V);
for(int i = 0; i < graph->E; ++i)
{
int A = find(parent, graph->edge[i].src);
int B = find(parent, graph->edge[i].dest);
if(Xll >= graph->edge[i].Xll-x && Xur <= graph->edge[i].Xur+x)
if(Yll >= graph->edge[i].Yll-y && Yur <= graph->edge[i].Yur+y)
return false;
if((Yll >= graph->edge[i].Yll-y && Yll <= graph->edge[i].Yur+y) || (Yur >= graph->edge[i].Yll-y && Yur <= graph->edge[i].Yur+y)){ //if on the right
if(Xll >= graph->edge[i].Xll-x && Xll <= graph->edge[i].Xur+x)
return false;
if(Xur <= graph->edge[i].Xur+x && Xur >= graph->edge[i].Xll-x)
return false;
}
if((Xll >= graph->edge[i].Xll-x && Xll <= graph->edge[i].Xur+x) || (Xur >= graph->edge[i].Xll-x && Xur <= graph->edge[i].Xur+x)){ //if on the right
if(Yll >= graph->edge[i].Yll-y && Yll <= graph->edge[i].Yur+y)
return false;
if(Yur <= graph->edge[i].Yur+y && Yur >= graph->edge[i].Yll-y)
return false;
}
if(A == B)
return true;
Union(parent, A, B);
}
cout<<endl;
return 0;
}
string delimiter(string temp){
if(temp.back() == ',')
temp.pop_back();
return temp;
}
Input
NumShapes=8
XSpacing=10
YSpacing=2
Dimension=100x30
10, 4, 20, 6
25, 5, 55, 8
10, 8, 15, 12
0, 20, 20, 22
0, 20, 10, 22
0, 10, 5, 15
25, 27, 75, 28
25, 10, 55, 13
Output
SIDE1
SIDE2
SIDE1
SIDE1
SIDE2
SIDE2
SIDE1
SIDE1