Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

NAME-SUBHA SANKAR CHAKRABORTY

NAME OF THE ASSIGNMENT:TOPICS ON ALGORITHM


ASSIGNMENT NO-1
M.TECH(CSE) 1ST YEAR
Binary Search Tree Operations
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *root;
void insert(struct node *p,int x)
{
if(p->data>x)
{
if(p->left==NULL)
{
struct node *z;
z=(struct node *)malloc(sizeof(struct node));
z->data=x;
z->left=NULL;
z->right=NULL;
p->left=z;
}
else
{
insert(p->left,x);
}
}
else
{
if(p->right==NULL)
{
struct node *z;

z=(struct node *)malloc(sizeof(struct node));


z->data=x;
z->left=NULL;
z->right=NULL;
p->right=z;
}
else
{
insert(p->right,x);
}
}
}
void preorder(struct node *p)
{
if(p!=NULL)
{
printf("%d\n",p->data);
preorder(p->left);
preorder(p->right);
}
}
void postorder(struct node *p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
printf("%d\n",p->data);
}
}
void inorder(struct node *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("%d\n",p->data);
inorder(p->right);
}
}
void main()
{

int x,y,z,num;
root=(struct node *)malloc(sizeof(struct node));
printf("Enter data of root: ");
scanf("%d",&num);
root->data=num;
root->right=root->left=NULL;
do
{
printf("Press 1 for preorder\n");
printf("Press 2 for postorder\n");
printf("Press 3 for inorder\n");
printf("Press 4 for insert\n");
printf("Press 5 for Exit\n");
scanf("%d",&x);
if(x==1)
{
preorder(root);
}
else if(x==2)
{
postorder(root);
}
else if(x==3)
{
inorder(root);
}
else if(x==4)
{
printf("\nEnter the data for inserting: ");
scanf("%d",&y);
insert(root,y);
}
else if(x==5)
{
exit(0);
}
else
{
printf("Invalid choice");
}
printf("\nPress 1 for continue\n");

scanf("%d",&z);
}
while(z==1);
getch();
}

Travelling Salespersons Problem


Problem Statement
Find the order of cities in which a salesman should travel in order to start from a city, reaching
back the same city by visiting all rest of the cities each only once and traveling minimum
distance for the same.

Algorithm
MIN_DISTANCE_LENGTH INFINITY
for each cities C in the graph G
for each cities C_N in the graph G such that C and C_N are different
mark all cities unvisited
mark C as visited
for staring cities as C and succeeding cities as C_N, find Cost
such that path staring from C_N in that Cost is minimum

cost from V_N for all unvisited vertices by


visiting each cities.( this path is obtained by greedy method).
if currently obtained City cost <= MIN_DISTANCE_LENGTH then
set MIN_DISTANCE_LENGTH=newly obtained value
copy the new city as minimal cost
end if
end for C_N
end for C

Pseudo Code
Input : Number of cities n and array of costs c(i,j) i,j=1,..n (We begin from city number 1)
Output:Vector of cities and total cost.
(* starting values *)
C=0
cost=0
visits=0
e=1 (*e=pointer of the visited city)
(* determination of round and cost)
for r=1 to n-1 do
choose of pointer j with
minimum=c(e,j)=min{c(e,k);visits(k)=0 and k=1,..,n}
cost=cost+minimum
e=j
C(r)=j
end r-loop
C(n)=1
cost=cost+c(e,1)
We can find situations in which the TSP algorithm doesnt give the best solution. We can also
succeed on improving the algorithm. For example we can apply the algorithm t times for t
different cities and keep the best round every time. We can also unbend the greeding in such a
way to reduce the algorithm problem that is there is no room for choosing cheep sides at the end
of algorithm because the cheapest sides have been exhausted.

You might also like