0% found this document useful (0 votes)
35 views24 pages

DAA Practical File

Uploaded by

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

DAA Practical File

Uploaded by

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

1|Pa ge

Course Objective

Course Outcomes
2|Pa ge

Syllabus
3|Pa ge

PROGRAM 1. To implement Strassen’s matrix multiplication algorithm.


#include<stdio.h>

int main(){

int z[2][2];

int i, j;

int m1, m2, m3, m4, m5, m6, m7;

int x[2][2] = {

{12, 34},

{22, 10}

};

int y[2][2] = {{3, 4}, {2,1}};

printf("The first matrix is : ");

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

printf("\n");

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

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

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

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

printf("\n");

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

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

m1 = (x[0][0] + x[1][1])*(y[0][0] + y[1][1]);

m2 = (x[1][0] + x[1][1]) * (y[0][0]);

m3 = x[0][0] * (y[0][1] - y[1][1]);

m4 = x[1][1] *(y[1][0] - y[0][0]);

m5 = (x[0][0] + x[0][1]) * y[1][1];

m6 = (x[1][0] - x[0][0]) * (y[0][0] + y[0][1]);

m7 = (x[0][1] - x[1][1])*(y[1][0] + y[1][1]);

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


4|Pa ge

z[0][1] = m3 + m5;

z[1][0] = m2 + m4;

z[1][1] = m1 - m2 + m3 + m6;

printf("\nProduct achieved using Strassen's algorithm: ");

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

printf("\n");

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

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

return 0;

}
5|Pa ge

OUTPUT
The first matrix is :

12 34

22 10

The second matrix is :

3 4

2 1

Product achieved using Strassen's algorithm:

104 82

86 98
6|Pa ge

PROGRAM 2. To implement Merge Sort Algorithm.


#include<stdio.h>

