Using edges from A
will prevent you from getting an actual minimum spanning tree unless the edges in A
are guaranteed to belong to an MST.
However, given a set A
you can find the MST that connects all the other vertices to A
.
You do not indicate a guarantee that the edges in A
form a single connected component, so I assume they do not.
In this case, using Prim's algorithm is a bad idea because Prim's algorithm assumes that at each step you are adding an edge between a valid MST and a point which is not in the MST. Since the "MST" formed by A
may be non-contiguous, this breaks the assumptions of Prim's algorithm.
Instead, use Kruskal's algorithm. It adds edges to an "MST set" by considering the edges in order of their length, always choosing the shortest edge first. If both ends of the edge are already in the MST set, then the edge is rejected. If only one edge is in the MST set, then the new edge is added to the MST set.
You can implement the algorithm like so:
KRUSKAL(G,A):
mst_set = ∅
for each vertex v ∈ G.V:
MAKE-SET(v)
for each edge (u,v) in A:
mst_set = mst_set ∪ {(u,v)}
UNION(u,v)
for each edge (u, v) in G.Edges ordered by weight(u, v), increasing:
if FIND-SET(u) ≠ FIND-SET(v):
mst_set = mst_set ∪ {(u, v)}
UNION(u, v)
return mst_set
where the MAKE-SET
, UNION
, and FIND-SET
operations are part of a disjoint-set data structure.