recently i was trying to implement the prim’s algorithm. i tried to modify the algo a bit according to my understanding but am getting wrong answer on that.
can someone tell me a test case on where my algo is going wrong …

n//total vertices
g[][]//the adjacency matrix for the graph
cost[]//stores the edge value
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(g[i][j]<c[j])
c[j]=g[i][j];}}

now to find the value of the MST am summing up the entire c[] array and that is the final answer…
here is my implementation…

The problem is that you’re not keeping account of the seen nodes in the graph.Thus, the nodes which you’ve already discovered also get updated which shouldn’t be the case as Prim’s MST takes the optimal decision from the fringe vertices.

An example where your code fails:
Edges:

A-B=1

B-C=0.5

A-C=3

Start from A.

cost[A]=0

cost**=inf

cost[C]=inf

Adj vertices B and C.

A-B<cost**//true

cost**=1

A-C<cost[C]//true

cost[C]=3

next iteration-B

B-A<cost[A]//false
B-C<cost[C]//true

cost[C]=0.5

next iteration-C

C-A<cost[A]//false
C-B<cost**//true{This shouldn't happened had you kept the seen vertices in account}

cost**=0.5

MST value=cost[A]+cost**+cost[C]=0+.5+.5=1(Wrong.)Correct answer is 1.5