Skip to main content
added 15 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

@JavaDeveloper, II would like to change a little your code:

for (Entry<Node, ArrayList<Edge>> entry : graph.getGraph().entrySet()) { Node currNode = entry.getKey(); if (currNode.getValue() == source) { currNode.setDistance(0); queue.add(currNode); } }

for (Entry<Node, ArrayList<Edge>> entry :  graph.getGraph().entrySet()) {
      Node currNode = entry.getKey();
      if (currNode.getValue() == source) {
          currNode.setDistance(0);
      queue.add(currNode);
    } 
}

I can provide 1 example.:

And it returns the wrong value for the start node and for the last one which should be 12 and not 19.

@JavaDeveloper, I would like to change a little your code:

for (Entry<Node, ArrayList<Edge>> entry : graph.getGraph().entrySet()) { Node currNode = entry.getKey(); if (currNode.getValue() == source) { currNode.setDistance(0); queue.add(currNode); } }

I can provide 1 example.

And it returns wrong value for the start node and for the last one which should be 12 and not 19.

I would like to change a little your code:

for (Entry<Node, ArrayList<Edge>> entry :  graph.getGraph().entrySet()) {
      Node currNode = entry.getKey();
      if (currNode.getValue() == source) {
          currNode.setDistance(0);
      queue.add(currNode);
    } 
}

I can provide 1 example:

And it returns the wrong value for the start node and for the last one which should be 12 and not 19.

I failed with link, now it directs to correct example
Source Link
qmaq
  • 31
  • 2

@JavaDeveloper, I would like to change a little your code:

for (Entry<Node, ArrayList<Edge>> entry : graph.getGraph().entrySet()) { Node currNode = entry.getKey(); if (currNode.getValue() == source) { currNode.setDistance(0); queue.add(currNode); } }

And make it like this:

Node start = new Node(source);
if (graph.getGraph().containsKey(start)) {
    start.setDistance(0);
    queue.offer(start);
}

It allows us to decrease search time from O(n) loop search to constant O(1) map lookup.


I can provide 1 example.

public static void main(String[] args) {
    Graphs G = new Graphs(7);
    G.addEdge(new Node(0), new Node(1), 4);
    G.addEdge(new Node(0), new Node(2), 3);
    G.addEdge(new Node(0), new Node(4), 7);
    G.addEdge(new Node(1), new Node(3), 5);
    G.addEdge(new Node(2), new Node(3), 11);
    G.addEdge(new Node(4), new Node(3), 2);
    G.addEdge(new Node(5), new Node(3), 2);
    G.addEdge(new Node(6), new Node(3), 10);
    G.addEdge(new Node(4), new Node(6), 5);
    G.addEdge(new Node(6), new Node(5), 3);
    
    
    Dijkstra dijkstra = new Dijkstra(G);
    Set<Node> path = dijkstra.findShortest(0);
    
    Iterator<Node> it = path.iterator();
    while (it.hasNext()) {
        System.out.println(it.next().getDistance());
    }
    
}

And it returns wrong value for the start node and for the last one which should be 12 and not 19.

ProofProof

@JavaDeveloper, I would like to change a little your code:

for (Entry<Node, ArrayList<Edge>> entry : graph.getGraph().entrySet()) { Node currNode = entry.getKey(); if (currNode.getValue() == source) { currNode.setDistance(0); queue.add(currNode); } }

And make it like this:

Node start = new Node(source);
if (graph.getGraph().containsKey(start)) {
    start.setDistance(0);
    queue.offer(start);
}

It allows us to decrease search time from O(n) loop search to constant O(1) map lookup.


I can provide 1 example.

