Ah good old seg faults.... Anyone getting a seg fault with
Code:
color[u->data]="gray";
in my DFS_Visit function... not sure why... probably overlooking something with these pointers and what not
here's the code:
Code:
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int operationsCounter=0;
int ntime=0, verts, edges;
string* color = new string[1];
int* pi = new int[1];
int* d = new int[1];
int* f = new int[1];
struct Node{
int data;
Node* next;
};
void InsertAfter(Node *head, int value){
if(head==NULL){
Node* temp=NULL;
temp = new Node;
temp->data = value;
temp->next = NULL;
head = temp;
}else{
Node *temp = new Node;
temp->data = value;
temp->next = (head)->next;
(head)->next = temp;
}
}
void DFS_Visit(Node* u)
{
cout << "---in DFS_Visit u=" << endl;
cout << "---" << &u->data << endl;
color[u->data]="gray";
cout << "---color" << endl;
d[u->data]=(++ntime);
cout << "---d" << endl;
int v=u->data;
cout << "---DFS_Visit before while" << endl;
while((u=u->next)!=NULL)
{
cout << "---DFS_Visit in while u=" << u->data << endl;
if (color[u->data]=="white")
{
pi[u->data]=v;
DFS_Visit(u);
}
}
cout << "---DFS_Visit after while" << endl;
color[v]="black";
f[v]=(++ntime);
}
void DFS(Node** G)
{
for (int u=0; u<verts; u++)
{
color[u]="white";
pi[u]=-1;
}
ntime=0;
cout << "---DFS 1" << endl;
for (int u=0; u<verts; u++)
{
if (color[u]=="white")
{
DFS_Visit(G[u]);
}
}
cout << "---DFS 2 done" << endl;
}
void printResults()
{
cout << " d f pi " << endl;
for (int i=0; i<verts; i++)
{
if(i<10)
cout << " " << i;
else if(i<100)
cout << " " << i;
else if(i<1000)
cout << " " << i;
else
cout << " " << i;
if(d[i]<10)
cout << " " << d[i];
else if(d[i]<100)
cout << " " << d[i];
else if(d[i]<1000)
cout << " " << d[i];
else
cout << " " << d[i];
if(f[i]<10)
cout << " " << f[i];
else if(f[i]<100)
cout << " " << f[i];
else if(f[i]<1000)
cout << " " << f[i];
else
cout << " " << f[i];
if(pi[i]<10)
cout << " " << pi[i] << endl;
else if(pi[i]<100)
cout << " " << pi[i] << endl;
else if(pi[i]<1000)
cout << " " << pi[i] << endl;
else
cout << " " << pi[i] << endl;
}
cout << endl << endl;
cout << "Number of operation: " << operationsCounter << endl;
}
bool alreadyInList(Node* llist, int data)
{
cout << "---already in list" << endl;
if(llist==NULL)
return false;
else if(data==llist->data)
return true;
else
return alreadyInList(llist->next, data);
}
Node** makeGraph()
{
int u, v;
u=0;
v=0;
Node** graph = new Node*[5];
for(int i=0; i<5; i++)
{
graph[i]=NULL;
InsertAfter(graph[i], i);
}
for(int i=0; i<edges; i++)
{
while(alreadyInList(graph[u], v))
{
u = rand() % verts;
v = rand() % verts;
}
InsertAfter(graph[u], v);
}
return graph;
}
Node** makeSample()
{
Node** graph = new Node*[5];
int i=0;
while(i<verts)
{
graph[i]=NULL;
InsertAfter(graph[i], i);
i++;
}
InsertAfter(graph[0], 1);
InsertAfter(graph[1], 2);
InsertAfter(graph[1], 3);
InsertAfter(graph[2], 3);
InsertAfter(graph[2], 4);
InsertAfter(graph[4], i);
cout << "---Sample graph made bitches." << endl;
return graph;
}
int main()
{
int option=0;
while (option!=3)
{
cout << "Please select an option: " << endl;
cout << "1) Run DFS on the sample graph" << endl;
cout << "2) Run DFS on a customized random graph" << endl;
cout << "3) Exit" << endl;
cin >> option;
if(option==1)
{
verts=5;
edges=6;
color = new string[verts];
pi = new int[verts];
d = new int[verts];
f = new int[verts];
operationsCounter=0;
DFS(makeSample());
printResults();
}
if(option==2)
{
cout << "How many vertices would you like the graph to have?" << endl;
cin >> verts;
cout << "How many edges would you like the graph to have?" << endl;
cin >> edges;
color = new string[verts];
pi = new int[verts];
d = new int[verts];
f = new int[verts];
operationsCounter=0;
DFS(makeGraph());
printResults();
}
}
cout << "Thank you. Goodbye!";
return 0;
}