There is a class that reads edges (in the (2, 4) format) and solves the problem of strongly connected components. Everything works fine. But when the edges are not 19, for example, 10,000 a stack overflow occurs due to the DFSR function

void Graph::DFSR(std::vector<std::pair<int, int>> G, int vertex) { m_ReadyTest.push_back(vertex); // V как протестированная // для каждого ребра (s, v) в G for (auto i : G) { if (vertex == i.first) { bool go(false); // если V еще не протестирован for_each(m_ReadyTest.begin(), m_ReadyTest.end(), [=, &go] (int x) { if (x == i.second) go = true; }); if (!go) { // DFSR(G, V) DFSR(G, i.second); } } } ++m_time; auto tmpProcT = make_pair(vertex, m_time); // t = t + 1 m_ProcessingTime.push_back(tmpProcT); // f[V] = t время завершения обработки вершины V } 

Actually, as I did not try to rewrite the function without recursion (I used the + stack cycle) ... it crashes, it does not correctly count. In general, I need your help.

ps or as an option. Explain in detail how to increase the stack in VisualStudio 15 pss thanks in advance!

Here is my option:

 void Graph::DFS(vector<pair<int, int>> G, int vertex) { /*1*/ m_ReadyTest.push_back(vertex); // V как протестированная /*2*/ stack<int> iStack; iStack.push(vertex); // V ложим на вершину стека /*3*/ while (!iStack.empty()) // пока стек НЕ пустой { /*4*/ int iU = iStack.top(); for (auto i : G) // ищим все V для U { if (iU == i.first) // i.second -- V { /*5*/ bool go(false); // если V еще не протестирован for_each(m_ReadyTest.begin(), m_ReadyTest.end(), [=, &go](int x) { if (x == i.second) // V протестирован go = true; }); if (!go) { /*6*/ m_ReadyTest.push_back(i.second); // V как протестировану /*7*/ iStack.push(i.second); // V в голову стеку } } }// вершина обработана... if (!iStack.empty() && iU == iStack.top()) { iStack.pop();// удаляем U из стека ++m_time; auto tmpProcT = make_pair(iU, m_time); // t = t + 1 m_ProcessingTime.push_back(tmpProcT); // f[V] = t время завершения обработки вершины V } } } 

The results of small tests are the same, the test result for 55k edges: The result of the last test

I'm afraid that when solving a problem, and there are 5105043 edges (as far as I remember), the result will be very bad. ps Though kill, I do not understand what else is wrong with my DFS function.

  • one
    I think you have one way out - it’s still to finish off the version without recursion ... and to increase the stack in visual studio is already unnecessary perversion. - ampawd
  • Yeah, join. Also show your attempt with a loop and a stack. - VladD
  • Added my DFS. - Andrey
  • From the test input, it works correctly with 2/3. In 1/3 there is just a looping. - Andrey
  • int iU = iStack.top(); : in any case, you need to remove the top from the stack as you pass it. And for the tested vertices, get better unordered_set . And local, and then suddenly there will be something from the past run. - VladD

2 answers 2

You can increase the stack like this: Project properties → C / C ++ → Command line and enter /F %количество_байт% in the Additional parameters field, that is:

 /F 20971520 – это стек в 20 Мб 

Here is an article about this on MSDN, there are other ways to increase the stack.

PS it is better to rewrite the function, of course, but to do this, show how you tried to do it and where it crashed, etc.

     void Graph::DFSR(std::vector<std::pair<int, int>> &G, int vertex) 
    • That it does not matter at all in this case. - Andrey
    • one
      You can calculate what is stored on your stack frame for each call - for (auto i : G) : at least 8 bytes can be rewritten to for (const auto &i : G) plus a vertex and a link to the graph and that 20 bytes plus minus - to 1000 peaks exactly enough. The original code without a link is at least slower plus adds the vector members to each frame - and why? - ugene
    • In the fifth test data file there are almost 55k graphs. Well, it's not hard for me to check. Just the essence of the job is just that it would not allow to perform it in a standard way. By this I did not immediately bother. - Andrey
    • overflow error still crashes - Andrey
    • By the way, in the file with the data for which you need to give the result (not test) - 5105043 graph. Just now I saw. - Andrey