You are on page 1of 41

Practical File

on
Design Analysis of Algorithm lab

AMITY UNIVERSITY HARYANA

Submitted By: Submitted To:


Name-Mayank Saini Dr.Rashmi Gupta
Course-BCA
Enrollment No-A50504820006

INDEX

S.no Name of the program Date Signature

1. Write a program of binary 24/1/2022


search
2. Write a program of linear search 24/1/2022

3. Write a program of merge sort 31/1/2022


in c.
4. Write a program of two 2 by 2 31/1/2022
matrix multiplication using
Strassen's algorithm.
5. Write a program of Quick sort. 7/2/2022

6. Write a program of Knapsack. 14/2/2022

7. Write a program of Selection 14/2/2022


Sort.

8. Write a program of Adjacency 21/2/2022


Matrix undirected graph.

9. Write a program of Adjacency 21/2/2022


List.
10. 28/2/2022
Write a program of DFs
transversal.
11. 7/3/2022
Write a BFS transversal.
12. 21/3/2022
Write a Kruskal Algorithm.

13. 28/3/2022
Write a prism Algorithm

Practical No. 1
Aim: Write a program of binary search in c.

Source Code:
#include <stdio.h>
int binary Search(int array[], int x, int low, int high) {
// Repeat until the pointers low and high meet each other
while (low <= high) {
int mid = low + (high - low) / 2;
if (array[mid] == x)
return mid;
if (array[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main(void) { int array[] = {3, 4, 5, 6, 7, 8, 9};
int n = sizeof(array) / sizeof(array[0]);
int x = 4;
int result = binary Search(array, x, 0, n - 1);
if (result == -1)
printf("Not found");
else
printf("Element is found at index %d", result);
return 0;
Output
Practical No. 2
Aim: write a program of linear search in c.

Source Code:
#include <stdio.h>
int main()
{
int array[100], search, c, n;

printf("Enter number of elements in array\n");


scanf("%d", &n);

printf("Enter %d integer(s)\n", n);

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


scanf("%d", &array[c]);

printf("Enter a number to search\n");


scanf("%d", &search);

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


{
if (array[c] == search) /* If required element is found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d isn't present in the array.\n", search)
return 0;
}

Output:
Practical No.3
Aim: Write a program of merge sort in c.

Source Code:
#include <stdio.h>

#define max 10

int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
int b[10];

void merging(int low, int mid, int high) {


int l1, l2, i;

for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
if(a[l1] <= a[l2])
b[i] = a[l1++];
else
b[i] = a[l2++];
}

while(l1 <= mid)


b[i++] = a[l1++];

while(l2 <= high)


b[i++] = a[l2++];

for(i = low; i <= high; i++)


a[i] = b[i];
}

void sort(int low, int high) {


int mid;

if(low < high) {


mid = (low + high) / 2;
sort(low, mid);
sort(mid+1, high);
merging(low, mid, high);
} else {
return;
}
}

int main() {
int i;

printf("List before sorting\n");

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


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

sort(0, max);

printf("\nList after sorting\n");

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


printf("%d ", a[i]);
}

Output:

Practical No.4
Aim: Write a program of two 2 by 2 matrix multiplication using Strassen's
algorithm.
Source Code:
#include<stdio.h>
int main(){
int a[2][2], b[2][2], c[2][2], i, j;
int m1, m2, m3, m4 , m5, m6, m7;

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

printf("Enter the 4 elements of second matrix: ");


for(i = 0; i < 2; i++)
for(j = 0;j < 2; j++)
scanf("%d", &b[i][j]);

printf("\n The first matrix is\n");


for(i = 0; i < 2; i++){
printf("\n");
for(j = 0; j < 2; j++)
printf("%d\t", a[i][j]);
}

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


for(i = 0;i < 2; i++){
printf("\n");
for(j = 0;j < 2; j++)
printf("%d\t", b[i][j]);
}

m1= (a[0][0] + a[1][1]) * (b[0][0] + b[1][1]);


m2= (a[1][0] + a[1][1]) * b[0][0];
m3= a[0][0] * (b[0][1] - b[1][1]);
m4= a[1][1] * (b[1][0] - b[0][0]);
m5= (a[0][0] + a[0][1]) * b[1][1];
m6= (a[1][0] - a[0][0]) * (b[0][0]+b[0][1]);
m7= (a[0][1] - a[1][1]) * (b[1][0]+b[1][1]);

c[0][0] = m1 + m4- m5 + m7;


c[0][1] = m3 + m5;
c[1][0] = m2 + m4;
c[1][1] = m1 - m2 + m3 + m6;

printf("\n After multiplication using Strassen's algorithm \n");


for(i = 0; i < 2 ; i++){
printf("\n");
for(j = 0;j < 2; j++)
printf("%d\t", c[i][j]);
}

return 0;
}
Output:
Practical No.5
Aim: Write a program of Quick sort.

Source Code:
#include <stdio.h>

void quick_sort(int[],int, int);


int partition(int[],int, int);

int main()
{
int a[50],n,i;
printf("How many elements?");
scanf("%d",&n);
printf("\nEnter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quick_sort(a,0,n-1);
printf("\n Array after sorting:");
for(i=0;i<n ; i++)
printf("%d ",a[i]);
return 0;
}

void quick_sort(int a[],int l,int u)


{
int j;
if(l<u)
{
j=partition(a, l, u);
quick_sort(a,l,j-1);
quick_sort(a,j+1,u);
}
}

int partition(int a[],int l,int u)


{
int v,i,j,temp;
v=a[l];
i=l;
j=u+1;
do
{
do
i++;
while(a[i]<v&&i<=u);
do
j--;
while(v<a[j]);
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}while(i<j);
a[l]=a[j];
a[j]=v;
return(j);
}

Output:

Practical No.6
Aim: Write a program of Knapsack.

Source Code:
#include<stdio.h>
#include<conio.h>
int w[10],p[10],v[10][10],n,i,j,cap,x[10]= {0};
int max(int i,int j) {
return ((i>j)?i:j);
}
int knap(int i,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]);
}
void main() {
int profit,count=0;

printf("\nEnter the number of elements\n");


scanf("%d",&n);
printf("Enter the profit and weights of the elements\n");
for (i=1;i<=n;i++) {
printf("For item no %d\n",i);
scanf("%d%d",&p[i],&w[i]);
}
printf("\nEnter the capacity \n");
scanf("%d",&cap);
for (i=0;i<=n;i++)
for (j=0;j<=cap;j++)
if((i==0)||(j==0))
v[i][j]=0; else
v[i][j]=-1;
profit=knap(n,cap);
i=n;
j=cap;
while(j!=0&&i!=0) {
if(v[i][j]!=v[i-1][j]) {
x[i]=1;
j=j-w[i];
i--;
} else
i--;
}
printf("Items 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);
getch();
}
Output:

Practical No.7
Aim: Write a program of Selection Sort.
Source Code:
#include<stdio.h>
#define SIZE 5

void swap(int *x, int *y)


{
int temp = *x;
*x = *y;
*y = temp;
}

void selectionSort(int arr[],int size)


{
int i,j;

for(i = 0; i < size-1; i++)


{
for(j = i+1; j < size; j++)
{
if(arr[i] > arr[j])
swap(&arr[i],&arr[j]);
}
}

int main()
{
int arr[SIZE] = {180,165,150,170,145},i;

selectionSort(arr,SIZE);

printf("After selection sort\n");


for(i = 0; i < SIZE; i++)
printf("%d ",arr[i]);

return 0;
}
Output:

Practical No.8
Aim: Write a program of Adjacency Matrix undirected graph.
Source Code:
// Adjacency Matrix representation in C

#include <stdio.h>
#define V 4

void init(int arr[][V]) {


int i, j;
for (i = 0; i < V; i++)
for (j = 0; j < V; j++)
arr[i][j] = 0;
}

void add Edge(int arr[][V], int i, int j) {


arr[i][j] = 1;
arr[j][i] = 1;
}

void print Adj Matrix(int arr[][V]) {


int i, j;

for (i = 0; i < V; i++) {


printf("%d: ", i);
for (j = 0; j < V; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
}

int main() {
int adj Matrix[V][V];

in it(adj Matrix);
add Edge(adj Matrix, 0, 1);
add Edge(adj Matrix, 0, 2);
add Edge(adj Matrix, 1, 2);
add Edge(adj Matrix, 2, 0);
add Edge(adj Matrix, 2, 3);

print Adj Matrix(adj Matrix);

return 0;
}
Output:

Practical No.9
Aim: Write a program of Adjacency List.

Source Code:
#include <stdio.h>
#include <stdlib.h>

struct node {
int vertex;
struct node* next;
};
struct node* createNode(int);

struct Graph {
int numVertices;
struct node** adjLists;
};

struct node* create Node(int v) {


struct node* new Node = malloc(size of(struct node));
new Node->vertex = v;
new Node->next = NULL;
return new Node;
}

struct Graph* create A Graph(int vertices) {


struct Graph* graph = malloc(size of(struct Graph));
graph->num Vertices = vertices;
graph->adj Lists = malloc(vertices * size of(struct node*));

int i;
for (i = 0; i < vertices; i++)
graph->adj Lists[i] = NULL;

return graph;
}

void add Edge(struct Graph* graph, int s, int d) {

struct node* new Node = create Node(d);


new Node->next = graph->adj Lists[s];
graph->adj Lists[s] = new Node;

new Node = create Node(s);


new Node->next = graph->adj Lists[d];
graph->adj Lists[d] = new Node;
}

void print Graph(struct Graph* graph) {


int v;
for (v = 0; v < graph->num Vertices; v++) {
struct node* temp = graph->adj Lists[v];
printf("\n Vertex %d\n: ", v);
while (temp) {
printf("%d -> ", temp->vertex);
temp = temp->next;
}
printf("\n");
}
}

int main() {
struct Graph* graph = create A Graph(4);
add Edge(graph, 0, 1);
add Edge(graph, 0, 2);
add Edge(graph, 0, 3);
add Edge(graph, 1, 2);

print Graph(graph);

return 0;
}

Output:
Practical No.10
Aim: Write a program of DFs transversal

Source Code:
#include <iostream>
#include<vector>
using namespace std;

int main()
{
cout << "\n\welcome to Studytonight :-)\n\n\n";
cout << " ===== Program to demonstrate the DFS Traversal on a Graph, in
CPP ===== \n\n";

int cost[10][10], i, j, k, n, e, top, v, stk[10], visit[10], visited[10];

cout << "Enter the number of vertices in the Graph: ";


cin >> n;
cout << "\n Enter the number of edges in the Graph : ";
cin >> e;
cout << "\nEnter the start and end vertex of the edges: \n";

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


{
cin >> i >> j;
cost[i][j] = 1;
}

cout << "\n Enter the initial vertex to start the DFS traversal with: ";
cin >> v;
cout << "\n The DFS traversal on the given graph is : \n";
cout << v << " ";

visited[v] = 1;

k = 1;

while (k < n)
{
for (j = n; j >= 1; j--)
{
if (cost[v][j] != 0 && visited[j] != 1 && visit[j] != 1)
{
visit[j] = 1;

stk[top] = j;
top++;
}
}

v = stk[--top];
cout << v << " ";
k++;

visit[v] = 0;
visited[v] = 1;
}

cout << "\n\n\n";

return 0;
}

Output:
Practical No.11
Aim: Write a BFS transversal.

Source Code:
#include<iostream>
#include <list>

using namespace std;

class Graph
{
int V;

list<int> *adj;
public:
Graph(int V);

void addEdge(int v, int w);

void BFS(int s);


};

Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}

void Graph::addEdge(int v, int w)


{
adj[v].push_back(w);
}

void Graph::BFS(int s)
{

bool *visited = new bool[V];


for(int i = 0; i < V; i++)
visited[i] = false;

list<int> queue;

visited[s] = true;
queue.push_back(s);

list<int>::iterator i;
while(!queue .empty())
{

s = queue. front();
cout << s << " ";
queue. pop_ front();

for (i = adj[s].begin(); i != adj[s].end(); ++i)


{
if (!visited[*i])
{
visited[*i] = true;
queue .push _back(*i);
}
}
}
}

int main()
{

Graph g(4);
g.addEdge(0, 1);
g. add Edge(0, 2);
g. add Edge(1, 2);
g. add Edge(2, 0);
g. add Edge(2, 3);
g. add Edge(3, 3);

cout << "Following is Breadth First Traversal "


<< "(starting from vertex 2) \n";
g. BFS(2);

return 0;
}

Output:
Practical No.12
Aim: Write a Kruskal Algorithm.
Source Code:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

#define edge pair<int, int>

class Graph {
private:
vector<pair<int, edge> > G;
vector<pair<int, edge> > T;
int *parent;
int V;
public:
Graph(int V);
void AddWeightedEdge(int u, int v, int w);
int find_set(int i);
void union_set(int u, int v);
void kruskal();
void print();
};
Graph::Graph(int V) {
parent = new int[V];

for (int i = 0; i < V; i++)


parent[i] = i;

G.clear();
T.clear();
}
void Graph::AddWeightedEdge(int u, int v, int w) {
G.push_back(make_pair(w, edge(u, v)));
}
int Graph::find_set(int i) {

if (i == parent[i])
return i;
else

return find_set(parent[i]);
}

void Graph::union_set(int u, int v) {


parent[u] = parent[v];
}
void Graph::kruskal() {
int i, uRep, vRep;
sort(G.begin(), G.end());
for (i = 0; i < G.size(); i++) {
uRep = find_set(G[i].second.first);
vRep = find_set(G[i].second.second);
if (uRep != vRep) {
T.push_back(G[i]);
union_set(uRep, vRep);
}
}
}
void Graph::print() {
cout << "Edge :"
<< " Weight" << endl;
for (int i = 0; i < T.size(); i++) {
cout << T[i].second.first << " - " << T[i].second.second << " : "
<< T[i].first;
cout << endl;
}
}
int main() {
Graph g(6);
g.AddWeightedEdge(0, 1, 4);
g.AddWeightedEdge(0, 2, 4);
g.AddWeightedEdge(1, 2, 2);
g.AddWeightedEdge(1, 0, 4);
g.AddWeightedEdge(2, 0, 4);
g.AddWeightedEdge(2, 1, 2);
g.AddWeightedEdge(2, 3, 3);
g.AddWeightedEdge(2, 5, 2);
g.AddWeightedEdge(2, 4, 4);
g.Add Weighted Edge(3, 2, 3);
g. Add Weighted Edge(3, 4, 3);
g.AddWeightedEdge(4, 2, 4);
g.AddWeightedEdge(4, 3, 3);
g.AddWeightedEdge(5, 2, 2);
g.AddWeightedEdge(5, 4, 3);
g.kruskal();
g.print();
return 0;
}

Output:

Practical No.13
Aim: Write a prism Algorithm.
Source Code:
#include <cstring>
#include <iostream>
using namespace std;
#define INF 9999999

#define V 5

int G[V][V] = {
{0, 9, 75, 0, 0},
{9, 0, 95, 19, 42},
{75, 95, 0, 51, 66},
{0, 19, 51, 0, 31},
{0, 42, 66, 31, 0}};

int main() {
int no_edge;

int selected[V];

memset(selected, false, sizeof(selected));

no_edge = 0;

selected[0] = true;
int x;
int y;

cout << "Edge"


<< " : "
<< "Weight";
cout << endl;
while (no_edge < V - 1) {
int min = INF;
x = 0;
y = 0;

for (int i = 0; i < V; i++) {


if (selected[i]) {
for (int j = 0; j < V; j++) {
if (!selected[j] && G[i][j]) {
if (min > G[i][j]) {
min = G[i][j];
x = i;
y = j;
}
}
}
}
}
cout << x << " - " << y << " : " << G[x][y];
cout << endl;
selected[y] = true;
no_edge++;
}

return 0;
}

Output:

You might also like