Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Yordanos moges

Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

AMITY UNIVERSITY DUBAI

LAB Record

Name :- Yordanos Moges


AUD :- Aud22880
Program :- B.tech (CSE)
Course :- CSE124 - Data Structure Using C
Date :- 5/12/24
Index

No. Date page


Program Name

1. 11-09-24 3
Linear search

2. 11-09-24 Binary search 4

1. 18-09-24 stack 5

2. 18-09-24 Factorial recursion 8

3. 18-09-24 Tower of hanoi 10

4. 25-09-24 queue 14

5. 09-10-24 Selection sort 15

6. 09-10-24 Insertion sort 16

7. 1-11-24 Bubble sort 17

8. 1-11-24 Quick sort 18

9. 1-11-24 Merge sort 20

10. 14-11-24 Infix to prefix 21

11. 14-11-24 Infix to postfix 24

12. 21-11-24 Single linked list 28

13. 21-11-24 Double linked list 31

14. 28-11-24 Binary search 35

15. 14-11-24 Kruskal algorithm 36


1. Write a program to search given element by using linear search -Lab 1
Date 11-09-24
#include <stdio.h>
int main()
{
int n, i, target, a[100];
int found = 0;
printf("NAME: Yordanos Moges AUD:22880\n");
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements of the array: ");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("Enter the target number: ");
scanf("%d", &target);
for (i = 0; i < n; i++) {
if (a[i] == target) {
printf("The target number %d is found at index %d.\n", target, i);
found = 1;
break;
}
}

if (found == 0) {
printf("Target number %d not found in the array.\n", target);
}

return 0;
}

2.Write a program to search given element by using binary search Lab 1


Date 11-09-24
#include <stdio.h>
int main()
{
int c, first, last, middle, n, s, array[100];
printf("NAME: Yordanos Moges AUD:22880\n");
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter value to find\n");
scanf("%d", &s);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last)
{
if (array[middle] < s) {
first = middle + 1;
}
else if (array[middle] == s) {
printf("%d found at location %d.\n", s, middle+1);
break;}
else {
last = middle - 1;}
middle = (first + last)/2;}
if (first > last) {
printf("%d isn't present in the list.\n", s);}
return 0;
}
3. Write a program to calculate the factorial of a number using a recursive
function. -Lab 2 Date 18-09-24

#include <stdio.h>

int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int num;
printf("NAME yordanos moges AUD 22880\n");
printf("Enter a number");
scanf("%d",&num);
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
4. Write a program to implement Stack using array. Implement Push, Pop and
display operations. -Lab 2 Date 18-09-24

#include <stdio.h>

int stack[100];
int top = -1;

void push(int value) {


if (top == 99) {
printf("Error: Stack overflow!\n");
return;
}
stack[++top] = value;
}

int pop() {
if (top == -1) {
printf("Error: Stack underflow!\n");
return -1;
}
return stack[top--];
}

int main() {
int choice, value;

while (1) {
printf("NAME yordanos moges AUD 22880\n");
printf("1. Push\n2. Pop\n3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(value);
break;

case 2:
value = pop();
if (value != -1) {
printf("Popped value: %d\n", value);
}
break;

case 3:
return 0;

default:
printf("Invalid choice!\n");
}
}
return 0;
}

5. Write a program to solve the Tower of Hanoi problem using a recursive


function. The program should use a main rod, an auxiliary rod, and a final rod to
move the disks. -Lab 2 Date 18-09-24
#include <stdio.h>

void towerOfHanoi(int n, char source, char auxiliary, char target) {


if (n == 1) {
printf("Move disk 1 from %c to %c\n", source, target);
return;
}
towerOfHanoi(n - 1, source, target, auxiliary);
printf("Move disk %d from %c to %c\n", n, source, target);
towerOfHanoi(n - 1, auxiliary, source, target);
}

int main() {
int num_disks;
printf("NAME yordanos moges Aud 22880\n");
printf("Enter the number of disks: ");
scanf("%d", &num_disks);
towerOfHanoi(num_disks, 'A', 'B', 'C');
return 0;
}

6. Write a program to implement Queue using array, which shows insertion and
deletion operations.-Lab 3 Date 25-09-24

