Blogger news

Followers

Monday, July 21, 2014
/*Implement any scheme to find the optimal solution for the Traveling Salesperson problem*/



#include<stdio.h>
int tspda(int,int,int,int);
int n,c[1000][1000],start,n,temp[1000],mincost,ccost,tour[1000],mintour[1000],i,j,k;
void main()
{
            printf("\nEnter the number of nodes\n");
            scanf("%d",&n);
            printf("\nEnter the cost matrix\n");
            for(i=1;i<=n;i++)
                        for(j=1;j<=n;j++)
                                                scanf("%d",&c[i][j]);
            printf("\nEntered cost matrix is:\n");
            for(i=1;i<=n;i++)
            {
                        for(j=1;j<=n;j++)
                                    printf("%d\t",c[i][j]);
                        printf("\n");
            }
            printf("\nThe approximate tour is:\n");
            tspda();
}
int tspda(int c[10][10],int tour[10],int start,int n)
{
            if(start==n-1)
                        return(c[tour[n-1][tour[n]+c[tour[n][tour[1]];
            for(i=start+1;i<=n;i++)
            {
                        for(j=1;j<=n;j++)
                                    temp[j]=tour[j];
                        temp[start+1]=tour[i];
                        temp[i]=temp[start+1];
                        if(c[tour[start][tour[i]]+(ccost=tspdp(c,temp,start+1,n)))<min)
                        {
                                    mincost=ccost+c[tour[start][tour[i]];
                                    for(k=1;k<=n;k++)
                                                mintour[k]=temp[k];
                        }
            }
            for(i=1;i<=n;i++)
                        tour[i]=mintour[i];
            return mincost;
}



0 comments: