Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To implement Dijkstra’s shortest path algorithm on unweighted graphs so that it runs in linear time, which data structure should be used?

To implement Dijkstra’s shortest path algorithm on unweighted graphs so that it runs in linear time, the data structure to be used is:

  1. Queue
  2. Stack
  3. Heap
  4. B-Tree

I found below answers:

  1. A Queue because we can find single source shortest path in unweighted graph by using Breadth first search (BFS) algorithm which using "Queue" data structure , which time O(m+n) (i.e. linear with respect to the number of vertices and edges. )

  2. A min heap is required to implement it in linear time because if we delete here a node in min heap it will not take any time in adjustment because all r having same weight so deletion will take O(1) for one node .. so for n-1 node it will be O(n).

Can someone explain which one is the correct answer?

like image 583
Nivedita Avatar asked Dec 13 '25 21:12

Nivedita


1 Answers

If the graph is unweighted, Dijkstra's algorithm is not needed. A simple BFS will work perfectly in O(E + V) time complexity, i.e. a linear time complexity.

A simple implementation of the algorithm needs a queue data structure.

like image 198
Méhdi Màick Avatar answered Dec 16 '25 23:12

Méhdi Màick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!