9.14. Knight’s Tour Analysis¶
There is one last interesting topic regarding the knight’s tour problem,
then we will move on to the general version of the depth first search.
The topic is performance. In particular, knightTour
is very
sensitive to the method you use to select the next vertex to visit. For
example, on a five-by-five board you can produce a path in about 1.5
seconds on a reasonably fast computer. But what happens if you try an
eight-by-eight board? In this case, depending on the speed of your
computer, you may have to wait up to a half hour to get the results! The
reason for this is that the knight’s tour problem as we have implemented
it so far is an exponential algorithm of size

Figure 12: A Search Tree for the Knight’s Tour¶

Figure 13: Number of Possible Moves for Each Square¶
We have already seen that the number of nodes in a binary tree of height
N is
Luckily there is a way to speed up the eight-by-eight case so that it
runs in under one second. In the listing below we show the code that
speeds up the knightTour
. This function (see Listing 4), called orderbyAvail
will be used in place of the call to u.getConnections
in the code previously
shown above. The critical line in the
orderByAvail
function is line 10. This line ensures that we
select the vertex to go next that has the fewest available moves. You
might think this is really counter productive; why not select the node
that has the most available moves? You can try that approach easily by
running the program yourself and inserting the line
resList.reverse()
right after the sort.
The problem with using the vertex with the most available moves as your next vertex on the path is that it tends to have the knight visit the middle squares early on in the tour. When this happens it is easy for the knight to get stranded on one side of the board where it cannot reach unvisited squares on the other side of the board. On the other hand, visiting the squares with the fewest available moves first pushes the knight to visit the squares around the edges of the board first. This ensures that the knight will visit the hard-to-reach corners early and can use the middle squares to hop across the board only when necessary. Utilizing this kind of knowledge to speed up an algorithm is called a heuristic. Humans use heuristics every day to help make decisions, heuristic searches are often used in the field of artificial intelligence. This particular heuristic is called Warnsdorff’s algorithm, named after H. C. Warnsdorff who published his idea in 1823.
The visualization below depicts the full process of a Knight’s Tour solution. It portrays the visitation of every spot on the chess board and an analysis on all neighboring locations, and finishes by showing the final solution wherein all locations on the chess board have been visited. The Warnsdorff heuristic is used in this visualization, so all neighbors with less moves are prioritized over neighbors with more moves. The individual components of the visualization are indicated by color and shape, which is described below.
The currently focused point on the chess board is a black circle.
Neighbors of that point that are being examined are higlighted in blue hexagon.
Distant neighbors are orange or brown triangles.
Triangles are orange if the spot on the chess board has not been visited yet.
Triangles are brown if the spot on the chess board has already been visited.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
Listing 4
1def orderByAvail(n):
2 resList = []
3 for v in n.getConnections():
4 if v.getColor() == 'white':
5 c = 0
6 for w in v.getConnections():
7 if w.getColor() == 'white':
8 c = c + 1
9 resList.append((c,v))
10 resList.sort(key=lambda x: x[0])
11 return [y[1] for y in resList]
Self Check
- O(k^n)
- You are correct! K is a small constant, and N is the total number of vertices (or spaces on a chessboard).
- O(n)
- No, the Knight's Tour is not linear.
- O(n^2)
- No, the Knight's Tour does not have a nested loop that iterates through all values twice.
- O(n!)
- No, the input is not processed in a fashion indicative of a factorial.
Q-2: What is the big O of the Knight’s Tour function?