0% found this document useful (0 votes)
43 views31 pages

Dsa Lab File

Complete DSA lab file

Uploaded by

Ashish Sharma
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)
43 views31 pages

Dsa Lab File

Complete DSA lab file

Uploaded by

Ashish Sharma
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/ 31

SHARDA SCHOOL OF ENGINEERING & TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE AND APPLICATIONS

PRACTICAL FILE

Subject : Data Structure using C

Code: BCP263

SUBMITTED BY SUBMITTED TO:

Student Name: Ashish Sharma Ms. Aparna Sharma

System Id: 2023000545 Assistant Professor

Course: BCA(AI/ML) sec-A CSA, SSET


INDEX

S.no. Name of Experiment Date Signature

1 WAP to insert element in an array and


display the elements
2 WAP to find maximum and minimum
element of an array.
3 WAP to merge two arrays in one array.
4 WAP for linear search
5 WAP to transpose a n*n matrices
6 WAP for addition & subtraction of two n*n
matrices
7 WAP for multiplication of two n*n matrices
8 WAP to implement stack using array
9 WAP to implement Queue using array
10 WAP to implement all linked list functions
11 WAP to implement binary search
12 WAP to implement bubble sort, selection
sort and insertion sort.
13 WAP to implement heap sort and quick sort.
14 WAP to implement traversal in binary tree.
15 WAP to implement insertion & deletion in
BST.
16 WAP to implement Dijkstra algorithm.
17 WAP to implement Prims algorithm
18 WAP to implement Kruskal’s algorithm

Q.1 - WAP to insert element in an array and display the elements.


Code – #include
<stdio.h> int
main() {
int arr[6] = {1, 2, 3, 4, 5};
int position = 2; // Position where new element is to be inserted
int element = 6; // Element to be inserted int n = 5; // Initial
size of array // Shifting elements to the right for (int i = n; i
>= position; i--) {
arr[i] = arr[i - 1];
}
// Inserting the element at the specified position
arr[position - 1] = element; // Printing the array
after insertion printf("Array after insertion of %d
is: ", element);
for (int i = 0; i <= n; i++)
{ printf("%d ", arr[i]);
}
return 0;
}

Output –
Q.2
Code –
- WAP to find maximum and minimum element of an array.

#include <stdio.h> int


main() {
int arr[] = {1, 2, 3, 4, 5}; int n
= sizeof(arr) / sizeof(arr[0]); int
max = arr[0];
int min = arr[0];
// Finding the maximum and minimum elements in the array
for (int i = 1; i < n; i++) { if (arr[i] > max) { max
= arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
// Printing the maximum and minimum elements
printf("The maximum element in the array is: %d\n", max);
printf("The minimum element in the array is: %d\n", min);
return 0;
}

Output –
Q.3 - WAP to merge two arrays in one array.

Code – #include <stdio.h> int main() {


int arr1[] = {1, 2, 3, 4, 5}; int arr2[]
= {6, 7, 8, 9, 10}; int n1 =
sizeof(arr1) / sizeof(arr1[0]); int n2 =
sizeof(arr2) / sizeof(arr2[0]); int
arr3[n1+n2];
// Copying elements from the first array
for (int i = 0; i < n1; i++) { arr3[i] =
arr1[i];
}
// Copying elements from the second array
for (i = 0; i < n2; i++)
{ arr3[i+n1] = arr2[i];
}
// Printing the merged array
printf("The merged array is: ");
for (i = 0; i < n1+n2; i++)
{ printf("%d ", arr3[i]);
}
return 0;
}

Output –
Q.4
Code –

- WAP for linear search.

#include <stdio.h> int main()


{ int arr[] = {1, 2, 3, 4, 5}; int
n = sizeof(arr) / sizeof(arr[0]);
int target = 3; // Element to be searched
int i;
// Performing linear search for (i = 0; i < n; i++) { if
(arr[i] == target) { printf("Element %d found at position
%d\n", target, i+1); return 0;
}
}
printf("Element %d not found in the array\n", target);
return 0;
}

Output –
Q.5 - WAP to transpose a n*n matrices.

Code –
#include <stdio.h> #define N 3
void transpose(int A[N][N])
{ for (int i = 0; i < N; i++)
for (int j = i+1; j < N; j++) {
int temp = A[i][j]; A[i][j]
= A[j][i];
A[j][i] = temp;}
} void printMatrix(int A[N][N])
{ for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++){
printf("%d ", A[i]
[j]); }printf("\n");} } int
main() {
int A[N][N] = { {1, 2, 3},{4, 5, 6},{7, 8, 9} };
printf("Original matrix:\n"); printMatrix(A);
transpose(A); printf("Transposed matrix:\n");
printMatrix(A); return 0;
}

