prim algortihm

This is a discussion on prim algortihm within the C Programming forums, part of the General Programming Boards category; 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; ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    80

    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

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Is it giving you an error when you compile it, or is it not producing the proper output?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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++)

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    80
    it doesnt work and i cant solve this
    it should give the minimum length of edges but it returns always 0

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    80
    i know it doesnt return anything i mean it prints zero for the length between two edges

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You are not printing the length:
    > printf("%d length is %d\n",vnear);

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. prim algorithm
    By condorx in forum C Programming
    Replies: 1
    Last Post: 11-11-2003, 03:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21