Prim's Algorithm Time Complexity

前端 未结 3 794
悲哀的现实
悲哀的现实 2021-01-11 23:17

I was looking at the Wikipedia entry for Prim\'s algorithm and I noticed that its time complexity with an adjacency matrix is O(V^2) and its time complexity with a heap and

相关标签:
3条回答
  • 2021-01-11 23:40

    Even though the heap saves you from searching through the array, it slows down the "update" part of the algorithm: array updates are O(1), while heap updates are O(log(N)).

    In essence, you trade speed in one part of the algorithm for speed in another.

    No matter what, you'll have to search N times. However, in dense graphs, you'll need to update a lot (~V^2), and in sparse graphs, you don't.

    Another example off the top of my head is searching for elements in an array. If you're only doing it once, linear search is the best - but if you do lots of queries, it's better to sort it and use binary search every time.

    0 讨论(0)
  • 2021-01-11 23:55

    I think you read it wrong to some degree. For dense graphs, the article talks about using Fibonacci heaps with time complexity O(E + V log V), which is significantly better.

    0 讨论(0)
  • 2021-01-12 00:04

    From the Introduction to Algorithms (Carmen)

    Time= Θ(V)·T(EXTRACT-MIN) + Θ(E)·T(DECREASE-KEY)

                       T(EXTRACT-MIN)   T(DECREASE-KEY)   Total
    
    1. array            O(V)             O(1)              O(V^2)
    2. binary heap      O(lgV)           O(lgV)            O(E lgV)
    3. Fibonacci heap   O(lgV)           O(1)              O(E + VlgV)
    

    Using different data structures causes different time complexities.

    0 讨论(0)
提交回复
热议问题