//Here is my copy of recursive, any suggestion on converting to Iterative? Please help!
#include <iostream>
#include <stdio.h>
#define INFINITY 9999
using namespace std;
int cost=0;
int n,a[10][10],visited[10];
int initial;
void read()
{
cout << "Please enter number of vertices: ";
cin >> n;
while (n <= 0)
{
cout << "Number of vertices has to be greater than 0. Please enter number of vertices again: ";
cin >> n;
}
};
void get(int a[10][10],int n,int visited[10])
{
int i,j,max,src,dest,wt;
for(i=0;i<n;i++)
{
visited[i]=0;
for(j=0;j<n;j++)
{
a[i][j]=999;
}
}
//display our input data
max=n*(n-1);
cout << "n = " << n << ", lines: " << max << endl;
for(i=0;i<max;i++)
{
cin >> src >> dest >>wt;
//a[src][dest]=wt;
a[dest][src]=wt;
}
//print-out matrix table
cout<<"\nCost Matrix";
cout<<"\n~\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]!=999)
cout<<a[i][j]<<"\t";
else
cout<<"0\t";
}
cout<<endl;
}
//get initial destination
cout << "Please enter your initial destination: ";
cin >> initial;
};
int least(int c,int n,int a[10][10],int visited[10])
{
int nc=999,min=999,kmin;
for(int i=0;i<n;i++)
if((a[c][i]!=0)&&(visited[i]==0))
{
if(a[c][i]<min)
{
min=a[i][initial]+a[c][i];
kmin=a[c][i];
nc=i;
}
}
if(min!=999)
cost+=kmin;
return nc;
};
//void mincost(int city,int n,int a[10][10],int visited[])
//{
// //==========================
// int counter;
// //cout<<"This is counter count: "<<counter;
// //==========================
// int i,ncity;
// visited[city]=1;
// cout<<city<<"<-";
// ncity=least(city,n,a,visited);
// if(ncity==999)
// {
// ncity=initial;
// printf("%d",ncity);
// cost+=a[city][ncity];
// return;
// }
// mincost(ncity,n,a,visited);
//};
void mincost(int city,int n,int a[10][10],int visited[])
{
int counter=0;
//cost = 0 variable
//cout<<"Mincost is running!"<<endl;
for (int i=0; i<n; i++)
{
int a[10][10];
int i,ncity;
visited[city]=1;
cout<<city<<"<-";
ncity=least(city,n,a,visited);
if(ncity==999)
{
ncity=initial;
printf("%d",ncity);
cost+=a[city][ncity];
return;
}
}
};
void put()
{
printf("\nMinimun cost for visiting all cities:%d\n",cost);
};
main()
{
read();
get(a,n,visited);
mincost(initial,n,a,visited);
cout<<endl;
put();
}
If you cannot find any iterative algorithm for the problem, you have to settle for a recursive one.
Recursive and non-recursive (also known as iterative) are simply two different approaches to solving a problem. Properly implemented, they should give the same result. If they do not, then something is wrong, and you should spend the time to figure out why.This is a generic answer, because the topic is too broad to answer here, as there are many different reasons that a particular algorithm may fail.
Depends... I teach algorithms and advice my students to choose whichever they find simpler to implement. Sometimes recursion is more intuitive than iteration and viceversa. All that is recursive can be done iterative and the other way around. The only problem you would have with recursion is having a stack overflow (if you call the recursive method too many times).
if it halts
Yes, the traveling salesman problem is an example of a co-NP-complete problem.
A good salesman is a good problem solver.
We don't avoid loops in programming. Loops are a fundamental feature of many algorithms. If we need to iterate over a data sequence in order to perform the same set of operations upon each data element, we would use an iterative loop. If we need to repeatedly reduce a larger problem into one or more smaller instances of the same problem until the problem is small enough to be solved we'd use a recursive loop.
Robert W. Starr has written: 'A multi-tour heuristic for the traveling salesman problem' -- subject(s): Traveling-salesman problem
Yes, you can. Any iterative method/algorithm that is used to solve a continuous mathematics problem can also be called a numerical method/algorithm.
1. Write mathematical analysis for recursive algorithms. Solve Tower of Hanoi Problem and analyze it.
Imagine an island with a road network joining several towns. You want to visit each town before returning to your start point. Finding the best solution to this is the travelling salesman problem. now imagine you want to resurface every road on the network. To do this in the shortest distance is the Chinese postman problem. Essentially for the travelling salesman problem you have to visit every vertex. For the Chinese postman problem every edge must be visited.
Willie Loman is a salesman in "Death of a Salesman" by Arthur Miller. At the end of Act 1 Willy's problem is that he is dissatisfied with how his neighborhood has developed and gotten crowded. He yearns for how things were in the past.