Help optimize this dijkstra algorithm:

public static void dijkstra(Set<Node> graph, Node start, Node end) {
start.setDistance(0);
Set<Node> visited = new HashSet<>();
dijkstraHelper(graph, visited, start, end);
}

public static void dijkstraHelper(Set<Node> unvisited, Set<Node> visited, Node currentNode, Node end) {
if (visited.contains(end)) {
Node prevNode = end;
System.out.println(prevNode);
while (prevNode.getPreviousNode() != null) {
System.out.println(prevNode.getPreviousNode());
prevNode = prevNode.getPreviousNode();
}
return;
}

Map<Node, Integer> neighbours = currentNode.getAdjacentNodes();
neighbours.forEach((node, distance) -> {
if (unvisited.contains(node)) {
int computeDistance = distance + currentNode.getDistance();
if (computeDistance <= node.getDistance()) {
node.setDistance(computeDistance);
node.setPrevNode(currentNode);
}
}
});

unvisited.remove(currentNode);
visited.add(currentNode);

AtomicReference<Node> nextNode = new AtomicReference<>();
neighbours.forEach((node, integer) -> {
if (nextNode.get() == null && unvisited.contains(node)) {
nextNode.set(node);
} else if (unvisited.contains(node) && node.getDistance() < nextNode.get().getDistance()){
nextNode.set(node);
}
});

dijkstraHelper(unvisited, visited, nextNode.get(), end);
}
public static void dijkstra(Set<Node> graph, Node start, Node end) {
start.setDistance(0);
Set<Node> visited = new HashSet<>();
dijkstraHelper(graph, visited, start, end);
}

public static void dijkstraHelper(Set<Node> unvisited, Set<Node> visited, Node currentNode, Node end) {
if (visited.contains(end)) {
Node prevNode = end;
System.out.println(prevNode);
while (prevNode.getPreviousNode() != null) {
System.out.println(prevNode.getPreviousNode());
prevNode = prevNode.getPreviousNode();
}
return;
}

Map<Node, Integer> neighbours = currentNode.getAdjacentNodes();
neighbours.forEach((node, distance) -> {
if (unvisited.contains(node)) {
int computeDistance = distance + currentNode.getDistance();
if (computeDistance <= node.getDistance()) {
node.setDistance(computeDistance);
node.setPrevNode(currentNode);
}
}
});

unvisited.remove(currentNode);
visited.add(currentNode);

AtomicReference<Node> nextNode = new AtomicReference<>();
neighbours.forEach((node, integer) -> {
if (nextNode.get() == null && unvisited.contains(node)) {
nextNode.set(node);
} else if (unvisited.contains(node) && node.getDistance() < nextNode.get().getDistance()){
nextNode.set(node);
}
});

dijkstraHelper(unvisited, visited, nextNode.get(), end);
}
5 Replies
JavaBot
JavaBot7mo ago
This post has been reserved for your question.
Hey @userexit! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
JavaBot
JavaBot7mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
userexit
userexitOP7mo ago
dont know a lot about queues so i made a lopp based on a list
JavaBot
JavaBot7mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server