DS Lab Manual
DS Lab Manual
Name
Roll Number
Branch MCA
Section
DEPARTMENT OF MASTER OF COMPUTER APPLICATIONS
VISION OF INSTITUTION
To be one of the nation’s premier Institutions for Technical and Management Education and
a key contributor for Technological and Socio-economic Development of the Nation.
MISSION OF INSTITUTION
Requirements & actions are identified to achieve the vision
M To produce technically competent Engineers and Managers by maintaining high
1 academic standards, world class infrastructure and core instructions.
M To enhance innovative skills and multidisciplinary approach of students through well
2 experienced faculty and industry interactions.
To inculcate global perspective and attitude of students to face real world
M
challenges by developing leadership qualities, lifelong learning abilities and ethical
3
values.
VISION OF DEPARTMENT
To be recognized as a centre of excellence in imparting quality technical education to succeed in
the field of Computer Applications and to develop our students as professionals in upcoming
technologies with right knowledge, skills and attitude to meet the global needs/challenges of the
ever changing IT industry.
MISSION OF DEPARTMENT
To achieve professional excellence through learning practices in an environment which is congenial
for technical education and there by develop competitive professionals with proper leadership,
commitment and moral values.
PSO 1 Domain Specific Knowledge: Apply the techniques to develop solutions in the domains
of algorithms, computer programming, multimedia, web, data and networking.
PSO 2 Software Product Development: Apply the design and deployment principles to deliver
a quality application for various heterogeneous domains.
DATA STRUCTURES LAB
LIST OF TASKS
TASK-1
1. Write a Program to Implement the following Searching Algorithms:
a) Linear Search b) Binary Search
TASK-2
1. Implement the following using arrays:
A. Write a Program to Implement Stack Operations
B. Write a Program to convert a given infix expression into its Postfix using stack.
C. Write a Program to evaluate the Postfix Expression using stack
TASK-3
1. Write a Program to Implement Queue Operations using Arrays
2. Write a Program to Implement Circular Queue Operations using Arrays
TASK-4
1. Write a Program to implement the operations of Singly Linked List
2. Write a Program to implement the operations of Doubly Linked List
TASK-5
1. Write a Program to implement stack operations using linked list
2. Write a Program to implement the operations of Circular Singly Linked List
TASK-6
1. Write a Program to Sort the set of
elements:
a) Insertion Sortb) Quick Sort
TASK-7
1. Write a Program to Sort the set of elements:
a) Merge Sort b) Heap Sort
TASK-8
1. Write a Program to implement the following on trees
a) Insertion and deletion operations
b) Traversals
2. Write a Program to implement Binary Search Tree Operations.
TASK-9
1. Write a Program to implement the following Graph Traversal Algorithms:
a) Depth first traversal b) Breadth first traversal
TASK-10
1. Write a Program to implement the following Minimum Spanning Tree Algorithms:
a) Kruskal’s Algorithm b) Prim’s Algorithm
INDEX
PAGE
S.NO DATE NAME OF THE TASK SIGN.
NO.
PAGE
S.NO DATE NAME OF THE TASK SIGN.
NO.
TASK -1 Date:
Problem Statement:
Write a Source Code to implement Linear or sequential Search on the elements of a given array.
if(a[i] ==key)
{
flag=1; location=i;
}
}
if(flag==1)
{
else
{
Output:
enter how many elements to search(n<10) :5
enter the 5 element :
enter a[0] element : 5
enter a[1] element : 4
enter a[2] element : 3
enter a[3] element : 2
enter a[4] element : 1
enter key element : 3 enter 2 found
Executed Output:
Problem Statement:
Write a Source Code to implement Binary Search on the elements of a given array.
scanf("%d",&arr[i]);
}
first = middle+1;
}
else if (arr[middle]==search)
{
last = middle-1;
}
if (first>last)
{
getch(); return 0;
}
Output:
enter 10 elements (in ascending order) : 1 2 3 4 5 6 7 8 9 10
enter element to be search : 6
the number,6 found at position 6
Executed Output:
TASK - 2 Date:
Source Code Statement:
Write a Source Code to implement operations on Stack using arrays.
#include<stdio.h>
int stack[100],choice,n,top,x,i; void push(void);
void pop(void); void display(void); int main()
{
top=-1;
printf("\n Enter the size of STACK[MAX=100]:"); scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t ");
printf("\n\t 1.PUSH\n\t 2.POP\n\tDISPLAY\n\t 4.EXIT");
do
{
push(); break;
}
case 2:
{
pop(); break;
}
case 3:
{
display(); break;
}
case 4:
{
}
}
while(choice!=4); return 0;
}
void push()
{
if(top>=n-1)
{
else
{
void pop()
{
if(top<=-1)
{
else
{
void display()
{
if(top>=0)
{
Output:
Enter the size of STACK[MAX=100]:5
STACK OPERATIONS USING ARRAY
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter the Choice:3 The STACK is empty Enter the Choice:1
Enter a value to be pushed:4
Enter the Choice:2
The popped elements is 4
Enter the Choice:5
Please Enter a Valid Choice(1/2/3/4)
Enter the Choice:4
EXIT POINT
Executed Output:
Problem Statement:
Write a Source Code to convert a given infix expression into its Postfix using stack.
Source Code: // Infix to Postfix Conversion
#include<stdio.h>
#include<ctype.h>
char stack[100];
int top = -1;
void push(char x)
{
stack[++top] = x;
}
char pop()
{
if(top == -1)
return -1; else
return stack[top--];
}
int priority(char x)
{
if(x == '(') return 0;
if(x == '+' || x == '-') return 1;
if(x == '*' || x == '/') return 2;
return 0;
}
int main()
{
char exp[100]; char *e, x;
printf("Enter the expression : "); scanf("%s",exp);
printf("\n"); e = exp;
while(*e != '\0')
{
if(isalnum(*e)) printf("%c ",*e);
else if(*e == '(') push(*e);
else if(*e == ')')
{
while((x = pop()) != '(') printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(*e)) printf("%c ",pop());
push(*e);
}
e++;
}
while(top != -1)
{
printf("%c ",pop());
}
return 0;
}
Output:
Enter the expression: a+b-c a b + c -
Executed Output:
Problem Statement:
Write a Source Code to evaluate the Postfix Expression using stack
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define SIZE 40 int pop();
void push(int);
char postfix[SIZE];
int stack[SIZE], top = -1; int main()
{
int i, a, b, result, pEval; char ch;
for(i=0; i<SIZE; i++)
{
stack[i] = -1;
}
printf("\nEnter a postfix expression: "); scanf("%s",postfix);
for(i=0; postfix[i] != '\0'; i++)
{
ch = postfix[i];
if(isdigit(ch))
{
push(ch-'0');
}
b = pop();
a = pop();
switch(ch)
{
push(result);
}
pEval = pop();
printf("\nThe postfix evaluation is: %d\n",pEval);
return 0;
}
void push(int n)
{
stack[++top] = n;
}
else
{
printf("Stack is full!\n"); exit(-1);
}}
int pop()
{
int n;
if (top > -1)
{
4.Exit
Enter the Choice:1 Enter no 1:9
Enter the Choice:1 Enter no 2:8
Enter the Choice:1 Enter no 3:7
Enter the Choice:2 Deleted Element is 9 Enter the Choice:3 Queue Elements are: 8
7
Enter the Choice:4 EXIT POINT
Executed Output:
Problem Statement:
Write a Source Code to Implement Circular Queue Operations using Arrays.
// Adding an element
void enQueue(int element) { if (isFull())
printf("\n Queue is full!! \n"); else {
if (front == -1) front = 0; rear = (rear + 1) % SIZE; items[rear] = element;
printf("\n Inserted -> %d", element);
}}
int main() {
// Fails because front = -1 deQueue();
enQueue(1); enQueue(2); enQueue(3); enQueue(4); enQueue(5);
// Fails to enqueue because front == 0 && rear == SIZE - 1 enQueue(6);
display(); deQueue();
display();
enQueue(7); display();
// Fails to enqueue because front == rear + 1 enQueue(8);
return 0;
}
Output:
Queue is empty !!
Inserted -> 1
Inserted -> 2
Inserted -> 3
Inserted -> 4
Inserted -> 5 Queue is full!!
Front -> 0
Items -> 1 2 3 4 5
Rear -> 4
Rear -> 4
Inserted -> 7
Front -> 1
Items -> 2 3 4 5 7
Rear -> 0 Queue is full!!
Executed Output:
TASK - 4 Date:
Problem Statement:
Write a Source Code to implement operations on Singly linked list.
void insert();
void display();
void delete();
int count();
int main() {
int option = 0;
printf("Singly Linked List Example - All Operations\n"); while (option < 5) {
printf("\nOptions\n");
printf("1 : Insert into Linked List \n"); printf("2 : Delete from Linked List \n"); printf("3 : Display
Linked List\n"); printf("4 : Count Linked List\n"); printf("Others : Exit()\n"); printf("Enter your
option:"); scanf("%d", &option);
switch (option) { case 1:
insert(); break; case 2: delete(); break; case 3: display(); break; case 4: count(); break; default: break;
}
}
return 0;
}
void insert() {
printf("\nEnter Element for Insert Linked List : \n"); scanf("%d", &data);
temp_node = (DATA_NODE *) malloc(sizeof (DATA_NODE)); temp_node->value = data;
head_node = prev_node;
}
} else
printf("\nInvalid Position \n\n");
}
Output:
Singly Linked List Example - All Operations
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List Others : Exit()
Enter your option:1
Enter Element for Insert Linked List : 2
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List Others : Exit()
Enter your option:1
Enter Element for Insert Linked List : 3
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List Others : Exit()
Enter your option:1
Enter Element for Insert Linked List : 4
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List Others : Exit()
Enter your option:1
Enter Element for Insert Linked List : 5
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List Others : Exit()
Enter your option:2
No Of Items In Linked List : 4 Display Linked List :
Options
1 : Insert into Linked List
2 : Delete from Linked List
3 : Display Linked List
4 : Count Linked List Others : Exit()
Enter your option:5
Executed Output:
Problem Statement:
Write a Source Code to implement the operations of Double Liked list
Source Code: //
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int count=0;
struct node
{
int data;
struct node *next,*prev;
}*head,*last,*newn,*trav;
//----------------------------------------------------
void create_list()
{
struct node *temp;
int value;
temp=last;
newn=(struct node *)malloc(sizeof (struct node));
printf("\n enter value");
scanf("%d",&value);
newn->data=value;
if(last==NULL)
{
head=last=newn;
head->prev=NULL;
head->next=NULL;
count++;
}
else
{
newn->next=NULL;
newn->prev=last;
last->next=newn;
last=newn;
count++;
}
}
//----------------------------------------------------
void insert_at_begning(int value)
{
newn=(struct node *)malloc(sizeof (struct node));
newn->data=value;
if(head==NULL)
{
head=last=newn;
head->prev=NULL;
head->next=NULL;
count++;
}
else
{
newn->next=head;
head->prev=newn;
newn->prev=NULL;
head=newn;
count++;
}
}
//----------------------------------------------------------
void insert_at_end(int value)
{
struct node *temp;
temp=last;
newn=(struct node *)malloc(sizeof (struct node));
newn->data=value;
if(last==NULL)
{
head=last=newn;
head->prev=NULL;
head->next=NULL;
count++;
}
else
{
newn->next=NULL;
newn->prev=last;
last->next=newn;
last=newn;
count++;
}
}
//------------------------------------------------------
int insert_at_middle()
{
if(count>=2)
{
struct node *var2,*temp;
int loc,value;
printf("\nselect location where you want to insert the data");
scanf("%d",&loc);
printf("\nenter which value do u want to inserted");
scanf("%d",&value);
newn=(struct node *)malloc(sizeof (struct node));
newn->data=value;
temp=head;
while(temp->data!=loc)
{
temp=temp->next;
if(temp==NULL)
{
printf("\nSORRY...there is no %d element",loc);
return 0;
}
}
if(temp->next==NULL)
{
printf("\n%d is last node..please enter midddle node values ",loc) ;
return;
}
var2=temp->next;
temp->next=newn;
newn->next=var2;
newn->prev=temp;
var2->prev=newn;
count++;
}
else
{
printf("\nthe no of nodes must be >=2");
}
}
//----------------------------------------------------------------
int delete_from_middle()
{
if(count>2)
{
struct node *temp,*var;
int value;
temp=head;
printf("\nenter the data that you want to delete from the list shown above");
scanf("%d",&value);
while(temp!=NULL)
{
if(temp->data == value)
{
if(temp->next==NULL)//if(temp==head)
{
printf("\n\n sorry %d is last node ..please enter middle nodes only",value);
return 0;
}
else
{
if(temp==head)
{
printf("\n\n %d is first node..plz enter middle nodes",value);
return;
}
var->next=temp->next;
temp->next->prev=var;
free(temp);
count--;
return 0;
}
}
else
{
var=temp;
temp=temp->next;
}
}
if(temp==NULL)
printf("\n%d is not avilable.. enter only middle elements..",value);
else
printf("\ndata deleted from list is %d",value);
}
else
{
printf("\nthere no middle elemts..only %d elemts is avilable",count);
}
}
//------------------------------------------------------------------
int delete_from_front()
{
struct node *temp;
if(head==NULL)
{
printf("no elements for deletion in the list");
return 0;
}
else if(head->next==NULL)
{
printf("deleted element is :%d",head->data);
head=last=NULL;
}
else
{
temp=head->next;
printf("deleted element is :%d",head->data);
head->next=NULL;
temp->prev=NULL;
head=temp;
count--;
return 0;
}
}
//--------------------------------------------------------
int delete_from_end()
{
struct node *temp,*var;
temp=last;
if(last==NULL)
{
printf("no elemts in the list");
return 0;
}
else if(last->prev==NULL)
{
printf("data deleted from list is %d",last->data);
head=last=NULL;
//free(last);
count--;
return 0;
}
else{
printf("data deleted from list is %d",last->data);
var=last->prev;
last->prev->next=NULL;
last=var;
count--;
return 0;
}
}
//------------------------------------------------------
int display()
{
trav=last;//head;
if(trav==NULL)
{
printf("\nList is Empty");
return 0;
}
else
{
printf("\n\nElements in the List is %d:\n",count);
while(trav!=NULL)
{
printf("%d<--> ",trav->data);
trav=trav->prev;//next;
}
printf("\n");
}
}
//---------------------------------------------------------------
int main()
{
int ch=0;
char ch1;
// clrscr();
head=NULL;
last=NULL;
while(1)
{
Printf("\n Double Linked List Operations")
printf("\n1.Create Double Linked List");
printf("\n2.insertion at begning of linked list");
printf("\n3.insertion at the end of linked list");
printf("\n4.insertion at the middle where you want");
printf("\n5.deletion from the front of linked list");
printf("\n6.deletion from the end of linked list ");
printf("\n7.deletion of the middle data that you want");
printf("\n8.display");
printf("\n9.exit\n");
printf("\nenter the choice of operation to perform on linked list");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
do{
create_list();
display();
printf("do you want to create list ,y / n");
getchar();
scanf("%c",&ch1);
}while(ch1=='y');
break;
}
case 2:
{
int value;
printf("\nenter the value to be inserted");
scanf("%d",&value);
insert_at_begning(value);
display();
break;
}
case 3:
{
int value;
printf("\nenter value to be inserted");
scanf("%d",&value);
insert_at_end(value);
display();
break;
}
case 4:
{
insert_at_middle();
display();
break;
}
case 5:
{
delete_from_front();
display();
}break;
case 6:
{
delete_from_end();
display();
break;
}
case 7:
{
display();
delete_from_middle();
display();
break;
}
case 8:display();break;
case 9:
{
exit(0);
}
}
}
getch();
}
Executed Output:
TASK - 5 Date:
Problem Statement:
Write a Source Code to implement Stack operations using linked list
Source Code:
// C program to implement a stack using singly linked list
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
// Creating a stack
Stack stack;
initializeStack(&stack);
return 0;
}
OUTPUT:
Linked List
30->20->10->NULL
Poped element = 30
20->10->NULL
Poped element = 20
10->NULL
Executed Output:
Problem Statement:
Write a Source Code to implement operations of Circular Singly Linked List
Source Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int isEmpty() {
return tail == NULL;
}
void deleteFromBeginning() {
if (isEmpty()) {
printf("List is empty.\n");
return;
}
struct Node* temp = tail->next;
if (tail->next == tail) { // Only one node
tail = NULL;
} else {
tail->next = temp->next;
}
free(temp);
}
void deleteFromEnd() {
if (isEmpty()) {
printf("List is empty.\n");
return;
}
struct Node* current = tail->next;
if (tail->next == tail) { // Only one node
free(tail);
tail = NULL;
} else {
while (current->next != tail) {
current = current->next;
}
current->next = tail->next;
free(tail);
tail = current;
}
}
void traverse() {
if (isEmpty()) {
printf("List is empty.\n");
return;
}
struct Node* current = tail->next;
do {
printf("%d -> ", current->data);
current = current->next;
} while (current != tail->next);
printf("(head)\n");
}
int main() {
insertAtEnd(10);
insertAtEnd(20);
insertAtBeginning(5);
traverse(); // Output: 5 -> 10 -> 20 -> (head)
deleteFromBeginning();
traverse(); // Output: 10 -> 20 -> (head)
deleteFromEnd();
traverse(); // Output: 10 -> (head)
return 0;
}
OUTPUT:
Executed Output:
TASK - 6 Date:
Problem Statement:
Write a Source Code to sort elements using insertion sort.
else break;
}
if (flag) array[d+1] = t;
}
return 0;
Output:
Enter number of elements 5
Enter 5 integers
22
55
33
11
10
Sorted list in ascending order: 10
11
22
33
55
Executed Output:
Problem Statement:
Write a Source Code to sort elements using quick sort.
int main(){
int i, count, number[25];
printf("Enter the no of elements: "); scanf("%d",&count);
printf("Enter %d elements: ", count); for(i=0;i<count;i++)
scanf("%d",&number[i]); quicksort(number,0,count-1);
printf("Order of Sorted elements: "); for(i=0;i<count;i++)
printf(" %d",number[i]); return 0;
}
Output:
Enter the no of elements: 5
Enter 5 elements: 52 43 12 25 42
Order of Sorted elements: 12 25 42 43 52
Executed Output:
TASK - 7 Date:
Problem Statement:
Write a Source Code to sort the elements using merge sort.
Source Code: // to sort the elements using merge sort
#include<stdio.h>
void merge(int arr[],int min,int mid,int max)
{
if(arr[j]<=arr[m])
{
tmp[i]=arr[j]; j++;
}
else
{
tmp[i]=arr[m]; m++;
}
}
if(j>mid)
{
tmp[i]=arr[k]; i++;
}
}
else
{
tmp[i]=arr[k]; i++;
}
}
int main()
{
sortm(arr,0,size-1);
printf("\n Sorted elements after using merge sort:\n\n"); for(i=0; i<size; i++)
printf(" %d ",arr[i]); return 0;
}
Problem Statement:
Write a Source Code to implement operations on trees
while(1) {
parent = current;
struct node* search(int data) { struct node *current = root; printf("Visiting elements: ");
while(current->data != data) { if(current != NULL)
printf("%d ",current->data);
return current;
}
void pre_order_traversal(struct node* root) { if(root != NULL) {
printf("%d ",root->data); pre_order_traversal(root->leftChild); pre_order_traversal(root->rightChild);
}
}
void inorder_traversal(struct node* root) { if(root != NULL) {
inorder_traversal(root->leftChild); printf("%d ",root->data); inorder_traversal(root->rightChild);
}
}
void post_order_traversal(struct node* root) { if(root != NULL) {
post_order_traversal(root->leftChild); post_order_traversal(root->rightChild); printf("%d ", root-
>data);
}
}
int main() { int i;
int array[7] = { 17, 14, 25, 12, 19, 52, 41 };
Output:
Visiting elements: 17 25 52 41 [ x ] Element not found (31). Visiting elements: 17 14 [ x ] Element not
found (15).
Preorder traversal: 17 14 12 25 19 52 41
Inorder traversal: 12 14 17 19 25 41 52
Post order traversal: 12 14 19 41 52 25 17
Executed Output:
Problem Statement:
Write a Source Code to perform operations creation, insertion, deletion and traversing on a binary
search tree.
Source Code: // to perform operations creation, insertion, deletion and traversing on a BST
#include <stdio.h>
#include <stdlib.h>
// structure of a node struct node
{
int data;
struct node *left; struct node *right;
};
switch(userChoice)
{
case 1:
data = get_data(); insert(data); break;
case 2:
data = get_data();
root = delete(root, data); break;
case 3:
data = get_data();
if (search(data) == 1)
{
printf("\nData was found!\n");
}
else
{
printf("\nData does not found!\n");
}
break;
case 4:
result = largest_node(root); if (result != NULL)
{
printf("\nLargest Data: %d\n", result->data);
}
break;
case 5:
result = smallest_node(root); if (result != NULL)
{
printf("\nSmallest Data: %d\n", result->data);
}
break;
case 6: inorder(root); break;
case 7: postorder(root); break;
case 8: preorder(root); break;
case 9: printf("\n\nSource Code was terminated\n"); break;
default: printf("\n\tInvalid Choice\n"); break;
}
printf("\n \nDo you want to continue? "); fflush(stdin);
scanf(" %c", &userActive);
}
return 0;
}
// creates a new node
struct node *create_node(int data)
{
struct node *new_node = (struct node *)malloc(sizeof(struct node));
if (new_node == NULL)
{
printf("\nMemory for new node can't be allocated"); return NULL;
}
new_node->data = data; new_node->left = NULL; new_node->right = NULL;
return new_node;
}
// inserts the data in the BST void insert(int data)
{
struct node *new_node = create_node(data);
if (new_node != NULL)
{
// if the root is empty then make a new node as the root node if (root == NULL)
{
root = new_node;
printf("\n* node having data %d was inserted\n", data); return;
}
struct node *temp = root; struct node *prev = NULL;
// traverse through the BST to get the correct position for insertion while (temp != NULL)
{
prev = temp;
if (data > temp->data)
{
temp = temp->right;
}
else
{
temp = temp->left;
}
}
// found the last node where the new node should insert
if (data > prev->data)
{
prev->right = new_node;
}
else
{
prev->left = new_node;
}
printf("\n* node having data %d was inserted\n", data);
}
}
// deletes the given key node from the BST
struct node *delete (struct node *root, int key)
{
if (root == NULL)
{
return root;
}
if (key < root->data)
{
root->left = delete (root->left, key);
}
else if (key > root->data)
{
root->right = delete (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 = smallest_node(root->right); root->data = temp->data;
root->right = delete (root->right, temp->data);
}
return root;
}
// search the given key node in BST int search(int key)
{
struct node *temp = root;
1. Insert
2. Delete
3. Search
4. Get Larger Node Data
5. Get smaller Node data
-- Traversals --
6. Inorder
7. Post Order
8. Pre Oder
9. Exit
1. Insert
2. Delete
3. Search
4. Get Larger Node Data
5. Get smaller Node data
-- Traversals --
6. Inorder
7. Post Order
8. Pre Oder
9. Exit
Problem Statement:
Write a Source Code to implement Breadth First Search Graph Traversal Algorithm
//graph functions
//add vertex to the vertex list void addVertex(char label) {
struct Vertex* vertex = (struct Vertex*) malloc(sizeof(struct Vertex)); vertex->label = label;
vertex->visited = false; lstVertices[vertexCount++] = vertex;
}
//add edge to edge array
void addEdge(int start,int end) { adjMatrix[start][end] = 1;
adjMatrix[end][start] = 1;
}
return -1;
}
//queue is empty, search is complete, reset the visited flag for(i = 0;i<vertexCount;i++) {
lstVertices[i]->visited = false;
}
addEdge(0, 1); // S - A
addEdge(0, 2); // S - B
addEdge(0, 3); // S - C
addEdge(1, 4); // A - D
addEdge(2, 4); // B - D
addEdge(3, 4); // C - D
return 0;
}
Output:
Breadth First Search: S A B C D
Executed Output:
Problem Statement:
Write a Source Code to implement Depth First Search Graph Traversal Algorithm
Output:
The Adjacency List of the Graph is:
1 => 3, 2, 5,
2 => 4, 7, 1,
3 => 6, 1,
4 => 2,
5 => 1,
6 => 3,
7 => 2,
DFS traversal of the graph:
1 -> 3 -> 6 -> 2 -> 4 -> 7 -> 5 ->
Executed Output:
TASK - 10 Date:
Problem Statement:
Write a Source Code to implement the following Minimum Spanning Tree Algorithms:
Kruskal’s Algorithm
Source Code:
// C code to implement Kruskal's algorithm
#include <stdio.h>
#include <stdlib.h>
// Comparator function to use in sorting
int comparator(const void* p1, const void* p2)
{
const int(*x)[3] = p1;
const int(*y)[3] = p2;
return (*x)[2] - (*y)[2];
}
// Initialization of parent[] and rank[] arrays
void makeSet(int parent[], int rank[], int n)
{
for (int i = 0; i < n; i++) {
parent[i] = i;
rank[i] = 0;
}
}
// Function to find the parent of a node
int findParent(int parent[], int component)
{
if (parent[component] == component)
return component;
return parent[component]
= findParent(parent, parent[component]);
}
void unionSet(int u, int v, int parent[], int rank[], int n)
{
// Finding the parents
u = findParent(parent, u);
v = findParent(parent, v);
2 -- 3 == 4
0 -- 3 == 5
0 -- 1 == 10
Executed Output:
Problem Statement:
Write a Source Code to implement the following Minimum Spanning Tree Algorithms:
Prim’s Algorithm
Source Code:
// A C program for Prim's Minimum
// Spanning Tree (MST) algorithm. The program is
// for adjacency matrix representation of the graph
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
return min_index;
}
// Driver's code
int main()
{
int graph[V][V] = { { 0, 2, 0, 6, 0 },
{ 2, 0, 3, 8, 5 },
{ 0, 3, 0, 0, 7 },
{ 6, 8, 0, 0, 9 },
{ 0, 5, 7, 9, 0 } };
return 0;
}
Output:
Edge Weight
0-1 2
1-2 3
0-3 6
1-4 5
Executed Output: