0% found this document useful (0 votes)
59 views38 pages

ADA Practicals

ADA Practicals

Uploaded by

Jignasha V Soni
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)
59 views38 pages

ADA Practicals

ADA Practicals

Uploaded by

Jignasha V Soni
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/ 38

ADA(3150703)

Practical:- 1
AIM : Implementation and Time analysis of sorting algorithms.Bubble sort, Selection
sort, Insertion sort, Merge sort and Quicksort.
Insertion sort :
Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards
in your hands.
Algorithm of insertion sort :
for j=2 to A.length
key = A[j]
i = j -1
while( A[i] > key && i > 0 )
A[ i + 1 ] = a[i]
i = i -1
A[ i + 1 } = key
C program of 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;
}
}
void printArray(int arr[], int n) {
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main()
{
int arr[] = {19, 20, 60, 25, 10};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);

1
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)

return 0;
}

Output:

Bubble sort :
In this algorithm, traverse from left and compare adjacent elements and the higher one is
placed at right side.

Algorithm of bubble sort :


int i,j,k
N = length(a);
for j = 1 to n do
for i =0 to N-1 do
if A[i] > A[ i + 1 ] then
temp = A[i]
A[i] = A[ i + 1]
A[ i + 1 ] = temp;

Program for bubble sort :


#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swapping the elements
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

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


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

2
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)

printf("\n");
}

int main()
{
int arr[] = {62, 43, 20, 23, 110, 10, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Output :

Selection sort :
Selection sort is an effective and efficient sort algorithm based on comparison
operations.

Algorithm of selection sort :


for i = 1 to n -1 do
min = I
for j = i + 1 to n do
// find the index of the i smallest element
if A[j] < A[min] then
min=j
end if
end for
swap A[min] and A[i]
end for
Program for selection sort :
#include <stdio.h>
void selectionSort(int arr[], int n) {
int i, j, minIndex, temp;
for (i = 0; i < n - 1; i++) {
minIndex = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex])
minIndex = j;
}

3
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)

// Swapping the minimum element with the current element


temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}

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


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

int main() {
int arr[] = {62, 90, 43, 23, 10};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);

return 0;
}

Output :

Merge sort :
Merge sort continuously cuts down a list into multiple sublists until each has only one item,
then merges those sublists into a sorted list.
Algorithm for merge sort :

Program for merge sort :


#include <stdio.h>
void merge(int arr[], int left, int mid, int right) {
int i, j, k;
int n1 = mid - left + 1;
int n2 = right - mid;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[left + i];

4
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)

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


R[j] = arr[mid + 1 + j];
i = 0;
j = 0;
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) {


arr[k] = R[j];
j++;
k++;
}
}

Output :

5
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)

Quick sort :
Quicksort is a divide-and-conquer algorithm. It works by selecting a 'pivot' element from the
array and partitioning the other elements into two sub-arrays, according to whether they are less than
or greater than the pivot. For this reason, it is sometimes called partition-exchange sort.
Algorithm for quick sort :

Program for quick sort :


#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) {
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {10, 16, 12, 9, 3, 5};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Given array: \n");
printArray(arr, size);

6
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)

quickSort(arr, 0, size - 1);


printf("\nSorted array: \n");
printArray(arr, size);
return 0;
}

Output :

7
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)

Practical :- 2
AIM: Implementation and Time analysis of linear and binary search algorithm.
Algorithm:
Program:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define size 50
void main()
{
int e, a[500], i, c = 0;
clock_t end, start;
double cpu;
printf("Enter element to search: ");
scanf("%d", &e);
printf("\n\n");
for (i = 0; i < 50; i++)
{
a[i] = rand() % 100;
}
for (i = 0; i < 50; i++)
{
printf("%d ", a[i]);
}
printf("\n\nThe found elements are: \n");
start = clock();
for (i = 0; i < 50; i++)
{
if (a[i] == e)
{
printf("%d ", a[i]);
c++;
}
}
end = clock();
cpu = ((double)(end = start) / CLOCKS_PER_SEC);
printf("\n\nThe number of elements found are: %d ", c);
printf("\n\nThe time taken is: %lf\n\n", cpu);
}

