Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   urgent help plz (http://www.programmingforums.org/showthread.php?t=8615)

ergawy Feb 27th, 2006 3:29 PM

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

Polyphemus_ Feb 27th, 2006 4:03 PM

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.

Kaja Fumei Feb 27th, 2006 4:19 PM

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)


All times are GMT -5. The time now is 6:37 PM.

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