Graph components C/C++

Hello. I want to count the components in a graph, but something is wrong with my C++ program - the output is sometimes right, but sometimes - wrong. Please help. I'm using BFS.
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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector< vector<int> > graph(100);
int nodes;
void get(){
    cin>>nodes;
    for(int i=0; i<nodes; i++){
        int neighb; cin>>neighb;
        for(int j=0; j<neighb; j++){
            int nX; cin>>nX;
            graph[i].push_back(nX);

        }
    }
}
vector<int>  vis(100, 0);
void bfs(int start){
    queue<int> prem;
    prem.push(start);
    while(!prem.empty()){
        int seg=prem.front();
        prem.pop();
        int siz=graph[seg].size();
        for(int i=0; i<siz; i++){
            int neighb=graph[seg][i];
            if(vis[neighb]==0&&neighb!=start){
                prem.push(neighb);
                vis[neighb]=vis[seg]+1;
            }
        }
    }
}
void counter(){
    int comps=0;
    for(int i=0; i<nodes; i++){
        if(vis[i]==0) {
            bfs(i);
            comps++;
        }
    }
    cout<<comps<<'\n';
}
int main()
{
get(); counter();
return 0;
}


In the input, I get the list of neighbours(Adjacency list) - first their number and then them.

Example input:
11
2 4 2
2 6 9
2 0 4
1 5
3 2 0 5
3 8 3 4
1 1
0
2 10 5
1 1
1 8

Graph for the example: https://i.stack.imgur.com/M6U3I.png
Last edited on
So is your test case an example of when it works, or when it doesn't work?

What's the simplest test case you can come up with that doesn't work?

With a really simple failing case, you can start to do things like this.
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
void bfs(int start){
    queue<int> prem;
    prem.push(start);
    while(!prem.empty()){
        int seg=prem.front();
        cout << "Debug:bfs:testing segment=" << seg << endl;
        prem.pop();
        int siz=graph[seg].size();
        for(int i=0; i<siz; i++){
            int neighb=graph[seg][i];
            if(vis[neighb]==0&&neighb!=start){
                cout << "Debug:bfs:new neighbour=" << neighb << endl;
                prem.push(neighb);
                vis[neighb]=vis[seg]+1;
            }
        }
    }
}
void counter(){
    int comps=0;
    for(int i=0; i<nodes; i++){
        if(vis[i]==0) {
            cout << "Debug:counter:testing node=" << i << endl;
            bfs(i);
            comps++;
        }
    }
    cout<<comps<<'\n';
}

You can either add debug cout statements (as above).

Or use a debugger to step through and examine variables without having to keep editing the code.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void get(istream &in){
    in>>nodes;
    for(int i=0; i<nodes; i++){
        int neighb; in>>neighb;
        for(int j=0; j<neighb; j++){
            int nX; in>>nX;
            graph[i].push_back(nX);
        }
    }
}

int main ( ) {
    ifstream inf("test.txt");
    get(inf);
    // get(cin);
}

Also, automatically reading your test file (as opposed to always typing it in) will simplify and speed up your testing.
Topic archived. No new replies allowed.