Escolar Documentos
Profissional Documentos
Cultura Documentos
NIM : 2015390003
1. Knapsack Problem
#include<iostream>
using namespace std;
int main()
{
int array[2][100], n, w, i, curw;
int used[100], maxi = -1, totalprofit = 0;
curw = w;
while (curw >= 0)
{
maxi = -1;
for (i = 0; i < n; i++)
{
if ((used[i] == 0) && ((maxi == -1) || (((float) array[1][i]
/ (float) array[0][i]) > ((float) array[1][maxi]
/ (float) array[0][maxi]))))
{
maxi = i;
}
}
used[maxi] = 1;
curw -= array[0][maxi];
totalprofit += array[1][maxi];
if (curw >= 0)
{
cout << "\nAdded object " << maxi + 1 << " Weight: "
<< array[0][maxi] << " Profit: " << array[1][maxi]
<< " completely in the bag, Space left: " << curw;
}
else
{
cout << "\nAdded object " << maxi + 1 << " Weight: "
<< (array[0][maxi] + curw) << " Profit: "
<< (array[1][maxi] / array[0][maxi]) * (array[0][maxi]
+ curw) << " partially in the bag, Space left: 0"
<< " Weight added is: " << curw + array[0][maxi];
totalprofit -= array[1][maxi];
totalprofit += ((array[1][maxi] / array[0][maxi]) * (array[0][maxi]
+ curw));
}
}
//print total worth of objects filled in knapsack
cout << "\nBags filled with objects worth: " << totalprofit;
return 0;
}
2. Closest Pair
#include <iostream>
#include <cfloat>
#include <cstdlib>
#include <cmath>
using namespace std;
struct Point
{
int x, y;
};
int main()
{
Point P[] = {{4, 6}, {13, 31}, {20, 25}, {25, 5}, {24, 20}, {3, 4}};
int n = sizeof(P) / sizeof(P[0]);
cout << "The smallest distance is " << closest(P, n);
return 0;
}
3. TSP Problem
#include<iostream>
int ary[10][10],completed[10],n,cost=0;
void takeInput()
{
int i,j;
completed[i]=0;
}
int least(int c)
{
int i,nc=999;
int min=999,kmin;
if(min!=999)
cost+=kmin;
return nc;
}
completed[city]=1;
cout<<city+1<<"--->";
ncity=least(city);
if(ncity==999)
{
ncity=0;
cout<<ncity+1;
cost+=ary[city][ncity];
return;
}
mincost(ncity);
}
int main()
{
takeInput();
int ary[10][10],completed[10],n,cost=0;
void takeInput()
{
int i,j;
completed[i]=0;
}
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;
}
completed[city]=1;
cout<<city+1<<"--->";
ncity=least(city);
if(ncity==999)
{
ncity=0;
cout<<ncity+1;
cost+=ary[city][ncity];
return;
}
mincost(ncity);
}
int main()
{
takeInput();
return 0;
}
4. Define what is the different between: Euler Path and Circuit, Hamilton Paths
and Circuits and Travelling Salesman Problem?
An Euler path is a path that uses every edge in a graph with no repeats. Being a path, it does not
have to return to the starting vertex.
An Euler circuit is a circuit that uses every edge in a graph with no repeats. Being a circuit, it
must start and end at the same vertex.
A Hamiltonian circuit is a circuit that visits every vertex once with no repeats. Being a circuit, it
must start and end at the same vertex. A Hamiltonian path also visits every vertex once with no
repeats, but does not have to start and end at the same vertex.
A traveler needs to visit all the cities from a list, where distances between all the cities are
known and each city should be visited just once.