0% found this document useful (0 votes)
43 views30 pages

R19CS380 Daa.

The document is a student's assignment submission for the subject "Design and Analysis of Algorithms" at the School of Computing and Information Technology. It includes source code and output for 6 programming problems: 1) String matching using brute force, 2) Quicksort sorting algorithm, 3) Minimum spanning tree using Kruskal's and Prim's algorithms, 4) Shortest paths using Dijkstra's algorithm, 5) 0-1 Knapsack problem using dynamic programming, and 6) All pairs shortest paths using Floyd's algorithm with OpenMP parallelization.

Uploaded by

Yashas C
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views30 pages

R19CS380 Daa.

The document is a student's assignment submission for the subject "Design and Analysis of Algorithms" at the School of Computing and Information Technology. It includes source code and output for 6 programming problems: 1) String matching using brute force, 2) Quicksort sorting algorithm, 3) Minimum spanning tree using Kruskal's and Prim's algorithms, 4) Shortest paths using Dijkstra's algorithm, 5) 0-1 Knapsack problem using dynamic programming, and 6) All pairs shortest paths using Floyd's algorithm with OpenMP parallelization.

Uploaded by

Yashas C
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

DESIGN AND ANALYSIS OF ALGORITHM

B18CS4010

SCHOOL OF COMPUTING AND INFORMATION TECHNOLOGY

NAME : YASHAS C
SECTION & COURSE : F ; CSE
SRN : R19CS380
SEM : 4

1|Page
PROGRAM 1:
Search for a given pattern in a text string using Brute Force String Matching

SOURCE CODE
#include <stdio.h>

#include <string.h>

int match(char [], char []);