#include<stdio.h>
int main() {
int n, front = -1, rear = -1;
printf("NAME yordanos moges Aud 22880\n");
printf("Enter the number of elements: ");
scanf("%d", &n);
int a[n];

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


if (i == 0) {
front = 0;
}
rear = i;
printf("Enter element %d: ", i + 1);
scanf("%d", &a[rear]);
}

printf("\nEnd of entry\n");
for (int i = 0; i < n; i++) {
printf("\n%d", a[i]);
}

return 0;
}

7. Write a program to sort the given array using insertion sort -Lab 4 Date 09-10-24

#include <stdio.h>
int main() {
int size;
int array[100];
printf("NAME yordanos moges Aud 22880\n");
printf("Enter the number of elements: ");
scanf("%d", &size);
printf("Enter %d elements: ", size);
for (int i = 0; i < size; i++) {
scanf("%d", &array[i]);
}
for (int i = 1; i < size; i++) {
int key = array[i];
int j = i - 1;
while (j >= 0 && array[j] > key) {
array[j + 1] = array[j];
j = j - 1;
}
array[j + 1] = key;
}
printf("Sorted array: \n");
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");

return 0;
}

8. Write a program to sort the given array using selection sort -Lab 4 Date 09-10-24

#include <stdio.h>

int main() {
int size;
int array[100];
printf("NAME yordanos moges Aud 22880\n");
printf("Enter the number of elements: ");
scanf("%d", &size);
printf("Enter %d elements: ", size);
for (int i = 0; i < size; i++) {
scanf("%d", &array[i]);
}
for (int i = 0; i < size - 1; i++) {
int min_index = i;
for (int j = i + 1; j < size; j++) {
if (array[j] < array[min_index]) {
min_index = j;
}
}
int temp = array[min_index];
array[min_index] = array[i];
array[i] = temp;
}
printf("Sorted array: \n");
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");
return 0;
}

9. Write a program to sort the given array using bubble sort -Lab 5 Date 1-11-24

#include <stdio.h>

int main() {
int array[100];
int size;
printf("NAME yordanos moges Aud 22880\n");
printf("Enter the number of elements: ");
scanf("%d", &size);

printf("Enter %d elements: ", size);


for (int i = 0; i < size; i++) {
scanf("%d", &array[i]);
}
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
printf("Sorted array: \n");
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");
return 0;
}

10. Write a program to sort the given array using QuickSort -Lab 5 Date 1-11-24

