Você está na página 1de 9

Name : Agatha Silvani Sekarningtyas

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;

cout << "Enter number of objects: ";


cin >> n;

cout << "Enter the weight of the knapsack: ";


cin >> w;

for (i = 0; i < n; i++)


{
cin >> array[0][i] >> array[1][i];
}

for (i = 0; i < n; i++)


{
used[i] = 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 compareX(const void* a, const void* b)


{
Point *p1 = (Point *)a, *p2 = (Point *)b;
return (p1->x - p2->x);
}

int compareY(const void* a, const void* b)


{
Point *p1 = (Point *)a, *p2 = (Point *)b;
return (p1->y - p2->y);
}

float dist(Point p1, Point p2)


{
return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}

float small_dist(Point P[], int n)


{
float min = FLT_MAX;
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
if (dist(P[i], P[j]) < min)
min = dist(P[i], P[j]);
}
}
return min;
}

float stripClosest(Point strip[], int size, float d)


{
float min = d;
for (int i = 0; i < size; ++i)
{
for (int j = i + 1; j < size && (strip[j].y - strip[i].y) < min; ++j)
{
if (dist(strip[i],strip[j]) < min)
min = dist(strip[i], strip[j]);
}
}
return min;
}

float closestUtil(Point Px[], Point Py[], int n)


{
if (n <= 3)
return small_dist(Px, n);
int mid = n / 2;
Point midPoint = Px[mid];
Point Pyl[mid + 1];
Point Pyr[n - mid - 1];
int li = 0, ri = 0;
for (int i = 0; i < n; i++)
{
if (Py[i].x <= midPoint.x)
Pyl[li++] = Py[i];
else
Pyr[ri++] = Py[i];
}
float dl = closestUtil(Px, Pyl, mid);
float dr = closestUtil(Px + mid, Pyr, n-mid);
float d = min(dl, dr);
Point strip[n];
int j = 0;
for (int i = 0; i < n; i++)
{
if (abs(Py[i].x - midPoint.x) < d)
strip[j] = Py[i], j++;
}
return min(d, stripClosest(strip, j, d));
}

float closest(Point P[], int n)


{
Point Px[n];
Point Py[n];
for (int i = 0; i < n; i++)
{
Px[i] = P[i];
Py[i] = P[i];
}
qsort(Px, n, sizeof(Point), compareX);
qsort(Py, n, sizeof(Point), compareY);
return closestUtil(Px, Py, n);
}

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>

using namespace std;

int ary[10][10],completed[10],n,cost=0;

void takeInput()
{
int i,j;

cout<<"how many number of villages you want? ";


cin>>n;

cout<<"\nPlease enter the Cost Matrix\n";

for(i=0;i < n;i++)


{
cout<<"\nEnter The Elements of Row: "<<i+1<<"\n";

for( j=0;j < n;j++)


cin>>ary[i][j];

completed[i]=0;
}

cout<<"\n\nThe cost list is:";

for( i=0;i < n;i++)


{
cout<<"\n";

for(j=0;j < n;j++)


cout<<"\t"<<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 mincost(int city)


{
int i,ncity;

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();

cout<<"\n\nHere is your path:\n";


mincost(0);

cout<<"\n\nMinimum cost is "<<cost;


return 0;
}
[08:44, 10/1/2018] Agatha Silvani: TSP
[08:44, 10/1/2018] Agatha Silvani: #include<iostream>

using namespace std;

int ary[10][10],completed[10],n,cost=0;

void takeInput()
{
int i,j;

cout<<"how many number of villages you want? ";


cin>>n;

cout<<"\nPlease enter the Cost Matrix\n";

for(i=0;i < n;i++)


{
cout<<"\nEnter The Elements of Row: "<<i+1<<"\n";

for( j=0;j < n;j++)


cin>>ary[i][j];

completed[i]=0;
}

cout<<"\n\nThe cost list is:";

for( i=0;i < n;i++)


{
cout<<"\n";

for(j=0;j < n;j++)


cout<<"\t"<<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 mincost(int city)


{
int i,ncity;

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();

cout<<"\n\nHere is your path:\n";


mincost(0);

cout<<"\n\nMinimum cost is "<<cost;

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.

Você também pode gostar