0% found this document useful (0 votes)
134 views

Algorithm Assignment

The document contains algorithms written by Vinod Jatav for their Algorithm Analysis and Design course. It includes 5 algorithms: [1] Depth First Search, [2] Kruskal's algorithm, [3] string permutation, [4] Prim's algorithm, and [5] the travelling salesman problem. For each algorithm, pseudocode is provided along with sample input/output.

Uploaded by

vinod jatav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
134 views

Algorithm Assignment

The document contains algorithms written by Vinod Jatav for their Algorithm Analysis and Design course. It includes 5 algorithms: [1] Depth First Search, [2] Kruskal's algorithm, [3] string permutation, [4] Prim's algorithm, and [5] the travelling salesman problem. For each algorithm, pseudocode is provided along with sample input/output.

Uploaded by

vinod jatav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Algorithm_Assignment-01

Name: Vinod Jatav


Roll No. : IIITU16110
Subject : Analysis and Design of Algorithm
Branch : CSE(IIIT-Una)

List of implemented algorithm :

1) Write an algorithm to implement Depth First Search (DFS) with output.

2) Write an algorithm to implement Kruskal’s algorithm with output.

3) Write an algorithm to find out the Permutation of given string with output.

4) Write an algorithm to implement Prims algorithm with output.

5) Write an algorithm to implement travellingSalesman problem with output.


1) Write an algorithm to implement Depth First Search (DFS) with
output.

Code:

#include <bits/stdc++.h>
using namespace std;
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];
int main()
{
int m;
//clrscr();
cout <<"Enter no of vertices:";
cin >> n;
cout <<"Enter no of edges:";
cin >> m;
cout <<"\nEDGES \n";
for(k=1; k<=m; k++)
{
cin >>i>>j;
cost[i][j]=1;
}
cout <<"Enter initial vertex to traverse from:";
cin >>v;
cout <<"DFS ORDER OF VISITED VERTICES:";
cout << v <<" ";
visited[v]=1;
k=1;
while(k<n)
{
for(j=n; j>=1; j--)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
stk[top]=j;
top++;
}
v=stk[--top];
cout<<v << " ";
k++;
visit[v]=0;
visited[v]=1;
}

return 0;
}
Output:
2) Write an algorithm to implement Kruskal’s algorithm with output.

Code:

#include<stdio.h>
#include<stdlib.h>
int i,j,k,a,b,u,v,n,ne=1;
int min,mincost=0,cost[9][9],parent[9];
int find(int);
int uni(int,int);
void main()
{

printf("\n\tImplementation of Kruskal's algorithm\n");


printf("\nEnter the no. of vertices:");
scanf("%d",&n);
printf("\nEnter the cost adjacency matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
}
printf("The edges of Minimum Cost Spanning Tree are\n");
while(ne < n)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j <= n;j++)
{
if(cost[i][j] < min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
u=find(u);
v=find(v);
if(uni(u,v))
{
printf("%d edge (%d,%d) =%d\n",ne++,a,b,min);
mincost +=min;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n\tMinimum cost = %d\n",mincost);
}
int find(int i)
{
while(parent[i])
i=parent[i];
return i;
}
int uni(int i,int j)
{
if(i!=j)
{
parent[j]=i;
return 1;
}
return 0;
}

Output:
3) Write an algorithm to find out the Permutation of given string with output.

Code:

#include<bits/stdc++.h>
using namespace std;
void swap(char *x, char *y)
{
char temp ;
temp = *x;
*x = *y;
*y = temp;
}
void permutation(char *a, int l, int r)
{
if(l==r)
{
cout<<a<<endl;
}
else
{
int i;
for(i=l;i<=r;i++)
{
swap(a+l,a+i);
permutation(a,l+1,r);//Backtracking
swap(a+l,a+i);
}
}
}
int main()
{
char s[100];
cout<<"Enter the string to find out its Permutation :"<<endl;
cin>>s;
int l = strlen(s);
cout<<"The Permutation of given string are:"<<endl;
permutation(s,0,l-1);
return 0;
}
Output:
4) Write an algorithm to implement Prims algorithm with output.

Code:

#include <bits/stdc++.h>
using namespace std;
#define INF 9999999
#define V 5
int G[V][V] = { {0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0}
};
int main () {

int no_edge; // number of edge


int selected[V];
memset (selected, false, sizeof (selected));

// set number of edge to 0


no_edge = 0;

// choose 0th vertex and make it true


selected[0] = true;

int x; // row number


int y; // col number

// print for edge and weight


cout << "Edge" << " : " << "Weight";
cout << endl;
while (no_edge < V - 1)
{
int min = INF;
x = 0;
y = 0;

for (int i = 0; i < V; i++) {


if (selected[i]) {
for (int j = 0; j < V; j++) {
if (!selected[j] && G[i][j]) { // not in selected and there is an edge
if (min > G[i][j]) {
min = G[i][j];
x = i;
y = j;
}
}
}
}
}
cout << x << " - " << y << " : " << G[x][y];
cout << endl;
selected[y] = true;
no_edge++;
}

return 0;
}

Output:
5) Write an algorithm to implement travellingSalesman problem with output.

Code:

#include<bits/stdc++.h>
using namespace std;

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

void takeInput()
{
int i,j;

cout<<"Enter the number of villages(means no. of nodes): ";


cin>>n;

cout<<"\nEnter the Cost Matrix\n";

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


{
cout<<"\nEnter 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\nThe Path is:\n";


mincost(0); //passing 0 because starting vertex

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


return 0;
}
Output:

You might also like