Output –
Q.6 –
Code –
WAP for addition and subtraction of two numbers in n*n matrices.

#include <stdio.h> #define


N3
void add(int A[N][N], int B[N][N], int C[N][N]) {
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
C[i][j] = A[i][j] + B[i][j];
}
void subtract(int A[N][N], int B[N][N], int C[N][N]) {
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
C[i][j] = A[i][j] - B[i][j];
}
void printMatrix(int A[N][N]) {
for (int i = 0; i < N; i++)
{ for (int j = 0; j < N; j++)
printf("%d ", A[i][j]);
printf("\n");
}
} int main() { int A[N]
[N] = { {1, 2, 3},
{4, 5, 6},
{7, 8, 9} };
int B[N][N] = { {10, 11, 12},
{13, 14, 15},
{16, 17, 18} };
int C[N][N]; printf("Matrix A:\n");
printMatrix(A); printf("Matrix B:\n");
printMatrix(B); add(A, B, C);
printf("Sum of matrices A and B:\n");
printMatrix(C); subtract(A, B, C);
printf("Difference of matrices A and B:\n");
printMatrix(C); return 0;
}

Output –

Q.7- WAP for multiplication of two n*n


matrices.
Code – #include
<stdio.h> #define
N3
void multiply(int A[N][N], int B[N][N], int C[N][N]) {
for (int i = 0; i < N; i++) { for
(int j = 0; j < N; j++) { C[i][j]
= 0; for (int k = 0; k < N; k+
+) C[i][j] += A[i][k] * B[k]
[j];
}
}
}
void printMatrix(int A[N][N]) {
for (int i = 0; i < N; i++)
{ for (int j = 0; j < N; j++)
printf("%d ", A[i][j]);
printf("\n");
}
} int main()
{
int A[N][N] = { {1, 2, 3},
{4, 5, 6},
{7, 8, 9} };
int B[N][N] = { {10, 11, 12},
{13, 14, 15}, {16,
17, 18} }; int C[N][N]; printf("Matrix
A:\n"); printMatrix(A); printf("Matrix
B:\n"); printMatrix(B); multiply(A, B,
C);
printf("Product of matrices A and B:\n");
printMatrix(C); return 0;
}

Output –

WAP to implement stack using array.


Q.8 –
Code –

#include<stdio.h>
#define MAX 10 int
arr[MAX]; int
add(int top); int
pop(int top); void
display(int top); int
main(){ int i=1,
ch, top=0; while(i
== 1){
printf("Enter your choice 1 - add 2 - remove 3 - display 4 - exit\n");
scanf("%d",&ch); switch(ch){ case 1: top = add(top);
break; case 2: top = pop(top);
break; case
3: display(top);
break;
case 4: i=0;
break;
default: printf("Invalid input!\n");
}} return 0; } int add(int
top){ if(top == MAX)
{ printf("Stack Overflow!\
n");
return top;} int val;
printf("Enter value to add : \n");
scanf("%d",&val);
if(top == 0)
{ arr[top] = val;
} top++;
arr[top] = val;
return top; } int
pop(int top)
{ if(top == 0)
{
printf("Stack Underflow!\n");
return top;
} arr[top]
= 0; top--;
return top;
}
void display(int top)
{ printf("Stack is :\n");
for(int i=top; i>0; i--)
printf("%d\n",arr[i]);
}

Output –

Q.9 WAP to implement queue using array.

