Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Path Finding Algorithms- A

comparison of Breadth First Search


(BFS),Dijkstra’s Algorithm

DISCRETE MATHEMATICS
Divyanshu katyan 2K20-CO-151
Gourav Gupta 2K20-CO-164
INTRODUCTION

GENERAL ALGORITHM

Content BREADTH FIRST SEARCH

DIJKSTRA’S ALGORITHM

COMPARISON
Introduction
What is the shortest path problem?
“In graph theory, theshortest path problemis
the problemof finding a path between
twovertices (or nodes)in a graph such thatthe
sum of the weights of its constituent edges is
minimized.”
Significance and importance of the path finding
algorithms
Path finding algorithms are important because
they are used in applications like google maps,
satellite navigation systems, routing packets
over the internet. The usage of pathfinding
algorithms isn’tjust limited to navigation
systems.The overarching idea can be applied to 07
other applications as well.
For example - Path-finding plays a very
significant role in many applications such
as mobile robot navigation. A grid-based
environment can be used to represent
the navigation space for robots in the real
world, to avoid obstacles, or to navigate
to a desired position. Efficient route
tracking architectures coupled with
proper statistical and data analysis
systems can help generate a roughly
accurate database to tackle medical
emergencies like organ transplantation
from hospital of one city to another by
making special corridors for efficient and
fast transportationand thereby reducing
fatalities
THIS FLOW CHART EXPLAINS THE GENERAL
ALGORITHM: General Algorithm:
Both algorithms have the same general
idea.We start from the source node and
explore the graph, node by node. In each
step, we always go to the node that is
closest to the source node .As we can
see, the first two steps are to initialize the
distances with a large value and to add
the source node to the queue. Next, we
perform iterations until the queue
becomes empty. In each iteration, we
extract a node from the queue that has
the shortest distance from the source
node. After that,we visit all the
neighbours of the extracted node and
check the new distance we were able to
achieve.
If the new distance is better than the old one, we update the
distance of this node and push it to the queue. Finally, the
algorithm moves to perform one more iteration until the
queue becomes empty.
After the algorithm ends,we’ll have the shortest paths from
the source node to all other nodes in the graph.
However, since graphs are either weighted or unweighted, we
can’t use the exact same algorithm for both cases. Therefore,
we have two algorithms. BFS calculates the shortest pathsin
unweighted graphs.On the other hand, Dijkstra’s algorithm
calculates the same thing in weighted graphs.
BFS Algorithm
When dealing with unweighted graphs,we always care about reducing the number of
visited edges. Therefore, we are sure that all the direct neighbours of the source node
have a distance equal to one. The next thing that we can be sure about is that all the
second neighbours of the source node have a distance equal to two, and so on.
This idea continues to be valid until we cover all the nodes of the graph. The only
exception is if we reached a node that has been visited before. In this case, we should
ignore it. The reason is that we must have reached it with a shorter path before.
It’s worth noting that in weighted graphs,where all edges have the same weight, the BFS
algorithm calculates the shortest paths correctly. The reason is that it cares about reducing
the number of visited edges, which is true in case of equal weights for all edges.
Here we think about the updates that need to be done to our general algorithm which we
studied initially. The main approach we follow is to always start from a node we reached
earlier.
Dijkstra’s Algorithm
When it comes to weighted graphs, it’s not necessary that neighbouring nodes
always have the shortest path.However, the neighbour with the shortest edge
can’tbe reached by any shorter path. The reason is that all other edges have
larger weights, so going through them alone would increase the distance.
Dijkstra’s algorithm uses this idea to come up with a greedy approach. In each
step, we choose the node with the shortest path.We fix this cost and add this
node’s neighbours to the queue. Therefore, the queue must be able to order
the nodes inside it based on the smallest cost. We can consider using a priority
queue achieve this.
We still have one problem. In unweighted graphs,when we reached a nodef rom
a different path, we were sure that the first time should have the shortest path.
In weighted graphs, that’s not always true. If we reached the node with a
shorter path,we must update its distance and add it to the queue. This means
that the same node could be added multiple times.

Therefore, we must always compare the cost of the extracted node with its
actual stored cost. If the extracted distance is larger than the stored one,it
means this node was added in an early stage.

Later, we must have found a shorter path and updated it. We have also added
the node again to the queue, so this extraction can be ignored safely.

Since we’re visiting each nodes’ neighbors only once, we’re visiting edges.

TIME COMPLEXITY IS O(V + E*LOG(V))


EXAMPLE
Take a look at the following graph. It
contains an example of applying the
BFS algorithm to a simple,
unweighted graph. The letter
corresponds to the node index, while
the number shows the stored distance
after performing the BFS algorithm:
We can clearly see that the algorithm visits the nodes, level by level. First, all the
neighbors are visited on level 1. Next, all the second neighbors are visited on level 2,
and so on. As a result,we’re able to calculate the shortest pathto all nodes starting
fromthe source node .
Now, let’s take a look at the result
of applying Dijkstra’s algorithm to
the weighted version of the same
graph:

Although node A is one stepaway from the source S, Dijkstra’s algorithm


first explored node B because the edge to B has the lowest cost.
Next, from B, it found a shorter pathto node A, which is the path that the
algorithm stored.
We can also note that although it’s one step away from S, we didn’t visit C
directly. The reason is that the edge between S and C has a very large cost.
Luckily, we were able to reach C from a shorter path than the direct edge.
Comparison
The following table shows a summarized comparison between the two
algorithms:
t
Conclusion
In this study, we presented the general algorithm for BFS and Dijkstra’s
algorithms. Next, we explained the main similarities and differences
between the two algorithms.

After that, we took a look at the result of applying both algorithms to


unweighted and weighted versions of the same graph.
Finally, we gave a comparison between both algorithms.

You might also like