So I am following this algorithm for Prim's MST
input: graph G(V,E) in the form of adjacency list
- Create a min heap for vertices using build heap time complexity: O(V)
- Repeat following steps until no more elements in heap.
- Call extract-min on heap to get vertex with minimum cost O(log V)
- For vertex extracted find the adjacent vertices in adjacency list.
- Perform decrease key operation on adjacent vertices in the heap. O(logn V)
The time complexity analysis for the algorithm given in my class goes something like this:
O(V) => to build heap
O(VlogV) => V times EXTRACT_MIN operation
2E => traverse all edges in adjacency list
ElogV => E times DECREASE_KEY operation (Worst Case)
EXTRACT_MIN => O(logV)
DECREASE_KEY => O(logV)
Total Time Complexity = V + V*logV + 2E + E*log V
= O((V+E) logV)
My question is before performing decease key operation won't I need to find the corresponding element in the min heap? And searching the element in the heap would take O(V) time.
For the above graph I would do something like this (min heap implemented using array)
A B C D E F
----------------------------------
chosen as min 0 INF INF INF INF INF ------> cost associated with vertices
------------------------------
A 7 2 6 INF INF
------------------------------
C 6 6 8 5
------------------------------
F 6 3 7
------------------------------
D 6 7
------------------------------
B 4
------------------------------
E
My array (min heap) would initially look like this :
Each element consists of two things: the vertex name, the cost.
The min heap is based on cost.
A,0 | B,INF | C,INF | D,INF | E,INF | F,INF
Now after getting first minimum element (A) I look for its adjacent vertices in adjacency list
and find B, C and D. Now I need to perform decrease key operation for these vertices in the min heap.
The DECREASE_KEY operation would work only if I know the index of the vertex on which decrease key operation is to be performed.
To get the index won't I need to first search for it in the array taking O(V) additional time?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…