The Knight distance problem with PHP

knight distance

First time I faced this during one of my codility tests for a  application. And I failed it. :) Just because I have never heard about such problems before. Anyway, my opinion about codility tests is the same as about writing code on a whiteboard.  A programmer should be able to think, to investigate and solve the problem in the end. You can’t train yourself for every existing problem and their endless variations. It’s true especially for whiteboard coding challenge because you have limited time and can’t use online resources. Stress not compliment process of thinking, it destroys this process. Well, it’s a topic for another post. In this post, I will provide my investigation on a problem and a .

I’m a kind of person who likes to accomplish things. And that’s why I did this investigation later after my test. I will give material step by step as I found it and realized new things.

The Knight distance problem could be described as a problem to find a count of moves which Knight needs to reach a point on a chess board. In our case chess board will be infinite.

The first link which I found was this GitHub repo with a very explanatory readme and even sketches.
https://github.com/Jarosh/Exercises/wiki/Finding-the-shortest-path-as-for-knight–in-the-game-of-chess-on-an-infinite-board

Because of confidence of an author I used this approach for my test. And now I know that this solution is totally wrong. It’s because an author supposed, that Knight can’t move backward, only forward. In this case, his solution is correct. But in the task wasn’t mentioned any restriction on Knight moves directions. This assumption leads to wrong task interpretations. Some GitHub users, as I am, already left an issue in this repo with their thoughts about a wrong solution. Anyway, I will provide a code.

The most pity about that solution is a fact, that I have found the best possible solution during the test and had both on hands. But because of wrong solution confident explanation from an author, I have chosen his version. And only now I have realized how this decision impacts on my life now…

NEXT TWO SOLUTIONS ARE WRONG. BECAUSE OF NO BACKWARD MOVE ASSUMPTION.

And the test code for these “solutions”.

Well, let’s move to something that really works correctly. But too slow.

Next thing which I have tried of course was a recursion. The idea is simple: we need to go deep and found and answer somewhere on the bottom. As a basis, I have used javascript code from this gist.
https://gist.github.com/adamloving/6a4c563b75110c1fade0

The problem here is a performance. Especially with PHP. Javascript is a language which all about functions and callbacks. And it’s optimized for a lot of functions calls. PHP isn’t. Even an author of this gist mentioned, that solution fast at depth 100 and became slow on 1000 depth. In PHP (version 7) this function can’t go deeper than 5 depth with adequate response time. :)
Anyway, I have translated the code into PHP code and will publish it.

And now the best question which could be asked – how we could do the same, but better? The answer as simple as Dijkstra and a BFS one.

Introducing BFS algorithm.

I haven’t tried Dijkstra but was very interested in BFS. First of all, I want to mention that I have never touched such algorithms before. Maybe only in University, but I don’t remember anything. So, I found BFS description. Then I found simple implementation with PHP in this git repository.
https://github.com/lextoumbourou/bfs-php

And, actually, I found a solution based on C++ and BFS.
http://www.techiedelight.com/chess-knight-problem-find-shortest-path-source-destination/

The problem here is the same as with recursion. Probably BFS could help to search deeply when it runs with C++, but not with PHP. PHP implementation became slow from positions like 7,7 and 8,8. It’s easy could be explained because of nature of BFS algorithm. As deeper we go, as many function calls, we have. It’s like a tree with all possible variants which we should call an check.

The code of BFS solution with PHP is here.

Looks great. But useless with PHP.

 

And the winner is …

An analytical function!

http://stackoverflow.com/questions/2339101/knights-shortest-path-chess-question/41704071#41704071

As I mentioned above I had the link to StackOverflow answer from the beginning. Just lack of experience and knowledge about such problems led me to choose a different solution (and also a confidence of an author of the wrong solution). In answers to this question was mentioned Dijkstra algorithm and a BFS. But I have recognized it’s later.

But the fastest solution with O(1) complexity is an analytical function which was proposed as a solution on SACO 2007 Day 1. It’s really fast and based on a formula described here.

The PHP code for this one is:

A commented out version is a wrong interpretation of a formula.

And yes, it works for 20,20 or for any point on an infinite board in just ms. The base assumptions here to get this formula are:
– x and y are symmetrical.
– we could convert all x,y in symmetrical one
– then we could use delta = x – y in a formula and all that we need consider is a position of y relative to this delta. so we have two possible cases when y is bigger or less the delta.

In the end, it’s easy to get into this formula. It requires just analytical skills and good math basis.

That’s it!

 


And again I want to put stress again on a fact, that sharing bad, not working or not researched to the end solutions is a very bad idea. Such solution when published could make a great impact on somebody life. It will not help, but otherwise, it could harm somebody career and change life decisions. In my case, it was a decision to work remotely or relocate to another country from Russia. And because I failed the test with a wrong solution I haven’t other options that start looking for a relocation job. And I have found it.

If I would send the second one solution, which is right and fastest, that probably I would continue work remotely. It’s a life, any small thing could have a huge influence of how and in which direction it would go. We just can’t, haven’t time and resources, to check all possible variants as in BFS algorithm.