![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2006
Posts: 7
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 134
Rep Power: 4
![]() |
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) |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|