首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Floyd-warshall算法

Floyd-warshall算法
EN

Stack Overflow用户
提问于 2012-04-11 17:16:24
回答 1查看 3K关注 0票数 2

我目前正在Java上做一个小的塔楼防御项目,我陷入了寻路的泥潭。我读了很多关于A* dijkstra之类的东西,但我认为使用Floyd-Warshall来寻找路径可能是最好的(至少在我看来它解决了所有配对最短路径问题)。

无论如何,我试着自己实现它,但它并不完全像它应该的那样工作。我使用维基百科上的代码作为开始http://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm

下面是我的代码:

代码语言:javascript
复制
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函数中,我希望得到下一个节点的索引,但只得到了null或10/11;

所以我的问题是,我做错了什么,还是我的方法根本就是错的?我希望有人能帮助我。如果您需要任何进一步的信息,请询问

另外,对我糟糕的英语表示抱歉;)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-11 18:21:56

我没有可用的Java,但是看起来你的initAdjacency()函数有缺陷。path的维度是CELL_X * CELLS_Y,当你迭代地图的维度时,它是CELL_X的,所以你没有将所有没有边的元素都设置为默认值1000,结果是0。

尝试添加

代码语言:javascript
复制
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()函数的开头,以便在循环之前正确初始化它。

您可能还希望执行以下操作

代码语言:javascript
复制
for (int i = 0; i < CELLS_X * CELLS_Y) path[i][i] = 0;

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10103086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档