I learnt about trees and graphs from my textbooks (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 don't directly mention that DFS/BFS should 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 programming competitions like "Codeforces"?

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


1 Answer 1


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$
    – asn
    Jan 11, 2019 at 11:15
  • $\begingroup$ @jay rosetta code is a good resource. You can google for more. $\endgroup$ Feb 18, 2019 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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