I learnt about trees and graphs from my textbooks in (Undergraduate level) last year.

But, now I'm doing competitive programming and unable to solve questions involving these concepts.

I fail to understand that a given question needs DFS or BFS for solving. I know these algorithms but when it comes to questions which are kind of problem statement without directly mentioning DFS/BFS to be applied then I'm unable to solve them

I always run away from these questions because I just know the concepts but fail to reproduce them in my code and have just learnt and never coded.

Can anyone help me in suggesting the path that I should follow to learn to solve such questions that appear in competitions programming like Codeforces?

Please, provide any references such as videos or any site for noobies.


In fact you are adressing an important issue: You were learning concepts, but you are unable to transfer the concepts to real world problems. This is a major issue in many higher education scenarios and e.g. my students always complain, that they do not find the answers to my question in the course material. Of course they don't, otherwise I would not have to ask the question - I assume they are capable of reading and writing ;-).

Now how can you come to that next level of understanding? The answer once again is practice! You have to understand the underlying structure of a problem (e.g. use a divide and conquer approach to decompose the problem) and build a solution based on well-known blocks (like DFS / BFS, sorting, ...). The final step is to adapt the algorithm to your data structures or other specific needs of your problem.

You have to do this over and over again to get practice. Often it helps if you are trying to understand how others did if for some problems, and websites like Codeforces can give you good examples of problems.

After some practice, you will have gained a good intuition on how to decompose a specific problem. Especially for trees and graphs it is always helpful to draw your data structures and think about how an algorithm would traverse the data structure.

  • $\begingroup$ Any references for learning the implementation? $\endgroup$ – Ajay Singh Negi Jan 11 at 11:15

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.