Open In App

Difference between BFS and Dijkstra’s algorithms when looking for shortest path?

Last Updated : 28 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

What is Dijkstra’s Algorithm?

Dijkstra’s Algorithm is used for finding the shortest path between any two vertices of a graph. It uses a priority queue for finding the shortest path. 

For more detail about Dijkstra’s Algorithm, you can refer to this article.

What is BFS Algorithm?

Breadth First Search (BFS) algorithm traverses a graph in a bread toward motion and uses a queue to remember to get the next vertex to start a search when a dead end occurs in any iteration. 

For more detail about BFS Algorithm, you can refer to this article.

Difference between BFS and Dijkstra’s algorithms when looking for the shortest path:

S.No. Dijkstra’s Algorithm

BFS Algorithm

1.

It is generally used for weighted graphs.

It is used for unweighted graphs.

2.

In each step, visit the node with the lowest weight.

Visit nodes level by level based on the closest to the source.

3.

It uses Priority Queue.

It uses Simple Queue.

4.

The time complexity for this Algorithm is O(V + ElogV).
here, V is the number of vertices and E is the number 
of edges in the graph.

The time complexity for this Algorithm for finding the 
shortest path will be O(V+E). where, V is the number 
of vertices and E is the number of edges in the graph.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads