用Java实现Dijkstra算法

时间:2014-02-07 17:08:25

标签: java algorithm dijkstra

我正在尝试制作一个支持自动机(图形)上的多个操作的程序。 这就是我的数据结构:

public class Automaton {
private ArrayList<Node> $Q; /* All the nodes */
private Node $startingNode; /* The startingNode */
private ArrayList<Connection> $delta; /* All the connections */
private ArrayList<Node> $F; /* All the end nodes */
private ArrayList<Character> $E; 


public class Connection { 
private Node $start; 
private Node $end; 
private char $condition; }


public class Node { 
private boolean $startingNode; 
private boolean $endNode; 
private String $name; }

其中一个操作是Dijkstra算法,这是我的代码到目前为止,问题是如果(D [v]> =(D [w] +值))它永远不会进入这个,因为1将永远不会小于1,我正在寻找一种方法,我可以计算我的路径的长度,所以它高于1.例如:从A-> D是长度3,因为你从A到B,从B到C ,从C到D.

public String[] shortestPath() {
    boolean[] visited = new boolean[$Q.size()];
    String[] P = new String[$Q.size()];
    int[] D = new int[$Q.size()];
    char c;
    int w;
    int value = 0;
    Node source = null;
    boolean found = false;

    for (int i = 0; i < visited.length; i++) {
        if ($Q.get(i).getStartingNode()) {
            visited[i] = true;
            source = $Q.get(i);
        } else
            visited[i] = false;
    }

    for (int i = 0; i < $Q.size(); i++) {
        /* If the source and the node are not the same */
        if (!($Q.get(i).getName().equals(source.getName()))) {
            P[i] = source.getName();
            /* If there is a connection */
            c = checkConnection(source, $Q.get(i));
            if (c != '\u0000') {/* There is a connection */
                if (c == '$')/* Epsilon transition */
                    D[i] = 0;
                else
                    D[i] = 1;
            } else
                /* There isnt a connection, put it on -1 */
                D[i] = -1;
        }

    }
    for(int i = 0; i < visited.length; i++)
        System.out.println(visited[i]);

    for (int i = 1; i < $Q.size() && !found; i++) {
        w = findMinimum(D, visited);
        visited[w] = true;

        System.out.println(w);

        for (int v = 0; v < $Q.size() && !found; v++) {
            if (!(visited[v])) {
                c = checkConnection($Q.get(w), $Q.get(v));
                if (c != '\u0000') {/* There is a connection */
                    if (c == '$')/* Epsilon transition */
                        value = 0;
                    else
                        value = 1;
                    /*Check if from source to v is shorter if you go via w*/
                    if (D[v] >= (D[w] + value)) {
                        D[v] = D[w] + value;
                        P[v] = $Q.get(w).getName();

                        if ($Q.get(w).getEndNode())
                            found = true;
                    }
                }/*Connection if*/
            }
        }
    }
    return P;

}

/** Returns the minimum cost of the connections from the source
 * @param d the array with the costs from the source to the other nodes
 * @param visited an array that remembers which node is already visited
 * @return returns the index of the position with the minimum cost
 */
private int findMinimum(int[] d, boolean[] visited) {
    // TODO Auto-generated method stub
    int min = 2; /* Since the cost will be 0 of 1 we can use 2 as a higher number */
    int index = 0;

    for (int i = 0; i < d.length; i++) {
        /* If we didnt visit the node yet, so a node outside s */
        if (!(visited[i])) {
            /* If there really is a connection */
            if (d[i] != -1) {
                /* If the cost is lower than our current minimum */
                if (d[i] <= min){
                    index = i;
                    min = d[i];
                }

            }
        }
    }

    /**
 * Checks if there is a connection between 2 nodes
 * @param start the startnode of the connection
 * @param end the endnode of the connection
 * @return returns the cost of the edge between the 2 nodes, else return null
 */
private char checkConnection(Node start, Node end){
    for(int i = 0; i < $delta.size(); i++){
        /*If the start node and the end node are the same*/
        if(($delta.get(i).getStart().getName().equals(start.getName())) && $delta.get(i).getEnd().getName().equals(end.getName())){
            return $delta.get(i).getCondition();
        }
    }
    return '\u0000'; /*Empty char*/
}
    return index;
}

0 个答案:

没有答案