Plain simple C++ code returns error

Hi guys. This is my biggest struggle ever. C++ has been constantly defying me for the last couple of days. I have written a code that performs a depth-based search on a given undirected graph, yet it does very things behind my back.

Example:
5 6
1 2 5
1 3 7
1 5 10
2 3 6
3 4 5
3 5 14

The code modifies the adjacency list of G[3] without me actually telling it to do so. I don't even know what to do here. Any opinion is highly regarded!!!

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
#include <fstream>
#include <vector>
using namespace std;

ifstream fin("data.in");
ofstream fout("data.out");

const int NMAX=810;
int n, m;

typedef pair<int,int> Pair;

vector<Pair> G[NMAX];
vector<Pair>::iterator it;

int to;
bool S[NMAX];

void DFS(int x) {
    S[x] = true; //we mark the fact the we have visited the current node
    for (it=G[x].begin(); it!=G[x].end(); it++) // we go through the current node's neighbors
    {
        to=it->first; // "to" is the current neighbor

        if(S[to]==false) //if we have not visited the neighbor before
            DFS(to); //we visit it by passing "to" by value
    }
}

int main()
{
    fin >> n >> m;

    for (int i = 1, x, y, w; i <= m; i++)
    {
        fin >> x >> y >> w;
        G[x].push_back(make_pair(y, w)); 
        //We store x's neighbours and the distance to them accordingly to the input. 
        //Example: G[1]: (2,5) (3,7) (5,10)
        G[y].push_back(make_pair(x, w));
        //We store y's neighbours and the distance to them accordingly to the input. We do this to y too because the graph is undirected.
    }

    DFS(1); //depth-first search from node 1

    return 0;
}
Last edited on
The code modifies the adjacency list of G[3] without me actually telling it to do so.


On line 38, x is sometimes 3. This code modifies the adjacency list of G[3] when x is 3.
On line 39, y is sometimes 3. This code modifies the adjacency list of G[3] when y is 3.

So, how are you not modifying the adjacency list of G[3]? Your program produces no output. What do you observe happening that you believe should not?

Why are nearly all of your variables global? Why do you have variables that are not used? Why do you have unused typedefs? Why do you not check to see if your reads were successful?

I'm sorry, I may have been quite laconic. Let me answer your questions:

So, how are you not modifying the adjacency list of G[3]?

I meant to say that the adjacency list of G[3] is not intentionally modified outside the main function.

Your program produces no output. What do you observe happening that you believe should not?

My program produces no output because I found no reason to produce output since the return code of main is not zero. The program does not work.

Why are nearly all of your variables global?

Because I wanted them so.

Why do you have variables that are not used?

Only one variable was unused due to the fact that the initial program was much larger and I had to strip its code down to its core in order to put the problem in a more obvious fashion. All variables are used now.

Why do you have unused typedefs?

The initial program was much larger and I had to strip its code down to its core in order to put the problem in a more obvious fashion. Typedefs are used now.

Why do you not check to see if your reads were successful?

I did. Reads are successful.
Last edited on
I have other questions, if it doesn’t mind you.
What should your 21th row do?
for (it=G[x].begin(); it!=G[x].end(); it++)
Should it start the for loop from the x-th element of G?
Should DFS() recursively call itself passing “to”? Is “cost” used somewhere? The row 27 could also be written
DFS(to);
?
What are the rows 38 and 39 supposed to do?
1
2
G[x].push_back(make_pair(y, w));
G[y].push_back(make_pair(x, w));

Should they insert the new pairs in a place different from the end of the vector?

Sorry, but your code is not easy to understand for me.
My bad, let me add comments!
I'm going to add my 2 cents to this:
My program produces no output because I found no reason to produce output since the return code of main is not zero. The program does not work.


I understand that you don't need output, but when trouble shooting a problem it's often helpful to create output every time you input/change/modify something to make sure that your changing or updating what you think is being changed and verify the results are also what you expect.

adding comments such as what the program does, what the input and output should look like, the loops and calculations just make it that much easier to troubleshoot an issue.
Thanks SamuelAdams, Enoizat, cire! It does work now, I have no idea why. No modifications have been done. Maybe I had to restart Codeblocks for some reason. Maybe it was because I restarted Windows. Maybe I am awfully wrong. I may never find out though.
Topic archived. No new replies allowed.