Code
#include<stdio.h> #define MAX 10 int arr[MAX], front=-1, rear=-1;
void add(); void pop(); void display(); int main(){ int i=1, ch;
while(i == 1){ printf("Enter your choice 1 - add 2 - remove 3 -
display 4 - exit :\t"); scanf("%d",&ch); switch(ch)
{ case 1: add(); break; case 2: pop(); break;
case 3: display();
break;
case 4: i=0;
break;
default: printf("Invalid input!\n");
}} return 0;} void
add(){ if(rear == MAX)
{ printf("Overflow!\
n");
return; } int val;
printf("Enter value to add : \t");
scanf("%d",&val);
if(front == -1){ //Empty queue
front = 0; rear = front;
arr[rear] = val; return; }
rear++; arr[rear] = val;
} void pop(){ if(front == rear){
printf("Stack underflow!\n");
return;
} front++;
}
void display(){ printf("Your
queue is :\n"); for(int i=front;
i<rear+1; i++){ printf("%d\
t",arr[i]);
}
}

Output –
Q.10 – WAP to implement linked list (all insertion and deletion operations).

Code –
#include<stdio.h>
#include<stdlib.h> struct
node{
int data; struct node* next; }*start, *temp, *p; void add(); void
del(); void display(); int main(){ int i=1, ch; while(i==1)
{ printf("Enter your choice 1 - add 2 - delete 3 - display 4 - exit:\
t"); scanf("%d",&ch); switch(ch){ case 1: add();
break; case 2: del(); break; case 3: display();
break;
case 4: i=0;
break;
default: printf("Invalid input!\n");
} }
return 0;
}
void addAtBeg(int val){ // Insert at begining
temp = (struct node*)malloc(sizeof(struct node));
temp->data = val; temp->next = NULL;
if(start == NULL){ //first element
start = temp;
return;
} p = start;
start = temp;
temp->next = p;
}
void addAtEnd(int val){ // Insert at last
temp = (struct node*)malloc(sizeof(struct node));
temp->data = val; temp->next = NULL;
if(start == NULL){ addAtBeg(val);
return;
}
struct node* currNode = start; while(currNode-
>next != NULL){
currNode = currNode->next;
}
currNode->next = temp;
}
void addAtPos(int val){ // Insert at position
int pos;
printf("Enter value of node to add after :\t");
scanf("%d",&pos); temp = (struct
node*)malloc(sizeof(struct node)); temp->data =
val; temp->next = NULL; struct node*
currNode = start; while(currNode->next !=
NULL){ if(currNode->data == pos)
{ temp->next = currNode->next;
currNode->next = temp; printf("Inserted!");
return;}
currNode = currNode-
>next; }printf("Not found!");
}
void add(){ // Add function
int val,ch; printf("Enter your choice 1 - add at begining 2 - add at last 3 - add at
position: \t"); scanf("%d",&ch); printf("Enter value to add :\t");
scanf("%d",&val);
switch(ch){ case 1:
addAtBeg(val); break;
case 2: addAtEnd(val);
break;
case 3: addAtPos(val);
break;
default: printf("Invalid input!\n");
}
}

void popBeg(){ // Remove from begining


p = start->next;
start = p;
}
void popLast(){ // Remove from last
struct node* currNode = start;
struct node* nextNode = currNode;
while(nextNode->next != NULL){
currNode = nextNode;
nextNode = nextNode->next;
}
currNode->next = NULL;
}
void remPos(){ // remove at position
int key; printf("Enter value of node at postion to
delete : \t"); scanf("%d",&key); struct node*
currNode = start; struct node* nextNode = start;
while(nextNode->next != NULL){ currNode =
nextNode; nextNode = nextNode->next;
if(nextNode->data == key){ currNode->next =
nextNode->next; return;
}
if(currNode->data == key){
popBeg();
return;
}}}
void del(){ // Remove node int ch; printf("Enter your choice 1 - remove at
begining 2 - remove at last 3 - remove at position:
\t");
scanf("%d",&ch);
switch(ch){ case
1: popBeg();
break; case
2: popLast();
break; case 3:
remPos();
break; default:
printf("Invalid input!\n"); }
}
void display(){ // Display
struct node* currNode = start;
while(currNode != NULL){ printf("%d\
t",currNode->data);
currNode = currNode->next;
}
}

Output –
Q.11 - WAP to implement binary search.

Code –
#include <stdio.h>
int binarySearch(int arr[], int l, int r, int x) {
while (l <= r) { int m = l + (r - l) / 2;
if (arr[m] == x) return m; if
(arr[m] < x) l = m + 1; else
r = m - 1;
} return -1; } int main(void) {
int arr[] = {2, 3, 4, 10, 40}; int n
= sizeof(arr) / sizeof(arr[0]); int x
= 10;
int result = binarySearch(arr, 0, n - 1, x);
(result == -1) ? printf("Element is not present in array\n")
: printf("Element is present at index %d\n", result); return 0;
}

Output –


Q.12 WAP to implement Bubble sort, selection sort, insertion sort.

Code
#include <stdio.h> void
swap(int *xp, int *yp) {
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void bubbleSort(int arr[], int n)
{ for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
printf("Bubble Sorted array: \n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
}
void selectionSort(int arr[], int n) {
int i, j, min_idx; for (i =
0; i < n-1; i++) { min_idx
= i; for (j = i+1; j < n; j+
+) if (arr[j] <
arr[min_idx]) min_idx
= j;
swap(&arr[min_idx], &arr[i]);
}

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;
}
printf("\nInsertion Sorted array: \n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
} int main()
{
int arr[] = {12, 11, 13, 5, 6}; int
n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
selectionSort(arr, n);
insertionSort(arr, n); return 0;
}

Output –

Q.13 WAP to implement heap sort and quick sort.




Code
#include <stdio.h> void
swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t; }
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);
} } void heapSort(int arr[], int n)
{ for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i); for (int i=n-1; i>=0;
i--) { swap(&arr[0], &arr[i]);
heapify(arr, i, 0);}} 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);}
} int main()
{
int arr[] = {10, 7, 8, 9, 1, 5}; int
n = sizeof(arr)/sizeof(arr[0]);
quickSort(arr, 0, n-1);
heapSort(arr, n); printf("Sorted
array: \n");
for (int i=0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}

Output –

Q.14 WAP to implement traversal in binary tree.

Code


#include <stdio.h>
#include <stdlib.h>
struct node { int
data; struct node*
left; struct node*
right;
};
struct node* newNode(int data) {
struct node* node = (struct node*)malloc(sizeof(struct node));
node->data = data; node->left = NULL; node->right =
NULL; return(node);
}
void printPreorder(struct node* node) {
if (node == NULL)
return; printf("%d ",
node->data);
printPreorder(node->left);
printPreorder(node->right);
}
void printInorder(struct node* node) {
if (node == NULL)
return; printInorder(node-
>left); printf("%d ", node->data);
printInorder(node->right);
}
void printPostorder(struct node* node) {
if (node == NULL)
return; printPostorder(node-
>left); printPostorder(node->right);
printf("%d ", node->data);
} int main()
{
struct node *root = newNode(1); root->left =
newNode(2); root->right = newNode(3); root-
>left->left = newNode(4); root->left->right =
newNode(5); printf("\nPreorder traversal of
binary tree is \n"); printPreorder(root);
printf("\nInorder traversal of binary tree is \n");
printInorder(root);
printf("\nPostorder traversal of binary tree is \n"); printPostorder(root);
return 0;
}
Output –

Q.15 WAP to implement insertion and deletion in BST.

Code
#include <stdio.h>
#include <stdlib.h>
struct node { int
key;
struct node *left, *right;
};


struct node *newNode(int item) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item; temp->left = temp->right = NULL;
return temp;
} struct node* insert(struct node* node, int key)
{ if (node == NULL) return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key) node->right
= insert(node->right, key); return node;
}
struct node * minValueNode(struct node* node) {
struct node* current = node; while (current
&& current->left != NULL)
current = current->left;
return current;}
struct node* deleteNode(struct node* root, int key) {
if (root == NULL) return root; if (key < root-
>key) root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
else {
if (root->left == NULL)
{ struct node *temp = root-
>right;
free(root);
return temp;
}
else if (root->right == NULL) {
struct node *temp = root->left;
free(root);
return temp;
}
struct node* temp = minValueNode(root->right); root-
>key = temp->key;
root->right = deleteNode(root->right, temp->key);
} return
root; }
void
inorder(str
uct node
*root)
{ if
(root !=
NULL) {
inorder(ro
ot->left);
printf("%d
\n", root-
>key);
inorder(ro
ot->right);
}
} int main() { struct node
*root = NULL; root =
insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
printf("Inorder traversal of the given tree \n");
inorder(root); printf("\nDelete 20\n"); root =
deleteNode(root, 20); printf("Inorder traversal of
the modified tree \n"); inorder(root); return 0;
}

Output –

Q.16 WAP to implement Dijkstra algorithm.



Code
#include <stdio.h>
#define INFINITY 9999 #define
MAX 10
void Dijkstra(int Graph[MAX][MAX], int n, int start);
void Dijkstra(int Graph[MAX][MAX], int n, int start) {
int cost[MAX][MAX], distance[MAX], pred[MAX];
int visited[MAX], count, mindistance, nextnode, i, j;
for (i = 0; i < n; i++) for (j = 0; j < n; j++) if
(Graph[i][j] == 0)
cost[i][j] = INFINITY;
else cost[i][j] =
Graph[i][j]; for (i = 0; i <
n; i++) { distance[i] =
cost[start][i]; pred[i] =
start; visited[i] = 0;
} distance[start] = 0;
visited[start] = 1;
count = 1; while
(count < n - 1) {
mindistance = INFINITY; for (i = 0; i < n;
i++) if (distance[i] < mindistance && !
visited[i]) { mindistance = distance[i];
nextnode = i;} visited[nextnode] = 1; for (i
= 0; i < n; i++)
if (!visited[i])
if (mindistance + cost[nextnode][i] < distance[i]) {
distance[i] = mindistance + cost[nextnode][i];
pred[i] = nextnode;
}
count++;
} for (i = 0; i < n;
i++) if (i != start) {
printf("\nDistance from source to %d: %d", i, distance[i]);
}
} int main()
{
int Graph[MAX][MAX], i, j, n, u;
n = 7;
Graph[0][0] = 0; Graph[0][1] = 0; Graph[0][2] = 1; Graph[0][3] = 2; Graph[0][4] = 0;
Graph[0][5] = 0; Graph[0][6] = 0; Graph[1][0] = 0; Graph[1][1] = 0; Graph[1][2] = 2;
Graph[1][3] = 0; Graph[1][4] = 0; Graph[1][5] = 3; Graph[1][6] = 0; Graph[2][0] = 1;
Graph[2][1] = 2; Graph[2][2] = 0; Graph[2][3] = 1; Graph[2][4] = 3; Graph[2][5] = 0;
Graph[2][6] = 0; Graph[3][0] = 2; Graph[3][1] = 0; Graph[3][2] = 1; Graph[3][3] = 0;
Graph[3][4] = 0; Graph[3][5] = 0; Graph[3][6] = 1; Graph[4][0] = 0; Graph[4][1] = 0;
Graph[4][2] = 3; Graph[4][3] = 0; Graph[4][4] = 0; Graph[4][5] = 2; Graph[4][6] = 0;
Graph[5][0] = 0; Graph[5][1] = 3; Graph[5][2] = 0; Graph[5][3] = 0; Graph[5][4] = 2;
Graph[5][5] = 0; Graph[5][6] = 1; Graph[6][0] = 0; Graph[6][1] = 0; Graph[6][2] = 0;
Graph[6][3] = 1; Graph[6][4] = 0; Graph[6][5] = 1; Graph[6][6] = 0;
Dijkstra(Graph, n, u);
return 0;
}

Output –
Q.17. WAP to implement Prim’s algorithm.

Code – #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; // number of edge
int selected[V];
memset(selected, false, sizeof(selected));
no_edge = 0; selected[0] = true; int
x; // row number int y; // col number
printf("Edge : Weight\n"); 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]) { // not in selected and there is an edge
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; }
Output –

Q.18 – WAP to implement kruskal’s algorithm.

Code –
#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++)
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]);
}
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;
}
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; 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;
Graph[5][5] = 0; Graph[5][6] = 0;
kruskalAlgo(); print();
}

Output –

You might also like