int main() {

char a[100], b[100];

int position;

printf("Enter some text\n");

gets(a);

printf("Enter a string to find\n");

gets(b);

position = match(a, b);

if (position != -1) {

printf("Found at location: %d\n", position + 1);

else {

printf("Not found.\n");

return 0;

int match(char text[], char pattern[]) {

2|Page
int c, d, e, text_length, pattern_length, position = -1;

text_length = strlen(text);

pattern_length = strlen(pattern);

if (pattern_length > text_length) {

return -1;

for (c = 0; c <= text_length - pattern_length; c++) {

position = e = c;

for (d = 0; d < pattern_length; d++) {

if (pattern[d] == text[e]) {

e++;

else {

break;

if (d == pattern_length) {

return position;

return -1;

OUTPUT
3|Page
PROGRAM 2:
Sort a set of elements in ascending order using Quick Sort algorithm.

SOURCE CODE

#include<stdio.h>

void quicksort(int number[25],int first,int last){

int i, j, pivot, temp;

if(first<last){

pivot=first;

i=first;

j=last;

4|Page
while(i<j){

while(number[i]<=number[pivot]&&i<last)

i++;

while(number[j]>number[pivot])

j--;

if(i<j){

temp=number[i];

number[i]=number[j];

number[j]=temp;

temp=number[pivot];

number[pivot]=number[j];

number[j]=temp;

quicksort(number,first,j-1);

quicksort(number,j+1,last);

void main()

int i, count, number[25];

printf("How many elements are u going to enter?:\n ");

scanf("%d",&count);

printf("Enter %d elements: ", count);

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

scanf("%d",&number[i]);

quicksort(number,0,count-1);

printf("Order of Sorted elements: ");

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

printf(" %d",number[i]);

5|Page
OUTPUT

Program 3a

Find minimum cost spanning tree of a given undirected graph using Kruskal’s
algorithm.

SOURCE CODE
#include<stdio.h>

#define INFINITY 999

#define MAX 100

int parent[MAX],cost[MAX][MAX],t[MAX][2];

int find(int v)

while(parent[v])

v=parent[v];

6|Page
}

return v;

void union1(int i,int j)

parent[j]=i;

void kruskal(int n)

int i,j,k,u,v,mincost,res1,res2,sum=0;

for(k=1;k<n;k++)

mincost=INFINITY;

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

for(j=1;j<=n;j++)

if(i==j) continue;

if(cost[i][j]<mincost)

u=find(i);

v=find(j);

if(u!=v)

res1=i;

res2=j;

mincost=cost[i][j];

7|Page
union1(res1,find(res2));

t[k][1]=res1;

t[k][2]=res2;

sum=sum+mincost;

printf("\nCost of spanning tree is %d\n",sum);

printf("\nEdges of spanning tree are\n");

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

printf("%d->%d\n",t[i][1],t[i][2]);

void main()

int i,j,n;

printf("\nEnter the number of vertices : ");

scanf("%d",&n);

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

parent[i]=0;

printf("\nEnter the cost adjacency matrix 0-for self edge and 999-if no edge\n");

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

for(j=1;j<=n;j++)

scanf("%d",&cost[i][j]);

kruskal(n);

OUTPUT

8|Page
Program 3b

Find minimum cost spanning tree of a given undirected graph using prims
algorithm

SOURCE CODE
#include<stdio.h>

#define INFINITY 999

int prim(int cost[10][10],int source,int n)

int i,j,sum=0,visited[10];

int distance[10],vertex[10];

int min,u,v;

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

vertex[i]=source;

visited[i]=0;

distance[i]=cost[source][i];

9|Page
}

visited[source]=1;

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

min=INFINITY;

for(j=1;j<=n;j++)

if(!visited[j]&&distance[j]<min)

min=distance[j];

u=j;

visited[u]=1;

sum=sum+distance[u];

printf("\n%d->%d",vertex[u],u);

for(v=1;v<=n;v++)

if(!visited[v]&&cost[u][v]<distance[v])

distance[v]=cost[u][v];

vertex[v]=u;

return sum;

void main()

int a[10][10],n,i,j,m,source;

printf("\n enter the number of vertices:\n");

10 | P a g e
scanf("%d",&n);

printf("\nenter the cost matrix:\n 0-self loop and 999-no edge:\n");

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

for(j=1;j<=n;j++)

scanf("%d",&a[i][j]);

printf("\n enter the source:\n");

scanf("%d",&source);

m=prim(a,source,n);

printf("\n the cost of spanning tree=%d",m);

OUTPUT

Program 4

11 | P a g e
From a given vertex in a weighted connected graph, find shortest paths to other
vertices using Dijkstra’s algorithm.

SOURCE CODE
#include<stdio.h>

#define INFINITY 999

void dijkstra(int cost[10][10],int n,int source,int distance[10])

int visited[10],min,u;

int i,j;

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

distance[i]=cost[source][i];

visited[i]=0;

visited[source]=1;

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

min=INFINITY;

for(j=1;j<=n;j++)

if(visited[j]==0 && distance[j]<min)

min=distance[j];

u=j;

visited[u]=1;

for(j=1;j<=n;j++)

if(visited[j]==0 && (distance[u]+cost[u][j])<distance[j])

distance[j]=distance[u]+cost[u][j];

12 | P a g e
}

void main()

int n,cost[10][10],distance[10];

int i,j,source,sum;

printf("\nEnter how many nodes : ");

scanf("%d",&n);

printf("\nCost Matrix\nEnter 999 for no edge\n");

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

for(j=1;j<=n;j++)

scanf("%d",&cost[i][j]);

printf("Enter the source node\n");

scanf("%d",&source);

dijkstra(cost,n,source,distance);

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

printf("\n\nShortest Distance from %d to %d is %d",source,i,distance[i]);

OUTPUT

13 | P a g e
Program 5

Implement 0 / 1 Knapsack problem using dynamic programming.

SOURCE CODE
#include<stdio.h>

int w[10],p[10],n;

int max(int a,int b)

return a>b?a:b;

int knap(int i,int m)

if(i==n) return w[i]>m?0:p[i];

14 | P a g e
if (w[i]>m) return knap(i+1,m);

return max(knap(i+1,m),knap(i+1,m-w[i])+p[i]);

void main()

int m,i,max_profit;

printf("\nEnter the number of objects: ");

scanf("%d",&n);

printf("\nEnter the knapsack capacity: ");

scanf("%d",&m);

printf("\nEnter profit followed by weight: ");

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

scanf("%d%d",&p[i],&w[i]);

max_profit=knap(1,m);

printf("\nMax profit = %d",max_profit);

OUTPUT

Program 6

Implement All-Pairs Shortest Paths Problem using Floyd's algorithm. Parallelize


this algorithm, implement it using OpenMP and determine the speed-up
achieved.

15 | P a g e
SOURCE CODE
#include<stdio.h>

#define INFINITY 999

int min(int i,int j)

if(i<j)

return i;

else

return j;

void floyd(int n,int p[10][10])

int i,j,k;

for(k=1;k<=n;k++)

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

for(j=1;j<=n;j++)

p[i][j]=min(p[i][j],p[i][k]+p[k][j]);

int main()

int i,j,n,a[10][10],d[10][10],source;

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]);

floyd(n,a);

printf("\n\nThe distance matrix is \n");

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

16 | P a g e
for(j=1;j<=n;j++)

printf("%d\t",a[i][j]);

printf("\n");

return 0;

OUTPUT

Program 7

Obtain the DFS ordering of vertices in a given digraph.

SOURCE CODE
#include<stdio.h>

int i,visit[20],n,adj[20][20],s,topo_order[10];

void dfs(int v)

int w;

17 | P a g e
visit[v]=1;

for(w=1;w<=n;w++)

if((adj[v][w]==1) && (visit[w]==0))

dfs(w);

topo_order[i--]=v;

void main()

int v,w;

printf("Enter the number of vertices:\n");

scanf("%d",&n);

printf("Enter the adjacency matrix:\n");

for(v=1;v<=n;v++)

for(w=1;w<=n;w++)

scanf("%d",&adj[v][w]);

for(v=1;v<=n;v++)

visit[v]=0;

i=n;

for(v=1;v<=n;v++)

if(visit[v]==0)

dfs(v);

printf("\nTopological sorting is:");

for(v=1;v<=n;v++)

printf("v%d ",topo_order[v]);

OUTPUT

18 | P a g e
Program 8

Implement Horspool’s algorithm for String Matching and find the number of
key comparisons in successful search and unsuccessful search

SOURCE CODE
#include<stdio.h>

#include<string.h>

#define MAX 500

int t[MAX];

void shifttable(char p[]) {

int i,j,m;

m=strlen(p);

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

t[i]=m;

for (j=0;j<m-1;j++)

t[p[j]]=m-1-j;

int horspool(char src[],char p[]) {

int i,j,k,m,n;

19 | P a g e
n=strlen(src);

m=strlen(p);

printf("\nLength of text=%d",n);

printf("\n Length of pattern=%d",m);

i=m-1;

while(i<n) {

k=0;

while((k<m)&&(p[m-1-k]==src[i-k]))

k++;

if(k==m)

return(i-m+1); else

i+=t[src[i]];

return -1;

void main() {

char src[100],p[100];

int pos;

printf("Enter the text in which pattern is to be searched:\n");

gets(src);

printf("Enter the pattern to be searched:\n");

gets(p);

shifttable(p);

pos=horspool(src,p);

if(pos>=0)

printf("\n The desired pattern was found starting from position %d",pos+1); else

printf("\n The pattern was not found in the given text\n");

OUTPUT

20 | P a g e
Program 9

Sort a given set of elements in ascending order which has duplicate


entries. Use the sorting by counting algorithm

SOURCE CODE
#include <stdio.h>

/* Counting sort function */

void counting_sort(int a[], int k, int n)

int i, j;

int b[15], c[100];

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

c[i] = 0;

for (j = 1; j <= n; j++)

c[a[j]] = c[a[j]] + 1;

for (i = 1; i <= k; i++)

c[i] = c[i] + c[i-1];

for (j = n; j >= 1; j--)

b[c[a[j]]] = a[j];

c[a[j]] = c[a[j]] - 1;

21 | P a g e
}

printf("The Sorted array is : ");

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

printf("%d,", b[i]);

void main()

int n, k = 0, a[15], i;

printf("Input number of elements: ");

scanf("%d", &n);

printf("Input the array elements one by one: \n");

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

scanf("%d", &a[i]);

if (a[i] > k) {

k = a[i];

counting_sort(a, k, n);

printf("\n");

OUTPUT

Program 10

22 | P a g e
Implement N Queen’s problem using back tracking.

SOURCE CODE
#include<stdio.h>

#include<stdlib.h>

#define MAX 50

int can_place(int c[],int r)

int i;

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

if(c[i]==c[r] || (abs(c[i]-c[r])==abs(i-r)))

return 0;

return 1;

void display(int c[],int n)

int i,j;

char cb[10][10];

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';

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

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

printf("%c",cb[i][j]);

printf("\n");

23 | P a g e
void n_queens(int n)

int r;

int c[MAX];

c[0]= -1;

r=0;

while(r>=0)

c[r]++;

while(c[r]<n && !can_place(c,r))

c[r]++;

if(c[r]<n)

if(r==n-1)

display(c,n);

printf("\n");

else

r++;

c[r]=-1;

else

r--;

void main()

int n;

24 | P a g e
printf("\nEnter the number of queens : ");

scanf("%d",&n);

n_queens(n);

OUTPUT

Program 11

Write a program to sort all transactions of Big Mall by quantity of sales.

SOURCE CODE
#include<stdio.h>

#include<conio.h>

struct BIG_MALL

int item_no,quantity;

float price;

char item_name[20];

}s[100],t;

void main()

25 | P a g e
{

int i,j,n;

float temp=3.2f;

printf("Enter the number of items to purchase n=");

scanf("%d",&n);

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

printf("Enter the %d Item number: \n",i+1);

scanf("%d",&s[i].item_no);

printf("Enter the Item name without spaces:");

scanf("%s",s[i].item_name);

printf("Enter the quantity of items:");

scanf("%d",&s[i].quantity);

printf("Enter the PRICE of item purchased");

fflush(stdin);

scanf("%f",&temp);

s[i].price=temp;

printf("\n PURSHACE details are\n");

printf("\nItem no\tItem Name\t\Quantity\tPrice\n");

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

printf("%d\t%s\t\t%d\t\t%3.2f\n", s[i].item_no,s[i].item_name,s[i].quantity,s[i].price);

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

for(j=0;j<n-1;j++)

if(s[j].quantity>s[j+1].quantity)

t=s[j];

s[j]=s[j+1];

26 | P a g e
s[j+1]=t;

printf("\n PURSHACE details after sorting by quantity are\n");

printf("\nItem no\tItem Name\t\Quantity\tPrice\n");

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

printf("%d\t%s\t\t%d\t\t%3.2f\n", s[i].item_no,s[i].item_name,s[i].quantity,s[i].price);

OUTPUT

27 | P a g e
Program 12

Write a program to find network of people of same location in Linkedin social


network.

SOURCE CODE
#include<stdio.h>

int visited[10];

void bfs(int n,int a[10][10],int source)

int i,q[10],u;

int front=1,rear=1;

visited[source]=1;

q[rear]=source;

while(front<=rear)

u=q[front];

28 | P a g e
front=front+1;

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

if(a[u][i]==1 && visited[i]==0)

rear=rear+1;

q[rear]=i;

visited[i]=1;

void main()

int n,a[10][10],i,j,source;

printf("Enter the number of persons : ");

scanf("%d",&n);

printf("Enter the adjacency matrix for the connection between people\n");

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

for(j=1;j<=n;j++)

scanf("%d",&a[i][j]);

printf("\nEnter the source person : ");

scanf("%d",&source);

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

visited[i]=0;

bfs(n,a,source);

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

if(visited[i]==0)

printf("\nThe person %d is not in the same location of the network",i);

else

printf("\nThe person %d is in the same location of the network",i);

29 | P a g e
}

OUTPUT

30 | P a g e

You might also like