8
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)

Output :

9
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)

Practical :- 3
AIM: Implementation of max-heap sort algorithm.

Algorithm:

Program:
#include <stdio.h>
#include <stdlib.h>
int size;
void max_heapify(int *, int);
void build_max(int *);
void heapsort(int *);
int heap_size;

void main()
{
printf("Enter size of array\n");
scanf("%d", &size);
int a[size], i, j, p, k = size - 1;
printf("Enter %d elements in array\n", size);
for (i = 0; i < size; i++)
scanf("%d", &a[i]);

heapsort(a);
printf("\n\n");
printf("Sorted heap is\n");
for (i = 0; i < size; i++)
printf("%d ", a[i]);

printf("\n\n");
int y = log2(size + 1) + 1, l = 0;
for (j = 0; j <= log2(size + 1) + 1; j++)
{
printf("\n");
for (p = y; p >= 0; p--)
printf(" ");
y--;
for (i = 0; (i < pow(2, j) && l < size); i++)
{
printf("%d ", a[k]);
k--;
l++;

10
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)

}
printf("\n");
}
}

void heapsort(int *b)


{
int i, p;
build_max(b);
for (i = size; i >= 0; i--)
{
p = b[0];
b[0] = b[i];
b[i] = p;
heap_size--;
max_heapify(b, 0);
}
}

void build_max(int *c)


{
int i;
heap_size = size;
for (i = size / 2; i >= 0; i--)
{
max_heapify(c, i);
}
}

void max_heapify(int *d, int a)


{
int lt, rt, largest, p;
lt = a * 2;
rt = ((a * 2) + 1);
if (lt <= heap_size && d[lt] > d[a])
largest = lt;
else
largest = a;
if (rt <= heap_size && d[rt] > d[largest])
largest = rt;
if (largest != a)
{
p = d[a];

11
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)

d[a] = d[largest];
d[largest] = p;
max_heapify(d, largest);
}
}

Output:

12
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)

Practical :- 4
AIM: Implementation and Time analysis of factorial program using iterative and
recursive method.
Algorithm:
Program:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int fact(int x)
{
double f = x;
if (x != 1)
f *= fact(x - 1);
else
return 1;
return f;
}

void main()
{
int i, n;
double a, fa = 1;
clock_t end, start;
double cpu;
printf("\nEnter a number to find factorial ");
scanf("%d", &n);
start = clock();

for (i = n; i > 0; i--)


{
fa *= i;
}
end = clock();
cpu = ((double)(end - start) / CLOCKS_PER_SEC);
printf("\nTime for iterative is %lf", cpu);
printf("\nFactorial of %d is %lf\n", n, fa);
start = clock();
a = fact(n);
end = clock();
cpu = ((double)(end - start) / CLOCKS_PER_SEC);
printf("\nTime for recursive is %lf", cpu);

13
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)

printf("\nFactorial of %d is %lf", n, a);


printf("\n");
}

Output:

14
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)

Practical :- 5
AIM: Implementation of a knapsack problem using dynamic programming.

Algorithm:
Program:

#include <stdio.h>
int max
{
return (a > b)? a : b;
}
// Returns the maximum value that can be put in a knapsack of capacity W
int knapsack(int W, int wt[], int val[], int n)
{
int i, w;
int K[n+1][W+1];

// Build table K[][] in bottom up manner


for (i = 0; i <= n; i++)
{
for (w = 0; w <= W; w++)
{
if (i==0 || w==0)
K[i][w] = 0;
else if (wt[i-1] <= w)
K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]);
else
K[i][w] = K[i-1][w];
}
}

return K[n][W];
}

