An algorithm for dynamically adapting neural network topologies