0% found this document useful (0 votes)
86 views56 pages

ADA Practical 09

The document discusses and analyzes the time complexities of various sorting and searching algorithms: - Bubble sort, selection sort, and insertion sort are analyzed, with all having average and worst case time complexities of O(n^2) and best case of O(n) for insertion sort. - Merge sort and quicksort are also analyzed. Merge sort runs in O(nlogn) time and quicksort has average time complexity of O(nlogn). - Binary search and linear search algorithms are provided with binary search having time complexities of O(logn) and linear search having average time complexity of O(n).

Uploaded by

jaytheteacher
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)
86 views56 pages

ADA Practical 09

The document discusses and analyzes the time complexities of various sorting and searching algorithms: - Bubble sort, selection sort, and insertion sort are analyzed, with all having average and worst case time complexities of O(n^2) and best case of O(n) for insertion sort. - Merge sort and quicksort are also analyzed. Merge sort runs in O(nlogn) time and quicksort has average time complexity of O(nlogn). - Binary search and linear search algorithms are provided with binary search having time complexities of O(logn) and linear search having average time complexity of O(n).

Uploaded by

jaytheteacher
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/ 56

ADA (3150703) 211290116062

Experiment-1
Aim: Implementation and Time analysis of sorting algorithms. Bubble sort,
Selection sort, and Insertion sort.
Bubble sort:

#include<stdio.h>

int main()

int i,j,temp,a[5];

printf("\n enter the element of arry");

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

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

