please help me!!!(graphs and trees)

hello nowadays i am having a rough time learning graph because i face difficulty understanding adjacency matrix and list.I don’t understand them very well.First help expecting from you all

1)am i the only one who faces this difficulty in these topics

2)can you drop a link to your dfs & bfs(or any other graph algorithm)implementation so that i can look upon it and try to implement them.(this will teach different ways to do same thing)

3)anyway to learn implementation effectively except geeksforgeeks(this website is good but i barely understand them :frowning: )

PS: i have watched many video lectures but they don’t teach implementation.my main point of concern is that i don’t get good implementation source.

I faced difficulty understanding graph(/trees) and everybody does.

you can see my solution to this problem. Its a very basic tree (or graph) problem.

For implementation of graph, best way is to see others solutions for graph problems from codechef and codeforces.

I had the same problem a few months ago and I posted the same question here. I got many useful resources. Among them, my personal favorite is e-maxx.ru. This is a Russian web site, so use google translator.

A few pointers before you go there.

  1. First learn adjacency matrix for undirected graph. It is a bit easier and easy to visualize as it considers all possibilities.

  2. Implement breadth first search and depth first search on adjacency matrix and solve a few easy problems on it.

  3. Most graph searching problems rely on connected components which is just a simple extension of regular traversal. (Ask if you are having trouble with it after learning dfs and bfs).

  4. After that, learn a few problems that are basically just a bit extension of the bfs and dfs like connected components and few others(I can’t recall right now, sorry).

  5. Then learn these above for directed graph and try to understand topological sort, finding articulation points, bridges etc. (e-maxx.ru).

  6. All the above will work if number of vertices are small. If not, you need adjacency matrix. So learn it now.

  7. If you still have problem, just try to implement and then ask with your code. It is easier to understand when a fault is found in your own code, rather in some one else’s code.

p.s. BFS requires a queue. You can implement it easily. But use STL as it provides list which is easy to use and is less prone to errors.

1 Like

hey i am in the same boat :smiley: but yeah i think i might be able to help you a bit.yo might google codechef tags and there you can find questions taged with bfs or dfs or whatever you want.as per implementation part i would recomend doing those question and see their editorial.i am posting a editorial of one good question as per implementation part in this editorial even code is given do see it as you will learn a lot from it.you might also see these tutorials




you might also search quora for this there are really really good answers for any coding problem.
this is the editorial which i was saying

a link for the tagged problems of bfs
http://www.codechef.com/tags/problems/bfs do some questions from here and do those which have more submission so it is to see the understandable code from others.
hope this help as i said earlier i am in the same boat :smiley:

Hi @sneha6966.

Yes you are right. For beginners ( and sometimes for experienced coders also ) trees and graph theories are difficult to understand and comprehend.

  1. No, I’m sure you are not the only one facing such difficulty.
  2. Check this and this. Also go through the wiki articles about these topics and search online for codes/help but do not implement blindly. Understand/modify /alter to suit your needs. Go through the links in other answers. @dragonemperor provided some very good basic steps for your problem.
  3. Geekforgeeks is a good site but if you are facing difficulty then ask for help there or go through different sites. Read books on Algorithm by Cormen or any other author (as you like) and go through tutorials, editorials on codechef, topcoder, codeforces, spoj.
  4. Always practice what you have learnt. Implement your logic into code and evaluate them. Test your code in various graph/tree problems available on codechef ( or other places) and check if your implementations are correct. If wrong, try again. If TLE then use efficient approach. Practice is the key to understand such problems ( actually all problems) .

Enjoy.

thanks…can you give a link to your dfs and bfs code along with problem link it will help me a lot…

thanks…can you give a link to your dfs and bfs code along with problem link it will help me a lot…

Try this problem and check out my solution</>

Also This problem and this solution.

Both problems are similar and use BFS.