java - 弗洛伊德-沃歇尔算法

标签 java algorithm path-finding floyd-warshall

我目前正在用 Java 开发一个小型的塔防项目,但遇到了寻路问题。 我读了很多关于 A* dijkstra 和类似的东西,但我认为使用 Floyd-Warshall 进行寻路可能是最好的(至少在我看来它解决了所有对最短路径问题)。

无论如何,我尝试自己实现它,但它并没有完全按预期工作。 我使用维基百科上的代码作为开始 http://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm

所以这是我的代码:

public class MyMap {
public class MyMapNode {
    // list of neighbour nodes
    public List<MyMapNode> neighbours;
    // Currently no need for this :)
    public int cellX, cellY;
    // NodeIndex for pathreconstruction
    public int index;
    // this value is checked by units on map to decide wether path needs
    // reconstruction
    public boolean isFree = true;

    public MyMapNode(int cellX, int cellY, int index) {
        this.cellX = cellX;
        this.cellY = cellY;
        this.index = index;
        neighbours = new ArrayList<MyMapNode>();
    }

    public void addNeighbour(MyMapNode neighbour) {
        neighbours.add(neighbour);
    }

    public void removeNeighbour(MyMapNode neighbour) {
        neighbours.remove(neighbour);
    }

    public boolean isNeighbour(MyMapNode node) {
        return neighbours.contains(node);
    }
}
//MapSize
public static final int CELLS_X = 10;
public static final int CELLS_Y = 10;

public MyMapNode[][] map;

public MyMap() {
    //Fill Map with Nodes
    map = new MyMapNode[CELLS_X][CELLS_Y];
    for (int i = 0; i < CELLS_X; i++) {
        for (int j = 0; j < CELLS_Y; j++) {
            map[i][j] = new MyMapNode(i, j, j + i * CELLS_Y);
        }
    }
    //-------------------------------------------------
    initNeighbours();
    recalculatePath();
}

public void initNeighbours() {
    //init neighbourhood without diagonals
    for (int i = 0; i < CELLS_X; i++) {
        for (int j = 0; j < CELLS_Y; j++) {
            int x, y;// untere Grenze
            if (j == 0)
                y = 0;
            else
                y = -1;
            if (i == 0)
                x = 0;
            else
                x = -1;
            int v, w;// obere Grenze
            if (j == CELLS_Y - 1)
                w = 0;
            else
                w = 1;
            if (i == CELLS_X - 1)
                v = 0;
            else
                v = 1;
            for (int h = x; h <= v; h++) {
                if (h != 0)
                    map[i][j].addNeighbour(map[i + h][j]);
            }
            for (int g = y; g <= w; g++) {
                if (g != 0)
                    map[i][j].addNeighbour(map[i][j + g]);
            }

        }
    }
}
//AdjacencyMatrix
public int[][] path = new int[CELLS_X * CELLS_Y][CELLS_X * CELLS_Y];
//for pathreconstruction
public MyMapNode[][] next = new MyMapNode[CELLS_X * CELLS_Y][CELLS_X
        * CELLS_Y];

public void initAdjacency() {
    for (int i = 0; i < map.length; i++) {
        for (int j = 0; j < map[0].length; j++) {
            path[i][j] = 1000;
            List<MyMapNode> tmp = map[i][j].neighbours;
            for (MyMapNode m : tmp) {
                path[m.index][map[i][j].index] = 1;
                path[map[i][j].index][m.index] = 1;
            }
        }
    }
}

public void floydWarshall() {
    int n = CELLS_X * CELLS_Y;
    for (int k = 0; k < n; k++) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (path[i][k] + path[k][j] < path[i][j]) {
                    path[i][j] = path[i][k] + path[k][j];
                    next[i][j] = getNodeWithIndex(k);
                }
            }
        }
    }
}

public void recalculatePath() {
    initAdjacency();
    floydWarshall();
}

public MyMapNode getNextWayPoint(MyMapNode i, MyMapNode j) {
    if (path[i.index][j.index] >=1000)
        return null;
    MyMapNode intermediate = next[i.index][j.index];
    if (intermediate == null)
        return j; /* there is an edge from i to j, with no vertices between */
    else
        return getNextWayPoint(i, intermediate);
}

public MyMapNode getNodeWithIndex(int k) {
    //for testing purpose,this can be done faster
    for (int i = 0; i < map.length; i++) {
        for (int j = 0; j < map[0].length; j++) {
            if (map[i][j].index == k)
                return map[i][j];
        }
    }
    return null;
}

public void removeMapNode(MyMapNode m) {
    //for testing purpose,this can be done faster
    m.isFree = false;
    for (int i = 0; i < map.length; i++) {
        for (int j = 0; j < map[0].length; j++) {
            if (map[i][j].neighbours.contains(m)) {
                map[i][j].neighbours.remove(m);
            }
        }
    }
}

Floyd-Warshall 算法旨在处理图,因此我创建了一个每个节点都知道其邻居(即它所连接的节点)的算法。

实际上我现在不知道哪里出了问题,但不知何故确实如此。 但至少看起来邻接矩阵的初始化是有效的。

在 floydwarshall 函数中,我希望获得 next[][] 中下一个节点的索引,但我只得到 null 或 10/11;

所以我的问题是我做错了什么或者我的方法完全错误? 我希望有一个人可以帮助我。 如果您需要任何进一步的信息,请询问

附言抱歉我的英语不好 ;)

最佳答案

我没有可用的 Java,但您的 initAdjacency() 函数似乎有缺陷。 path[][] 的维度是 [CELL_X * CELLS_Y][CELLS_X * CELLS_Y] 而你迭代 map 的维度是 [CELL_X][CELL_Y] 所以你没有将所有没有边缘的元素设置为默认值值为 1000,最终为 0。

尝试添加

for (int i = 0; i < CELLS_X * CELLS_Y; i++)
    for (int j = 0; j < CELLS_X * CELLS_Y; j++)
        path[i][j] = 1000;

到 initAdjacency() 函数的开头,在循环之前,正确地初始化它。

你可能还想做

for (int i = 0; i < CELLS_X * CELLS_Y) path[i][i] = 0;

之后以防万一,我不确定这会影响算法。

关于java - 弗洛伊德-沃歇尔算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10103086/

相关文章:

java - Jenkins 钩子(Hook)不工作 - Jenkins bitbucket

java - 如何使用 MSI 从 Java 向 Azure 存储进行身份验证?

c++ - C++中的大整数

algorithm - 交错等距探路者

c++ - 为什么我对数组中的图 block 进行评分的函数不起作用?

algorithm - 多源多目的地最短路径

c++ - 用于测试路径查找算法的可能数据集

java - 不兼容的类型 : String cannot be converted to Word

javascript - 在 12000 毫秒内执行 middlePermutation

java - Jackson 无法解析嵌套对象,异常构造参数 #6 没有属性名称注释