DS&CA Lab Manual
DS&CA Lab Manual
DS&CA Lab Manual
Data structure
&
Algorithms
2 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Ex no:01
Array Implementation of Stack
Aim:
To write a program for the array implementation of Stack in C.
Algorithm:
1) Start the Program.
2) Type the codings in Turbo C.
3) Define a array stack of size max = 5
4) Display a menu listing stack operations.
5) Use if..else statement to check the conditions.
6) Compile the program filename.cpp.
7) Run the program.
8) Save the program with extension .cpp.
9) Stop the program.
3 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Source code:
#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\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
4 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
5 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Output:
Enter the size of STACK[MAX=100]:10
98
24
12
Press Next Choice
Enter the Choice:2
24
12
Press Next Choice
Enter the Choice:4
EXIT POINT
Result:
Thus the program has been successfully obtained and the output is verified.
6 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Ex no:02
ARRAY Implementation of Queues
Aim:
To write a program for the array implementation of Queues in C.
Algorithm:
1) Start the Program.
2) Type the codings in Turbo C.
3) Define a array stack of size max = 5
4) Display a menu listing queue operations.
5) Use if..else statement to check the conditions.
6) Compile the program filename.cpp.
7) Run the program.
8) Save the program with extension .cpp.
9) Stop the program.
7 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Source code:
#include<stdio.h>
#define n 5
int main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\nQueue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
8 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
return 0;
}
9 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Output:
Queue using Array
1.Insertion
2.Deletion
3.Display
4.Exit
Deleted Element is 10
Enter the Choice:3
Result:
Thus the program has been successfully obtained and the output is verified.
10 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Ex no:03
LINKED LIST Implementation of STACKS
Aim:
To write a program for the linked list implementation of stacks in C.
Algorithm:
1) Start the Program.
2) Type the codings in Turbo C.
3) Define a array stack of size max = 5
4) Display a menu listing stack operations.
5) Use if..else statement to check the conditions.
6) Compile the program filename.cpp.
7) Run the program.
8) Save the program with extension .cpp.
9) Stop the program.
11 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Source code:
#include <stdio.h>
#include <stdlib.h>
int count = 0;
// Push() operation on a stack
void push(int data) {
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
}
else
{
temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
}
count++;
printf("Node is Inserted\n\n");
}
int pop() {
top1 = top;
if (top1 == NULL)
{
printf("\nStack Underflow\n");
return -1;
}
else
top1 = top1->ptr;
int popped = top->info;
free(top);
top = top1;
count--;
return popped;
}
12 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
void display() {
// Display the elements of the stack
top1 = top;
if (top1 == NULL)
{
printf("\nStack Underflow\n");
return;
}
int main() {
int choice, value;
printf("\nImplementation of Stack using Linked List\n");
while (1) {
printf("\n1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("\nEnter your choice : ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter the value to insert: ");
scanf("%d", &value);
push(value);
break;
case 2:
printf("Popped element is :%d\n", pop());
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
}
}
}
13 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Output:
Push Operation:
Node is Inserted
1. Push
2. Pop
3. Display
4. Exit
Node is Inserted
1. Push
2. Pop
3. Display
4. Exit
Node is Inserted
1. Push
2. Pop
3. Display
4. Exit
14 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Pop Operation:
The stack is
56--->45--->12--->NULL
1. Push
2. Pop
3. Display
4. Exit
1. Push
2. Pop
3. Display
4. Exit
1. Push
2. Pop
3. Display
4. Exit
1. Push
2. Pop
3. Display
4. Exit
1. Push
2. Pop
3. Display
4. Exit
RESULT:
Thus the program has been successfully obtained and the output is verified.
15 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Ex no:04
Linked list Implementation of Queues
Aim:
To write a program for the implementation of Queue using Linked list in C.
Algorithm:
1) Start the Program.
2) Type the codings in Turbo C.
3) Define a array stack of size max = 5
4) Display a menu listing Queues operations.
5) Use if..else statement to check the conditions.
6) Compile the program filename.cpp.
7) Run the program.
8) Save the program with extension .cpp.
9) Stop the program.
16 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Source code:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;
int frontelement();
void enq(int data);
void deq();
void empty();
void display();
void create();
void queuesize();
int count = 0;
void main()
{
int no, ch, e;
printf("\n 1 - Enque");
printf("\n 2 - Deque");
printf("\n 3 - Front element");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Display");
printf("\n 7 - Queue size");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
enq(no);
break;
case 2:
deq();
break;
17 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
case 3:
e = frontelement();
if (e != 0)
printf("Front element : %d", e);
else
printf("\n No front element in Queue as queue is empty");
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
queuesize();
break;
default:
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}
18 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
else
{
temp=(struct node *)malloc(1*sizeof(struct node));
rear->ptr = temp;
temp->info = data;
temp->ptr = NULL;
rear = temp;
}
count++;
}
if (front1 == NULL)
{
printf("\n Error: Trying to display elements from empty queue");
return;
}
else
if (front1->ptr != NULL)
{
front1 = front1->ptr;
printf("\n Dequed value : %d", front->info);
free(front);
19 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
front = front1;
}
else
{
printf("\n Dequed value : %d", front->info);
free(front);
front = NULL;
rear = NULL;
}
count--;
}
20 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Output:
1 - Enque
2 - Deque
3 - Front element
4 - Empty
5 - Exit
6 - Display
7 - Queue size
Enter choice : 1
Enter data : 14
Enter choice : 1
Enter data : 85
Enter choice : 1
Enter data : 38
Enter choice : 3
Front element : 14
Enter choice : 6
14 85 38
Enter choice : 7
Queue size : 3
Enter choice : 2
Dequed value : 14
Enter choice : 6
85 38
Enter choice : 7
Queue size : 2
Enter choice : 4
Queue not empty
Result:
Thus the program has been successfully obtained and the output is verified.
21 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Ex no:05
Binary tree traversals
(inorder, preorder, postorder)
Aim:
To write a program for the binary tree traversals in C.
Algorithm:
1) Start the Program.
2) Type the codings in Turbo C.
3) Define a array stack of size max = 5
4) Display a menu listing stack operations.
5) Use if..else statement to check the conditions.
6) Compile the program filename.cpp.
7) Run the program.
8) Save the program with extension .cpp.
9) Stop the program.
22 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Source code:
#include <stdio.h>
#include <stdlib.h>
struct node {
int element;
struct node* left;
struct node* right;
};
return (Node);
}
23 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
int main()
{
struct node* root = createNode(36);
root->left = createNode(26);
root->right = createNode(46);
root->left->left = createNode(21);
root->left->right = createNode(31);
root->left->left->left = createNode(11);
root->left->left->right = createNode(24);
root->right->left = createNode(41);
root->right->right = createNode(56);
root->right->right->left = createNode(51);
root->right->right->right = createNode(66);
return 0;
}
24 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Output:
36 26 21 11 24 31 46 41 56 51 66
11 21 24 26 31 36 41 46 51 56 66
11 24 21 31 26 41 51 66 56 46 36
Result:
Thus the program has been successfully obtained and the output is verified.
25 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Ex no:06
IMPLEMENTATION of linear search and
binary search
Aim:
To write a program for the implementation of Linear search and Binary search in C.
Algorithm:
1) Start the Program.
2) Type the codings in Turbo C.
3) Read number of array elements n.
4) Define array elements Ai, i = 0,1,2,…n–1.
5) Declare if Ai = search then found = 1.
6) Use if..else statement to check the conditions.
7) Compile the program filename.cpp.
8) Run the program.
9) Save the program with extension .cpp.
10) Stop the program.
26 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Source code:
Linear search
/* Implementation of linear search */
#include<stdio.h>
int ILS ( int A [ ] , int n , int key )
{
int i ;
for ( i = 0 ; i < n ; i++ )
{
if ( key == A [ i ] )
{
return i ;
}
}
return - 1 ;
}
void main()
{
int A [ 30 ] ;
int key , n , i , x ;
printf ( " Enter size of array : " ) ;
scanf ( "%d" , &n ) ;
printf ( "Enter Array \n " ) ;
for ( i = 0 ; i < n ; i++ )
{
scanf ( "%d" , &A [ i ] ) ;
}
printf ( " Enter value to search : " ) ;
scanf("%d" , &key ) ;
x = ILS ( A , n , key ) ;
if ( x != -1 )
{
printf ( " Value present at index : %d " , x ) ;
}
else
{
printf ( " Value not found ! \n ") ;
}
}
27 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Output:
Enter Array
12
13
15
8
7
9
6
11
10
4
B)binary search
28 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
int key , l , h , n , i , x ;
printf ( " Enter size of array : " ) ;
scanf ( "%d" , &n ) ;
h=n-1;
l=0;
printf ( " Enter sorted array : \n " ) ;
for ( i = 0 ; i < n ; i++ )
{
scanf ( "%d" , &A [ i ] ) ;
}
printf ( " Enter value to search : " ) ;
scanf ( "%d" , &key ) ;
x = IBS ( A , l , h , key ) ;
if ( x != - 1 )
{
printf ( " Value present at index : %d " , x ) ;
}
else
{
printf ( " Value is not present \n " ) ;
}
}
Output:
Enter the size of an array: 10
Enter Sorted Array
12
13
15
18
20
22
26
30
40
44
Enter the value to search: 20
Value present at index: 4
Result:
Thus the program has been successfully obtained and the output is verified.
29 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Ex no:07
IMPLEMENTATION insertion sort, quick
sort and merge sort
Aim:
To write a program for the implementation of Insertion sort, Quick sort, Merge sort in C.
Algorithm:
1. Start the program.
2. Read number of array elements n.
3. Read array elements Ai.
4. Outer index i varies from second element to last element.
5. Inner index j is used to compare elements to left of outer index.
6. Divide the array into 3 sequences: elements < x, x, elements >x.
7. Merge the sorted sub-arrays on to a single sorted array.
8. Insert the element into the appropriate position.
9. Display the array elements after each pass.
10. Display the sorted array elements.
11. Stop the program.
30 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Source code:
Insertion sort
#include <stdio.h>
void insertionSort(int arr[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
int main() {
int arr[] = { 12, 11, 13, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
Output:
Insertion sort-
5 6 11 12 13
31 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
quick sort
#include <stdio.h>
// Function to swap two elements
void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
int main() {
int arr[] = { 12, 17, 6, 25, 1, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
32 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Output:
1 5 6 12 17 25
merge sort
#include <stdio.h>
/* Function to merge the subarrays of a[] */
void merge(int a[], int beg, int mid, int end)
{
int i, j, k;
int n1 = mid - beg + 1;
int n2 = end - mid;
33 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
}
while (i<n1)
{
a[k] = LeftArray[i];
i++;
k++;
}
while (j<n2)
{
a[k] = RightArray[j];
j++;
k++;
}
}
int main()
{
int a[] = { 12, 31, 25, 8, 32, 17, 40, 42 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArray(a, n);
mergeSort(a, 0, n - 1);
printf("After sorting array elements are - \n");
printArray(a, n);
return 0;
}
34 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Output:
12 31 25 8 32 17 40 42
8 12 17 25 31 32 40 42
Result:
Thus the program has been successfully obtained and the output is verified.
35 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Ex no:08
Implementation of DFS &
bfs of graphs
Aim:
To write a program for the implementation of DFS & BFS in C.
Algorithm:
1. Start the Program.
2. Type the codings in Turbo C.
3. Read number of array elements n.
4. Define array elements Ai, i = 0,1,2,…n–1.
5. Use if..else statement to check the conditions.
6. Compile the program filename.cpp.
7. Run the program.
8. Save the program with extension .cpp.
9. Stop the program.
36 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Source code:
#include<stdio.h>
int q[20],top=-1,front=-1,rear=-1,a[20][20],vis[20],stack[20];
int delete();
void add(int item);
void bfs(int s,int n);
void dfs(int s,int n);
void push(int item);
int pop();
void main()
{
int n,i,s,ch,j;
char c,dummy;
printf("ENTER THE NUMBER VERTICES ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("ENTER 1 IF %d HAS A NODE WITH %d ELSE 0 ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("THE ADJACENCY MATRIX IS\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf(" %d",a[i][j]);
}
printf("\n");
}
do
{
for(i=1;i<=n;i++)
vis[i]=0;
printf("\nMENU");
printf("\n1.B.F.S");
printf("\n2.D.F.S");
printf("\nENTER YOUR CHOICE");
scanf("%d",&ch);
printf("ENTER THE SOURCE VERTEX :");
scanf("%d",&s);
37 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
switch(ch)
{
case 1:bfs(s,n);
break;
case 2:
dfs(s,n);
break;
}
printf("DO U WANT TO CONTINUE(Y/N) ? ");
scanf("%c",&dummy);
scanf("%c",&c);
}while((c=='y')||(c=='Y'));
}
38 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
else
{
if(rear==-1)
{
q[++rear]=item;
front++;
}
else
q[++rear]=item;
}
}
int delete()
{
int k;
if((front>rear)||(front==-1))
return(0);
else
{
k=q[front++];
return(k);
}
}
39 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
dfs(i,n);
}
void push(int item)
{
if(top==19)
printf("Stack overflow ");
else
stack[++top]=item;
}
int pop()
{
int k;
if(top==-1)
return(0);
else
{
k=stack[top--];
return(k);
}
}
40 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Output:
ENTER THE NUMBER VERTICES 3
MENU
1.B.F.S
2.D.F.S
MENU
1.B.F.S
2.D.F.S
Result:
Thus the program has been successfully obtained and the output is verified.
41 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Ex no:09
Finding all pairs of shortest path of a
graph
Aim:
To write a program for the implementation finding shortest path of a graph in C.
Algorithm:
1. Start the Program.
2. Type the codings in Turbo C.
3. Read number of array elements n.
4. Define array elements Ai, i = 0,1,2,…n–1.
5. Use if..else statement to check the conditions.
6. Compile the program filename.cpp.
7. Run the program.
8. Save the program with extension .cpp.
9. Stop the program.
42 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Source code:
/*To find the shortest path between two vertices in a graph
* using the Floyd-Warshall algorithm
*/
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n, i, j;
printf("Enter the number of vertices: ");
scanf("%d", &n);
int **graph = (int **)malloc((long unsigned) n * sizeof(int *));
for (i = 0; i < n; i++)
{
graph[i] = (int *)malloc((long unsigned) n * sizeof(int));
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (i == j)
graph[i][j] = 0;
else
graph[i][j] = 100;
}
}
printf("Enter the edges: \n");
for (i = 0; i < n; i++)
43 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
{
for (j = 0; j < n; j++)
{
printf("[%d][%d]: ", i, j);
scanf("%d", &graph[i][j]);
}
}
printf("The original graph is:\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", graph[i][j]);
}
printf("\n");
}
floydWarshall(graph, n);
printf("The shortest path matrix is:\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", graph[i][j]);
}
printf("\n");
}
return 0;
}
44 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Output:
Enter the number of vertices: 4
Result:
Thus the program has been successfully obtained and the output is verified.
45 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Ex no:10
Finding single source shortest path of a
graph
Aim:
To write a program for single source shortest path a graph in C.
Algorithm:
1. Start the Program.
2. Type the codings in Turbo C.
3. Read number of array elements n.
4. Define array elements Ai, i = 0,1,2,…n–1.
5. Use if..else statement to check the conditions.
6. Compile the program filename.cpp.
7. Run the program.
8. Save the program with extension .cpp.
9. Stop the program.
46 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
Source code:
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#define V 9
return min_index;
}
void printSolution(int dist[])
{
printf("Vertex \t\t Distance from Source\n");
for (int i = 0; i < V; i++)
printf("%d \t\t\t\t %d\n", i, dist[i]);
}
void dijkstra(int graph[V][V], int src)
{
int dist[V]; // The output array. dist[i] will hold the
bool sptSet[V]; // sptSet[i] will be true if vertex i is
for (int i = 0; i < V; i++)
dist[i] = INT_MAX, sptSet[i] = false;
dist[src] = 0;
for (int count = 0; count < V - 1; count++) {
int u = minDistance(dist, sptSet);
sptSet[u] = true;
for (int v = 0; v < V; v++)
if (!sptSet[v] && graph[u][v]
&& dist[u] != INT_MAX
&& dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
printSolution(dist);
}
int main()
{
int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
47 SACWC
DATA STRUCTURES& ALGORITHMS LAB FEBRUARY 2024
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
dijkstra(graph, 0);
return 0;
}
Output:
Vertex Distance from Source
0 0
1 4
2 12
3 19
4 21
5 11
6 9
7 8
8 14
RESULT:
Thus the program has been successfully obtained and the output is verified.
48 SACWC