void merge(int arr[], int left, int middle, int right){

int n1 = middle - left + 1;

int n2 = right - middle;

int L[n1], R[n2];

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

L[i] = arr[left + i];

for(int j = 0; j < n2; j++)

R[j] = arr[middle + 1 + j];

int i = 0, j = 0;

int k = left;

while(i < n1 && j < n2){

if(L[i] <= R[j]){

arr[k] = L[i];

i++;

} else{

arr[k] = R[j];

j++;

k++;

while(i < n1){

arr[k] = L[i];

i++;

k++;

while(j < n2){


7|Pa ge

arr[k] = R[j];

j++;

k++;

void mergeSort(int arr[], int left, int right){

if(left < right){

int middle = left + (right - left)/ 2;

mergeSort(arr, left, middle);

mergeSort(arr, middle + 1, right);

merge(arr, left, middle, right);

void printArray(int arr[], int size){

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

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

printf("\n");

int main(){

int arr[] = {12, 11, 13, 5, 6, 7};

int arr_size = sizeof(arr) / sizeof(arr[0]);

printf("Given array is \n");

printArray(arr, arr_size);

mergeSort(arr, 0 , arr_size-1);

printf("\nSorted array is \n");

printArray(arr, arr_size);

return 0;

}
8|Pa ge

OUTPUT
Given array is

12 11 13 5 6 7

Sorted array is

5 6 7 11 12 13
9|Pa ge

PROGRAM 3. To implement Quick Sort Algorithm.


#include<stdio.h>

void swap(int* a, int* b){

int t = *a;

*a = *b;

*b = t;

int partition(int arr[], int low, int high){

int pivot = arr[high];

int i = (low - 1);

for(int j = low; j<=high - 1; j++){

if(arr[j] <= pivot){

i++;

swap(&arr[i], &arr[j]);

swap(&arr[i+1], &arr[high]);

return(i + 1);

void quickSort(int arr[], int low, int high){

if(low < high){

int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);

quickSort(arr, pi+1, high);

void printArray(int arr[], int size){

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

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

printf("\n");

}
10 | P a g e

int main(){

int arr[] = {10, 2, 43,6 , 34 ,1,4};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Given array is \n");

printArray(arr, n);

quickSort(arr, 0, n - 1);

printf("\nSorted array is \n");

printArray(arr, n);

return 0;

}
11 | P a g e

OUTPUT
Given array is

10 2 43 6 34 1 4

Sorted array is

1 2 4 6 10 34 43
12 | P a g e

PROGRAM 4. To implement fractional Knapsack Problem Algorithm.


#include <stdio.h>

#include <stdlib.h>

struct Item {

int value;

int weight;

};

int compare(const void *a, const void *b) {

double r1 = (double)((struct Item *)a)->value / ((struct Item *)a)->weight;

double r2 = (double)((struct Item *)b)->value / ((struct Item *)b)->weight;

if (r1 > r2) return -1;

if (r1 < r2) return 1;

return 0;

double fractionalKnapsack(int W, struct Item arr[], int n) {

qsort(arr, n, sizeof(arr[0]), compare);

int curWeight = 0;

double finalValue = 0.0;

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

if (curWeight + arr[i].weight <= W) {

curWeight += arr[i].weight;

finalValue += arr[i].value;

} else {

int remain = W - curWeight;

finalValue += arr[i].value * ((double) remain / arr[i].weight);

break;

return finalValue;

int main() {
13 | P a g e

int W = 50;

struct Item arr[] = {{60, 10}, {100, 20}, {120, 30}};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Maximum value in Knapsack = %.2f\n", fractionalKnapsack(W, arr, n));

return 0;

}
14 | P a g e

OUTPUT
Maximum value in Knapsack = 240.00
15 | P a g e

PROGRAM 5. To implement Job Sequencing with Deadlines Algorithm.


#include <stdio.h>

#include <stdlib.h>

struct Job {

char id;

int deadline;

int profit;

};

int compare(const void *a, const void *b) {

return ((struct Job *)b)->profit - ((struct Job *)a)->profit;

void jobSequencingWithDeadline(struct Job arr[], int n) {

qsort(arr, n, sizeof(struct Job), compare);

int result[n];

int slot[n];

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

slot[i] = 0;

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

for (int j = arr[i].deadline - 1; j >= 0; j--) {

if (slot[j] == 0) {

result[j] = i;

slot[j] = 1;

break;

printf("Following is the sequence of jobs that maximizes profit:\n");

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

if (slot[i])

printf("%c ", arr[result[i]].id);

}
16 | P a g e

printf("\n");

int main() {

struct Job arr[] = {{'a', 2, 100}, {'b', 1, 19}, {'c', 2, 27}, {'d', 1, 25}, {'e', 3, 15}};

int n = sizeof(arr) / sizeof(arr[0]);

jobSequencingWithDeadline(arr, n);

return 0;

}
17 | P a g e

OUTPUT
Following is the sequence of jobs that maximizes profit:

cae
18 | P a g e

PROGRAM 6. To implement Kruskal’s and prims algorithms.


• Kruskal’s algorithms.
#include <stdio.h>

#include <stdlib.h>

#define MAX 100

struct Edge {

int src, dest, weight;

};

struct Graph {

int V, E;

struct Edge* edge;

};

struct Subset {

int parent;

int rank;

};

int compare(const void* a, const void* b) {

struct Edge* a1 = (struct Edge*)a;

struct Edge* b1 = (struct Edge*)b;

return a1->weight > b1->weight;

struct Graph* createGraph(int V, int E) {

struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));

graph->V = V;

graph->E = E;

graph->edge = (struct Edge*) malloc(graph->E * sizeof(struct Edge));

return graph;

int find(struct Subset subsets[], int i) {

if (subsets[i].parent != i)

subsets[i].parent = find(subsets, subsets[i].parent);


19 | P a g e

return subsets[i].parent;

void Union(struct Subset subsets[], int x, int y) {

int xroot = find(subsets, x);

int yroot = find(subsets, y);

if (subsets[xroot].rank < subsets[yroot].rank)

subsets[xroot].parent = yroot;

else if (subsets[xroot].rank > subsets[yroot].rank)

subsets[yroot].parent = xroot;

else {

subsets[yroot].parent = xroot;

subsets[xroot].rank++;

void KruskalMST(struct Graph* graph) {

int V = graph->V;

struct Edge result[V];

int e = 0;

int i = 0;

qsort(graph->edge, graph->E, sizeof(graph->edge[0]), compare);

struct Subset* subsets = (struct Subset*) malloc(V * sizeof(struct Subset));

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

subsets[v].parent = v;

subsets[v].rank = 0;

while (e < V - 1 && i < graph->E) {

struct Edge next_edge = graph->edge[i++];

int x = find(subsets, next_edge.src);

int y = find(subsets, next_edge.dest);

if (x != y) {

result[e++] = next_edge;
20 | P a g e

Union(subsets, x, y);

printf("Following are the edges in the constructed MST\n");

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

printf("%d -- %d == %d\n", result[i].src, result[i].dest, result[i].weight);

return;

int main() {

int V = 4;

int E = 5;

struct Graph* graph = createGraph(V, E);

graph->edge[0].src = 0;

graph->edge[0].dest = 1;

graph->edge[0].weight = 10;

graph->edge[1].src = 0;

graph->edge[1].dest = 2;

graph->edge[1].weight = 6;

graph->edge[2].src = 0;

graph->edge[2].dest = 3;

graph->edge[2].weight = 5;

graph->edge[3].src = 1;

graph->edge[3].dest = 3;

graph->edge[3].weight = 15;

graph->edge[4].src = 2;

graph->edge[4].dest = 3;

graph->edge[4].weight = 4;

KruskalMST(graph);

return 0;

}
21 | P a g e

OUTPUT
Following are the edges in the constructed MST

2 -- 3 == 4
22 | P a g e

• Prims Algorithm
#include <stdio.h>

#include <limits.h>

#include <stdbool.h>

#define V 5

int minKey(int key[], bool mstSet[]) {

int min = INT_MAX, min_index;

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

if (mstSet[v] == false && key[v] < min)

min = key[v], min_index = v;

return min_index;

void printMST(int parent[], int graph[V][V]) {

printf("Edge \tWeight\n");

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

printf("%d - %d \t%d \n", parent[i], i, graph[i][parent[i]]);

void primMST(int graph[V][V]) {

int parent[V];

int key[V];

bool mstSet[V];

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

key[i] = INT_MAX, mstSet[i] = false;

key[0] = 0;

parent[0] = -1;

for (int count = 0; count < V - 1; count++) {

int u = minKey(key, mstSet);

mstSet[u] = true;

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

if (graph[u][v] && mstSet[v] == false && graph[u][v] < key[v])


23 | P a g e

parent[v] = u, key[v] = graph[u][v];

printMST(parent, graph);

int main() {

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

return 0;

}
24 | P a g e

OUTPUT
Edge Weight

0-1 2

1-2 3

0-3 6

1-4 5

0—3==5

0—1==10

You might also like