[Introduction to Algorithm] Find the shortest path [Python3]

theme

Find a route that connects the start point and the goal point at the lowest cost on a map that has terrain cost as information. (For example, in SRPG games, terrain costs are taken into account when moving to flatlands, houses, forests, and mountains.)

algorithm

  1. Prepare an array corresponding to the coordinates and terrain cost (cost_map)
  2. Find the cost of moving to each point starting from the goal point and create an array (rout_cost)
  3. Based on the movement cost (rout_cost) of each point, follow the point with the lowest cost from the starting point.

Note) Supplement (Technique used when writing a program) Use a recursive function.

Practice

  1. For example, consider the shortest path from the start (x3, y0) to the goal (x1, y6) on a map with the following terrain cost.
y0 y1 y2 y3 y4 y5 y6
x0 4 3 1 1 1 2 3
x1 3 2 1 2 1 1 1
x2 2 1 3 3 2 3 2
x3 2 3 5 3 2 4 3

Prepare an array cost_map with terrain cost as an element and an array rout_cost with an infinite value as an element.

alg_01.py


s = [3,0]; g = [1,6]  ## start and goal position
cost_map = [[4,3,1,1,1,2,3],\
            [3,2,1,2,1,1,1],\
            [2,1,3,3,2,3,2],\
            [2,3,5,3,2,4,3]]
m = len(cost_map)
n = len(cost_map[0])
rout_cost = [[float("inf") for j in range(n)] for i in range(m)]
  1. Calculate the movement cost to each point starting from the goal point, and update the rout_cost prepared above based on the calculation result.

alg_02.py


def calc_rout_cost(x,y,cost_tmp):
    cost_tmp += cost_map[x][y]
    if cost_tmp < rout_cost[x][y]:
        rout_cost[x][y] = cost_tmp
        if y < n-1:
            calc_rout_cost(x,y+1,cost_tmp)
        if y > 0:
            calc_rout_cost(x,y-1,cost_tmp)
        if x < m-1:
            calc_rout_cost(x+1,y,cost_tmp)
        if x > 0:
            calc_rout_cost(x-1,y,cost_tmp)
calc_rout_cost(g[0],g[1],-cost_map[g[0]][g[1]])

The completed rout_cost is as follows. Note that the goal point (x6, y1) will be zero.

x0 x1 x2 x3 x4 x5 x6
y0 12 8 5 4 3 3 3
y1 10 7 5 4 2 1 0
y2 10 8 8 7 4 4 2
y3 12 11 13 9 6 8 5
  1. Finally, find the shortest route from the start to the goal.

alg_03.py


def search_rout(x,y):
    tmp = rout_cost[x][y]
    tmp_x, tmp_y = x, y
    if x > 0 and rout_cost[x-1][y] < tmp :
        tmp_x, tmp_y = x-1, y
        tmp = rout_cost[tmp_x][tmp_y]
    if x < m-1 and rout_cost[x+1][y] < tmp :
        tmp_x, tmp_y = x+1, y
        tmp = rout_cost[tmp_x][tmp_y]
    if y > 0 and rout_cost[x][y-1] < tmp :
        tmp_x, tmp_y = x, y-1
        tmp = rout_cost[tmp_x][tmp_y]
    if y < n-1 and rout_cost[x][y+1] < tmp :
        tmp_x, tmp_y = x, y+1
        tmp = rout_cost[tmp_x][tmp_y]
    if rout_cost[x][y] == 0: return;
    rout_list.append([tmp_x,tmp_y])
    search_rout(tmp_x,tmp_y)

rout_list = []
rout_list.append(s)
search_rout(s[0],s[1])

The shortest path is

result.py


print(rout_list)
## [[3, 0], [2, 0], [2, 1], [1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6]]

Will be.

References

Masakazu Yanai "Code Puzzle for Programmers" Gijutsu-Hyoronsha (2014) Please refer to the literature for a detailed explanation. It is written in Javascript.

Recommended Posts

[Introduction to Algorithm] Find the shortest path [Python3]
Find the shortest path with the Python Dijkstra's algorithm
Try to solve the shortest path with Python + NetworkX + social data
[What is an algorithm? Introduction to Search Algorithm] ~ Python ~
Find the maximum Python
Introduction to Python Let's prepare the development environment
Introduction to Python language
Introduction to OpenCV (python)-(2)
[Introduction to Python3 Day 20] Chapter 9 Unraveling the Web (9.1-9.4)
[Algorithm x Python] How to use the list
Introduction to Python with Atom (on the way)
[Introduction to Python] How to iterate with the range function?
The first algorithm to learn with Python: FizzBuzz problem
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
[Introduction to Udemy Python3 + Application] 30. How to use the set
[Introduction to Python] Basic usage of the library matplotlib
Introduction to dictionary lookup algorithm
Introduction to Python Django (2) Win
Find the difference in Python
Introduction to serial communication [Python]
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)
An introduction to Python Programming
Introduction to Python For, While
Find the maximum python (improved)
I tried to find the entropy of the image with python
Write a python program to find the editing distance [python] [Levenshtein distance]
Introduction to Python scikit-learn, matplotlib, single-layer algorithm (~ towards B3 ~ part3)
[Introduction to Python] How to get data with the listdir function
[Introduction to Udemy Python 3 + Application] 58. Lambda
[Introduction to Udemy Python 3 + Application] 31. Comments
Leave the troublesome processing to Python
[Python] Find the second smallest value.
Introduction to Python Numerical Library NumPy
Practice! !! Introduction to Python (Type Hints)
[Introduction to Python3 Day 1] Programming and Python
[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]
Introduction to Python Hands On Part 1
Get the desktop path in Python
[Introduction to Python] How to parse JSON
[Introduction to Udemy Python 3 + Application] 56. Closure
Get the script path in Python
[Introduction to Python3 Day 14] Chapter 7 Strings (7.1.1.1 to 7.1.1.4)
How to get the Python version
Introduction to Protobuf-c (C language ⇔ Python)
[Introduction to Udemy Python3 + Application] 59. Generator
[Python] How to import the library
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)
Redis isn't just the shortest introduction (4), Redis
Get the desktop path in Python
[Introduction to Python] Let's use pandas
Cython to try in the shortest
[Introduction to Udemy Python 3 + Application] Summary
Introduction to image analysis opencv python
[Introduction to Python] Let's use pandas
An introduction to Python for non-engineers
python beginners tried to find out
Introduction to Python Django (2) Mac Edition
[AWS SAM] Introduction to Python version
[Introduction to Python3 Day 21] Chapter 10 System (10.1 to 10.5)
[Python] Change the alphabet to numbers