AOA Lab Manual
AOA Lab Manual
LAB MANUAL
C
Branch : Computer Science engineering
INDEX
S. No. Contents Page
No.
1 Vision and Mission of the Institute iv
2 Vision and Mission of the Department v
3 Program Educational Objectives (PEOs) vi
C
4 Program Outcomes (POs) vii
5 PSO of the Department (PSOs) viii
6 RTU Syllabus with List of Experiments ix-x
7 Course Outcomes (COs) xi-xii
8 CO/PO-PSO mapping xiii
CR
9 Introduction about Lab &its Applications xiv
10 Instructions Sheet xv
Experiment List (As per RTU, Kota Syllabus)
Sort a given set of elements using the Quicksort method and determine
the time required to sort the elements. Repeat the experiment for
Experiment 1 different values of n, the number of elements in the list to be sorted and
plot a graph of the time taken versus n. The elements can be read from
a file or can be generated using the random number generator.
Experiment 2 Implement a parallelized Merge Sort algorithm to sort a given set of
elements and determine the time required to sort the elements. Repeat
JE
Experiment 6 From a given vertex in a weighted connected graph, find shortest paths
to other vertices using Dijkstra's algorithm.
Experiment 7 Find Minimum Cost Spanning Tree of a given undirected graph using
Kruskal's algorithm.
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
Experiment 8 a. Print all the nodes reachable from a given starting node in a digraph
using BFS method. b. Check whether a given graph is connected or not
using DFS method.
Experiment 9 Find Minimum Cost Spanning Tree of a given undirected graph using
Prim’s algorithm.
C
10
15
C
CR
JE
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
To become a renowned centre of outcome based learning, and work towards academic,
professional, cultural and social enrichment of the lives of individuals and communities.
C
Mission of the Institute
CR
M1: Focus on evaluation of learning outcomes and motivate students to inculcate research
aptitude by project based learning.
M2: Identify, based on informed perception of Indian, regional and global needs, areas of
focus and provide platform to gain knowledge and solutions.
M4: Develop human potential to its fullest extent so that intellectually capable and
imaginatively gifted leaders can emerge in a range of professions.
JE
5
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
To become renowned Centre of excellence in Computer Science and Engineering and make
competent engineers & professionals with high ethical values prepared for lifelong learning.
C
M1: To impart outcome based education for emerging technologies in the field of Computer
Science and Engineering.
CR
M2: To provide opportunities for interaction between academia and industry.
M3: To provide platform for lifelong learning by accepting the change in technologies
M4: To develop aptitude of fulfilling social responsibilities
JE
6
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
PEO1: To provide students with the fundamentals of Engineering Sciences with more
emphasis in Computer Science & Engineering by way of analysing and exploiting
engineering challenges.
PEO2: To train students with good scientific and engineering knowledge so as to
comprehend, analyse, design, and create novel products and solutions for the real life
C
problems in Computer Science and Engineering
PEO3: To inculcate professional and ethical attitude, effective communication skills,
teamwork skills, multidisciplinary approach, entrepreneurial thinking and an ability to relate
engineering issues with social issues for Computer Science & Engineering.
CR
PEO4: To provide students with an academic environment aware of excellence, leadership,
written ethical codes and guidelines, and the self-motivated life-long learning needed for a
successful professional career in Computer Science & Engineering.
PEO5: To prepare students to excel in Industry and Higher education by Educating Students
along with High moral values and Knowledge in Computer Science & Engineering.
JE
7
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
C
3. Design/development of solutions: Design solutions for complex Computer Science and
Engineering problems and design system components or processes that meet the specified
needs with appropriate consideration for the public health and safety, and the cultural,
societal, and environmental considerations.
CR
4. Conduct investigations of complex problems: Use research-based knowledge and research
methods including design of Computer Science and Engineering experiments, analysis and
interpretation of data, and synthesis of the information to provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modelling to complex Computer
Science Engineering activities with an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant
to the professional Computer Science and Engineering practice.
JE
8
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
C
PROGRAM SPECIFIC OUTCOMES (PSOs)
PSO1. Ability to interpret and analyze network specific and cyber security issues, automation in
CR
real word environment.
PSO2. Ability to Design and Develop Mobile and Web-based applications under realistic
constraints.
JE
9
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
Branch: Computer Science & Engineering Examination Time = Two (2) Hours
C
Schedule (Per Week Practical Hrs.): Two
Maximum Marks = 50
(2)
[Sessional /Mid-term (30) & End-term
(20)
CR
5CS4-23: Analysis of Algorithms Lab
number of elements in the list to be sorted and plot a graph of the time taken
versus n. The elements can be read from a file or can be generated using the
random number generator.
2 Implement a parallelized Merge Sort algorithm to sort a given set of elements and
determine the time required to sort the elements. Repeat the experiment for
different values of n, the number of elements in the list to be sorted and plot a
graph of the time taken versus n. The elements can be read from a file or can be
generated using the random number generator.
From a given vertex in a weighted connected graph, find shortest paths to other
10
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
Find Minimum Cost Spanning Tree of a given undirected graph using Kruskal's
6
algorithm.
a. Print all the nodes reachable from a given starting node in a digraph using BFS
7 method. b. Check whether a given graph is connected or not using DFS method.
C
Find Minimum Cost Spanning Tree of a given undirected graph using Prim’s
8.
algorithm.
Course Outcomes
11
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
C
Mapping of Experiments with Cos & BT Level
SN List of Experiments COs BT
Level
1 Sort a given set of elements using the Quicksort method and CO1 BT3
CR
determine the time required to sort the elements. Repeat the
experiment for different values of n, the number of elements
in the list to be sorted and plot a graph of the time taken
versus n. The elements can be read from a file or can be
generated using the random number generator.
2 Implement a parallelized Merge Sort algorithm to sort a CO1 BT3
given set of elements and determine the time required to sort
the elements. Repeat the experiment for different values of n,
the number of elements in the list to be sorted and plot a
graph of the time taken versus n. The elements can be read
from a file or can be generated using the random number
JE
generator.
3 a. Obtain the Topological ordering of vertices in a given CO1 BT3
digraph. b. Compute the transitive closure of a given directed
graph using Warshall's algorithm.
4 Implement Fractional Knapsack problem using Greedy CO3 BT3
Method.
12
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
C
* BT - Bloom's Taxonomy
CR
Project Management and Finance
Environment and Sustainability
Design/Development of Solution
Life-long Learning
Problem analysis
Communication
JE
Ethics
PO PO PO PO PO PO PO PO PO PO PO PO PSO PSO
1 2 3 4 5 6 7 8 9 10 11 12 1 2
CO-1 3 3 3 3 - - - - - - - 2 1 2
CO-2 3 3 3 3 3 1 - - - - - 2 2 2
CO-3 3 3 3 3 3 1 - - - - - 2 2 2
13
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
This laboratory is focused towards reinforcing the learning of students in the field of
developing and analyzing algorithms and applying those learning in practical environments.
The experiments in laboratory cover concepts on the developing and analyzing algorithms
including, but not limited to, Identify and evaluate complex problems using principles of
mathematics and engineering science.
Experiments performed in this course will provide students a better understanding about
C
Solve problems by applying appropriate algorithms.
• Analyze the efficiency of various algorithms.
• Apply techniques of stacks and queues to solve problems.
• Develop a program that can be solved in many ways using different techniques.
CR
• Identify and evaluate complex problems using principles of mathematics and engineering
science.
The understanding of Identify and evaluate complex problems developed in this laboratory,
along-with theoretical concepts, will help students in design and analysis of algorithms. This
will enhance the employability of students in Software development, testing and research
industry.
JE
INSTRUCTIONS SHEET
We need your full support and cooperation for smooth functioning of the lab.
14
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
DO’s
C
Properly shutdown the system before leaving the lab.
Keep the bag outside in the racks.
Enter the lab on time and leave at proper time.
Maintain the decorum of the lab.
Utilize lab hours in the corresponding experiment.
Get your Cd / Pen drive checked by Lab in Charge before using it in the lab.
CR
DON’Ts
Don’t carry any lab equipment outside the lab.
All the students are supposed to prepare the theory regarding the next program.
Students are supposed to bring the practical file and the lab copy.
Previous programs should be written in the practical file.
Any student not following these instructions will be denied entry in the lab.
15
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
Each student should work on his/her assigned computer at each turn of the lab.
Take responsibility of valuable accessories.
Concentrate on the assigned practical and do not play games.
If anyone caught red handed carrying any equipment of the lab, then he will have to
face serious consequences.
C
CR
JE
16
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
Experiment No. 1
AIM:
Experiment-1
Aim - Sort a given set of elements using the Quick Sort method and determine the time
required to sort the elements. The elements can be read from a file or can be generated
using the random number generator.
C
/* program to sort elements of an array using Quick Sort */
#include<stdio.h>
#include<conio.h>
17
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
while(pivot>a[i]&&i<=high)
i++;
while(pivot<a[j]&&j>=low)
j--;
if(i<j)
{
t=a[i];
a[i]=a[j];
C
a[j]=t;
}
else
break;
}
a[low]=a[j];
CR
a[j]=pivot;
quicksort(a,low,j-1);
quicksort(a,j+1,high);
}
}
JE
18
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
Experiment – 2
Aim - Implement Merge Sort algorithm to sort a given set of elements and determine
the time required to sort the elements. The elements can be read from a file or can be
generated using the random number generator.
#include<stdio.h>
#include<conio.h>
void disp();
C
void mergesort(int,int,int);
void msortdiv(int,int);
int a[50],n;
void main()
{
CR
int i;
clrscr();
printf("\nEnter the n value:");
scanf("%d",&n);
printf("\nEnter elements for an array:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nBefore Sorting the elements are:");
disp();
msortdiv(0,n-1);
printf("\nAfter Sorting the elements are:");
disp();
JE
getch( );
}
void disp( )
{
int i;
for(i=0;i<n;i++)
printf("%d ",a[i]);
}
if(a[i]>=a[j])
t[k++]=a[j++];
else
t[k++]=a[i++];
}
while(i<=mid)
t[k++]=a[i++];
while(j<=high)
t[k++]=a[j++];
C
for(i=low;i<=high;i++)
a[i]=t[i];
}
20
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
Experiment – 3
#include <stdio.h>
C
int main(){
int i,j,k,n,a[10][10],indeg[10],flag[10],count=0;
for(i=0;i<n;i++){
indeg[i]=0;
flag[i]=0;
}
JE
for(i=0;i<n;i++)
for(j=0;j<n;j++)
indeg[i]=indeg[i]+a[j][i];
while(count<n){
for(k=0;k<n;k++){
if((indeg[k]==0) && (flag[k]==0)){
printf("%d ",(k+1));
flag [k]=1;
}
for(i=0;i<n;i++){
if(a[i][k]==1)
21
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
indeg[k]--;
}
}
count++;
}
return 0;
}
C
b. Compute the transitive closure of a given directed graph using Warshall's algorithm.
//Compute the transitive closure of a given directed graph using Warshall's algorithm.
CR
# include <stdio.h>
# include <conio.h>
int n,a[10][10],p[10][10];
void path()
{
int i,j,k;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
JE
p[i][j]=a[i][j];
for(k=0;k<n;k++)
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(p[i][k]==1&&p[k][j]==1) p[i][j]=1;
}
void main()
{
int i,j;
clrscr();
printf("Enter the number of nodes:");
22
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
path();
C
printf("\nThe path matrix is showm below\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
CR
printf("%d ",p[i][j]);
printf("\n");
}
getch();
}
JE
Experiment – 4
23
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
C
u = capacity;
for (i = 0; i < n; i++)
x[i] = 0.0;
for (i = 0; i < n; i++)
{
if(weight[i] > u)
CR
break;
else
{
x[i] = 1.0;
tp = tp + profit[i];
u = u - weight[i];
}
}
if(i < n)
x[i] = u / weight[i];
tp = tp + (x[i] * profit[i]);
printf("\nThe result vector is:- ");
JE
void main()
{
float weight[20], profit[20], capacity;
int num, i, j;
float ratio[20], temp;
clrscr();
printf("\nEnter the no. of objects:- ");
scanf("%d", &num);
printf("\nEnter the profits and wts of %d object ",num);
for (i = 0; i < num; i++)
{
24
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
C
for (i = 0; i < num; i++)
{
for (j = i + 1; j < num; j++)
{
if (ratio[i] < ratio[j])
{
CR
temp = ratio[j];
ratio[j] = ratio[i];
ratio[i] = temp;
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
JE
}
}
}
knapsack(num, weight, profit, capacity);
getch();
}
25
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
Experiment – 5
Aim - Implement 0/1 Knapsack problem using Dynamic Programming.
#include <conio.h>
#include <stdio.h>
void knapsack01(int v[],int w[],int n,int capacity)
{
int x=0,b[100][100],i=0;
for(x=0;x<=capacity;x++)
C
b[0][x]=0;
for(i=1;i<=n;i++)
b[i][0]=0;
for(i=1;i<=n;i++)
{
CR
for(x=1;x<=capacity;x++)
{
if(w[i]<=x)
{
if( (v[i]+b[i-1][x-w[i]] ) > b[i-1][x] )
b[i][x]= (v[i]+b[i-1][x-w[i]] ) ;
else
b[i][x]=b[i-1][x];
}
else
b[i][x]=b[i-1][x];
}
JE
}
printf("the matix is\n");
for(i=0;i<=n;i++)
{
for(x=0;x<=capacity;x++)
printf("\t%d",b[i][x]);
printf("\n");
}
printf("the benifits is \n%d",b[n][capacity]);
printf("\nthe selected items is\n");
i=n,x=capacity;
while(i>0 && x>0)
{
if(b[i][x]!=b[i-1][x])
{
printf("\t%d",i);
b[i][x]=b[i-1][x-w[i]];
26
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
x=x-w[i];
i=i-1;
}
else
{
b[i][x]=b[i-1][x];
i=i-1;
}
}
C
}
void main()
{
int i,n,capacity,v[100],w[100];
CR
clrscr();
printf("enter the total capacity of knapsack \n");
scanf("%d",&capacity);
printf("enter the no of items\n");
scanf("%d",&n);
//value of each item's
printf("enter the weight and value of items\n");
for(i=1;i<=n;i++)
scanf("%d %d",&w[i],&v[i]);
knapsack01(v,w,n,capacity);
getch();
JE
27
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
Experiment – 6
From a given vertex in a weighted connected graph, find shortest paths to other
vertices using Dijkstra's algorithm.
#include<stdio.h>
#include<conio.h>
C
#define INFINITY 9999
#define MAX 10
int main()
CR
{
int G[MAX][MAX],i,j,n,u;
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
printf("\nEnter the starting node:");
scanf("%d",&u);
JE
dijkstra(G,n,u);
return 0;
}
int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;
//pred[] stores the predecessor of each node
//count gives the number of nodes seen so far
//create the cost matrix
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=INFINITY;
28
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
else
cost[i][j]=G[i][j];
//initialize pred[],distance[] and visited[]
for(i=0;i<n;i++)
{
distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;
C
}
distance[startnode]=0;
visited[startnode]=1;
count=1;
while(count<n-1)
{
CR
mindistance=INFINITY;
//nextnode gives the node at minimum distance
for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
{
mindistance=distance[i];
nextnode=i;
}
//check if a better path exists through nextnode
visited[nextnode]=1;
JE
for(i=0;i<n;i++)
if(!visited[i])
if(mindistance+cost[nextnode][i]<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}
j=i;
do
{
j=pred[j];
printf("<-%d",j);
}while(j!=startnode);
}
}
C
CR
JE
Experiment – 7
30
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
Aim - Find Minimum Cost Spanning Tree of a given undirected graph using Kruskal's
algorithm.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int i,j,k,a,b,u,v,n,ne=1;
C
int min,mincost=0,cost[9][9],parent[9];
int find(int);
int uni(int,int);
CR
void main()
{
clrscr();
printf("\n\n\tImplementation of Kruskal's algorithm\n\n");
printf("\nEnter the no. of vertices\n");
scanf("%d",&n);
printf("\nEnter the cost adjacency matrix\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
JE
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
}
printf("\nThe edges of Minimum Cost Spanning Tree are\n\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;
31
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
}
}
}
printf("The min value is %d",min);
u=find(u);
v=find(v);
if(uni(u,v))
{
printf("\n%d edge (%d,%d) =%d\n",ne++,a,b,min);
C
mincost +=min;
}
cost[a][b]=cost[b][a]=999; //remove the minimum edge
}
printf("\n\tMinimum cost = %d\n",mincost);
getch();
CR
}
int find(int i)
{
while(parent[i])
i=parent[i];
return i;
}
if(i!=j)
{
parent[j]=i;
return 1;
}
return 0;
}
Experiment – 8
32
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
a. Print all the nodes reachable from a given starting node in a digraph using
BFS method.
b. Check whether a given graph is connected or not using DFS method.
a. Print all the nodes reachable from a given starting node in a digraph using BFS
C
method.
#include<stdio.h>
#include<conio.h>
CR
int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1;
void bfs(int v)
{
for(i=1;i<=n;i++)
if(a[v][i] && !visited[i])
q[++r]=i;
if(f<=r)
{
JE
visited[q[f]]=1;
bfs(q[f++]);
}
}
void main()
{
int v;
clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&n);
for(i=1;i<=n;i++)
33
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
{
q[i]=0;
visited[i]=0;
}
printf("\n Enter graph data in matrix form:\n");
C
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the starting vertex:");
CR
scanf("%d",&v);
bfs(v);
printf("\n The node which are reachable are:\n");
for(i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i);
getch();
}
JE
Output:
34
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
#include<stdio.h>
#include<conio.h>
int a[20][20],reach[20],n;
void dfs(int v)
C
{
int i;
reach[v]=1;
for(i=1;i<=n;i++)
CR
if(a[v][i] && !reach[i])
{
printf("\n %d->%d",v,i);
dfs(i);
}
}
void main()
{
JE
int i,j,count=0;
clrscr();
printf("\n Enter number of vertices:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
reach[i]=0;
for(j=1;j<=n;j++)
a[i][j]=0;
}
printf("\n Enter the adjacency matrix:\n");
35
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
dfs(1);
printf("\n");
C
for(i=1;i<=n;i++)
{
if(reach[i])
count++;
CR
}
if(count==n)
printf("\n Graph is connected");
else
printf("\n Graph is not connected");
getch();
}
JE
Output
36
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
C
CR
Experiment – 9
Aim - Find Minimum Cost Spanning Tree of a given undirected graph using Prim’s
algorithm.
JE
#include<stdio.h>
#include<conio.h>
int a,b,u,v,n,i,j,ne=1;
int visited[10]={0},min,idx,mincost=0,cost[10][10];
void main()
{
clrscr();
printf("\n Enter the number of nodes:");
scanf("%d",&n);
printf("\n Enter the 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)
37
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
cost[i][j]=999;
}
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
{
idx=i;
printf("the value of i is %d\t",i);
}
C
visited[idx]=1;
printf("\n");
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
CR
if(cost[i][j]<min)
if(visited[i]!=0)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0 || visited[v]==0)
{
printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min);
mincost+=min;
JE
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n Minimun cost=%d",mincost);
getch();
}
38
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
C
CR
Experiment – 10
Aim: Implement All-Pairs Shortest Paths Problem using Floyd's algorithm.
JE
#include<stdio.h>
int min(int,int);
void floyds(int p[10][10],int n)
{
int i,j,k;
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(i==j)
p[i][j]=0;
else
p[i][j]=min(p[i][j],p[i][k]+p[k][j]);
}
int min(int a,int b)
39
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
{
if(a<b)
return(a);
else
return(b);
}
void main()
{
int p[10][10],w,n,e,u,v,i,j;;
C
printf("\n Enter the number of vertices:");
scanf("%d",&n);
printf("\n Enter the number of edges:\n");
scanf("%d",&e);
for(i=1;i<=n;i++)
{
CR
for(j=1;j<=n;j++)
p[i][j]=999;
}
for(i=1;i<=e;i++)
{
printf("\n Enter the end vertices of edge%d with its weight \n",i);
scanf("%d%d%d",&u,&v,&w);
p[u][v]=w;
}
printf("\n Matrix of input data:\n");
for(i=1;i<=n;i++)
JE
{
for(j=1;j<=n;j++)
printf("%d \t",p[i][j]);
printf("\n");
}
floyds(p,n);
printf("\n Transitive closure:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d \t",p[i][j]);
printf("\n");
}
printf("\n The shortest paths are:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
40
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
if(i!=j)
printf("\n <%d,%d>=%d",i,j,p[i][j]);
}
}
Output
C
CR
JE
41
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
C
CR
JE
42
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
Experiment – 11
Aim - Implement N Queen's problem using Back Tracking.
#include <stdio.h>
#include<conio.h>
int row[8],s=0;
int safe(int,int);
void putboard();
C
void queen(int);
void putboard()
{
int x,y;
printf("\nSolution # %d",++s);
printf(":\n---------------------------------\n");
for(y=0;y<8; y++)
JE
{
for (x=0;x<8;x++)
if(x==row[y])
printf("| Q ");
else
printf("| ");
printf("|\n---------------------------------\n");
}
getch();
}
void queen(int y)
{
int x;
for(x=0;x<8;x++)
{
row[y-1]=x;
43
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
if( safe(x,y-1) )
if (y<8)
queen(y+1);
else
putboard();
}
}
void main()
{
C
clrscr();
queen(1);
}
CR
JE
44
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
Experiment – 12
Aim - Implement Bubble Sort algorithm to sort a given set of elements and determine
the time required to sort the elements. The elements can be read from a file or can be
generated using the random number generator.
#include<stdio.h>
#include<conio.h>
void main()
C
{
int i,n,temp,j,arr[25];
clrscr();
printf("Enter the number of elements in the Array: ");
scanf("%d",&n);
printf("\nEnter the elements:\n\n");
CR
for(i=0 ; i<n ; i++)
{
printf(" Array[%d] = ",i);
scanf("%d",&arr[i]);
}
for(i=0 ; i<n ; i++)
{
for(j=0 ; j<n-i-1 ; j++)
{
if(arr[j]>arr[j+1]) //Swapping Condition is Checked
{
temp=arr[j];
JE
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("\nThe Sorted Array is:\n\n");
for(i=0 ; i<n ; i++)
{
printf(" %d",arr[i]);
}
getch();
}
45
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
Experiment – 13
Aim - Implement Linear Search algorithm to search a value from a given set of
elements. The elements can be read from a file or can be generated using the random
number generator.
//Write a C program that searches a value in a stored array using linear search.
#include<stdio.h>
#include<conio.h>
C
int linear(int [ ],int,int);
void main( )
{
int a[20], pos = -1, n, k, i;
clrscr( );
CR
printf("\nEnter the n value:");
scanf("%d",&n);
printf("\nEnter elements for an array:");
for(i=0; i<n ;i++)
scanf("%d",&a[i]);
printf("\nEnter the element to be searched:");
scanf("%d",&k);
pos=linear(a,n,k);
if(pos != -1)
printf("\n Search successful element found at position %d",pos);
else
JE
////////////////////////////////////////////////////////////////////
46
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
//Write a C program that searches a value in a stored array using recursive linear search.
/* recursive program for Linear Search*/
#include<stdio.h>
#include<conio.h>
void main( )
{
C
int a[20],pos=-1,n,k,i;
clrscr();
printf("\nEnter n value:");
scanf("%d",&n);
printf("\nEnter elements for an array:");
for(i=0;i<n;i++)
CR
scanf("%d",&a[i]);
printf("\n Enter the element to be searched:");
scanf("%d",&k);
pos=linear(a,n,k);
if(pos!=-1)
printf("\n Search successful, Element found at Position %d",pos);
else
printf("Search unsuccessful, element not found ");
getch( );
}
JE
47
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
Experiment – 14
Aim - Implement Binary Search algorithm to search a value from a given set of
elements. The elements can be read from a file or can be generated using the random
number generator.
//Write a C program that searches a value in a stored array using non recursive binary search.
C
#include<stdio.h>
#include<conio.h>
void main( )
CR
{
int a[20],pos,n,k,i;
clrscr();
printf("\nEnter the n value:");
scanf("%d",&n);
printf("\nEnter elements for an array:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter the key value:");
scanf("%d",&k);
pos=bsearch(a,n,k);
if(pos!= -1)
JE
48
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
else if(k>a[mid])
lb=mid+1;
else if(k==a[mid])
return(mid);
}
return -1;
}
///////////////////////////////////////////////////////////////////////////////////////
C
// Write a program for recursive binary search to find the given element within array
#include<stdio.h>
#include<conio.h>
CR
int bsearch(int [ ],int, int, int);
void main( )
{
int a[20],pos,n,k,i,lb,ub;
clrscr( );
printf("\nEnter the n value:");
scanf("%d",&n);
printf("\nEnter elements for an array:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
JE
49
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
mid=(lb+ub)/2;
if(k<a[mid])
{
ub=mid-1;
}
else if(k>a[mid])
{
lb=mid+1;
}
C
else if(k==a[mid])
{
return(mid);
}
return(bsearch(a,k,lb,ub));
}
CR
return -1;
}
JE
50
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
Experiment – 15
Aim - Compute two 2 by 2 matrix multiplication to find multiplication matrix using
Strassen’s Matrix Multiplication.
//C code of two 2 by 2 matrix multiplication using Strassen algorithm:
C
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2],b[2][2],c[2][2],i,j;
CR
int m1,m2,m3,m4,m5,m6,m7;
clrscr();
printf("Enter the 4 elements of first matrix: ");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
51
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
c[0][0]=m1+m4-m5+m7;
C
c[0][1]=m3+m5;
c[1][0]=m2+m4;
c[1][1]=m1-m2+m3+m6;
getch();
}
JE
Experiment – 16
52
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
C
{
if(i==0 || j==0)
return;
if(b[i][j]=='c')
{
print_lcs(i-1,j-1);
CR
printf(" %c",x[i-1]);
}
else if(b[i][j]=='u')
print_lcs(i-1,j);
else
print_lcs(i,j-1);
}
void lcs_length(void)
{
m=strlen(x);
n=strlen(y);
for(i=0;i<=m;i++)
JE
c[i][0]=0;
for(i=0;i<=n;i++)
c[0][i]=0;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
{
if(x[i-1]==y[j-1])
{
c[i][j]=c[i-1][j-1]+1;
b[i][j]='c';
}
else if(c[i-1][j]>=c[i][j-1])
{
c[i][j]=c[i-1][j];
b[i][j]='u';
}
53
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
else
{
c[i][j]=c[i][j-1];
b[i][j]='l';
}
}
print_lcs(m,n);
}
void main()
C
{
printf("Enter 1st sequence : ");
gets(x);
printf("Enter 2nd sequence : ");
gets(y);
printf("\nlongest common subsequence is : ");
CR
lcs_length();
getch();
}
JE
Experiment – 17
54
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
void main()
C
{
int i,j,p[100],n;
clrscr();
printf("enter the no. of matrix chian");
scanf("%d",&n);
printf("enter the order of chain matrix");
CR
for(i=0;i<=n;i++)
scanf("%d",&p[i]);
matrixchain(p,n);
printf("cost of matrix \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("\t%d",m[i][j]);
}
printf("\n");
}
JE
printf("sequence matrix\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("\t%d",s[i][j]);
}
printf("\n");
}
prin(1,n);
printf("\ncost of matrix chain multiplication %d", m[1][n]);
getch();
}
55
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
{
int i,j,k,l,q;
for(i=1;i<=n;i++)
{
m[i][i]=0;
}
for(l=2;l<=n;l++)
{
for(i=1;i<=n-l+1;i++)
C
{
j=i+l-1;
m[i][j]=9999;
for(k=i;k<=j-1;k++)
{
q=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];
CR
if(q<m[i][j])
{
m[i][j]=q;
s[i][j]=k;
}
}
}
}
}
JE
}
}
Experiment – 18
56
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
C
x[k]=x[i]+1; //assign higher color than x[i]
}
}
int main(){
int n,e,i,j,k,l;
CR
printf("Enter no. of vertices : ");
scanf("%d",&n); //total vertices
printf("Enter no. of edges : ");
scanf("%d",&e); //total edges
for(i=0;i<n;i++)
for(j=0;j<n;j++)
G[i][j]=0; //assign 0 to all index of adjacency matrix
scanf("%d %d",&k,&l);
G[k][l]=1;
G[l][k]=1;
}
for(i=0;i<n;i++)
next_color(i); //coloring each vertex
return 0;
}
NOTE : This code is written,compiled and run with GCC compiler under Linux environment
Ubuntu12.04LTS Precise Pangolin.
57
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
----------------------------------------------------------------------------------------------------------------
----------
OUTPUT
----------------------------------------------------------------------------------------------------------------
----------
no. of vertices : 4
Enter no. of edges : 5
Enter indexes where value is 1-->
01
C
12
13
23
30
Colors of vertices -->
Vertex[1] : 1
CR
Vertex[2] : 2
Vertex[3] : 1
Vertex[4] : 3
JE
Experiment – 19
58
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
C
}
void main() {
int i,j,n,temp,total=0;
clrscr();
printf("\n Enter how many numbers:\n");
scanf("%d",&n);
CR
printf("\n Enter %d numbers to th set:\n",n);
for (i=0;i<n;i++) {
scanf("%d",&w[i]);
total+=w[i];
}
printf("\n Input the sum value to create sub set:\n");
scanf("%d",&sum);
for (i=0;i<=n;i++)
for (j=0;j<n-1;j++)
if(w[j]>w[j+1]) {
temp=w[j];
w[j]=w[j+1];
JE
w[j+1]=temp;
}
printf("\n The given %d numbers in ascending order:\n",n);
for (i=0;i<n;i++)
printf("%d \t",w[i]);
if((total<sum))
printf("\n Subset construction is not possible"); else {
for (i=0;i<n;i++)
inc[i]=0;
printf("\n The solution using backtracking is:\n");
sumset(-1,0,total);
}
getch();
}
void sumset(int i,int wt,int total) {
int j;
if(promising(i,wt,total)) {
59
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
if(wt==sum) {
printf("\n{\t");
for (j=0;j<=i;j++)
if(inc[j])
printf("%d\t",w[j]);
printf("}\n");
} else {
inc[i+1]=TRUE;
sumset(i+1,wt+w[i+1],total-w[i+1]);
C
inc[i+1]=FALSE;
sumset(i+1,wt,total-w[i+1]);
}
}
}
CR
JE
60