通过迭代计算坐标表(COO)稀疏矩阵的希尔伯特曲线

时间:2018-11-17 00:53:20

标签: java sparse-matrix pagerank hilbert-curve space-filling-curve

我正在研究PageRank问题,并且有一个协调列表(Coo)矩阵。矩阵具有一个源数组和目标数组,如下所示。每个来源都指向目的地的同一位置

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 };

我正在尝试对边缘进行预处理,以给出像希尔伯茨(Hilberts)这样的空间填充曲线所要计算的顺序。从(x,y)转换为d再转换为(x,y)时遇到麻烦。

当前代码:

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

关于此代码有什么问题的任何想法?我知道我对全局变量有些过分了

我从here那里获得了代码

编辑

我刚刚了解到数组通过引用进入方法,可以用来删除java中的全局变量。我仍然有相同的结果

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;
    }
}

0 个答案:

没有答案