Depth First Search - Won't compile
Jan 13, 2013 at 3:11pm UTC
Hi, I've been trying to implement this Depth First Search graph algorithm using STL, and I get some error from Visual C++ 2012 at compilation.
The code is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include <fstream>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
#define Max 100
#define Fin fstream in("graph.in", ios::in)
#define Fout fstream out("graph.out", ios::out)
int adjacent[Max][Max];
int nodes;
stack<int > st;
vector<int > v;
void Read()
{
Fin;
in >> nodes;
st.push(0);
for (int i = 0; i <= nodes; ++i)
for (int j = 0; j <= nodes; ++j)
in >> adjacent[i][j];
}
void Compute()
{
Fout;
while (!st.empty())
{
int t = st.top();
for (int i = nodes; i >= 0; --i)
{
if ((adjacent[t][i] != 0) && (find(v.begin(),v.end(),i) == v.end()))
st.push(i);
if (find(v.begin(),v.end(),t) == v.end())
v.push_back(t);
else
st.pop();
}
}
for (int i = 0; i < v.size(); ++i)
out << v[i]+1 << " " ;
}
int main(int argc, char **argv)
{
Read();
Compute();
system("pause" );
return 0;
}
And the error says something like this:
Debug Assertion Failed!
Program: C:\Windows\SYSTEM32\MSVCP110D.dll
File: d:\...\vs2012\vc\include\deque
Line: 1497
Expression: deque empty before pop
Any ideas about what might cause this problem ?
Jan 13, 2013 at 3:16pm UTC
Never mind. Found the problem. Sorry for the post.
It was the extra brackets at lines 34 and 41 and the wrong comparison on lines 22 and 23 ( <= instead of < ).
Topic archived. No new replies allowed.