public static void main(String[] args) {
    Graphs G = new Graphs(7);
    G.addEdge(new Node(0), new Node(1), 4);
    G.addEdge(new Node(0), new Node(2), 3);
    G.addEdge(new Node(0), new Node(4), 7);
    G.addEdge(new Node(1), new Node(3), 5);
    G.addEdge(new Node(2), new Node(3), 11);
    G.addEdge(new Node(4), new Node(3), 2);
    G.addEdge(new Node(5), new Node(3), 2);
    G.addEdge(new Node(6), new Node(3), 10);
    G.addEdge(new Node(4), new Node(6), 5);
    G.addEdge(new Node(6), new Node(5), 3);
    
    
    Dijkstra dijkstra = new Dijkstra(G);
    Set<Node> path = dijkstra.findShortest(0);
    
    Iterator<Node> it = path.iterator();
    while (it.hasNext()) {
        System.out.println(it.next().getDistance());
    }
    
}

And it returns wrong value for the start node and for the last one which should be 12 and not 19.

Proof

@JavaDeveloper, I would like to change a little your code:

for (Entry<Node, ArrayList<Edge>> entry : graph.getGraph().entrySet()) { Node currNode = entry.getKey(); if (currNode.getValue() == source) { currNode.setDistance(0); queue.add(currNode); } }

And make it like this:

Node start = new Node(source);
if (graph.getGraph().containsKey(start)) {
    start.setDistance(0);
    queue.offer(start);
}

It allows us to decrease search time from O(n) loop search to constant O(1) map lookup.


I can provide 1 example.

public static void main(String[] args) {
    Graphs G = new Graphs(7);
    G.addEdge(new Node(0), new Node(1), 4);
    G.addEdge(new Node(0), new Node(2), 3);
    G.addEdge(new Node(0), new Node(4), 7);
    G.addEdge(new Node(1), new Node(3), 5);
    G.addEdge(new Node(2), new Node(3), 11);
    G.addEdge(new Node(4), new Node(3), 2);
    G.addEdge(new Node(5), new Node(3), 2);
    G.addEdge(new Node(6), new Node(3), 10);
    G.addEdge(new Node(4), new Node(6), 5);
    G.addEdge(new Node(6), new Node(5), 3);
    
    
    Dijkstra dijkstra = new Dijkstra(G);
    Set<Node> path = dijkstra.findShortest(0);
    
    Iterator<Node> it = path.iterator();
    while (it.hasNext()) {
        System.out.println(it.next().getDistance());
    }
    
}

And it returns wrong value for the start node and for the last one which should be 12 and not 19.

Proof

Source Link
qmaq
  • 31
  • 2

@JavaDeveloper, I would like to change a little your code:

for (Entry<Node, ArrayList<Edge>> entry : graph.getGraph().entrySet()) { Node currNode = entry.getKey(); if (currNode.getValue() == source) { currNode.setDistance(0); queue.add(currNode); } }

And make it like this:

Node start = new Node(source);
if (graph.getGraph().containsKey(start)) {
    start.setDistance(0);
    queue.offer(start);
}

It allows us to decrease search time from O(n) loop search to constant O(1) map lookup.


I can provide 1 example.

public static void main(String[] args) {
    Graphs G = new Graphs(7);
    G.addEdge(new Node(0), new Node(1), 4);
    G.addEdge(new Node(0), new Node(2), 3);
    G.addEdge(new Node(0), new Node(4), 7);
    G.addEdge(new Node(1), new Node(3), 5);
    G.addEdge(new Node(2), new Node(3), 11);
    G.addEdge(new Node(4), new Node(3), 2);
    G.addEdge(new Node(5), new Node(3), 2);
    G.addEdge(new Node(6), new Node(3), 10);
    G.addEdge(new Node(4), new Node(6), 5);
    G.addEdge(new Node(6), new Node(5), 3);
    
    
    Dijkstra dijkstra = new Dijkstra(G);
    Set<Node> path = dijkstra.findShortest(0);
    
    Iterator<Node> it = path.iterator();
    while (it.hasNext()) {
        System.out.println(it.next().getDistance());
    }
    
}

And it returns wrong value for the start node and for the last one which should be 12 and not 19.

Proof