#Dijkstras help!
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
Sure, we can help with that! Dijkstra's algorithm is a popular algorithm for finding the shortest path between nodes in a graph. Here's a high-level overview of how it works:
- Initialize the distance to all nodes as infinity, except the starting node which is 0.
- Create a priority queue and add the starting node to it with distance 0.
- While the priority queue is not empty, extract the node with the smallest distance.
- For each neighbor of the extracted node, calculate the new distance as the sum of the extracted node's distance and the edge weight between them.
- If this new distance is smaller than the current distance to the neighbor, update it and add the neighbor to the priority queue.
- Repeat steps 3-5 until all nodes have been visited.
You can implement this algorithm using Java and data structures like PriorityQueue and HashMap to store distances. Make sure to handle edge cases like disconnected nodes or negative edge weights carefully.
If you have specific questions or issues with your implementation, feel free to ask for more help here! Good luck with your project!
show your code, explain your problem, etc and someone will help you
always here to help. but it seems u havent asked any concrete question, so u kinda missed my time window now :0
no idea where ur stuck, as u havent shared anything unfortunately, but i guess i can drop some stuff while im here:
usually i explain dijkstra as the logical extension of BFS on weighted graphs
for bfs, here is a good base to work with:
Graph traversal can be accomplished easily using BFS or DFS. The algorithms only differ in the order in which nodes are visited: https://i.imgur.com/n9WrkQG.png
The code to accomplish them is identical and only differs in the behavior of the Queue they are based on. BFS uses a FIFO-queue and DFS a LIFO-queue.
Queue<Node> nodesToProcess = ... // depending on BFS or DFS
nodesToProcess.add(rootNode); // add all starting nodes
Set<Node> visitedNodes = new HashSet<>();
while (!nodesToProcess.isEmpty()) {
// Settle node
Node currentNode = nodesToProcess.poll();
if (!visitedNodes.add(currentNode)) {
continue; // Already visited before
}
// Do something with the node
System.out.println(currentNode); // Replace by whatever you need
// Relax all outgoing edges
for (Node neighbor : currentNode.getNeighbors()) {
nodesToProcess.add(neighbor);
}
}
To get BFS, use a FIFO-queue:
Queue<Node> nodesToProcess = new ArrayDeque<>();
And for DFS a LIFO-queue:
Queue<Node> nodesToProcess = Collections.asLifoQueue(new ArrayDeque<>());
Thats all, very simple to setup and use.
For directed graphs relax all outgoing edges.
For trees the visitedNodes logic can be dropped, since each node can only have maximally one parent, simplifying the algorithm to just:
Queue<Node> nodesToProcess = ... // depending on BFS or DFS
nodesToProcess.add(rootNode); // add all starting nodes
while (!nodesToProcess.isEmpty()) {
// Settle node
Node currentNode = nodesToProcess.poll();
// Do something with the node
System.out.println(currentNode); // Replace by whatever you need
// Relax all outgoing edges
for (Node child : currentNode.getChildren()) {
nodesToProcess.add(child);
}
}
now, knowing bfs, dijkstra is just one step ahead
in a nutshell u just swap out the Queue that BFS uses against a PriorityQueue
and that's it
for the priorityqueue, the sorting criteria is on what's called the tentative distance
which u maintain as u explore the graph
the hardest part is to create correctly the weight function
for example in a separate Map<Node, Double> or, if ur having nodes as simple integers, perhaps also as normal array double[]
but thats details
also, remember that u first have to fully understand an algorithm before u start coding
so possibly u should step back, grab pen and paper and try to visualize whats going on, drawing the typical process of dijkstra that's also shown on all the illustrations and animations
wikipedia has a great example + animation