int main()
{
int val[] = {60, 100, 120};
int wt[] = {10, 20, 30};
int W = 50;
int n = sizeof(val)/sizeof(val[0]);
printf("\nValue = %d", knapsack(W, wt, val, n));
return 0;

15
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)

Output:

16
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
Practical :-:6

AIM: Implementation of chain matrix multiplication using dynamic programming.

Program:

#include <stdio.h>
#include <stdlib.h>
void sel(int arr[5][5], int, int);
int n, i, j, s[10][10];

int main()
{
int l, k, q;
printf("Enter number of matrices\n");
scanf("%d", &n);
int p[n + 1], m[n + 1][n + 1];
printf("\nEnter P array values\n");

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


{
printf("p[%d] = ", i);
scanf("%d", &p[i]);
}
for (i = 0; i < n + 1; i++)
{
for (j = 0; j < n + 1; j++)
{
s[i][j] = 0;
m[i][j] = 0;
}
}
for (l = 2; l < n + 1; l++)
{
for (i = 1; i < n - l + 2; i++)
{
j = i + l - 1;
m[i][j] = 100000;
for (k = i; k < j; 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;
s[i][j] = k;
}
}
}

17
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
}
printf("\n\nm Table\n\n");
for (i = 1; i < n + 1; i++)
{
for (j = 1; j < n + 1; j++)
{
printf("%d\t", m[i][j]);
}
printf("\n");
}
printf("\n\n\ns Table\n\n");

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


{
for (j = 1; j < n + 1; j++)
{
printf("%d\t", s[i][j]);
}
printf("\n");
}
printf("\n\nThe solution is : ");
sel(s, 1, n);
printf("\n\n");
return 0;
}

void sel(int s1[5][5], int i, int j)


{
int a, b;
for (a = 0; i < n + 1; a++)
{
for (b = 0; b < n + 1; b++)
{
s1[a][b] = s[a][b];
}
}
if (i == j)
{
printf("A%d", i);
return;
}
else
{
printf(" ( ");
sel(s1, i, s1[i][j]);
sel(s1, (1 + s1[i][j]), j);
printf(" ) ");
}
}

18
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)

Output:

19
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)

Practical :- :7

AIM: Implementation of making a change problem using dynamic programming

Program:
#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++)
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];

20
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
}
}
printf("\n");
for (i = 0; i < n + 1; i++)
{
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\n");
return 0;
}

Output:

21
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
Practical :-:8

AIM: Implementation of a knapsack problem using greedy algorithm

Program:
#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]);
}
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;

22
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
}
}
}
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;
}
}
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;
}

23
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)

Output:

24
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)

Practical :- : 9

AIM: Implementation of Graph and Searching (DFS and BFS).


Graph and Searching Implementation using DFS

Program :
#include<stdio.h>
#include<stdlib.h>

typedef struct node


{
struct node *next;
int vertex;
}node;
node *G[20];
//heads of linked list
int visited[20];
int n;
void read_graph();
//create adjacency list
void insert(int,int);
//insert an edge (vi,vj) in te adjacency list
void DFS(int);
void main()
{
int i;
read_graph();
//initialised visited to 0

for(i=0;i<n;i++)
visited[i]=0;
DFS(0);
}
void DFS(int i)
{
node *p;

printf("\n%d",i);
p=G[i];
visited[i]=1;
while(p!=NULL)
{
i=p->vertex;

if(!visited[i])
DFS(i);
p=p->next;

25
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
}
}
void read_graph()
{
int i,vi,vj,no_of_edges;
printf("Enter number of vertices:");

scanf("%d",&n);
//initialise G[] with a null

for(i=0;i<n;i++)
{
G[i]=NULL;
//read edges and insert them in G[]

printf("Enter number of edges:");


scanf("%d",&no_of_edges);
for(i=0;i<no_of_edges;i++)
{
printf("Enter an edge(u,v):");
scanf("%d%d",&vi,&vj);
insert(vi,vj);
}
}
}
void insert(int vi,int vj)
{
node *p,*q;

//acquire memory for the new node


q=(node*)malloc(sizeof(node));
q->vertex=vj;
q->next=NULL;
//insert the node in the linked list number vi
if(G[vi]==NULL)
G[vi]=q;
else
{
//go to end of the linked list
p=G[vi];

while(p->next!=NULL)
p=p->next;
p->next=q;
}
}

