Recursive search applying functions from starting node

Spread the word!

The problem here is basically the same as in this post, but I wanted to make the implementation more general and modular.
The principle is the same: you have a starting node, for example a numerical value, and you can use a discrete set of functions. Each function takes the first value as parameter and produce an output. Again each output became the new input of each function.

The following tree shows the principle:

tree

Given should be: starting node, searched node, functions list, max number of steps. If the algorithm will not find the searched node/value, then it will stop after the given number of steps. If it finds the node, it stops and don’t go on.

The algorithm implemented in PHP can be divided in two functions.

Let’s see how to use them with an example. We can customized as many functions as we want. Here I just wrote three simple functions to apply:

After this, starting from the number 5 we want to stop at 450.

To display the given “path” of applied functions starting from 5 till 450 we can easily use the following line:

Be the first to comment

Leave a Reply

Your email address will not be published.


*