[help] DFS, cant find my mistake...

I have a DFS algorithm here, and when i run the thing and try to find the path, my console stops responding (it crashes), so i would like to ask if anyone can tell me what is wrong?? -I worked on this for hours but cant identify the problem.

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include<iostream>
#include<fstream>
#include <stack>

using namespace std;


void menu(){
     cout << "DFS " << endl;
     cout << "1) Read graph from file" << endl;
     cout << "2) Search: s - d " << endl;     
     cout << "3) EXIT" << endl << endl;
}



struct Vert{
       int name;
       int elder;
       int depth;
       int status;       
};

int size;
int C[30][30];

void read(string f){
  fstream dat(f.c_str(), fstream::in);
  dat >> size;
  int i=0;
  int p, q, c;

  while(!dat.eof())
  {
    dat >> p >> q >> c;
	C[p-1][q-1] = c;
	C[q-1][p-1] = c;
    i++;
  }
  dat.close();
}



int top;
int stacks[30];

void PUSH(int v){
     stacks[top]=v;
     top++;     
}

int POP(int a){
    top--;
    stacks[top]=a;
    return stacks[top];    
}


void DEPTH_SEARCH(int C[30][30], int vel, int s){

     Vert V[30]; 
     
     for(int x=0; x<vel; x++){
             V[s].name=x;
             V[s].depth=99999;
             V[s].elder=-1;
             V[s].status=1;
     }
     s[V].depth=0;
     s[V].elder=-1;
     s[V].status=1;
     
     PUSH(s);
     
     int v;
     while(!(top==0)){
           POP(v);
           
           for(int i=0; i<vel; i++){
                   if(C[v][i] == s  && V[i].status == 0){
                              V[i].depth=V[v].depth+1;
                              V[i].elder=v;
                              V[i].status=1;
                              PUSH(i);
                   }
                   V[v].status=2;   
           }           
     }
       

          
}

void PRINT_PATH(Vert V[30], int s, int d){
     if(s==d){
              cout << "Vert: " << s << ", depth: " << V[s].depth << endl;
                       
     }else{
           if(V[d].elder==-1){
                   cout << "No path. " << endl;                        
           }      
           else{
                PRINT_PATH(V,s,V[d].elder);
                cout << "Vert: " << d << ", depth: " << V[d].depth << endl;     
           }
           
     }
}


int main(){

    int select;
    int s;
    int d;
    int vel;
    Vert V[30];
    
    
    
    do{
         menu();
         cout << "Selection: ";
         cin >> select;
         cout << endl;
         
         switch(select){
                case 1:
                      read("graph.txt");
                      cout << "size: " << size << endl;
                      for(int j=0;j<size;j++){
	                      for(int i=0;i<size;i++){
	                         cout << C[i][j] << " ";
                          }
                          cout << endl;
                       }
                       cout << endl;
                     break;
                     
                case 2:
                     cout << "Enter s: ";
                     cin >> s;
                     cout << endl;
                     cout << "Enter d: ";
                     cin >> d;
                     cout << endl;
                     DEPTH_SEARCH(C, vel, s);
                     PRINT_PATH(V, s, d);
                     break;
                     
                case 3:
                     break;               
         }
         
    }while(select!=3);
   
    system("PAUSE");
    return 0;    
}
closed account (jyU4izwU)
Try
1
2
3
4
5
6
7
8
9
10
11
using namespace std;


void menu(void)
{
     cout << "DFS " << endl;
     cout << "1) Read graph from file" << endl;
     cout << "2) Search: s - d " << endl;     
     cout << "3) EXIT" << endl << endl;
}
no, doesnt work, still gives: '...stopped working' and my programme crashes...

It runs fine, but when i press 2, to call the DEPTH_SEARCH function, it crashes...
_ Uninitialized variables
_ Bad reading (that may cause invalid access). Don't loop on eof

Provide an example run.
Also, perform a backtrace when it crashes
Topic archived. No new replies allowed.