### Lean how to discover the shortest path in between two nodes in a huge data set by utilizing the Dijkstra Algorithm. In part 2, we\"ll sheathe A Star (A*) algorithms.

You are watching: Difference between a* and dijkstra

by

Finding a path from one node to another is a standard graph search difficulty that we deserve to solve utilizing multiple traversal algorithms prefer *BFS* or *DFS*. But, if we desire to uncover the shortest course from one node come another, *BFS* or *DFS* traversals won’t help us as well much, so we should use other algorithms like Bellman-Ford, Floyd-Warshall or Dijkstra. In this article, I desire to focus on the Dijkstra algorithm and see what we have the right to do to boost it, to do it faster, thus, transforming it right into an A star (A*) algorithm.

## Finding the Shortest path in a positive Weighted Graph

In a positive weighted graph, we deserve to calculate the course with the lowest price from suggest *A* to allude *B*, utilizing the *Dijkstra* algorithm. If there are courses with an adverse costs, *Dijkstra* i will not ~ work, so we would have to look to various other methods, prefer Bellman-Ford or Floyd-Warshall algorithms.

The main idea of this algorithm is that starting from one node, the next node to visit is the one through the lowest expense over traversal indigenous the source to it. So this looks nearly like *BFS*, by having a queue with nodes, and, at each iteration, us pull an element from the queue and also visit it. The difference between *Dijkstra* and also *BFS* is that with *BFS* we have actually a straightforward *FIFO* queue, and the following node to visit is the an initial node the was included in the queue. But, utilizing *Dijkstra,* we should pull the node with the lowest price so far.

Let’s watch a an easy example:

We desire to walk from *A* come *E* making use of *BFS*, so, we\"ll usage a straightforward FIFO queue. Beginning from node *A*, we acquire the neighboring nodes, *B* and also *C*, and we put them in the queue. Next, we pull from the queue the an initial element that has been added, node *B*, and we inspect the bordering nodes,*A* and also *D*. *A* to be visited, therefore we overlook it, and also we include node *D* come the queue. For this reason the queue now has nodes *C* and also *D*.

We move forward and also we acquire node *C* native the queue, inspect the surrounding nodes, *A* and also *E.*We ignore *A* because we currently visited it, and also we watch that we reached *E* and also we stop. So we uncovered a path,* A -> C -> E*, with a full cost of 22. Is this the route with the shortest cost? Well, no. Let’s readjust the algorithm to find the shortest path, by changing the way of retrieving the facets from the queue.

As a tiny observation, the classic *BFS* algorithm traverses all the graph, and also it stops once the queue is empty. This is a slightly readjusted algorithm that\"s an ext greedy, wherein we stop when we uncover our destination.

So let\"s check out what we get, if we pull indigenous the queue the node v the lowest price so far. Starting from *A*, we inspect the surrounding nodes,*B* and also *C*, and also we calculate the path cost from *A* to each of them. Therefore we have actually a queue with:

Queue: B, cost_so_far: 2 C, cost_so_far: 10 Now, using *Dijkstra*, we choose to visit the node with the shortest cost_so_far, i m sorry is *B*. We examine the neighboring nodes, *A* and also *D*, we see that node *D* no have any kind of cost_so_far, and also we calculation it. Thecost_so_farfor node *D* is thecost_so_farfor B add to the cost between *B* and also *D*, i beg your pardon is 3. So we have a total cost that 5. We put it in the queue and the queue will certainly look favor this:

Queue C, cost_so_far: 10, D, cost_so_far: 5Following the exact same process, we pullnode *D* indigenous the queue, because it has actually the shortest cost_so_far, in spite of the fact that *C* was included before. We check it, and we watch that we reached *E* v acost_so_farequal come 10 + 4 = 14. We add it in the queue, and, again, we select the following node come visit from the queue. The queue will look like this:

Queue E, cost_so_far: 14 C, cost_so far: 10We don’t protect against here, however I will explain this a small later. But, for now, let’s continue the process. We look at the queue, and we pull the node with the lowestcost_so_farwhich is *C*. *C* has two neighbors, *A* i m sorry was went to before, and also E with acost_so_farequal come 14. Now, if we calculate the path cost to *E*, through *C*, we will have actually acost_so_farequal to 22, i beg your pardon is higher than the currentcost_so_far14, so we don’t readjust it.

Moving forward, we will have a queue through one solitary node, *E*. Us take it and also we watch that we got to our destination, so we stop. We now have the course with the shortest cost, native *A* come *E*, *A -> B -> D -> E,* with a total cost of 14. So, utilizing *BFS* we found a route with the full cost the 22, but, utilizing *Dijkstra*, we witnessed a much better path v a lower cost of 14.

So, why didn’t us stop earlier when we first reached node*E*? Well, let’s readjust the sheet cost between *E* and also *D* come 20.

In this case, all the measures from*A* to *D*, will certainly be the same. But, after us visitnode D, the queue will look like this:

Queue E, cost_so_far: 25 C, cost_so far: 10We take it *C*, due to the fact that it has the lowest cost_so_far, and also we calculate the potentialcost_so_farfor *E* through *C*, i beg your pardon is 22, and also we watch that the brand-new value is reduced than the present cost_so_far, 25. For this reason we update it and also *E* will have a newcost_so_farequal to 22, with *C*. When we pull node *E* indigenous the queue, we see that we got to our destination and also we uncovered the course with the lowest cost from *A* to *E*, i m sorry is *A -> C -> E* v a full cost that 22.

So, even if as soon as we room visiting a node, us “see” our destination, and we calculation thecost_so_farfor it, we don’t protect against there, since there could be one more path v a lower cost. So, every time we visit a node, us look come its next-door neighbors and, also if we already calculated acost_so_farwhen we formerly visited an additional node, we inspect to see if we found a much better path, by to update thecost_so_farif it is reduced than the current one. It is why the *Dijkstra* algorithm is able to find the course with the lowest cost.

*Dijkstra*Implementation Proposal

Let’s see exactly how we deserve to implement this. Us will start with a node that has a name and also an adjacency perform for that is neighbors. Next to those fields, we can also add acost_so_farfield, let\"s speak to it g_cost, and a parent. The looking algorithm will collection these two fields and we will usage them to uncover the route with the lowest expense from one node come another. We can initialize these areas by setup theg_costto it is in infinite and the parent to it is in null. This could look favor this:

static class Node public final String name; public perform adjacency = brand-new ArrayList(); windy Node parent; public twin g_cost = Integer.MAX_VALUE; publicly Node(String name) this.name = name; windy void addNeighbour(Node neighbour, int cost) edge edge = new Edge(neighbour, cost); adjacency.add(edge); windy String toString() return name; An edge has actually a cost and also a target node.

See more: What Does Alaye Mean In English, What Does Alaye Mean

static class Edge publicly final twin cost; public last Node target; windy Edge(Node target, double cost) this.target = target; this.cost = cost; And currently the algorithm. Favor I stated before, the key idea the this algorithm is the when beginning from one node, the following node come be saw is the one through the lowest expense so much from the source to it. For this, we deserve to use a *Priority Queue*, let’s dubbed it a frontier, through nodes sorted by the g_cost. When we visit a node, if we find a better way for its neighbors, us just have actually to include them in the frontier and also update theg_cost, and, due to the fact that the frontier is a priority queue, that will keep the nodes sorted by theg_cost value. Once we visit the destination, it method that we found the ideal path from resource to destination and we stop.

This is the algorithm implementation:

public static void dijkstraSearch(Node source, Node destination) { source.g_cost = 0; PriorityQueue frontier = brand-new PriorityQueue(new Comparator() {