26
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
Output :

BFS :
#include<stdio.h>

// creating queue data structure using arrays


int queue[100];

// defining pointers of the queue to perform pop and push


int front=0,back=0;

// defining push operation on the queue


void push(int var)
{
queue[back] = var;
back++;
}

// defining pop operation on queue


void pop()
{
queue[front] = 0;
front++;
}

// creating a visited array to keep the track of visited nodes


int visited[7] = {0};

int main()
{
// defining the number of total persons
int N = 6;

27
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)

// adjacenty matrix representing graph


int graph[6][6] = {{0,1,1,0,0,0},
{1,0,1,0,0,0},
{1,1,0,1,1,0},
{0,0,1,0,0,0},
{0,0,1,0,0,1},
{0,0,0,0,1,0}};

// front == back stands for empty queue


// until queue is not empty perfroming bfs

// adding a starting node in the list


push(1);
visited[0] = 1; // marking 1st node as visited
while(front != back)
{
int current = queue[front];

// printing current element


printf("%d ", current);

// popping the front element from the queue


pop();

for(int i=0;i<6;i++)
{
// adding non-visited connected nodes of the current node to the queue
if((graph[current-1][i] == 1) && (visited[i] == 0))
{
visited[i] = 1; // marking visisted
push(i+1);
}
}
}
return 0;
}
OUTPUT:

28
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
PRACTICAL :-10
AIM: Implement prim’s algorithm
PROGRAM:
#include <iostream>
using namespace std;
int main()
{
int n;
char c = 'a';
cout << "enter number of nodes";
cin >> n;
int g[n][n];
int stree[n][3];
for (int i = 0; i < n; i++)
stree[i][0] = stree[i][1] = stree[i][2] = -1;
cout << "enter graph egdes 0 for no egde and weight for edge\n";
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
g[i][j] = 0;
if (i != j)
{
cout << "(" << ((char)(c + i)) << "," << (char)(c + j) << "):-";
cin >> g[i][j];
g[j][i] = g[i][j];
}
}
}
cout << "\t ";
for (int i = 0; i < n; i++)
cout << (char)(c + i) << "\t";
for (int i = 0; i < n; i++)
{
cout << endl
<< (char)(c + i) << "\t ";
for (int j = 0; j < n; j++)
{
if (g[i][j] != 0)
cout << "|" << g[i][j];
else
cout << "|-";
cout << "\t";
}
}
// root node
stree[0][0] = stree[0][1] = 0;
stree[0][2] = 1;
29
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
// making stree
int counter = 0, i = 0;
while (counter < n)
{
for (int k = 0; k < n; k++)
{
if (g[i][k] != 0)
{
if (stree[k][0] == -1 && stree[k][2] != 1)
{
stree[k][0] = i;
stree[k][1] = g[i][k];
}
else
{
if (stree[k][1] > g[i][k] && stree[k][2] != 1)
{
stree[k][0] = i;
stree[k][1] = g[i][k];
}
}
}
}
int temp = -1;
for (int k = 0; k < n; k++)
{
if (stree[k][2] != 1 && stree[k][0] != -1)
{
if (temp == -1)
temp = k;
else
{
if (stree[temp][1] > stree[k][1])
temp = k;
}
}
}
i = temp;
stree[i][2] = 1;
counter++;
}
for (i = 0; i < n; i++)
cout << endl
<< (char)(c + i) << ":-" << (char)(c + stree[i][0]) << "," << stree[i][1];
}

30
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)

OUTPUT:

31
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)

PRACTICAL :-:11
AIM: Implement Kruskal’s Algorithm

