1

I am working on the PageRank problem and have a Coordinated list (Coo) matrix. The matrix has a source and destination array as seen below. Every source points at the destination at the same position

int[] destination = new int[] { 5, 0, 5, 0, 5, 0, 5, 0, 2, 3, 5, 0, 3, 4 };
int[] source = new int[] { 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 5 };

I am trying to pre-process the edges to give the order that a space-filling curve like Hilberts would calculate. I am having some trouble when converting converting from (x,y) to d back to (x,y).

Current Code:

public static void main(String[] args) {
    int n = 2;
    int[] destination = new int[] { 5, 0, 5, 0, 5, 0, 5, 0, 2, 3, 5, 0, 3, 4 };
    int[] source = new int[] { 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 5 };
    int[] d = new int[destination.length];

    for (int i = 0; i < destination.length; i++) {
        x = source[i];
        y = destination[i];

        d[i] = xy2d(n);
    }

    x = y = 0;
    for (int z = 0; z < 4; z++) {
        System.out.println("Grid:" + x);
        for (int i = 0; i < d.length; i++) {
            if (d[i] == z) {
                d2xy(n, d[i]);

                System.out.println("source: " + source[i] + " dest:" + destination[i] + " --> d:" + d[i]
                        + "--> x:" + x + ":y:" + y);

            }
        }
    }
    System.out.println();
}

static int x, y, rx, ry, s, t, d;

// convert (x,y) to d
static int xy2d(int n) {
    d = 0;
    for (s = n / 2; s > 0; s /= 2) {
        rx = (x & s) >> 0;
        ry = (y & s) >> 0;
        d += s * s * ((3 * rx) ^ ry);
        rot(s);
    }
    return d;
}

// convert d to (x,y)
static void d2xy(int n, int d) {
    t = d;
    x = y = 0;
    for (s = 1; s < n; s *= 2) {
        rx = 1 & (t / 2);
        ry = 1 & (t ^ rx);
        rot(n);
        x += s * rx;
        y += s * ry;
        t /= 4;
    }
}

// rotate/flip a quadrant appropriately
static void rot(int n) {
    if (ry == 0) {
        if (rx == 1) {
            x = n - 1 - x;
            y = n - 1 - y;
        }

        // Swap x and y
        int t = x;
        x = y;
        y = t;
    }
}

Code Results

Any ideas on what is wrong with this code? I know I have went a little over board with global variables

I got the code from here

EDIT

I have just learnt that arrays go into a method by reference and can be used to remove the global variables in java. I still have the same results

public static void main(String[] args) {
    int n = 2;
    int[] destination = new int[] { 5, 0, 5, 0, 5, 0, 5, 0, 2, 3, 5, 0, 3, 4 };
    int[] source = new int[] { 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 5 };
    int[] d = new int[destination.length];
    for (int i = 0; i < destination.length; i++) {
        int[] values = new int[] { source[i], destination[i] };
        d[i] = xy2d(n, values);
    }

    for (int z = 0; z < n * n; z++) {
        for (int i = 0; i < d.length; i++) {
            if (d[i] == z) {
                int[] values = new int[2];
                d2xy(n, d[i], values);
                System.out.println("source: " + source[i] + " dest:" + destination[i] + " --. d:" + d[i] + "--> x: "
                        + values[0] + " y:" + values[1]);
            }
        }
    }

}

// convert (x,y) to d
static int xy2d(int n, int[] values) {
    int rx, ry, s, d = 0;
    for (s = n / 2; s > 0; s /= 2) {
        rx = (values[0] & s) >> 0;
        ry = (values[1] & s) >> 0;
        d += s * s * ((3 * rx) ^ ry);
        rot(s, values, rx, ry);
    }
    return d;
}

// convert d to (x,y)
static void d2xy(int n, int d, int[] values) {
    int rx, ry, s, t = d;
    values[0] = 0;
    values[1] = 0;
    for (s = 1; s < n; s *= 2) {
        rx = 1 & (t / 2);
        ry = 1 & (t ^ rx);
        rot(s, values, rx, ry);
        values[0] += s * rx;
        values[1] += s * ry;
        t /= 4;
    }
}

// rotate/flip a quadrant appropriately
static void rot(int n, int[] values, int rx, int ry) {
    if (ry == 0) {
        if (rx == 1) {
            values[0] = n - 1 - values[0];
            values[1] = n - 1 - values[1];
        }

        // Swap x and y
        int t = values[0];
        values[0] = values[1];
        values[1] = t;
    }
}
  • In xy2d I don't think you want the int in front of variables x, y, rx, ry, s, t, d. This makes local variables, so when you call rot it doesn't use values ry and rx that result from xy2d. Not sure if that is intended or not. – Justin Nov 17 '18 at 01:13
  • @Justin I just removed that and have the same results – Martin O'Donnell Nov 17 '18 at 09:56

0 Answers0