0

if I try to calculate the shortest Distance from Node A to Z in my Graph the Dijkstra Algorithm ist not working very well. I'm not sure why, i think it is a problem in the calculation of the Distance form Node to Node.

PriorityQueue<Node> prio_station = new PriorityQueue<Node>();
    // PrioritaetsQueue prio_station = new
    // PrioritaetsQueue(arrayList_Nodes.size());
    final_way = new ArrayList<Node>();

    for (Node node : arrayList_Nodes) {
        if (node.getData().getStation().equals(source.getData().getStation())) {
            node.setPrevious(null);
            node.setDistance(0);
            prio_station.add(node);
        } else {
            node.setDistance(Integer.MAX_VALUE);
            node.setPrevious(null);
            prio_station.add(node);
        }
    }

    // for(Node n : prio_station){
    // System.out.println("Station in Prio: " + n.getData().getStation());
    // }

    Node min;
    Node start = prio_station.peek();
    while (prio_station.isEmpty() == false) {
        min = prio_station.poll();
        System.out.println(min.getData().getStation());
        for (Edge neighbor : min.getArrayList_Edges()) {
            Node adjNode = neighbor.getDestinationNode();
            int alt = min.getDistance() + neighbor.getDistanceCost();
            int dist = adjNode.getDistance();
            if (alt < dist) {
                adjNode.setDistance(alt);
                adjNode.setPrevious(min);

If I want to Navigate form E to J i have a method, which gaves me the previous Node and then I have w while which do this so long till my startnode is reached. My expected Output Should be JHGFE But my Output is endless JHJHJHJHJHJHJHJHJHJH

Reptain
  • 9
  • 2
  • Please supply working code, the expected result, and the actual result, otherwise it is very unlikely anyone will be able to help you. – David Conrad May 06 '16 at 21:28
  • If I want to Navigate form E to J i have a method, which gaves me the previous Node and then I have w while which do this so long till my startnode is reached. My expected Output Should be JHGFE But my Output is endless JHJHJHJHJHJHJHJHJHJH – Reptain May 07 '16 at 07:05

0 Answers0