PROGRAM:

#include <iostream>
using namespace std;
class edge
{
public:
int w = 0, e1 = 0, e2 = 0;
};
int main()
{
int n;
char c = 'a';
int k = 0;
cout << "enter number of nodes";
cin >> n;
int g[n][n];
int stree[n][n], snode[n];
edge e[n * n];
// defalut values for tree
for (int i = 0; i < n; i++)
stree[i][0] = stree[i][1] = stree[i][2] = -1;
cout << "enter graph egdes 0 for no egde and weight for edge\n";
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
g[i][j] = 0;
if (i != j)
{
cout << "(" << ((char)(c + i)) << "," << (char)(c + j) << "):-";
cin >> g[i][j];
g[j][i] = g[i][j];
if (g[i][j] != 0)
{
e[k].w = g[i][j];
e[k].e1 = i;
e[k++].e2 = j;
}
}
}
}
// printing graph table
cout << "\t ";
32
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
for (int i = 0; i < n; i++)
cout << (char)(c + i) << "\t";
for (int i = 0; i < n; i++)
{
cout << endl
<< (char)(c + i) << "\t ";
for (int j = 0; j < n; j++)
{
if (g[i][j] != 0)
cout << "|" << g[i][j];
else
cout << "|-";
cout << "\t";
}
}
// sorting
for (int i = 0; i < k; i++)
{
for (int j = k - 1; j > i; j--)
{
if (e[i].w > e[j].w)
{
edge tem;
tem = e[i];
e[i] = e[j];
e[j] = tem;
}
}
}
// printing sorted edges
for (int i = 0; i < k; i++)
cout << endl
<< e[i].w << "(" << (char)(c + e[i].e1) << "," << (char)(c + e[i].e2) << ")";
// make graph-tree table;
for (int i = 0; i < n; i++)
{
snode[i] = -1;
for (int j = 0; j < n; j++)
stree[i][j] = -1;
}
int gp = 0;
for (int i = 0; i < k; i++)
{
if ((snode[e[i].e1] == -1 && snode[e[i].e2] == -1))
{
stree[e[i].e1][e[i].e2] = stree[e[i].e2][e[i].e1] = e[i].w;
snode[e[i].e1] = snode[e[i].e2] = gp;
gp++;
}

33
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
else if (stree[e[i].e1][e[i].e2] == -1 && (snode[e[i].e1] != snode[e[i].e2]))
{
stree[e[i].e1][e[i].e2] = stree[e[i].e2][e[i].e1] = e[i].w;
if (snode[e[i].e1] != -1 && snode[e[i].e2] == -1)
{
snode[e[i].e2] = snode[e[i].e1];
}
else if (snode[e[i].e2] != -1 && snode[e[i].e1] == -1)
{
snode[e[i].e1] = snode[e[i].e2];
}
else
{
int temp = snode[e[i].e2];
for (int l = 0; l < n; l++)
{
if (snode[l] == temp)
{
snode[l] = snode[e[i].e1];
}
}
}
}
}
cout << endl
<< "\t ";
for (int i = 0; i < n; i++)
cout << (char)(c + i) << "\t ";
for (int i = 0; i < n; i++)
{
cout << endl
<< (char)(c + i) << "\t";
for (int j = 0; j < n; j++)
{
if (stree[i][j] != -1)
cout << "|" << stree[i][j];
else
cout << "|-";
cout << "\t";
}
}
}

34
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)

OUTPUT:

35
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)

PRACTICAL :-: 12
AIM: Implement LCS problem.

PROGRAM:

#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')
{
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])
{

36
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
c[i][j] = c[i - 1][j - 1] + 1;
b[i][j] = 1; // diagonal
}
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++)
{
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)
37
VIEAT/B.E./SEM-V/220943107040
ADA(3150703)
{
lcs(arr, ar, a, p - 1);
}
}

OUTPUT:

38
VIEAT/B.E./SEM-V/220943107040

You might also like