DAALab Instructors Manual
DAALab Instructors Manual
DAALab Instructors Manual
IV SEMESTER B.E
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
4. Write a program to obtain the Topological ordering of vertices in a given digraph using
a) Vertices deletion method b)DFS method
5. Write a program to print all the nodes reachable from a given starting node in a graph using
Breadth First Search method. Also check connectivity of the graph. If the graph is not
connected, display the number of components in the graph.
6. Write a program to sort a given set of elements using Heap sort method. Find the time
complexity.
7a. Write a program to implement Horspool algorithm for String Matching
7b. Write a program to implement all pair shortest paths problem using Floyd’s algorithm.
9. Write a program to find Minimum cost spanning tree of a given undirected graph using
Prim’s algorithm.
10. Write a program to find Minimum cost spanning tree of a given undirected graph using
Kruskal’s algorithm.
11. Write a program to find the shortest path using Dijkstra’s algorithm for a weighted connected
graph.
12. Write a program to implement Subset-Sum problem using Back Tracking
13. Write a program to implement TSP using branch and bound algorithm
14. Write a program to implement n-queens problem
#include<stdio.h>
#define MAX 1000
int count;
int main()
{
int i,j,n,a[MAX],b[MAX],c[MAX];
int c1,c2,c3;
printf("\n Enter n: ");
scanf("%d",&n);
count=0;
mergesort(a,0,n-1);
i = low;
j = mid+1;
k = low;
OUTPUT :
Enter n: 6
Enter elements: 14 5 7 89 2 34
Sorted elements:
2
5
7
14
34
89
#include <stdio.h>
#include <stdlib.h>
//Function declarations
void quicksort(int a[MAX], int low, int high);
int partition(int a[MAX], int low, int high);
int count;
int main()
{
int n; //No. of elements
int a[MAX],b[MAX],c[MAX]; //Array to store elements
int i; //Index variable
int c1,c2,c3;
printf("\nEnter n: ");
scanf("%d",&n);
count=0;
quicksort(a,0,n-1);
while(1)
{
while ((key >= a[i]) && i < high)
i++;
if(i < j)
{
temp = a[i]; a[i] = a[j]; a[j] = temp;
}
else
{
temp = a[low]; a[low] = a[j]; a[j] = temp;
return j;
}//end if
}//end while
}//end function
OUTPUT :
Enter n: 6
Enter elements: 14 5 7 89 2 34
Sorted elements:
2
5
7
14
34
89
Department of Computer Science & Engineering Page 6
Data structures Lab Manual (12CS/IS45)
#include <stdio.h>
int main()
{
int n;
int a[10][10];
int v[10];
int source;
int i, j;
int count = 0;
for(i=0;i<n;i++)
v[i] = 0;
dfs(a,n,v,source);
for(i=0;i<n;i++)
{
if(v[i] == 0)
{
dfs(i,a,n,v);
count++;
}
}
printf("Result: ");
if(count == 1)
printf("Graph is Connected");
else
printf("Graph is NOT Connected with %d Components\n",count);
return 0;
}
v[source] = 1;
for(i=0; i<n; i++)
if(a[source][i] == 1 && v[i] == 0)
dfs(a,n,v,i);
}
#include<stdio.h>
int main()
{
int n;
int a[10][10];
int i,j,k,node;
int in[10]={0};
int v[10]={0};
printf("Enter n: ");
scanf("%d",&n);
if(a[i][j] == 1)
in[j]++;
}
}
for(i=1;i<=n;i++)
if(a[node][i] == 1)
in[i]--;
}
printf("\n\n");
}
#include<stdio.h>
#include<stdlib.h>
int j=0;pop[10],v[10];
int main()
{
int n,i,j,a[10][10];
printf(“\n Enter the no of Vertices : “);
Department of Computer Science & Engineering Page 10
Data structures Lab Manual (12CS/IS45)
scanf(“%d”,&n);
printf(“\n Enter the Adjacency matrix\n”);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf(“%d”,&a[i][j]);
topo(n,a);
printf(“\n The topological ordering is\n”);
for(i=n-1;i>=0;i--)
printf(“%d\t”,pop[i]);
}
#include <stdio.h>
int main()
{
int n;
int a[10][10];
int v[10];
int source;
int i, j,count=0;
bfs(a,n,v,source);
for(i=0;i<n;i++)
{
if(v[i] == 0)
{
bfs(a,n,v,i);
count++;
}
}
printf("Result: ");
if(count == 1)
printf("Graph is Connected");
else
printf("Graph is NOT Connected with %d Components\n",count);
return 0;
}
v[source] = 1;
q[++rear] = source;
while(front <= rear)
{
node = q[front++];
for(i=0;i<n;i++)
if(a[node][i] == 1 && v[i] == 0)
{
v[i] = 1;
q[++rear] = i;
}
}//end while
}//end bfs
#include<stdio.h>
#define MAX 1000
int count =0;
void main()
{
int a[MAX],b[MAX],c[MAX];
int n,i,j,c1,c2,c3;
printf("\n enter the number of elements to be sorted : ");
scanf("%d",&n);
printf("\n Enter the elements to be sorted\n");
#include<stdio.h>
int main()
{
int n,a[10][10],d[10][10];
int i,j,k;
printf("Enter the no.of nodes: ");
scanf("%d",&n);
printf("\nEnter the adjacency matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
d[i][j] = a[i][j];
}
floyd(n,a);
printf("\n\nThe distance matrix is \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%5d",d[i][j]);
printf("\n");
}
return 0;
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
while(i<n)
{ k=0;
while((k<m) && (pat[m-1-k]==src[i-k]))
k++;
if(k==m)
return (i-m+1);
else
{
i=i+t[src[i]];
count=count+1;
}
}
return -1;
}
int main()
{
char src[100],pat[10];
int pos;
printf("\n Enter the main source string\n");
gets(src);
printf("\n Enter the pattern to be searched\n");
gets(pat);
shifttable(pat);
pos=horspool(src,pat);
if(pos>=0)
Department of Computer Science & Engineering Page 17
Data structures Lab Manual (12CS/IS45)
{
printf("\n Found at %d position ",pos+1);
printf("\n number of shifts are %d",count);
}
else
printf("\n String match failed");
return 0;
}
#include <stdio.h>
//Function declarations
int knap(int n,int m);
int big(int a,int b);
//Global variables
int w[MAX]; //Array to store weights of each item
int p[MAX]; //Array to store profits of each item
int v[MAX][MAX]; //Optimal solution of 'i' items with 'j' capacity
int main()
{
int i, j, profit, n, m;
profit = knap(n,m);
return 0;
}
return v[n][m];
}
#include<stdio.h>
int main()
{
int n; //no. of nodes
int cost[10][10]; //Adjacency matrix of graph
int source; //source node
int i, j; //index variables
prims(n,cost,source);
return 0;
}
for(i=1;i<=n;i++)
{
v[i] = 0;
d[i] = cost[source][i];
vertex[i] = source;
}
v[source] = 1;
for(i=1;i<n;i++)
{
least = INFINITY;
for(j=1; j<=n; j++)
v[u] = 1;
sum += d[u];
printf("%d --> %d = %d Sum = %d\n\n",vertex[u],u, d[u],sum);
for(j=1;j<=n;j++)
{
if(v[j] == 0 && cost[u][j] < d[j])
{
d[j] = cost[u][j];
vertex[j] = u;
}
}
}
printf("Total cost: %d",sum);
}
/*
Output1:
1 --> 2 = 20 Sum = 20
1 --> 3 = 10 Sum = 30
3 --> 4 = 40 Sum = 70
Total cost: 70
*/
#include<stdio.h>
//Function declarations
void kruskal(int n);
int get_parent(int v);
void join(int i,int j);
void sort_edges();
void display();
struct EDGE
{
int x, y, wt;
}e[MAX];
int parent[MAX];
int cost[MAX][MAX]; //cost matrix
int t[MAX][2]; //Result: edges in spanning tree
int nedges; //no. of edges
int eno; //edge number (used as index in e[])
int main()
{
int i,j;
int n; //no. of nodes
//3. Read cost matrix of graph and Identify edges and store in e
eno = 1;
printf("\nEnter the cost adjacency matrix: 0 = self loop & 999 =
no edge\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
//add edge
e[eno].x = i;
e[eno].y = j;
e[eno].wt = cost[i][j];
eno++; nedges++;
}
}
return 0;
}
return v;
}
/* OUTPUT:
Run1:
enter the number of vertices:4
Run 2:
Enter the no.of vertices: 5
Enter the cost adjacency matrix: 0 = self loop & 999 = no edge
0 999 10 7 999
999 0 999 32 999
10 999 0 9 999
7 32 9 0 23
999 999 999 23 0
*/
#include <stdio.h>
void dijk(int cost[10][10], int n, int source, int v[10], int d[10]);
int main()
{
int n; //no. of nodes
int cost[10][10]; //Adjacency matrix of graph
int source; //source node
int v[10]; //visited array. keeps track to nodes visited
int d[10]; //distance array.shortest distance from source node
int i, j; //index variables
return 0;
}
//B. From each node find shortest distance to nodes not visited
for(i=1; i<=n; i++)
{
//B1. Assume least as infinity
least = INFINITY;
//B2. Find u and d(u) such that d(u) is minimum i.e., Find
//the next nearest node
for(j=1; j<=n; j++)
{
if(v[j] == 0 && d[j] < least)
{
least = d[j];
u = j;
}
}
}
}//end for outer
}//end function
#include <stdio.h>
int main()
{
int n; //No. of elements in set
int d; //Required subset sum
int s[10]; //Array: Elements in the set
int i; //index variable
int sum = 0;
return 0;
}
sum = 0;
while(1)
{
if(k <= n && x[k] == 1)
{
if(sum+s[k] == d)
{
printf("Solution is \n");
for(i = 1; i <= n; i++)
{
if(x[i] == 1)
printf("%5d", s[i]);
}
printf("\n");
x[k] = 0;
}
else if(sum + s[k] < d)
sum += s[k];
else
x[k] = 0;
}
else
{
k--;
while(k > 0 && x[k] == 0)
k--;
if(k == 0) break;
x[k] = 0;
sum = sum - s[k];
}
k = k + 1;
x[k] = 1;
}
}
/*
Run1:
Enter the value of n5
Enter the set in increasing order
1
2
3
4
5
Enter the maximum subset value of d: 7
Solution is
1 2 4
Solution is
2 5
Solution is
3 4
#include<stdio.h>
//Function declarations
int tsp_dp(int source,int v[10]);
int tsp_nn(int source,int v[10]);
int g(int source,int s[10]);
int setempty(int s[10]);
//Global variables
int n,cost[10][10],start;
//Main function
int main()
{
int v[10] = {0}; //Initialise all elements of v[] = 0
int i, j;
int mincost1, mincost2;
//Print result
printf("\n\nCost using NN = %5d\n\n",mincost1);
printf("\n\nCost using DP = %5d\n\n",mincost2);
printf("Deviation: %f\n\n",(float)mincost1/mincost2);
return 0;
Department of Computer Science & Engineering Page 31
Data structures Lab Manual (12CS/IS45)
}
//Function to find the optimal path from source to source through all
//the remaining nodes(k)
int g(int source,int s[10])
{
int k,sum,least;
//Compute least cost path from source to source through all the
//remaining nodes(k)
//for all combinations of remaining(k) nodes
least = 999;
for(k=1; k<=n; k++)
{
if(s[k] == 1) //If node k already visited then
ignore
continue;
s[k] = 1;
sum = cost[source][k] + g(k,s);
return least;
}// end g
return sum;
Department of Computer Science & Engineering Page 32
Data structures Lab Manual (12CS/IS45)
}
return sum;
}
/*
Run 1:
Enter Source: 2
Cost using NN = 25
Cost using DP = 25
Deviation: 1.000000
Run 2:
Enter Source: 4
Cost using NN = 39
Cost using DP = 35
Deviation: 1.114286
*/
#include <stdio.h>
//Function declarations
void nqueens(int n);
int can_place(int c[10],int r);
void display(int c[10],int r);
//Global variable
int count = 0;
int main()
{
int n;
return 0;
}
void nqueens(int n)
{
int r; //Contains row no.
int c[10]; //Stores queens positions in each row
int i;
if(c[r] < n)
{
Department of Computer Science & Engineering Page 35
Data structures Lab Manual (12CS/IS45)
if(r == n-1) /if all n queens - display
{
printf("Solution %d: ",++count);
for(i=0;i<n;i++)
printf("%4d",c[i]+1);
display(c,n);
}
else //else place the next queen in next row
{
r++;
c[r] = -1;
}
}
else
r--; //backtracking (go to previous row)
}
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cb[i][j] = '-';
for(i=0;i<n;i++)
cb[i][c[i]] = 'Q';