0

Please let me know the reason for the error that is coming in the program. I was trying to create a graph class and implement printing of elements using dfs.

The link to the following code is:

#include <bits/stdc++.h>
using namespace std;

class graph {
    public:
        int **adj;
        int *visited;
        int n,e;

        graph(int vert, int edges) {
            e = edges;
            n = vert;
            adj = new int*[n];

            for(int i=0; i<n; i++) {
                adj[i] = new int[n];
                for(int j=0; j<n; j++) {
                    adj[i][j] = 0;
                }
            }
            
            visited = new int[n];
            for(int i=0; i<n; i++) {
                visited[i] = 0;
            }

        }

        void inputAdj() {
            for(int i=0; i<e; i++) {
                int l,r;
                cin>>l>>r;
                adj[l][r] = 1;
                adj[r][l] = 1;
            }
        }

        void DFS(int k) {
            cout<<k<<" ";
            visited[k] = 1;
            for(int i=0; i<n; i++) {
                if(k == i) continue;

                if(adj[k][i] == 1) {
                    if(visited[i]) {
                        continue;
                    }
                }

                DFS(i);
            }
        }
        
        ~graph() {
            for(int i=0; i<n; i++) {
                delete[] adj[i];
            }
            delete[] adj;
            delete[] visited;
        }
};

void printDFS(graph *g, int sz) {
    for(int i=0; i<sz; i++) {
        if(!g->visited[i]) {
            g->DFS(i);
        }
    }
}

int main() {

    int n = 7, e = 6;
    // cin>>n>>e;
    graph g(n,e);

    g.inputAdj();

    // int etf;
    // cin>>etf;

    printDFS(&g, n);

    return 0;
}

the input i was giving was :

0 1
1 2
2 3
3 4
4 0
5 6

The error that i recieved:

run: line 1:     3 File size limit exceeded(core dumped) LD_LIBRARY_PATH=/usr/local/gcc-8.3.0/lib64 ./a.out

I think the cause of this error is some sort of infinite loop that is being created here. I am not being able to locate it. Please helpp!!

0 Answers0