for(i=1; i<5; i++) {

for(j=0; j<5; j++) {

if(a[j]>a[j+1]) {

temp=a[j+1];

a[j=1]=a[j];

a[j]=temp;

GMIT, Bhavnagar 1
ADA (3150703) 211290116062

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

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

return 0;

Output:

Time Analysis:

Time complexity for Average case = O(n2)


Time complexity for Worst case = O(n2)
Time complexity for Best case = O(n2)

GMIT, Bhavnagar 2
ADA (3150703) 211290116062

Selection sort:

#include <stdio.h>

int main() {

int arr[10]={6,12,0,18,11,99,55,45,34,2};

int n=10;

int i, j, position, swap;

for (i = 0; i < (n - 1); i++) {

position = i;

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

if (arr[position] > arr[j])

position = j;

if (position != i) {

swap = arr[i];

arr[i] = arr[position];

arr[position] = swap;

GMIT, Bhavnagar 3
ADA (3150703) 211290116062

printf("arry after selection sorting\n");for (i = 0; i < n; i++)

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

return 0;

Output:

Time Analysis:

Time complexity for Average case : O(n2)


Time complexity for Worst case : O(n2)
Time complexity for Best case : O(n2)

GMIT, Bhavnagar 4
ADA (3150703) 211290116062

Insertion sort:

#include <stdio.h>

void insertionSort(int arr[], int n) {

int i, key, j;

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

key = arr[i];

j = i - 1;

while (j >= 0 && arr[j] > key) {

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

j = j - 1;

arr[j + 1] = key;

int main() {

int arr[] = { 19, 66, 55, 45, 67 };

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

insertionSort(arr, n);

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

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

printf("\n");

return 0;

GMIT, Bhavnagar 5
ADA (3150703) 211290116062

Output:

Time Analysis:

Time complexity for average case : O(n2)


Time complexity for Worst case : O(n2)
Time complexity for Best case : O(n)

GMIT, Bhavnagar 6
ADA (3150703) 211290116062

Experiment-2
Aim: Implementation and Time analysis of sorting algorithms. Merge
sort,Quick short
Merge short:

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

GMIT, Bhavnagar 7
ADA (3150703) 211290116062

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

GMIT, Bhavnagar 8
ADA (3150703) 211290116062

Output:

Time Analysis:

Left half : C(n/2),right half : C(n/2),All : n

Case 1: if f(n) ∈ O(nᴱ⁻ᵉ), for e>0,n ∈ O(n¹⁻¹),n ∈ O(n⁰)

case 1 does not match

T(n) = 2*T(n/2) + n

a = 2, b = 2, f(n) =

E = log bª = log₂2 = 1

Case 2: if f(n) ∈ Θ (n¹), for e>0

n ∈ O(n)

then T(n) ∈ Θ (f(n) * logn)

T(n) ∈ Θ(n*logn)

GMIT, Bhavnagar 9
ADA (3150703) 211290116062

Quick sort:

#include <stdio.h>

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

int temp = *a;

*a = *b;

*b = temp;

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

GMIT, Bhavnagar 10
ADA (3150703) 211290116062

int main() {

int n;

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

scanf("%d", &n);

if (n <= 0) {

printf("Invalid input. Number of elements must be greater than 0.\n");

return 1;

int arr[n];

printf("Enter %d integers:\n", n);

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

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

// Perform Quick Sort on the array

quickSort(arr, 0, n - 1);

// Display the sorted array

printf("Sorted array: ");

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

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

GMIT, Bhavnagar 11
ADA (3150703) 211290116062

printf("\n");

return 0;

Output:

Time Analysis:

Left half : C(n/2),right half : C(n/2),All : n

T(n) = 2*T(n/2) + n

a = 2, b = 2, f(n) =

n E = log bª

= log₂2= 1

Case 1: if f(n) ∈ O(nᴱ⁻ᵉ), for e>0,n ∈ O(n¹⁻¹),n ∈ O(n⁰) (case 1 does not match)

Case 2: if f(n) ∈ Θ (n¹), for e>0,n ∈ O(n)

then T(n) ∈ Θ (f(n) * logn)

T(n) ∈ Θ(n*logn

GMIT, Bhavnagar 12
ADA (3150703) 211290116062

Experiment-3
Aim: Implementation and Time analysis of linear and binary search , linear
search algorithm
Binary serch:

#include<stdio.h>

#define MAX_SIZE 5

void binary_search(int[],int);

int main() {

int arr_search[MAX_SIZE], i,element;

printf("Simple Binary Search Example - Array and Functions\n");

printf("\nEnter %d Elements for Searching : \n", MAX_SIZE);

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

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

printf("Enter Element to Search : ");

scanf("%d", &element);

binary_search(arr_search,element);

void binary_search(int fn_arr[],int element) {

int f = 0, r = MAX_SIZE,mid;

while (f <= r) {

mid = (f+r)/2;

if (fn_arr[mid] == element) {

GMIT, Bhavnagar 13
ADA (3150703) 211290116062

printf("\nSearch Element : %d : Found : Position : %d.\n", element, mid+1);

break;

else if (fn_arr[mid] < element)

f = mid + 1;

else

r = mid - 1;

if (f > r)

printf("\nSearch Element : %d : Not Found \n", element);

Output:

Time Analysis:

Best-case time complexity: O(1)

Average-case time complexity: O(log n)

Worst-case time complexity: O(log n)

GMIT, Bhavnagar 14
ADA (3150703) 211290116062

Linear Search:

#include <stdio.h>

int linearSearch(int arr[], int n, int target) {

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

if (arr[i] == target) {

return i; // Return the index where the target element is found

return -1; // Return -1 if the target element is not found

int main() {

int n;

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

if (n <= 0) {

printf("Invalid input. Number of elements must be greater than 0.\n");

return 1;

int arr[n];

printf("Enter %d integers in the array:\n", n);

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

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

GMIT, Bhavnagar 15
ADA (3150703) 211290116062

int target;

printf("Enter the element to search: ");

scanf("%d", &target);

// Perform Linear Search on the array

int result = linearSearch(arr, n, target);

if (result != -1) {

printf("Element %d found at index %d\n", target, result);

} else {

printf("Element %d not found in the array\n", target);}

return 0; }

Output:

Time Analysis:

Best-case time complexity: O(1)

Average-case time complexity: O(n)

Worst-case time complexity: O(n)

GMIT, Bhavnagar 16
ADA (3150703) 211290116062

Experiment-4
Aim: Implementation and Time analysis of Heap sort Algorithme.
Heap sort:

#include <stdio.h>

void swap(int* a, int* b)

int temp = *a;

*a = *b;

*b = temp;

void heapify(int arr[], int N, int i)

int largest = i;

int left = 2 * i + 1;

int right = 2 * i +

2;

if (left < N && arr[left] > arr[largest])

largest = left;

if (right < N && arr[right] > arr[largest])

largest = right;

if (largest != i) {

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

heapify(arr, N, largest);

}
GMIT, Bhavnagar 17
ADA (3150703) 211290116062

void heapSort(int arr[], int N)

for (int i = N / 2 - 1; i >= 0; i--)

for (int i = N - 1; i >= 0; i--) {

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

heapify(arr, i, 0);

void printArray(int arr[], int N)

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

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

printf("\n");

int main()

int arr[] = { 13, 26 ,88, 65 ,98};

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

heapSort(arr, N);

printf("Sorted array is\n");

printArray(arr, N);

GMIT, Bhavnagar 18
ADA (3150703) 211290116062

Output:

Time Analysis:

Best-case time complexity: O(1)

Average-case time complexity: O(n)

Worst-case time complexity: O(n)

GMIT, Bhavnagar 19
ADA (3150703) 211290116062

Experiment-5
Aim: Implementation and Time analysis of factorial program using iterative
and recursive method
iterative method:

#include <stdio.h>

int main() {

int n, i;

long long fact = 1;

printf("Enter a number: ");

scanf("%d", &n);

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

fact *= i;

printf("Factorial of %d = %lld", n, fact);

return 0;

GMIT, Bhavnagar 20
ADA (3150703) 211290116062

Output:

Time Analysis:

Best-case time complexity: O(1)

Average-case time complexity: O(n)

Worst-case time complexity: O(n)

GMIT, Bhavnagar 21
ADA (3150703) 211290116062

recursive method:

#include <stdio.h>

long int multiplyNumbers(int n);

int main() {

int n;

printf("Enter a positive integer: ");

scanf("%d", &n);

printf("Factorial of %d = %ld", n, multiplyNumbers(n));

return 0;

long int multiplyNumbers(int n) {

if (n >= 1)

return n * multiplyNumbers(n - 1);

else

return 1;

GMIT, Bhavnagar 22
ADA (3150703) 211290116062

Output:

Time Analysis:

Best-case time complexity: O(1)

Average-case time complexity: O(n)

Worst-case time complexity: O(n)

GMIT, Bhavnagar 23
ADA (3150703) 211290116062

Experiment-6
Aim:Implementation of a knapsack problem using dynamic programming.
knapsack:

void main() {

int n, m, w[100], p[100], ratio[100], i, j, u, temp;

float xr, x[100], total_profit=0, total_weight=0;

printf("Enter the number of items(n): ");

scanf("%d", &n);

printf("Enter the capacity of the Knapsack(m): ");

scanf("%d", &m);

u = m;

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

x[i] = 0;

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

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

printf("Weight of item %d = ", i + 1);

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

printf("Enter the Profit Values of items: ");

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

printf("Profit of item %d = ", i + 1);

GMIT, Bhavnagar 24
ADA (3150703) 211290116062

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

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

ratio[i] = p[i] / w[i];

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

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

if (ratio[j] < ratio[i]) {

temp = ratio[i];

ratio[i] = ratio[j];

ratio[j] = temp;

temp = w[i];

w[i] = w[j];

w[j] = temp;

temp = p[i];

p[i] = p[j];

p[j] = temp;

printf("\nThe Table After Sorting based on the Ratio:\n");

printf("Item:\t\t");

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

printf("%d\t", i + 1);

GMIT, Bhavnagar 25
ADA (3150703) 211290116062

printf("\nProfit:\t\t");

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

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

printf("\nWeights:\t");

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

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

printf("\nRATIO:\t\t");

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

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

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

if (w[i] <= u) {

x[i] = 1;

u = u - w[i];

} else if (w[i] > u) {

break;

if (i <= n) {

xr = (float)u / w[i];

x[i] = xr;

GMIT, Bhavnagar 26
ADA (3150703) 211290116062

printf("\nX = [");

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

printf("%.3f, ", x[i]);

printf("]");

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

total_profit += x[i] * p[i];

total_weight += x[i] * w[i];

printf("\nTotal Profit = %.2f\nTotal Weight = %.2f", total_profit, total_weight);

GMIT, Bhavnagar 27
ADA (3150703) 211290116062

Output:

GMIT, Bhavnagar 28
ADA (3150703) 211290116062

Experiment-7
Aim: Implement of chain matrix multiplication using dynamic programming.
#include<stdio.h>

#include<limits.h>

int MatrixChainMultiplication(int p[], int n)

int m[n][n];

int i, j, k, L, q;

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

m[i][i] = 0;

for (L=2; L<n; L++)

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

j = i+L-1;

m[i][j] = INT_MAX;

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

q = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j];

if (q < m[i][j])

m[i][j] = q;

GMIT, Bhavnagar 29
ADA (3150703) 211290116062

return m[1][n-1];

int main()

int n,i;

printf("Enter number of matrices\n");

scanf("%d",&n);

n++;

int arr[n];

printf("Enter dimensions \n");

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

printf("Enter d%d :: ",i);

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

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

printf("Minimum number of multiplications is %d ", MatrixChainMultiplication(arr, size));

return 0;

GMIT, Bhavnagar 30
ADA (3150703) 211290116062

Output:

GMIT, Bhavnagar 31
ADA (3150703) 211290116062

Experiment-8
Aim: Implement of making achange problem using dynamic programming.
#include <stdio.h>

#include

<stdlib.h> int

main()

int m, n, i, j;

printf("Enter the amount of which you want change\n");

scanf("%d", &m);

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

scanf("%d", &n);

int c[n + 1][m + 1], dc[n + 1]; printf("\

nEnter the determinants\n"); dc[0] =

0;

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

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

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

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

c[i][j] = 0;

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

GMIT, Bhavnagar 32
ADA (3150703) 211290116062

c[i][0] = 0;

for (i = 0; i < m + 1; i++)

c[0][i] = i;

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

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

if (dc[1] > c[0][j])

c[i][j] = 0;

else if (dc[i] <= j)

if ((c[i][j - dc[i]] + 1) < c[i - 1][j])

c[i][j] = (c[i][j - dc[i]] + 1);

else

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

else

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

printf("\n \n");

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

GMIT, Bhavnagar 33
ADA (3150703) 211290116062

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

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

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

printf("\n");

printf("\nThe change you get is \n");

i = n;

j = m;

while (i != 1)

if (c[i][j] == c[i - 1][j])

i = i - 1;

else

j = j - dc[i];

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

printf("\n");

return 0;

GMIT, Bhavnagar 34
ADA (3150703) 211290116062

Output:

GMIT, Bhavnagar 35
ADA (3150703) 211290116062

Experiment-9
Aim: Implementation of a knapsack problem using greedy algorithm

knapsack :

#include <stdio.h>

#include

<stdlib.h> int

main()

int m, n, i, j;

printf("Enter maximum weight of knapsack ");

scanf("%d", &m);

printf("\nEnter number of objects ");

scanf("%d", &n);

int wt = 0, k = 0;

float cal[n], p[n], w[n], x[n], prof = 0;

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

x[i] = 0;

printf("\nEnter weights\n");

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

printf("w[%d] = ", i);

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

GMIT, Bhavnagar 36
ADA (3150703) 211290116062

printf("\nEnter profits\n");

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

printf("p[%d] = ", i);

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

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

cal[i] = p[i] / w[i];

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

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

if (cal[i] < cal[j])

int t1, t2, t3;

t1 = cal[i];

cal[i] = cal[j];

cal[j] = t1;

t2 = w[i];

w[i] = w[j];

w[j] = t2;

t3 = p[i];

p[i] = p[j];

p[j] = t3;

GMIT, Bhavnagar 37
ADA (3150703) 211290116062

printf("\n\n p[i]\t\t w[i]\t\t cal[i]\n");

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

printf("%f\t %f\t %f\t\n", p[i], w[i], cal[i]);

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

if ((wt + w[i]) <= m)

k++;

x[i] = 1;

wt += w[i];

prof += p[i];

else

k++;

x[i] = (m - wt) / w[i];

w[i] = m - wt;

wt = m;

prof += (x[i] * p[i]);

p[i] = (x[i] * p[i]);

break;

GMIT, Bhavnagar 38
ADA (3150703) 211290116062

printf("\nThe selected weights are \n\ni\t w[i]\t\t p[i]\n");

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

printf("%d\t%f\t%f\n", i + 1, w[i], p[i]);

printf("\n\nThe total profit is %f\n\n", prof);

return 0;

Output:

GMIT, Bhavnagar 39
ADA (3150703) 211290116062

Experiment-10
Aim:Implementation of Graph and Searching (DFS and BFS).

#include <stdio.h>

int a[20][20], reach[20], n;

void dfs(int v)

int i;

reach[v] = 1;

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

if (a[v][i] && !reach[i])

printf("\n %d->%d", v, i);

dfs(i);

void main()

int i, j, count = 0;

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

GMIT, Bhavnagar 40
ADA (3150703) 211290116062

scanf("%d", &n);

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

reach[i] = 0;

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

a[i][j] = 0;

printf("\nEnter adjacency matrix:\n");

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

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

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

dfs(1);

printf("\n");

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

if (reach[i])

count++;

if (count == n)

printf("\n Graph is connected");

else

printf("\n Graph is not connected");

GMIT, Bhavnagar 41
ADA (3150703) 211290116062

Output:

GMIT, Bhavnagar 42
ADA (3150703) 211290116062

Experiment-11
Aim: Implement prim’s algorithm.
prim’s algorithm:

#include<stdio.h>

#include<stdbool.h>

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

printf("Edge : Weight\n");

GMIT, Bhavnagar 43
ADA (3150703) 211290116062

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;

printf("%d - %d : %d\n", x, y, G[x][y]);

selected[y] = true;

no_edge++;

return 0;

GMIT, Bhavnagar 44
ADA (3150703) 211290116062

Output:

GMIT, Bhavnagar 45
ADA (3150703) 211290116062

Experiment-12
Aim: Implement kruskal’s algorithm.
kruskal’s algorithm:

#include <stdio.h>

#define MAX 30

typedef struct edge {

int u, v, w;

} edge;

typedef struct edge_list {

edge data[MAX];

int n;

} edge_list;

edge_list elist;

int Graph[MAX][MAX], n;

edge_list spanlist;

void kruskalAlgo();

int find(int belongs[], int vertexno);

void applyUnion(int belongs[], int c1, int c2);

void sort();

void print();

void kruskalAlgo() {

int belongs[MAX], i, j, cno1, cno2;

elist.n = 0;

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

GMIT, Bhavnagar 46
ADA (3150703) 211290116062

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

if (Graph[i][j] != 0) {

elist.data[elist.n].u = i;

elist.data[elist.n].v = j;

elist.data[elist.n].w = Graph[i][j];

elist.n++;

sort();

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

belongs[i] = i;

spanlist.n = 0;

for (i = 0; i < elist.n; i++) {

cno1 = find(belongs, elist.data[i].u);

cno2 = find(belongs, elist.data[i].v);

if (cno1 != cno2) {

spanlist.data[spanlist.n] = elist.data[i];

spanlist.n = spanlist.n + 1;

applyUnion(belongs, cno1, cno2);

int find(int belongs[], int vertexno) {

return (belongs[vertexno]);

GMIT, Bhavnagar 47
ADA (3150703) 211290116062

void applyUnion(int belongs[], int c1, int c2) {

int i;

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

if (belongs[i] == c2)

belongs[i] = c1;

void sort() {

int i, j;

edge temp;

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

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

if (elist.data[j].w > elist.data[j + 1].w) {

temp = elist.data[j];

elist.data[j] = elist.data[j + 1];

elist.data[j + 1] = temp;

void print() {

int i, cost = 0;

for (i = 0; i < spanlist.n; i++) {

printf("\n%d - %d : %d", spanlist.data[i].u, spanlist.data[i].v, spanlist.data[i].w);

cost = cost + spanlist.data[i].w;

GMIT, Bhavnagar 48
ADA (3150703) 211290116062

printf("\nSpanning tree cost: %d", cost);

int main() {

int i, j, total_cost;

n = 6;

Graph[0][0] = 0;

Graph[0][1] = 4;

Graph[0][2] = 4;

Graph[0][3] = 0;

Graph[0][4] = 0;

Graph[0][5] = 0;

Graph[0][6] = 0;

Graph[1][0] = 4;

Graph[1][1] = 0;

Graph[1][2] = 2;

Graph[1][3] = 0;

Graph[1][4] = 0;

Graph[1][5] = 0;

Graph[1][6] = 0;

Graph[2][0] = 4;

Graph[2][1] = 2;

Graph[2][2] = 0;

GMIT, Bhavnagar 49
ADA (3150703) 211290116062

Graph[2][3] = 3;

Graph[2][4] = 4;

Graph[2][5] = 0;

Graph[2][6] = 0;

Graph[3][0] = 0;

Graph[3][1] = 0;

Graph[3][2] = 3;

Graph[3][3] = 0;

Graph[3][4] = 3;

Graph[3][5] = 0;

Graph[3][6] = 0;

Graph[4][0] = 0;

Graph[4][1] = 0;

Graph[4][2] = 4;

Graph[4][3] = 3;

Graph[4][4] = 0;

Graph[4][5] = 0;

Graph[4][6] = 0;

Graph[5][0] = 0;

Graph[5][1] = 0;

Graph[5][2] = 2;

GMIT, Bhavnagar 50
ADA (3150703) 211290116062

Graph[5][3] = 0;

Graph[5][4] = 3;

Graph[5][5] = 0;

Graph[5][6] = 0;

printf("Name: YASHRAJSINH GOHIL\n");

printf("Enrollment Number: 211290116009\n");

kruskalAlgo();

print();

Output:

GMIT, Bhavnagar 51
ADA (3150703) 211290116062

Experiment-13
Aim: Implement LCS problem.
LCS:

#include <stdio.h>

void lcs(int arr[][10], char ar[], int, int);

int l, k;

void main()

char s1[10], s2[10];

int i, j;

printf("\nEnter the strings\nX = ");

s1[0] = 0;

char ch = getchar();

while (ch != '\n')

s1[++l] = ch;

ch = getchar();

printf("Y = ");

s2[0] = 0;

char c1 = getchar();

while (c1 != '\n')

GMIT, Bhavnagar 52
ADA (3150703) 211290116062

s2[++k] = c1;

c1 = getchar();

int c[l + 1][k + 1], b[l + 1][k + 1];

for (i = 0; i < l + 1; i++)

for (j = 0; j < k + 1; j++)

b[i][j] = 0;

c[i][j] = 0;

// 1=diagonal 2=up 3=left

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

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

if (s1[i] == s2[j])

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

b[i][j] = 1; // diagonal

GMIT, Bhavnagar 53
ADA (3150703) 211290116062

else if (c[i - 1][j] >= c[i][j - 1])

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

b[i][j] = 2; // up

else

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

b[i][j] = 3; // left

printf("\n\n\t");

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

printf("\t %c", s2[i]);

printf("\n");

for (i = 0; i < l + 1; i++)

printf("%c\t", s1[i]);

for (j = 0; j < k + 1; j++)

GMIT, Bhavnagar 54
ADA (3150703) 211290116062

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

printf("\n");

printf("\n\n");

printf("Longest common substring of X and Y is ");

lcs(b, s1, l, k);

printf("\n\n");

void lcs(int arr[l + 1][k + 1], char ar[l + 1], int a, int p)

if (a == 0 || p == 0)

return;

if (arr[a][p] == 1)

lcs(arr, ar, a - 1, p - 1);

printf("%c ", ar[a]);

else if (arr[a][p] == 2)

lcs(arr, ar, a - 1, p);

else if (arr[a][p] == 3)

GMIT, Bhavnagar 55
ADA (3150703) 211290116062

lcs(arr, ar, a, p - 1);

Output:

GMIT, Bhavnagar 56

You might also like