Question

For Dijkstra’s shortest path algorithm: a. Give the Big-O time for Dijkstra’s shortest path algorithm and...

For Dijkstra’s shortest path algorithm:
a. Give the Big-O time for Dijkstra’s shortest path algorithm and explain your answer.
b. Does the answer to (a) depend on whether we use an adjacency matrix or list? Explain your answer.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a.

The Big-O time of Dijkstra's algorithm depends upon the data structure used in algorithm.

Mostly and mainly MinHeap(Priority Queue) is used.

//Pseudocode of Dijkstra's algorithm

Using MinHeap and Adjacency list representation of graph, Time complextity of Dijkstra's algorithm = O(n) + O(n) + O(n logn) + O(e) + O(e logn) = O( n + e logn )

For sparse matrix, e = O(n)

For dense matrix, e = O(n2)

So we can say, in best case O( n logn ) is time complexity.

In worst case O( n2 logn ) is time complexity.

b.

Yes the Time complextity of Dijkstra's algorithm depends upon whether Adjacency list or Adjacency matrix is used for representation of graph.

If adjacency list is used then, complextity of Dijkstra's algorithm:

= O(n) + O(n) + O(n logn) + O(e) + O(e logn) = O( n + e logn )

If adjacency matrix is used then, complextity of Dijkstra's algorithm:

= O(n) + O(n) + O(n logn) + O(n2) + O(e logn) = O( n2 + e logn )

REASON:

//See a, b, c, c1, c2, c3 and c4 steps and their complexity in pseudocode above.

(a) Initializing dist and prev vectors with infinity and -1 respectively takes O(n) time.

(b) Building minHeap (say Q) takes O(n) time

(c) while loop runs for n iterations means O(n) time.

    (c1) Inside while a vertex (say u) is deleted which takes O(log n) time, then for n iterations of while    loop total complexity is O(n) * O(log n) = O(n log n)

    (c2) Inside while, finding adjacent of deleted vertex u, taked O(deg(u)) in adjacency list but it takes O(n) in adjacency matrix. Here is the difference in their complexity.

  • For n iterations of while loop, adj list takes n * Deg(u) = 2*e ,which is equivalent to order of e (asymptotically). It means O(e) in this step.
  • For n iterations of while loop, adj matrix takes n * n = n2 ,which is equivalent O(n2) (asymptotically). It means O(n2) in this step.

(c3) For loop runs for deg(u) iterations and it contains key decrement function (c4 step) which requires O(log n). So in total keyDecrement in for loop which is inside while loop, total becomes = log n * deg (u) * n = O(e * log n)

In this way, adj list takes O(n) + O(n) + O(n logn) + O(e) + O(e logn) = O( n + e logn ) in total.

Whereas adj list takes O(n) + O(n) + O(n logn) + O(n2) + O(e logn) = O( n2 + e logn ) in total.

Best case is sparse (e = n) and worst is dense (e = n2).

Adj list, best case = O(n logn), worst case = O(n2 logn)

Adj matrix, best case = O(n2), worst case = O(n2 logn)

Add a comment
Know the answer?
Add Answer to:
For Dijkstra’s shortest path algorithm: a. Give the Big-O time for Dijkstra’s shortest path algorithm and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT