Skip to main content

Posts

Showing posts with the label JAVA

Graphs - DFS

Its been a while I posted because I had been busy with other stuff.From now on i would be posting basically what I learn and how I implement it in my journey of preparing for the best interviews. Now I am going to implement GRAPH DFS algorithm. Graphs as we all know many people tend to fear as we implement but once you start  realising  the underlying beauty and power of the data-structure, you can not hold yourself implementing it. Graphs can be represented as 2 ways: Adjacency lists or Adjacency matrices. Here I use adjacency list because as i am using java it pretty easy to implement it using Collections framework. DFS is pretty simple : Start with a node timestamp it and go deep and deep until u cannot go anywhere and then timestamp it. Finally you would end up creating a forest of multiple trees(Starting nodes). Code below is self explanatory and if any one has any queries please let me know. And the algorithm is direct implementation of book by Corm...