I find a way to implement this calculation using existing software.
First import the shp file into PostgreSQL using the PostGIS plugin.
Secondly, use the ST_Touches function to compute each polygon of adjacent polygons.
Third, take each polygon as a point and build a new network.
Finally, calculate the shortest path between two points using Dijkstra's algorithm.
source share