Posts

Showing posts from July, 2017

BFS to solve 'Snake and Ladder' Problem.

Image
Howdy folks! Thank you for visiting my blog once again. 😊 Today, let’s implement another interesting algorithm, that is Snake and Ladder board in Java. Consider below snake & ladder board, fig.1 Problem Statement: In above board, there are 100 cells. Considering player has a full control over a dice he throws. write an algorithm to find out minimum number of throws required to reach to the last cell which is 100 th cell from the 1 st cell. Approach: Let’s convert above board into a graph (Unaware about graph DS? click on mypost ) having 100 nodes. Each node is connected to maximum 6 nodes as dice has maximum six numbers. In between if I have the ladder then the top of ladder node is directly connected to the source node. So, in our board Node -1 is connected to the {2,3,4, 25 ,6,7}. Also, if there is a snake then it will take you to the bottom of it. So, in our board if I’m at node 6 then I can go to node {7,8,9,29,30, 1...

Quick guide about how to implement dijkstra's algorithm in java with example.

Image
Let’s find out when to use Dijkstra’s algorithm and how it can be applied to the real-time problems. Well, we all know that Graph is a data structure in a programming language. which has Nodes or vertices and each node is connected to other node by an edge OR Path having specific weight. Graph can have self-loop too unlike Trees.(Refer my previous post  to find out the differences between Tree and Graph). There are mainly two type of Graphs,          Directed Graphs.          Undirected Graphs. Directed Graph Undirected Graph. Problem Statement: Person X wants to travel from location-A to location-B and there are multiple paths by which these two locations are connected. Each path has different distances and so as the time. but person X is running out of time and he must follow the route with the least cost to reach destination as early as possible. To Make above problem state...