Shortest path finder


Problem

Given a start cell and a destination cell on a grid, find the shortest path from the start cell to the destination cell, if possible. There may or may not be obstacle cells in between the shortest path.


Guide

Initially, a blank grid is displayed as shown below.

start screen

First mouse left click on any cell changes the cell to be the start cell (the cell color changes to orange). The second mouse left click on any cell changes the cell to be the destination cell (the cell color changes to light blue). Second mouse left click on the start cell won't change it to the destination cell. After choosing the start and the destination cell, mouse left clicks can be used to set up obstacle cells (the obstacle cells are assigned black color). Start and destination cells can't be changed to obstacle cells. An example of how the screen would look like after selecting start, destination and obstacle cells is shown below.

start, destination and obstacles placed on the grid

At the bottom of the grid, there are 2 buttons. The left button is to select Dijkstra's single source shortest path algorithm and the right button is to select A star algorithm. White color on a button indicates that particular algorithm is selected. By default Dijkstra's single source shortest path algorithm is selected.

Once the start, destination, obstacle cells and the desired algorithm is selected, press SPACEBAR key to start running the selected algorithm. As the program runs, the cells which are explored are colored green and the cells that are in the min heap are colored red. Shown below is an example of running Dijkstra's single source shortest path algorithm.

Shown below is an example of running A star algorithm.

Start, destination and obstacle cells can be changed to blank cells by using mouse right clicks. Mouse right click on the start or destination or obstacle cell to reset that cell. If you reset the start cell or the destination cell, the next mouse left click on any cell will change the selected cell to be the start or the destination cell. An example shown below.

If you want to reset the grid, that is, change the grid to be a blank grid like the intially screen, press Left CTRL key.

NOTE: Once visualization started, using Left CTRL key, SPACEBAR key, mouse right click and mouse left click won't work until the visualization is completed.

If you want to create a random grid of obstacle cells, press g key. The g key resets the grid and changes every cell to be obstacle cell. Then DFS is used to randomly reset some cells. Then, place the start and destination cell anywhere, select the desired algorithm using the bottom buttons and start the selected algorithm. An example shown below.

Sometimes there can be a situation where there is no path from the start cell to the destination cell. In this case, the algorithm explores all possible cells and stops after all the reachable cells are explored. An example shown below.


Important points


Code

Python code

References