Question

Special productions are made in the aluminum profile factory. Aluminum profile orders for 10 different customers...

Special productions are made in the aluminum profile factory. Aluminum profile orders for 10 different customers have been produced and ready for distribution. All of the prepared orders can be loaded into a vehicle and distributed. The employee who will distribute the profiles prepared at the factory will load them into the vehicle and return to the factory after delivering the orders to all the customers. To meet the demand of all customers, write the C code that operates the steps of the “nearest neighbor algorithm” to find the tour to follow. Note: randomly determine the distance between customers.

Homework Answers

Answer #1

We can solve this problem by using Dynamic progaraming .

For this problem we create cost matrix by using the distances.

like shown below.

to do this problem we can go throw the following formula:

T (i , s) = min ( ( i , j) + T ( j , S – { j }) ) ; S!= Ø   ; j € S ;

S is set that contains non visited vertices

= ( i, 1 ) ; S=Ø, This is base condition for this recursive equation.

Here,

T (i, S) means We are travelling from a vertex “i” and have to visit set of non-visited vertices “S” and have to go back to vertex 1 (let we started from vertex 1).

( i, j ) means cost of path from node i to node j

If we observe the first recursive equation from a node we are finding cost to all other nodes (i,j) and from that node to remaining using recursion ( T (j , {S-j}))

But it is not guarantee that every vertex is connected to other vertex then we take that cost as infinity. After that we are taking minimum among all so the path which is not connected get infinity in calculation and won’t be consider.

If S is empty that means we visited all nodes, we take distance from that last visited node to node 1 (first node). Because after visiting all he has to go back to initial node.

PROGRAM:

#include<stdio.h>
 
int ary[10][10],completed[10],n=10,cost=0;
 
void takeInput()   //This function is to take distance between factory to evrery costomers and every costomer to other costomer
{
        int i,j;

 
        printf("\nEnter the Cost Matrix\n");  //We generally take input as cost matrix
 
        for(i=0;i < n;i++)
        {
                printf("\nEnter Elements of Row: %d\n",i+1);
 
                for( j=0;j < n;j++)
                        scanf("%d",&ary[i][j]);
 
                completed[i]=0;
        }
 
        printf("\n\nThe cost list is:");
 
        for( i=0;i < n;i++)
        {
                printf("\n");
 
                for(j=0;j < n;j++)
                        printf("\t%d",ary[i][j]);
        }
}
int least(int c)
{
        int i,nc=999;
        int min=999,kmin;
 
        for(i=0;i < n;i++)
        {
                if((ary[c][i]!=0)&&(completed[i]==0))
                        if(ary[c][i]+ary[i][c] < min)
                        {
                                min=ary[i][0]+ary[c][i];
                                kmin=ary[c][i];
                                nc=i;
                        }
        }
 
        if(min!=999)
                cost+=kmin;
 
        return nc;
}
 
 
void nearestneighbor(int city)
{
        int i,ncity;
 
        completed[city]=1;
 
        printf("%d--->",city+1);
        ncity=least(city);
 
        if(ncity==999)
        {
                ncity=0;
                printf("%d",ncity+1);
                cost+=ary[city][ncity];
 
                return;
        }
 
        nearestneighbor(ncity);
}
 

int main()
{
        takeInput();
 
        printf("\n\nThe Path is:\n");
        nearestneighbor(0); //passing 0 because starting vertex
 
        printf("\n\nMinimum distance is %d\n ",cost);
 
        return 0;
}

Sample Output:


Enter the Cost Matrix

Enter Elements of Row: 1
0 1 2 1 3 4 5 6 4 1

Enter Elements of Row: 2
2 0  4 1 5 3 6 2 7 1

Enter Elements of Row: 3
4 1 0 4 5 4 1 5 3 4 1

Enter Elements of Row: 4
2 4 4 0 4 6 3 1 8 4 2

Enter Elements of Row: 5
5 4 1 2 0 4 3 2 6 1 4

Enter Elements of Row: 6
4 1 5 2 4 0 1 2 3 6 4

Enter Elements of Row: 7
4 1 6 2 4 6 0 4 9 5 1

Enter Elements of Row: 8
1 4 3 1 2 6 4 0 4 2 3

Enter Elements of Row: 9
4 2 3 6 5 1 4 2 3 0 4

Enter Elements of Row: 10
4 5 1 2 3 4 2 4 2 4 0


The cost list is:
        0       1       2       1       3       4       5       6       4       1
        2       0       4       1       5       3       6       2       7       1
        4       1       0       4       5       4       1       5       3       4
        1       2       4       4       0       4       6       3       1       8
        4       2       5       4       1       2       0       4       3       2
        6       1       4       4       1       5       2       4       0       1
        2       3       6       4       4       1       6       2       4       6
        0       4       9       5       1       1       4       3       1       2
        6       4       0       4       2       3       4       2       3       6
        5       1       4       2       3       0       4       4       5       1

The Path is:
1--->4--->2--->8--->10--->5--->9--->7--->6--->3--->1

Minimum distance is 26
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
1. The failure of the new supply chain system affected Nike adversely. What were the reasons...
1. The failure of the new supply chain system affected Nike adversely. What were the reasons for the failure and how did the breakdown harm Nike? 2. What are the important elements to be kept in mind while implementing a new system in an organization? What is the importance of a good working relationship between partners and the sharing of responsibility in implementing critical projects? What mistakes did Nike and i2 make? 3. comment on the lessons learned and the...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From the April 2004 Issue Save Share 8.95 In 1991, Progressive Insurance, an automobile insurer based in Mayfield Village, Ohio, had approximately $1.3 billion in sales. By 2002, that figure had grown to $9.5 billion. What fashionable strategies did Progressive employ to achieve sevenfold growth in just over a decade? Was it positioned in a high-growth industry? Hardly. Auto insurance is a mature, 100-year-old industry...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT