-4

I want to print the level of last node. I am new to STL and Graph Theory so help me out in finding out the level of the last node of graph. Probably done the algorithm correct but still some issues with vectors in STL and suggest me how to print the level of the last node.

Here is my code:

// Sample code to perform I/O:

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

int N,M;
vector <int> adj[100];
bool visited[100];
vector <int> level[100];

void initialize();


int main() {

    cin>>N>>M;
    int x,y;
    while(M--)
    {
        cin>>x>>y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    initialize();
    for(int i=0;i<N;i++)
    {
        if(visited[i] == false)
        {
                list <int> q;
                q.push_back(i);
                while(!q.empty())
                {
                    int p=q.front();
                    q.pop_front();
                    for(int j=0;j<adj[p].size();j++)
                    {
                        if(visited[adj[p][j]] == false)
                        {
                            level[adj[p][j]] = level[adj[p]] + 1;
                            q.push_back(adj[p][j]);
                            visited[adj[p][j]] == true;
                        }
                    }

                }
        }
    }           


}
void initialize()
{
    for(int i=0;i<N;i++)
        visited[i]=false;
}

And my errors are:

In function ‘int main()’: 36:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

40:53: error: no match for ‘operator[]’ (operand types are ‘std::vector [100]’ and ‘std::vector’)

42:48: warning: value computed is not used [-Wunused-value]

Help me out to solve this

Dominique Fortin
  • 2,116
  • 11
  • 20

1 Answers1

0

Ok, I'm not sure what do you want to achieve with this code to be honest. I guess that by 'level' you mean a depth of node. First of all, a bit of theory:

1) Depth/level of a node depends on a starting node. You can't just start from any node, because the result may vary. In your case I suspect that the starting node would be 1 (or 0). But then in your code you assume, that your graph may not contain just one connected component (nodes A and B might not be connected). I'm not sure if you meant it or just found a bfs code on the Internet that works for multiple connected components.

2) A depth of node is rather a term in a tree, not a graph (because of cycles).

3) Ok, what is 'the last node'? Really, is it your last visited node or a node with number n on it? Maybe you mean the deepest node?

I assume you want to find the maximum depth in your tree. Then you can go with a BFS or DFS for sure.

Now let's move to your code:

1) Visited array is not needed if you're using 'level' array. Node with level that equals 0 is just not visited yet, so you can cut down on memory you use by deleting visited array.

2) I'd use queue instead of list for BFS, it's slightly faster.

3) You forgot to write [j] in level[adj[p]]. (WRONG, my fault, you should change it to level[p] instead)

That's all, ignore the warnings.

EDIT: Btw, I guess you're trying to make some graph tasks on some websites. Be carful with your I/O. Cin and cout are usually a lot slower than printf and scanf and may cause your code get a time limit exceeded error (if you like using cin and cout you can write a line 'ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);' at the beginning of yout main function to make it faster, but DO NOT USE printf or scanf then).

Maras
  • 972
  • 7
  • 15
  • I cant write level[adj[p][j]] it will change the whole logic....Level[adj[p][j]]= Level [adj[p]] + 1 means it will increase the level of evry child node[p][j] of that parent p by 1 – Arunava Saha Jul 09 '18 at 05:10
  • Yeah, right, my fault, try level[p] instead, that's what your parent's level is – Maras Jul 09 '18 at 07:27
  • And also be sure to check visited[adj[p][j]] == true;, you want it to be level[adj[p][j]] = true; – Maras Jul 09 '18 at 07:34