Base code for running of the algorithm