Im unable to debug

Can someone help me debug my code. There’s something wrong in this Topological sorting code using DFS.

Thanks

Link: https://ide.codingblocks.com/s/224523

What exactly is the issue? Also mention the expected output.

I’m missing a node in my sort output.

I’m getting 4 as a key in my printGraph function,

But not in my topological sort function.

You have to visit each and every node. If you visit only i.first in adjacency list then some nodes will be left out. Problem was due to line 54. You can dry run and check with the adjacency list formed.
Check now:

But i is the map object, so every key in the map object is being visited once. when i use the iterator. Thanks for the working code though. but i dont see why iterator should fail.

Also, the solution is working when I know the numer of nodes. what if I dont know the number of nodes? Thanks

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.