114 - Analysis and Design of Algorithm
114 - Analysis and Design of Algorithm
PRACTICAL 1
Aim: Implementation and Time Analysis of Sorting Algorithms.
1. Bubble Sort
Description
It is a simple sorting algorithm that works by
Comparing each pair of adjacent items and swapping them if they are
in the wrong order.
The pass through the list is repeated until no swaps are needed, which
indicates that the list is sorted.
As it only uses comparisons to operate on elements, it is a comparison
sort.
Although the algorithm is simple, it is too slow for practical use.
Algorithm
int flag=1;
for(i = 1; i < n; i++)
for(j = 1; j < n-i; j++)
if(A[j] > A[j+1])
flag=0;
swap(A[j],A[j+1])
if(flag == 1)
cout<<"already sorted"<<endl
break;
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, j, a[30], temp;
printf("\nEnter total: ");
scanf("%d",&n);
int flag = 1;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
Page 1
TYIT 2 B 190410116114
Output
Page 2
TYIT 2 B 190410116114
Time Complexity
Worst Case Time Complexity : O(n2)
Best Case Time Complexity : Ω (n)
Average Time Complexity: θ (n2)
Space Complexity: O(1)
2. Selection Sort
Description
Selection sort divides the array or list into two parts,
The sorted part at the left end
and the unsorted part at the right end.
Initially, the sorted part is empty and the unsorted part is the entire
list.
The smallest element is selected from the unsorted array and
swapped with the leftmost element, and that element becomes a part
of the sorted array.
Then it finds the second smallest element and exchanges it with the
element in the second leftmost position.
This process continues until the entire array is sorted.
Algorithm
for i ← 1 to n-1 do
minj ← i;
minx ← A[i];
for j ← i + 1 to n do
if A[j] < minx then
minj ← j;
minx ← A[j];
A[minj] ← A[i];
A[i] ← minx;
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, j, a[30], minj,minx;
printf("\nEnter total: ");
scanf("%d",&n);
Page 3
TYIT 2 B 190410116114
for(i=0;i<n-1;i++)
{
minj = i;
minx = a[i];
for(j=i+1;j<n;j++)
{
if (a[j] < minx)
{
minj = j;
minx = a[j];
}
}
a[minj] = a[i];
a[i] = minx;
}
Output
Page 4
TYIT 2 B 190410116114
Time Complexity
Worst Case Time Complexity : O(n^2)
Best Case Time Complexity : Ω(n^2)
Average Time Complexity : θ(n^2)
Space Complexity: O(1)
3. Insertion Sort
Description
Insertion sort works similarly as we sort cards in our hand in a card
game.
We assume that the first card is already sorted then, we select an
unsorted card. If the unsorted card is greater than the card in hand, it
is placed on the right otherwise, to the left. In the same way, other
unsorted cards are taken and put at their right place.
A similar approach is used by insertion sort.
Insertion sort is a sorting algorithm that places an unsorted element
at its suitable place in each iteration.
Algorithm
for i ← 2 to n do
x ← T[i];
j ← i - 1;
while x < T[j] and j > 0 do
T[j+1] ← T[j];
j ← j – 1;
T[j+1] ← x;
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, j, a[30], x;
printf("\nEnter total: ");
scanf("%d",&n);
Page 5
TYIT 2 B 190410116114
for(i=1;i<n;i++)
{
x = a[i];
j = i-1;
while(x < a[j] && j >= 0)
{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = x;
}
Output
Time Complexity
Worst Case Time Complexity : O(n^2)
Best Case Time Complexity : Ω(n)
Average Time Complexity : θ(n^2)
Space Complexity: O(1)
Page 6
TYIT 2 B 190410116114
4. Counting Sort
Description
Counting sort is a sorting algorithm that sorts the elements of an array
by counting the number of occurrences of each unique element in the
array.
The count is stored in an auxiliary array and the sorting is done by
mapping the count as an index of the auxiliary array.
This sorting technique is effective when the difference between
different keys are not so big, otherwise, it can increase the space
complexity.
Algorithm
for i ← 1 to k do
c[i] ← 0
for j ← 1 to n do
c[A[j]] ← c[A[j]] + 1
for i ← 2 to k do
c[i] ← c[i] + c[i-1]
for j ← n downto 1 do
B[c[A[j]]] ← A[j]
c[A[j]] ← c[A[j]] – 1
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, j, a[30],k=0;
int c[k],b[n];
for(i=0;i<=k;i++)
Page 7
TYIT 2 B 190410116114
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;
}
Output:
Time Complexity
Worst Case Time Complexity : O(max+size)
Best Case Time Complexity : Ω(max+size)
Average Time Complexity : θ(max+size)
Space Complexity: O(max)
Page 8
TYIT 2 B 190410116114
PRACTICAL 2
Aim: Implementation and Time Analysis of Sorting Algorithms and
Binary Search Algorithm.
1. Merge Sort
Description
Merge Sort is a kind of Divide and Conquer algorithm in computer
programming.
It is one of the most popular sorting algorithms and a great way to
develop confidence in building recursive algorithms.
Using the Divide and Conquer technique, we divide a problem into sub
problems. When the solution to each sub problem is ready, we
'combine' the results from the sub problems to solve the main
problem.
The Merge Sort function repeatedly divides the array into two halves
until we reach a stage where we try to perform Merge Sort on a
subarray of size 1.
Algorithm
mergesort(A,lb,ub)
if(lb<ub)
mid=(lb+ub)/2;
mergesort(A,lb,mid)
mergesort(A,mid+1,ub)
merge(A, lb, mid ,ub)
merge(A,lb,mid,ub) (main back born of merge sort)
i ← lb;
j ← mid+1;
K ← ub
While i<=mid && j<=ub
if A[i]<=A[j]
B[k]=A[i] and i++, k++
else
B[k]=A[j] and j++, k++
If i>mid (when size of list in j is larger than i)
while(j<=ub)
B[k]=A[j] and j++,k++
Else (when size of list in i is larger than j)
while(i<=mid)
B[k]=A[i] and i++,k++
For k is lb to ub
A[k]=B[k]
Page 9
TYIT 2 B 190410116114
Program
#include <stdio.h>
#include <stdlib.h>
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
Page 10
TYIT 2 B 190410116114
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
Page 11
TYIT 2 B 190410116114
int main()
{
int arr[30];
int n;
printf("Enter total: ");
scanf("%d", &n);
printf("Enter array elements: ");
for(int i=0;i<n;i++)
scanf("%d", &arr[i]);
mergeSort(arr, 0, n - 1);
printf("\nSorted array is \n");
printArray(arr, n);
return 0;
}
Output
Time Complexity
Worst Case Time Complexity : O(n*log n)
Best Case Time Complexity : Ω (n*log n)
Average Time Complexity : θ (n*log n)
Space Complexity: O(n)
Page 12
TYIT 2 B 190410116114
2. Quick Sort
Description
Quick sort is an algorithm based on divide and conquer approach in
which the array is split into subarrays and these sub-arrays are
recursively called to sort the elements.
The array is divided into subparts taking pivot as the partitioning
point. The elements smaller than the pivot are placed to the left of the
pivot and the elements greater than the pivot are placed to the right.
The left and the right subparts are again partitioned using the by
selecting pivot elements for them. This can be achieved by recursively
passing the subparts into the algorithm.
This step does not play a significant role in quicksort. The array is
already sorted at the end of the conquer step.
Algorithm
quicksort(T[i,…,j])
{ Sorts subarray T[i,…,j] into ascending order }
if j – i is sufficiently small
then insert (T[i,…,j])
else
pivot(T[i,…,j],l)
quicksort(T[i,…,l - 1])
quicksort(T[l+1,…,j]
pivot(T[i,…,j]; var l)
p ← T[i]
k←i
l←j+1
repeat k ← k+1 until T[k] > p or k ≥ j
repeat l ← l-1 until T[l] ≤ p
while k < l do
Swap T[k] and T[l]
Repeat k ← k+1 until T[k] > p
Repeat l ← l-1 until T[l] ≤ p
Swap T[i] and T[l]
Program
#include<stdio.h>
#include<stdlib.h>
*a = *b;
*b = t;
}
Page 14
TYIT 2 B 190410116114
int main()
{
int arr[30];
int n;
printf("Enter total: ");
scanf("%d", &n);
printf("Enter array elements: ");
for(int i=0;i<n;i++)
scanf("%d", &arr[i]);
quickSort(arr, 0, n-1);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}
Output
Time Complexity
Worst Case Time Complexity : O(n^2)
Best Case Time Complexity : Ω (n*log n)
Average Time Complexity : θ (n*log n)
Space Complexity: O (n*log n)
3. Binary Search
Description
Search a sorted array by repeatedly dividing the search interval in half.
Begin with an interval covering the whole array.
Page 15
TYIT 2 B 190410116114
If the value of the search key is less than the item in the middle of the
interval, narrow the interval to the lower half.
Otherwise narrow it to the upper half. Repeatedly check until the value
is found or the interval is empty.
Algorithm
Function biniter(T[1,…,n], x)
if x > T[n] then return n+1
i ← 1;
j ← n;
while i < j do
k ← (i + j ) ÷ 2
if x ≤ T [k] then j ← k
else i ← k + 1
return i
Function binsearch(T[1,…,n], x)
if n = 0 or x > T[n] then return n + 1
else return binrec(T[1,…,n], x)
Function binrec(T[i,…,j], x)
if i = j then return i
k ← (i + j) ÷ 2
if x ≤ T[k] then
return binrec(T[i,…,k],x)
else return binrec(T[k + 1,…,j], x)
Program (Iterative)
#include<stdio.h>
void main()
{
int a[20],n,start,end,mid,i,x;
printf("Enter total: ");
scanf("%d",&n);
printf("\nEnter array elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter element to be found: ");
scanf("%d",&x);
start = a[0];
end = a[n-1];
while(start<end)
{
mid = (start+end)/2;
Page 16
TYIT 2 B 190410116114
if(a[mid]==x)
printf("%d is at %d position",x,mid);
if(a[mid]<x)
start = mid+1;
else
end = mid;
}
}
Output:
Program (Recursive)
#include<stdio.h>
Page 17
TYIT 2 B 190410116114
void main()
{
int a[20],n,i,x,res;
printf("Enter total: ");
scanf("%d",&n);
printf("\nEnter array elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter element to be found: ");
scanf("%d",&x);
res = BinarySearch(a,0,n-1,x);
printf("%d is present at %d position",x,res);
}
Output
Time Complexity
log2 (n)
Space Complexity: O(1)
Page 18
TY IT 2 B 190410116114
PRACTICAL 3
Aim: Implementation of Max Heap Sort algorithm.
1. Max Heap Sort
Description
A heap data structure is a binary tree with the following two properties.
It is a complete binary tree: Each level of the tree is completely filled, except
possibly the bottom level. At this level it is filled from left to right.
It satisfies the heap order property: the data item stored in each node is
greater than or equal to the data item stored in its children node.
Max-Heap − Where the value of the root node is greater than or equal to
either of its children.
Min-Heap − Where the value of the root node is less than or equal to either of
its children.
Algorithm
Heap_Sort(A[1,….,n])
Build-Max-Heap(A)
heap-size[A] heap-size[A] -1
Max-Heapify(A,i,n)
Max-Heapify(A,i,n)
l left(i)
rright(i)
then largest l
else largest i
then largest r
Page 19
TY IT 2 B 190410116114
if largest != i
Max-Heapify(A, largest, n)
Program
#include<stdio.h>
#include <stdlib.h>
*a = *b;
*b = temp;
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
largest = left;
Page 20
TY IT 2 B 190410116114
largest = right;
if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
heapify(arr, n, i);
swap(&arr[0], &arr[i]);
Page 21
TY IT 2 B 190410116114
heapify(arr, i, 0);
int main() {
int n,arr[30],i,j;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
heapSort(arr, n);
printf("\n");
Page 22
TY IT 2 B 190410116114
Output:
Time Complexity
Page 23
TY IT 2 B 190410116114
PRACTICAL 4
Aim: Implementation and Time analysis of factorial program using
iterative and recursive method.
1. Factorial Program
Description
Factorial of a non-negative integer, is multiplication of all integers
smaller than or equal to n. For example factorial of 6 is 6*5*4*3*2*1
which is 720.
The function is used, among other things, to find the number of way
“n” objects can be arranged.
Algorithm
Recursive Algorithm
Fact(n)
Begin
if n == 0 or 1 then
Return 1;
else
endif
End
Iterative Algorithm
int factorial( int N )
int product = 1;
product *= j;
return product;
Page 24
TY IT 2 B 190410116114
Program
Recursive
#include <stdio.h>
if (n == 0)
return 1;
int main()
int num;
scanf("%d", &num);
return 0;
Iterative
#include <stdio.h>
int res = 1, i;
res *= i;
Page 25
TY IT 2 B 190410116114
return res;
int main()
int num;
scanf("%d",&num);
printf(
return 0;
Output
Time Complexity
Time Complexity: O(n)
Page 26
TY IT 2 B 190410116114
PRACTICAL 5
Aim: Implementation of a knapsack problem using dynamic
programming.
A. Knapsack Problem
Description
Given weights and values of n items, put these items in a knapsack of
capacity W to get the maximum total value in the knapsack.
In other words, given two integer arrays val[0..n-1] and wt[0..n-1]
which represent values and weights associated with n items
respectively.
You cannot break an item, either pick the complete item or don’t pick
it (0-1 property).
Algorithm
We need to generate table V(1,…,n, 0,…,W)
2. If j < wi then
V[i][j] = V[i-1][j]
3. If j >= wi then
Program
#include <stdio.h>
if (n == 0 || W == 0)
return 0;
if (wt[n - 1] > W)
else
return max(
int main()
int wt[] = { 5, 4, 6, 3 };
int W = 10;
return 0;
Page 28
TY IT 2 B 190410116114
Output:
Time Complexity
Time Complexity: O(2^n)
Page 29
TY IT 2 B 190410116114
PRACTICAL 6
Aim: Implementation of Chain Matrix Multiplication Using Dynamic
Programming.
A. Chain Matrix Multiplication
Description
Given a sequence of matrices, find the most efficient way to multiply
these matrices together.
The problem is not actually to perform the multiplications, but merely to
decide in which order to perform the multiplications.
We have many options to multiply a chain of matrices because matrix
multiplication is associative.
In other words, no matter how we parenthesize the product, the result
will be the same. For example, if we had four matrices A, B, C, and D, we
would have:
(ABC)D = (AB)(CD) = A(BCD) = ....
Algorithm
Matrix Table M[i][j] stores the cost of multiplications.
To generate M[i][j] use the following algorithm:
If i = j then M[i][j] = 0
If j = i + 1 then M[i][j] = Pi-1 . Pi . Pi+1
If i < j then
M[i][j] = min(M[i][k] + M[k+1][j] + Pi-1 . Pk . Pj) with i <= k < j
Program
#include<stdio.h>
#include<limits.h>
Page 30
TY IT 2 B 190410116114
return m[1][n-1];
int main()
{
int n,i;
printf("Enter number of matrices\n");
scanf("%d",&n);
n++;
Page 31
TY IT 2 B 190410116114
int arr[n];
for(i=0;i<n;i++)
{
printf("Enter d%d :: ",i);
scanf("%d",&arr[i]);
}
return 0;
}
Page 32
TY IT 2 B 190410116114
Output:
Time Complexity
Page 33
TY IT 2 B 190410116114
PRACTICAL 7
Aim: Implementation of making a change problem using dynamic
programming.
A. Making a Change Problem
Description
There are infinite number of coins of n different values. These
values are given. Using these coins, you have to make change for
Rs. Sum.
Algorithm
We need to generate a table c[n][N], where n = number of denominations,
N = amount for which you need to make a change.
Program
#include <stdio.h>
int M[n+1];
Page 34
TY IT 2 B 190410116114
M[0] = 0;
int i, j;
M[j] = minimum;
return M[n];
int main() {
int d[20],n,k,i;
scanf("%d",&k);
scanf("%d",&n);
for(i=0;i<k;i++)
scanf("%d",&d[i]);
return 0;
Page 35
TY IT 2 B 190410116114
Output
Time Complexity
Time Complexity: O(n*k)
Page 36
TY IT 2 B 190410116114
PRACTICAL 8
Aim: Implementation of a knapsack problem using greedy algorithm.
Description
Algorithm
For i = 1 to n do
X[i] 0
weight 0
X[i] 1
else
Page 37
TY IT 2 B 190410116114
weight W
return x
Program
# include<stdio.h>
float x[20], tp = 0;
int i, j, u;
u = capacity;
x[i] = 0.0;
if (weight[i] > u)
break;
else
{
x[i] = 1.0;
tp = tp + profit[i];
Page 38
TY IT 2 B 190410116114
u = u - weight[i];
if (i < n)
x[i] = u / weight[i];
tp = tp + (x[i] * profit[i]);
printf("%f\t", x[i]);
int main()
int num, i, j;
Page 39
TY IT 2 B 190410116114
scanf("%d", &num);
scanf("%f", &capacity);
Page 40
TY IT 2 B 190410116114
temp = ratio[j];
ratio[j] = ratio[i];
ratio[i] = temp;
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
return(0);
}
Page 41
TY IT 2 B 190410116114
Output:
Time Complexity
Page 42
TY IT 2 B 190410116114
PRACTICAL 9
Aim: Implementation of Graph and Searching (DFS and BFS).
A. Graph and Searching (DFS and BFS)
Description
Depth-first search is an algorithm for traversing or searching tree or
graph data structures.
The algorithm starts at the root node (selecting some arbitrary node as
the root node in the case of a graph) and explores as far as possible along
each branch before backtracking.
So the basic idea is to start from the root or any arbitrary node and mark
the node and move to the adjacent unmarked node and continue this loop
until there is no unmarked adjacent node.
Then backtrack and check for other unmarked nodes and traverse them.
Finally print the nodes in the path.
BFS is a traversing algorithm where you should start traversing from a
selected node (source or starting node) and traverse the graph layerwise
thus exploring the neighbour nodes (nodes which are directly connected
to source node). You must then move towards the next-level neighbour
nodes.
Algorithm
Depth First Algorithm (DFS)
procedure dfsearch(G)
for each v Є N
do mark[v] ← not-visited
for each v Є N
do if mark[v] ≠ visited
then dfs(v)
procedure dfs(v)
{Node v has not previously been visited}
mark[v] ← visited
for each node w adjacent to v
do if mark[w] ≠ visited
then dfs(w)
Page 43
TY IT 2 B 190410116114
enqueue v into Q
while Q is not empty
do u ← first(Q)
dequeue u from Q
for each node w adjacent to u
do if mark[w] ≠ visited
then mark[w] ← visited
enqueue w into Q
procedure search(G)
for each v Є N
do mark[v] ← not visited
for each v Є N
do if mark[v] ≠ visited
then bfs(v)
Program
Depth First Search (DFS)
#include <stdio.h>
#include <stdlib.h>
struct node {
int vertex;
struct node* next;
};
struct Graph {
int numVertices;
int* visited;
Page 44
TY IT 2 B 190410116114
graph->visited[vertex] = 1;
printf("Visited %d \n", vertex);
if (graph->visited[connectedVertex] == 0) {
DFS(graph, connectedVertex);
}
temp = temp->next;
}
}
Page 45
TY IT 2 B 190410116114
int i;
for (i = 0; i < vertices; i++) {
graph->adjLists[i] = NULL;
graph->visited[i] = 0;
}
return graph;
}
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}
Page 46
TY IT 2 B 190410116114
int main() {
struct Graph* graph = createGraph(4);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 2, 3);
printGraph(graph);
DFS(graph, 2);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define SIZE 40
struct queue {
int items[SIZE];
int front;
int rear;
};
struct node {
Page 47
TY IT 2 B 190410116114
int vertex;
struct node* next;
};
struct Graph {
int numVertices;
struct node** adjLists;
int* visited;
};
graph->visited[startVertex] = 1;
enqueue(q, startVertex);
while (!isEmpty(q)) {
printQueue(q);
int currentVertex = dequeue(q);
printf("Visited %d\n", currentVertex);
while (temp) {
int adjVertex = temp->vertex;
if (graph->visited[adjVertex] == 0) {
graph->visited[adjVertex] = 1;
enqueue(q, adjVertex);
}
temp = temp->next;
}
Page 48
TY IT 2 B 190410116114
}
}
int i;
for (i = 0; i < vertices; i++) {
graph->adjLists[i] = NULL;
graph->visited[i] = 0;
}
return graph;
}
Page 49
TY IT 2 B 190410116114
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}
Page 50
TY IT 2 B 190410116114
if (isEmpty(q)) {
printf("Queue is empty");
} else {
printf("\nQueue contains \n");
for (i = q->front; i < q->rear + 1; i++) {
printf("%d ", q->items[i]);
}
}
}
int main() {
struct Graph* graph = createGraph(6);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
Page 51
TY IT 2 B 190410116114
addEdge(graph, 1, 2);
addEdge(graph, 1, 4);
addEdge(graph, 1, 3);
addEdge(graph, 2, 4);
addEdge(graph, 3, 4);
bfs(graph, 0);
return 0;
}
Output:
Depth First Search (DFS)
Page 52
TY IT 2 B 190410116114
Time Complexity
Time Complexity of DFS Algorithm: O(V + E), where V is the number of vertices
and E is the number of edges in the graph.
Time Complexity of BFS Algorithm: O(V + E), where V is the number of vertices
and E is the number of edges in the graph.
Page 53
TY IT 2 B 190410116114
PRACTICAL 10
Aim: Implement prim’s algorithm.
A. Prim’s Algorithm
Description
Prim's Algorithm is used to find the minimum spanning tree from a
graph.
Prim's algorithm finds the subset of edges that includes every vertex
of the graph such that the sum of the weights of the edges can be
minimized.
Prim's algorithm starts with the single node and explore all the
adjacent nodes with all the connecting edges at every step.
The edges with the minimal weights causing no cycles in the graph got
selected.
Algorithm
Prim's algorithm shows how we create two sets of vertices U and V-U.
U contains the list of vertices that have been visited and V-U the list of
vertices that haven't. One by one, we move vertices from set V-U to set
U by connecting the least weight edge.
T = ∅;
U = { 1 };
while (U ≠ V)
let (u, v) be the lowest cost edge such that u ∈ U and v ∈ V - U;
T = T ∪ {(u, v)}
U = U ∪ {v}
Program
#include<stdio.h>
#include<stdbool.h>
#include <string.h>
#define V 5
int G[V][V] = {
Page 54
TY IT 2 B 190410116114
int main() {
int no_edge;
int selected[V];
no_edge = 0;
selected[0] = true;
int x;
int y;
printf("Edge : Weight\n");
x = 0;
y = 0;
Page 55
TY IT 2 B 190410116114
if (selected[i]) {
min = G[i][j];
x = i;
y = j;
selected[y] = true;
no_edge++;
return 0;
Output
Page 56
TY IT 2 B 190410116114
Time Complexity
Time Complexity: O(n2)
Page 57
TY IT 2 B 190410116114
PRACTICAL 11
Aim: Implement kruskal’s algorithm.
A. Kruskal’s Algorithm
Description
Kruskal's algorithm initially places all the nodes of the original
graph isolated from each other, to form a forest of single node
trees, and then gradually merges these trees, combining at each
iteration any two of all the trees with some edge of the original
graph.
Then begins the process of unification: pick all edges from the first
to the last (in sorted order), and if the ends of the currently picked
edge belong to different subtrees, these subtrees are combined,
and the edge is added to the answer.
After iterating through all the edges, all the vertices will belong to
the same sub-tree, and we will get the answer.
Algorithm
Function Kruskal(G = (N, A))
repeat
ucomp ← find(u)
vcomp ← find(v)
T ← T U {e}
return T
Page 58
TY IT 2 B 190410116114
Program
#include <stdio.h>
#define MAX 30
int u, v, w;
} edge;
edge data[MAX];
int n;
} edge_list;
edge_list elist;
int Graph[MAX][MAX], n;
edge_list spanlist;
void kruskalAlgo();
void sort();
void print();
void kruskalAlgo() {
elist.n = 0;
Page 59
TY IT 2 B 190410116114
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();
belongs[i] = i;
spanlist.n = 0;
if (cno1 != cno2) {
spanlist.data[spanlist.n] = elist.data[i];
spanlist.n = spanlist.n + 1;
Page 60
TY IT 2 B 190410116114
return (belongs[vertexno]);
int i;
if (belongs[i] == c2)
belongs[i] = c1;
void sort() {
int i, j;
edge temp;
temp = elist.data[j];
elist.data[j + 1] = temp;
void print() {
int i, cost = 0;
Page 61
TY IT 2 B 190410116114
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;
Page 62
TY IT 2 B 190410116114
Graph[2][0] = 4;
Graph[2][1] = 2;
Graph[2][2] = 0;
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;
Graph[5][3] = 0;
Graph[5][4] = 3;
Page 63
TY IT 2 B 190410116114
Graph[5][5] = 0;
Graph[5][6] = 0;
kruskalAlgo();
print();
Output
Time Complexity
Time Complexity: O(ElogE)
Page 64
TY IT 2 B 190410116114
PRACTICAL 12
Aim: Implementation of LCS Problem.
A. LCS Problem
Description
A subsequence of a given sequence is just the given sequence with some
elements left out.
Algorithm
X and Y be two given sequences
X.label = X
Y.label = Y
LCS[0][] = 0
LCS[][0] = 0
If X[i] = Y[j]
Else
Program
Page 65
TY IT 2 B 190410116114
#include <stdio.h>
#include <string.h>
int i, j, m, n, LCS_table[20][20];
void lcsAlgo() {
m = strlen(S1);
n = strlen(S2);
LCS_table[i][0] = 0;
LCS_table[0][i] = 0;
} else {
Page 66
TY IT 2 B 190410116114
lcsAlgo[index] = '\0';
int i = m, j = n;
i--;
j--;
index--;
i--;
else
j--;
}
printf("S1 : %s \nS2 : %s \n", S1, S2);
int main() {
lcsAlgo();
printf("\n");
Output
Page 67
TY IT 2 B 190410116114
Time Complexity
Time Complexity: O(2n)
Page 68