# prim algortihm

Printable View

• 11-14-2003
condorx
prim algortihm
Code:

```void prim(long W[n][n]){         int i ,vnear,min,t;         int nearest[5];         int distance[5];         for(i = 0;i <= 5;i++)         {                 nearest[i] = 1;                 distance[i] = W[0][i];         }         for(t = 0;t<5;t++)         {                 min = MAX_LENGTH;                 for(i = 0;i <= n;i++)                         if(0<=distance[i]<min)                         {                                 min = distance[i];                                 vnear = i;                                 printf("\n%d from ",vnear);                         }                         distance[vnear] = -1;                         for(i =0 ;i<=n;i++)                                 if(W[i][vnear] < distance[i])                                 {                                         distance[i] = W[i][vnear];                                         nearest[i] = vnear;                                         printf("%d length is %d\n",vnear);                                 }         } }```
whats wrong here i couldnt find it pls help me
• 11-14-2003
XSquared
Is it giving you an error when you compile it, or is it not producing the proper output?
• 11-14-2003
swoopy
You're stepping past the end of your arrays:
> for(i = 0;i <= 5;i++)

> for(i = 0;i <= n;i++)

> for(i =0 ;i<=n;i++)

Change <= to < for all these:
for(i = 0;i < 5;i++)

for(i = 0;i < n;i++)

for(i =0 ;i<n;i++)
• 11-15-2003
condorx
it doesnt work and i cant solve this
it should give the minimum length of edges but it returns always 0
• 11-15-2003
quzah
Quote:

Originally posted by condorx
it doesnt work and i cant solve this
it should give the minimum length of edges but it returns always 0

It doesn't return anything. The example code here is a void function.

Quzah.
• 11-15-2003
condorx
i know it doesnt return anything i mean it prints zero for the length between two edges
• 11-15-2003
swoopy
You are not printing the length:
> printf("%d length is %d\n",vnear);

Instead:
printf("%d length is %d\n",vnear,distance[i]);