This is simple cleaning robot algorithm written in Python. It assumes the following:
The goal of the algorithm is to make the robot travel around room, with any point in the room visited at least one.
The algorithm (sweeper.py) works as follow:
# matrix is a 2d array, with 0 indicating unobstructed, and anything else indicating obstructed
robot = Robot(matrix, start_position, start_direction)
sweeper = Sweeper(robot)
sweeper.sweep()
Theoretically, the time complexity of the algorithm is O(N2). It need to find at most N unvisited points, and each needs at most N steps to get there. But in practice, since it only find the nearest unvisited position, so it can efficiently do it in O(N).
The space complexity is O(N), to store the observed map.
An optimized version of the algorithm, called Spiral BFS, can help to reduce the number of steps taken by 5-10%. When visualizing the algorithm, I found out that the robot occasionally misses the uncleaned part (since it favors moving in one absolute direction), and needs to waste time traveling back. By making the robot favoring turning left (or right), it will minimize the chance of missing uncleaned parts. Two modes can be switched easily by setting spiral
attribute of the algorithm
sweeper.spiral = True
A demonstration can be found in https://dungba88.github.io/cleaner_robot/showdown/
The code for the demonstration is placed under showdown/
folder. It will compare the efficiency of different algorithms in randomly generated matrices.