#include <stdio.h>
int main() {
int size;
int array[100];
printf("NAME yordanos moges Aud 22880\n");
printf("Enter the number of elements: ");
scanf("%d", &size);
printf("Enter %d elements: ", size);
for (int i = 0; i < size; i++) {
scanf("%d", &array[i]);
}
int low = 0;
int high = size - 1;
if (size > 1) {
int stack[size];
int top = -1;
stack[++top] = low;
stack[++top] = high;
while (top >= 0) {
high = stack[top--];
low = stack[top--];
int pivot = array[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (array[j] < pivot) {
i++;
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
int temp = array[i + 1];
array[i + 1] = array[high];
array[high] = temp;
int pi = i + 1;
if (pi - 1 > low) {
stack[++top] = low;
stack[++top] = pi - 1;
}
if (pi + 1 < high) {
stack[++top] = pi + 1;
stack[++top] = high;
}
}
}
printf("Sorted array: \n");
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");

return 0;
}

11. Write a program to sort the given array using MergeSort -Lab 5 Date 1-11-24

#include <stdio.h>
int main() {
int size;
printf("NAME yordanos moges Aud 22880\n");
printf("Enter the number of elements: ");
scanf("%d", &size);
int array[size];
printf("Enter %d elements: ", size);
for (int i = 0; i < size; i++) {
scanf("%d", &array[i]);
}

for (int mid = 1; mid < size; mid *= 2) {


for (int left = 0; left < size; left += 2 * mid) {
int right = left + 2 * mid - 1;
int mid_index = left + mid - 1;
if (right >= size) right = size - 1;
if (mid_index >= size) mid_index = size - 1;

int n1 = mid_index - left + 1;


int n2 = right - mid_index;
int L[n1], R[n2];

for (int i = 0; i < n1; i++) L[i] = array[left + i];


for (int j = 0; j < n2; j++) R[j] = array[mid_index + 1 + j];

int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
array[k] = L[i];
i++;
} else {
array[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
array[k] = L[i];
i++;
k++;
}
while (j < n2) {
array[k] = R[j];
j++;
k++;
}
}
}
printf("Sorted array: \n");
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");
return 0;
}

12. Write a program to change infix to postfix - Lab 6 Date 14-11-24

#include <stdio.h>
#define MAX 100
char stack[MAX];
int top = -1;
void push(char item) {
if (top >= MAX - 1) {
printf("Stack overflow.\n");
return;
}
stack[++top] = item;
}
char pop() {
if (top == -1) {
printf("Stack underflow.\n");
return -1;
}
return stack[top--];
}
int precedence(char op)
{
if (op == '+' || op == '-') return 1;
if (op == '*' || op == '/') return 2;
if (op == '^') return 3;
return 0;
}
int isOperator(char ch)
{
return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^';
}
int isOperand(char ch)
{
return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9');
}
void infixToPostfix(char* infix)
{
char postfix[MAX];
int i = 0, j = 0;

while (infix[i] != '\0') {


char ch = infix[i];

if (isOperand(ch)) {
postfix[j++] = ch;
}
else if (ch == '(') {
push(ch);
}
else if (ch == ')') {
while (top != -1 && stack[top] != '(') {
postfix[j++] = pop();
}
pop();
}
else if (isOperator(ch)) {
while (top != -1 && precedence(stack[top]) >= precedence(ch)) {
postfix[j++] = pop();
}
push(ch);
}
i++;
}
while (top != -1)
{
postfix[j++] = pop();
}

postfix[j] = '\0';
printf("Postfix Expression: %s\n", postfix);
}

int main()
{
char infix[MAX];
printf("Enter an infix expression: ");
scanf("%s", infix);
infixToPostfix(infix);
return 0;
}

#include <stdio.h>
#define MAX 100
char stack[MAX];
int top = -1;
void push(char item) {
if (top >= MAX - 1) {
printf("Stack overflow.\n");
return;
}
stack[++top] = item;
}
char pop() {
if (top == -1) {
printf("Stack underflow.\n");
return -1;
}
return stack[top--];
}
int precedence(char op)
{
if (op == '+' || op == '-') return 1;
if (op == '*' || op == '/') return 2;
if (op == '^') return 3;
return 0;
}
int isOperator(char ch)
{
return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^';
}
int isOperand(char ch)
{
return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9');
}
void infixToPostfix(char* infix)
{
char postfix[MAX];
int i = 0, j = 0;

while (infix[i] != '\0') {


char ch = infix[i];

if (isOperand(ch)) {
postfix[j++] = ch;
}
else if (ch == '(') {
push(ch);
}
else if (ch == ')') {
while (top != -1 && stack[top] != '(') {
postfix[j++] = pop();
}
pop();
}
else if (isOperator(ch)) {
while (top != -1 && precedence(stack[top]) >= precedence(ch)) {
postfix[j++] = pop();
}
push(ch);
}
i++;
}
while (top != -1)
{
postfix[j++] = pop();
}

postfix[j] = '\0';
printf("Postfix Expression: %s\n", postfix);
}

int main()
{
char infix[MAX];
printf("NAME: Yordanos Moges AUD22880\n");
printf("Enter an infix expression: ");
scanf("%s", infix);
infixToPostfix(infix);
return 0;
}

13. Write a program to change infix to prefix - Lab 6 Date 14-11-24

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 100

char stack[MAX];
int top = -1;

void push(char item) {


if (top >= MAX - 1) {
printf("Stack overflow.\n");
return;
}
stack[++top] = item;
}

char pop() {
if (top == -1) {
printf("Stack underflow.\n");
return -1;
}
return stack[top--];
}

int precedence(char op) {


if (op == '+' || op == '-') return 1;
if (op == '*' || op == '/') return 2;
if (op == '^') return 3;
return 0;
}

int isOperator(char ch) {


return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^';
}

int isOperand(char ch) {


return isalnum(ch);
}

void reverseString(char* str) {


int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}
void infixToPrefix(char* infix) {
char postfix[MAX];
char reversedInfix[MAX];
int i = 0, j = 0;

strcpy(reversedInfix, infix);
reverseString(reversedInfix);

for (i = 0; reversedInfix[i] != '\0'; i++) {


if (reversedInfix[i] == '(') reversedInfix[i] = ')';
else if (reversedInfix[i] == ')') reversedInfix[i] = '(';
}

i = 0;
while (reversedInfix[i] != '\0') {
char ch = reversedInfix[i];
if (isOperand(ch)) {
postfix[j++] = ch;
} else if (ch == '(') {
push(ch);
} else if (ch == ')') {
while (top != -1 && stack[top] != '(') {
postfix[j++] = pop();
}
pop();
} else if (isOperator(ch)) {
while (top != -1 && precedence(stack[top]) >= precedence(ch)) {
postfix[j++] = pop();
}
push(ch);
}
i++;
}

while (top != -1) {


postfix[j++] = pop();
}

postfix[j] = '\0';

reverseString(postfix);

printf("Prefix Expression: %s\n", postfix);


}

int main() {
char infix[MAX];
printf("NAME: Yordanos Moges AUD22880\n");
printf("Enter an infix expression: ");
scanf("%s", infix);
infixToPrefix(infix);
return 0;
}

14. Write a program of link list - Lab 7 Date 21-11-24

#include <stdio.h>
#include <stdlib.h>
void create();
void insert();
void deleteNode();
void display();

struct node {
int data;
struct node *next;
};

struct node *first = NULL, *last = NULL, *cur, *prev, *next;

void create() {
cur = (struct node *)malloc(sizeof(struct node));
if (cur == NULL) {
printf("Memory allocation failed\n");
return;
}
printf("Enter the data: ");
scanf("%d", &cur->data);
cur->next = NULL;

if (first == NULL) {
first = cur;
last = cur;
} else {
last->next = cur;
last = cur;
}
}
void insert() {
int pos, i = 1;
cur = (struct node *)malloc(sizeof(struct node));
if (cur == NULL) {
printf("Memory allocation failed\n");
return;
}
printf("Enter the data: ");
scanf("%d", &cur->data);
printf("Enter the position: ");
scanf("%d", &pos);

if (pos == 1) {
cur->next = first;
first = cur;
} else {
prev = first;
while (i < pos - 1 && prev != NULL) {
prev = prev->next;
i++;
}
if (prev != NULL) {
cur->next = prev->next;
prev->next = cur;
} else {
printf("Invalid position\n");
free(cur);
}
}
}
void deleteNode() {
int pos, i = 1;
printf("Enter the position to delete: ");
scanf("%d", &pos);

if (pos == 1) {
cur = first;
if (first != NULL) {
first = first->next;
free(cur);
} else {
printf("List is empty\n");
}
} else {
cur = first;
while (i < pos && cur != NULL) {
prev = cur;
cur = cur->next;
i++;
}
if (cur != NULL) {
prev->next = cur->next;
free(cur);
} else {
printf("Invalid position\n");
}
}
}
void display() {
if (first == NULL) {
printf("List is empty\n");
} else {
cur = first;
printf("The elements in the list are: ");
while (cur != NULL) {
printf("%d -> ", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
}
int main() {
int ch;
printf("NAME yordanos moges Aud 22880\n");
do {
printf("\nMenu:\n");
printf("1. Create\n");
printf("2. Insert\n");
printf("3. Delete\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);

switch (ch) {
case 1:
create();
break;
case 2:
insert();
break;
case 3:
deleteNode();
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (ch != 5);

return 0;
}

15. Write a program for double linked list - Lab 7 Date 21-11-24
#include <stdio.h>
#include <stdlib.h>
void create();
void insert();
void deleteNode();
void display();

struct node {
int data;
struct node *prev;
struct node *next;
};

struct node *first = NULL, *last = NULL, *cur, *temp;


void create() {
cur = (struct node *)malloc(sizeof(struct node));
if (cur == NULL) {
printf("Memory allocation failed\n");
return;
}
printf("Enter the data: ");
scanf("%d", &cur->data);
cur->prev = NULL;
cur->next = NULL;

if (first == NULL) {
first = cur;
last = cur;
} else {
last->next = cur;
cur->prev = last;
last = cur;
}
}
void insert() {
int pos, i = 1;
cur = (struct node *)malloc(sizeof(struct node));
if (cur == NULL) {
printf("Memory allocation failed\n");
return;
}
printf("Enter the data: ");
scanf("%d", &cur->data);
printf("Enter the position: ");
scanf("%d", &pos);

if (pos == 1) {
cur->next = first;
cur->prev = NULL;
if (first != NULL) {
first->prev = cur;
}
first = cur;
if (last == NULL) {
last = cur;
}
} else {
temp = first;
while (i < pos - 1 && temp != NULL) {
temp = temp->next;
i++;
}
if (temp != NULL) {
cur->next = temp->next;
cur->prev = temp;
if (temp->next != NULL) {
temp->next->prev = cur;
}
temp->next = cur;
if (cur->next == NULL) {
last = cur;
}
} else {
printf("Invalid position\n");
free(cur);
}
}
}
void deleteNode() {
int pos, i = 1;
printf("Enter the position to delete: ");
scanf("%d", &pos);

if (first == NULL) {
printf("List is empty\n");
return;
}

if (pos == 1) {
cur = first;
first = first->next;
if (first != NULL) {
first->prev = NULL;
} else {
last = NULL;
}
free(cur);
} else {
cur = first;
while (i < pos && cur != NULL) {
cur = cur->next;
i++;
}
if (cur != NULL) {
if (cur->prev != NULL) {
cur->prev->next = cur->next;
}
if (cur->next != NULL) {
cur->next->prev = cur->prev;
}
if (cur == last) {
last = cur->prev;
}
free(cur);
} else {
printf("Invalid position\n");
}
}
}

void display() {
if (first == NULL) {
printf("List is empty\n");
} else {
cur = first;
printf("The elements in the list are: ");
while (cur != NULL) {
printf("%d <-> ", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
}
int main() {
int ch;
printf("NAME yordanos Moges Aud22880\n");
do {
printf("\nMenu:\n");
printf("1. Create\n");
printf("2. Insert\n");
printf("3. Delete\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);

switch (ch) {
case 1:
create();
break;
case 2:
insert();
break;
case 3:
deleteNode();
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (ch != 5);

return 0;
}
16. write a Program for binary tree lab 8 Date 21-11-24

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

struct Node {
int data;
struct Node *left;
struct Node *right;
};

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}

struct Node* insert(struct Node* root, int data) {


if (root == NULL) {
root = createNode(data);
} else if (data < root->data) {
root->left = insert(root->left, data);
} else {
root->right = insert(root->right, data);
}
return root;
}

void inOrder(struct Node* root) {


if (root != NULL) {
inOrder(root->left);
printf("%d ", root->data);
inOrder(root->right);
}
}

int main() {
struct Node* root = NULL;
int n, value;
printf("NAME yordanos Moges Aud22880\n");
printf("Enter the number of nodes: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter value for node %d: ", i + 1);
scanf("%d", &value);
root = insert(root, value);
}

printf("In-order Traversal: ");


inOrder(root);
printf("\n");

return 0;
}
17. write a Program for Kruskal's Algorithm lab 9 Date 28-11-24

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

struct Edge {
int src, dest, weight;
};

struct Graph {
int V, E;
struct Edge* edges;
};

struct Subset {
int parent;
int rank;
};

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


struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->V = V;
graph->E = E;
graph->edges = (struct Edge*)malloc(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);
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++;
}
}

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


struct Edge* edgeA = (struct Edge*)a;
struct Edge* edgeB = (struct Edge*)b;
return edgeA->weight - edgeB->weight;
}

void KruskalMST(struct Graph* graph) {


int V = graph->V;
struct Edge result[V];
int e = 0, i = 0;
qsort(graph->edges, graph->E, sizeof(graph->edges[0]), compareEdges);
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 nextEdge = graph->edges[i++];
int x = find(subsets, nextEdge.src);
int y = find(subsets, nextEdge.dest);
if (x != y) {
result[e++] = nextEdge;
Union(subsets, x, y);
}
}
printf("Edges in the Minimum Spanning Tree:\n");
int totalWeight = 0;
for (i = 0; i < e; i++) {
printf("%d -- %d == %d\n", result[i].src, result[i].dest, result[i].weight);
totalWeight += result[i].weight;
}
printf("Total weight of MST: %d\n", totalWeight);
free(subsets);
}

int main() {
int V, E;
printf("NAME yordanos moges AUD22880\n");
printf("Enter the number of vertices and edges: ");
scanf("%d %d", &V, &E);
struct Graph* graph = createGraph(V, E);
printf("Enter the edges (source, destination, weight):\n");
for (int i = 0; i < E; i++) {
scanf("%d %d %d", &graph->edges[i].src, &graph->edges[i].dest, &graph-
>edges[i].weight);
}
KruskalMST(graph);
free(graph->edges);
free(graph);
return 0;
}

You might also like