Search code examples
javagridgain

Translate a recursive algorithm to GridGain


I'm pretty new to the grid world, I need guidance on how to approach an algorithm using GridGain, the algorithm is a recursive TravellingSalesmanProblem.

TSP looks like this:

public int tsp(int hops, byte[] path, int length, int minimum,
        DistanceTable distance) {
    int city, dist, me;
    int NTowns = distance.dist.length;

    // stop searching, this path is too long...
    if (length + distance.lowerBound[NTowns - hops] >= minimum) {
        return minimum;
    }

    if (hops == NTowns) {
        /* Found a full route better than current best route,
         * update minimum. */
        return length;
    }

    /* "path" really is a partial route.
     * Call tsp recursively for each subtree. */
    me = path[hops - 1]; /* Last city of path */

    /* Try all cities that are not on the initial path,
     * in "nearest-city-first" order. */
    for (int i = 0; i < NTowns; i++) {
        city = distance.toCity[me][i];
        if (city != me && !present(city, hops, path)) {
            dist = distance.dist[me][i];
            int min;
            path[hops] = (byte) city;
            min = tsp(hops + 1, path, length + dist, minimum, distance);
            minimum = minimum < min ? minimum : min;         
        }
    }
    return minimum;
}

I believe I need to do an aggregation, like GG's Fibonacci example, the problem is I don't know what to set to the GridFuture, since I have a recursive call within a loop (I believe I can't create as many futures as recursive calls I have, doesn't make sense). I've search for more examples but I couldn't map any to my algorithm.

Basically I need to translate that into GridGain... any suggestion will be greatly appreciated.


Solution

  • There is no problem in creating futures to launch recursive computations. I think you should create as man futures as you have invocations and you can do that in the loop. Have you given that a try?

    GridGain Fibonacci example is exactly the right approach here.