5CS4-23_AOA LAB MANUAL_2022-23
5CS4-23_AOA LAB MANUAL_2022-23
5CS4-23_AOA LAB MANUAL_2022-23
EXPERIMENT-1
OBJECTIVE
Sort a given set of elements using the Quicksort method 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.
PROGRAM
#include <stdio.h>
#include <time.h>
ret urn;
key=lo w;
i=low+1;
j=high;
while(i<=j)
if(i<J)
Exch(&a[i], &a[j]);
Exch(&a[j], &a[key]);
3
AOA Lab (5CS4-23) Manual Department of Computer Engineering
Poornima College of Engineering, Jaipur AOA Lab Manual
} void main()
{ int n, a[1000],k;
scanf("%d", &n);
et=clock();
ts=(double)(et-st)/CLOCKS _PER_SEC;
OUTPUT
4
AOA Lab (5CS4-23) Manual Department of Computer Engineering
Poornima College of Engineering, Jaipur AOA Lab Manual
EXPERIMENT-2
OBJECTIVE
Implement 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.
PROGRAM
#include
<stdio.h> #include<time. h> int b[50000];
void Merge(int a[], int low, int mid, int high){ int i, j, k;
i=low; j=mid+1; k=low;
while ( i<=mid && j<=high ) { if( a[i] <= a[j] ) b[k++] = a[i++] ;
else
b[k++] = a[j++] ;
while (i<=mid)
b[k++] = a[i++] ;
while (j<=high)
b[k++] = a[j++] ;
for(k=low; k<=high;
k++) a[k] =
b[k];
}
voidMergeSort(int a[], int low, int high)
{ int mid;
if(low >= high) return;
mid = (low+high)/2 ;
MergeSort(a, low, mid);
MergeSort(a, mid+1, high);
Merge(a, low, mid, high);
}
void main(){
int n, a[50000],k;
clock_tst,et; doublets;
printf("\n Enter How many Numbers:");
scanf("%d", &n); printf("\nThe Random Numbers are:\n");
for(k=1; k<=n; k++) {
a[k]=rand(); printf("%d\t", a[k]);
5
AOA Lab (5CS4-23) Manual Department of Computer Engineering
Poornima College of Engineering, Jaipur AOA Lab Manual
}
st=clock(); MergeSort(a, 1, n);
et=clock(); ts=(double)(et-
st)/CLOCKS_PER_SEC;
printf("\n Sorted Numbers are : \n ");
for(k=1; k<=n; k++)
printf("%d\t", a[k]);
printf("\nThe time taken is %e",ts);
}
OUTPUT
6
AOA Lab (5CS4-23) Manual Department of Computer Engineering
Poornima College of Engineering, Jaipur AOA Lab Manual
EXPERIMENT-3
OBJECTIVE
a.) Obtain the Topological ordering of vertices in a given digraph.
PROGRAM
Topological ordering
In topological sorting, a temporary stack is used with the name “s”. The node number is not
printed immediately; first iteratively call topological sorting for all its adjacent vertices, then
push adjacent vertex to stack. Finally, print contents of stack. Note that a vertex is pushed to
stack only when all of its adjacent vertices (and their adjacent vertices and so on) are already in
stack.
Transitive closure
Given a directed graph, find out if a vertex j is reachable from another vertex i for all vertex
pairs (i, j) in the given graph. Here reachable mean that there is a path from vertex i to j. The
reach- ability matrix is called transitive closure of a graph.
PROCEDURE:
1. Create: Open Dev C++, write a program after that save the program with .c extension.
2. Compile: Alt + F9
3. Execute: Ctrl + F10
SOURCE CODE:
7
AOA Lab (5CS4-23) Manual Department of Computer Engineering
Poornima College of Engineering, Jaipur AOA Lab Manual
// Topological ordering
#include<stdio.h>
intj,i,sum; for(j=0;j<n;j++) {
sum=0; for(i=0;i<n;i
++)
sum+=a[i][j]; indegre[j]=sum;
}
}
void topology(){
inti,u,v,t[10],s[10],top=- 1,k=0; find_indegre(); for(i=0;i<n;i++){
if(indegre[i]==0) s[++top]=i;
}
while(top!=-1) {
u=s[top--];
t[k++]=u; //top element of stack is stored in temporary array for(v=0;v<n;v++){
if(a[u][v]==1){
indegre[v]--; if(indegre[v]==0)
s[++top]=v; //Pushing adjacent vertex to stack
}
}
}
printf ("The topological Sequence is:\n"); for(i=0;i<n;i++)
printf ("%d ",t[i]);
}
void main(){
inti,j;
printf("Enter number of jobs:"); 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]);
}
8
AOA Lab (5CS4-23) Manual Department of Computer Engineering
Poornima College of Engineering, Jaipur AOA Lab Manual
topology();
}
PROGRAM
9
AOA Lab (5CS4-23) Manual Department of Computer Engineering
Poornima College of Engineering, Jaipur AOA Lab Manual
OUTPUT
10
AOA Lab (5CS4-23) Manual Department of Computer Engineering
Poornima College of Engineering, Jaipur AOA Lab Manual
EXPERIMENT-4
OBJECTIVE
#include<stdio.h>
int w[10],p[10],v[10][10],n,i,j,cap,x[10]={0};
int max(inti,int j){
return ((i>j)?i:j);
}
int knap(inti,int j){
int value; if(v[i][j]<0){
if(j<w[i])
value=knap(i-1,j);
else
value=max(knap(i-1,j),p[i]+knap(i-1,j-w[i]));
v[i][j]=value;
}
return(v[i][j]);
}
int main(){
intprofit,count=0;
printf("\nEnter the number of objects "); scanf("%d",&n);
printf("Enter the profit and weights of the elements
\n "); for(i=1;i<=n;i++){
printf("\nEnter profit and weight For object no %d :",i); scanf("%d%d",&p[i],&w[i]);
}
printf("\nEnter the capacity ");
scanf("%d",&ca p); for(i=0;i<=n;i+
+)
for(j=0;j<=cap;j
++)
if((i==0)||(j==0))
11
AOA Lab (5CS4-23) Manual Department of Computer Engineering
Poornima College of Engineering, Jaipur AOA Lab Manual
v[i][j]=0;
else
profit=knap(n,cap); i=n;
j=cap; while(j!=0&&i!=0){
v[i][j]=-1;
if(v[i][j]!=v[i-1][j]){
x[i]=1;
j=j-w[i];
i--;
}
else
}
i--;
printf("object included are
\n ");
printf("Sl.no\tweight\tprofit\ n"); for(i=1;i<=n;i++)
if(x[i])
printf("%d\t%d\t%d\n",++count,w[i],p[i])
; printf("Total profit = %d\n",profit);
}
OUTPUT
+_*
3
12
AOA Lab (5CS4-23) Manual Department of Computer Engineering
Poornima College of Engineering, Jaipur AOA Lab Manual
EXPERIMENT-5
OBJECTIVE
From a given vertex in a weighted connected graph, find shortest paths to other vertices using
Dijkstra's algorithm.
PROGRAM
#include<stdio. h>
#define infinity 999
13
AOA Lab (5CS4-23) Manual Department of Computer Engineering
Poornima College of Engineering, Jaipur AOA Lab Manual
}
int main(){
int n,v,i,j,cost[20][20],dist[20]; printf("enter the number of nodes:"); scanf("%d",&n);
printf("\n enter the cost 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]=infinity;
}
printf("\n enter the source matrix:"); scanf("%d",&v); dij(n,v,cost,dist);
printf("\n shortest path : \n"); for(i=1;i<=n;i++)
if(i!=v)
printf("%d->%d,cost=%d\n",v,i,dist[i]);
}
OUTPUT
14
AOA Lab (5CS4-23) Manual Department of Computer Engineering
Poornima College of Engineering, Jaipur AOA Lab Manual
EXPERIMENT-6
OBJECTIVE
Find Minimum Cost Spanning Tree of a given undirected graph using Kruskal's
algorithm.
PROGRAM
15
AOA Lab (5CS4-23) Manual Department of Computer Engineering
Poornima College of Engineering, Jaipur AOA Lab Manual
min=cost[i
][j]; a=u=i; b=v=j;
}
}
}
u=find(u
);
v=find( v); if(uni(u,
v)){
printf("\n%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;
}
intuni(inti,int j){
if(i!=j) {
}
parent[j]=i; return 1;
return 0;
}
16
AOA Lab (5CS4-23) Manual Department of Computer Engineering
Poornima College of Engineering, Jaipur AOA Lab Manual
OUTPUT
17
AOA Lab (5CS4-23) Manual Department of Computer Engineering
Poornima College of Engineering, Jaipur AOA Lab Manual
EXPERIMENT-7
OBJECTIVE
a.) Print all the nodes reachable from a given starting node in a digraph using BFS method.
PROGRAM
#include<stdio.h> #include<conio.h>
int a[20][20],q[20],visited[20],n,i,j,f=- 1,r=0; voidbfs(int v){
q[++r]=v; visited[v]
=1;
while(f<
=r) {
for(i=1;i<=n;i++)
if(a[v][i] && !visited[i]){
visited[i]=1; q[++r]=i;
}
void main(){
int v;
}
f++;
v=q[f];
}
printf("\n Enter the number of vertices:"); scanf("%d",&n);
for(i=1;i<=n;i++){
q[i]=0;
visited[i]=0;
18
AOA Lab (5CS4-23) Manual Department of Computer Engineering
Poornima College of Engineering, Jaipur AOA Lab Manual
}
printf("\n Enter graph data in matrix form:\n"); for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i] [j]); printf("\n Enter the starting
vertex:"); 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",q[i]);
else
}
printf("\n Bfs is not possible");
OUTPUT
b.) Check whether a given graph is connected or not using DFS method.
PROGRAM
19
AOA Lab (5CS4-23) Manual Department of Computer Engineering
Poornima College of Engineering, Jaipur AOA Lab Manual
else
}
printf("\n Graph is not connected");
20
AOA Lab (5CS4-23) Manual Department of Computer Engineering
Poornima College of Engineering, Jaipur AOA Lab Manual
OUTPUT
21
AOA Lab (5CS4-23) Manual Department of Computer Engineering
Poornima College of Engineering, Jaipur AOA Lab Manual
EXPERIMENT-8
OBJECTIVE
Find Minimum Cost Spanning Tree of a given undirected graph using Prim’s algorithm.
PROGRAM
#include <stdio.h>
#include <limits.h>
#define V 5
return min_index;
}
int main() {
/* Let us create the following graph
2 3
(0)--(1)--(2)
| /\ |
6| 8/ \5 |7
|/ \|
(3)-------(4)
9 */
int graph[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 }, };
primMST(graph);
23
AOA Lab (5CS4-23) Manual Department of Computer Engineering
Poornima College of Engineering, Jaipur AOA Lab Manual
return 0;
}
OUTPUT
$ gcc PrimsMST.c
$ ./a.out
Edge Weight
0-1 2
1-2 3
0-3 6
1-4 5
24
AOA Lab (5CS4-23) Manual Department of Computer Engineering