Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 27th, 2006, 3:29 PM   #1
ergawy
Newbie
 
Join Date: Feb 2006
Posts: 7
Rep Power: 0 ergawy is on a distinguished road
urgent help plz

I am trying to make an algorithm to find cycles in an undirected unweighted graph and it works for small cycles but for bigger cycles it produces this runtime error
Unhandeled exception stack overflow
If anyone knows how to solve this problem please tell me and if you have a better algorithm I will be so glad if you send it.
thnaks in advance

here you are the code:

void find_cycle(graph g, int parent, int grand, int source)
{
cout<<"p "<<parent<<endl;
for(int i=0 ; i<g.degree[parent] ; i++)
if(g.edges[parent][i] != grand)
{
cout<<"c "<<g.edges[parent][i]<<endl;
if(g.edges[parent][i] == source)
{
cout<<"cycle"<<endl;
return;
}
find_cycle(g, g.edges[parent][i], parent, source);
}
}

if anyone of you is good in graphs and graph algorithms please I need your advice.I need you to tell me what books should I study and what are the references that are sufficient to me to be good in this aspect but please keep things simple
ergawy is offline   Reply With Quote
Old Feb 27th, 2006, 4:03 PM   #2
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
I understand you are in a hurry, still read the forum rules: use code tags next time.

When a function is called, a few values are pushed on the stack. This means when your cycle is big, it pushes a lot of values on the stack. The solution would be enlarging your stack. This is platform specific, and I don't know on what platform you're working.
Polyphemus_ is offline   Reply With Quote
Old Feb 27th, 2006, 4:19 PM   #3
Kaja Fumei
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 134
Rep Power: 3 Kaja Fumei is on a distinguished road
Or your could save stack space by passing a reference to the graph as opposed to copies of the graph for every recursive call.

Try changing the function header to:
void find_cycle(graph &g, int parent, int grand, int source)
Kaja Fumei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:18 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC