Manish Vishwakarma Software
Manish Vishwakarma Software
SESSION – 2020-21
by
Manish Vishwakarma
Roll no. - 513304920002
Enrollment no. – BJ5659
Submitted to
Dr. Dipti Verma
S.no
Experiments Date Remark
.
INDEX
EXPERIMENT - 01
#include <stdio.h>
void main()
{
int heap[10], no, i, j, c, root, temp;
}
root = c;
} while (c < j);
}
printf("\n The sorted array is : ");
for (i = 0; i < no; i++)
printf("\t %d", heap[i]);
printf("\n Complexity : \n Best case = Avg case = Worst case = O(n
logn) \n");
}
#include<stdio.h>
#include<math.h>
int board[20],count;
int main()
{
int n,i,j;
void queen(int row,int n);
for(i=1;i<=n;++i)
printf("\t%d",i);
for(i=1;i<=n;++i)
{
printf("\n\n%d",i);
for(j=1;j<=n;++j) //for nxn board
{
if(board[i]==j)
printf("\tQ"); //queen at i,j position
else
printf("\t-"); //empty slot
}
}
}
#include <stdio.h>
return K[n][W];
}
int main()
{
int val[] = {60, 100, 120};
int wt[] = {10, 20, 30};
int W = 50;
int n = sizeof(val)/sizeof(val[0]);
printf("\nValue = %d", knapsack(W, wt, val, n));
return 0;
}
#include<stdio.h>
void DFS(int);
int G[10][10],visited[10],n; //n is no of vertices and graph is sorted in array
G[10][10]
void main()
{
int i,j;
printf("Enter number of vertices:");
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
DFS(0);
}
void DFS(int i)
{
int j;
printf("\n%d",i);
visited[i]=1;
for(j=0;j<n;j++)
if(!visited[j]&&G[i][j]==1)
DFS(j);
}
OUTPUT:-
BFS: #include<iostream>
#include<list>
void Graph::BFS(int s)
{
// Mark all the vertices as not visited
bool *visited = new bool[V];
for(int i = 0; i < V; i++)
visited[i] = false;
while(!queue.empty())
{
// Dequeue a vertex from queue and print it
s = queue.front();
cout << s << " ";
queue.pop_front();
return 0;
}
Aim - Write a program to find the largest common subsequence for the
given graph
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int i,j,m,n,matrix[10][10];
return;
if(matrix2[i][j]=='c')
{
print(i-1,j-1); /*recursive call */
printf("%c",y[j-1]);
print(i-1,j);
else
print(i,j-1);
}
m=strlen(x);
n=strlen(y);
for(i=0;i<=m;i++)
matrix[i][0]=0;
matrix2[i][0]='0';
for(j=0;j<=n;j++)
matrix[0][j]=0;
matrix2[0][j]='0';
\
}
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
if(x[i-1]==y[j-1])
matrix[i][j]=matrix[i-1][j-1]+1;
matrix2[i][j]='c';
else if(matrix[i-1][j]>=matrix[i][j-1])
{
matrix[i][j]=matrix[i-1][j];
else
{
matrix[i][j]=matrix[i][j-1];
matrix2[i][j]='l';
}
}
int main()
{
printf("\nEnter the first sequence: \n");
scanf("%s",x);
printf("\nEnter the second sequence: \n");
scanf("%s",y);
lcs();
printf("\nThe longest common subsequence is: \n");
print(m,n);
return 0
OUTPUT:-
EXPERIMENT – 06
Aim - Write a program to find the shortest path for each pair of source
and destination by using Dijkstra’s Algorithm
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
int shortest(int ,int);
int cost[10][10],dist[20],i,j,n,k,m,S[20],v,totcost,path[20],p;
main()
{
int c;
cout <<"enter no of vertices";
cin >> n;
cout <<"enter no of edges";
cin >>m;
cout <<"\nenter\nEDGE Cost\n";
for(k=1;k<=m;k++)
{
cin >> i >> j >>c;
cost[i][j]=c;
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]==0)
cost[i][j]=31999;
cout <<"enter initial vertex";
cin >>v;
cout << v<<"\n";
shortest(v,n);
}
OUTPUT:-
enter no of vertices6
enter no of edges11
enter
EDGE Cost
1 2 50
1 3 45
1 4 10
2 3 10
2 4 15
3 5 30
4 1 10
4 5 15
5 2 20
5 3 35
653
enter initial vertex1
1
14
145
1452
13
EXPERIMENT – 06
Server side
//*******************************udp_server.c*******************************//
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int sock;
int IPlength, TotalByte;
char message[512];
struct sockaddr_in serverAdd , clientAdd;
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
perror("Socket Creation Failed");
exit(1);
}
serverAdd.sin_family = AF_INET;
serverAdd.sin_port = htons(5000);
serverAdd.sin_addr.s_addr = INADDR_ANY;
bzero(&(serverAdd.sin_zero),8);
if (bind(sock,(struct sockaddr *)&serverAdd,sizeof(struct sockaddr)) == -1)
{
perror("Bind Error");
exit(1);
}
IPlength = sizeof(struct sockaddr);
printf("\nUDP Server Waiting for client Message\n");
fflush(stdout);
while (1)
{
TotalByte = recvfrom(sock,message,512,0,(struct sockaddr *)&clientAdd,
&IPlength);
message[TotalByte] = '\0';
printf("Received Message: %s", message);
fflush(stdout);
}
return 0;
}
//*******************************udp_server.c*******************************//
Save the file in directory and compile it. After successful compilation, run the
executable file from command terminal. You may refer to the tutorial How to
make first C program in Linux if you are not aware of compilation and
execution process.
Client side
//*******************************udp_client.c*******************************//
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int sock;
struct sockaddr_in serverAdd;
struct hostent *host;
char sendMessage[512];
host= (struct hostent *) gethostbyname((char *)"Server’s IP address");
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
perror("socket Creation Failed");
exit(1);
}
serverAdd.sin_family = AF_INET;
serverAdd.sin_port = htons(5000);
serverAdd.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(serverAdd.sin_zero),8);
while (1)
{
printf("Enter the Message:");
fgets(sendMessage,100,stdin);
sendto(sock, sendMessage, strlen(sendMessage), 0,(struct sockaddr
*)&serverAdd, sizeof(struct sockaddr));
memset (sendMessage , 0 , sizeof(sendMessage));
}
}