Time complexity of Prim's MST algorithm is O(|V|^2)
if you use adjacency matrix representation.
I am trying to implement Prim's algorithm using adjacency matrix. I am using this as a reference.
V = {1,2...,n}
U = {1}
T = NULL
while V != U:
/*
Now this implementation means that
I find lowest cost edge in O(n).
How do I do that using adjacency list?
*/
let (u, v) be the lowest cost edge
such that u is in U and v is in V - U;
T = T + {(u,v)}
U = U + {v}
EDIT:
- I understand Prim's algorithm very well.
- I know how to implement it efficiently using heaps and priority queues.
- I also know about better algorithms.
- I want to use adjacency matrix representation of graph and get O(|V|^2) implementation.
I WANT THE INEFFICIENT IMPLEMENTATION