Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-10-2008, 02:25 AM
JJJIrish05 JJJIrish05 is offline
Newbie
 
Join Date: Mar 2008
Posts: 10
Rep Power: 0
JJJIrish05 is on a distinguished road
Default DFS - seg fault problem

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;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Peculiar UI Problem Needs Tackling adriyel C# Programming 2 04-06-2008 07:46 AM
Problem read pwd protected Access2K dbase - CR9 & VB6 mrbar Visual Basic Programming 2 03-10-2008 04:50 AM
How to tackle a programming problem? TcM General Programming 10 01-07-2008 11:29 AM
i have a problem please help me!!!???? stack Java Help 8 09-22-2007 03:17 PM
[C] Comparison problem Alhazred C and C++ 1 08-29-2007 04:58 AM


All times are GMT -5. The time now is 10:30 AM.

Contest Stats

Xav ........ 164.00000
dargueta ........ 128.00000
John ........ 127.00000
gaylo565 ........ 18.00000
XaNaX ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

Ads