Doubt doubt doubt

Problem with code??.. getting wrong answers.

There is no node=6…but still i am getting it in the output…

Hi Parth,


here this extra node 6 is being shown bcoz initially we made an array of size v and initialized it to zero… now if indegree is 0 we push it in queue… so here we have to make a check if that node is actually present in graph or not… to avoid all this we can simply maintain a map of indegree…
refer this --> https://ide.codingblocks.com/s/300574

Hi… is it clear now??

No…your code also not working. You have given no. of edges as 6…make it 7 and you will get a wrong answer.

Hi Parth,
Graph G(6) 6 over here doesn’t mean no of edges… it basically defines the no of vertices in my graph… and since have i have taken 0 based indexing the vertices in graph come out to be (0,1,2,3,4,5)… hope it clears ur doubt

But the problem with this approach is that if take number greater than 6…i am getting addtional nodes. I am asking how to fix that…?? You said use map…but still same problem.

for that what u can do is store all the nodes through which there is any edge in a set or something and while pushing in queue when u check indegree = 0 u would have to place an additional check if that node is present in the set or not which we used above…

ok…

shall i mark it as resolved now??

yeah…

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.