Professional Documents
Culture Documents
AratrikaLab Record Download
AratrikaLab Record Download
Aim:
Write a program that use recursive functions to perform the Binary search searching operations for a Key value
Page No: 1
in a given list of integers.
Source Code:
recursive-binarysearch.c
ID: 2100331540014
2021-2025-CSE-DS-A-G1
Raj Kumar Goel Institute Of Technology
#include<stdio.h>
void binary_search(int[],int ,int,int);
int main()
{
int key,size,i;
Page No: 2
int list[25];
printf("Enter n: ");
scanf("%d",&size);
for(i=0;i<size;i++)
{
ID: 2100331540014
scanf("%d",&list[i]);
}
printf("Enter the key element: ");
scanf("%d",&key);
binary_search(list,0,size,key);
}
void binary_search(int list[],int lo,int hi,int key)
{
int mid;
if(lo>hi)
{
printf("The element is not found");
2021-2025-CSE-DS-A-G1
return;
}
mid=(lo+hi)/2;
if(list[mid]==key)
{
printf("The element is found at %d",mid+1);
}
else if(list[mid]>key)
{
binary_search(list,lo,mid-1,key);
User Output
Enter n:
3
Page No: 3
369
Enter the key element:
3
ID: 2100331540014
The element is found at 1
Test Case - 2
User Output
Enter n:
3
369
Enter the key element:
2
The element is not found
2021-2025-CSE-DS-A-G1
Raj Kumar Goel Institute Of Technology
S.No: 2 Exp. Name: Recursive Linearsearch Date: 2023-12-06
Aim:
Write a program that use recursive functions to perform the Linearsearch searching operations for a Key value in
Page No: 4
a given list of integers.
Source Code:
recursive-linearsearch.c
ID: 2100331540014
#include<stdio.h>
void main()
{
int n,a[50],key,i,flag;
printf("Enter n: ");
scanf("%d",&n);
for (i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to search: ");
scanf("%d",&key);
2021-2025-CSE-DS-A-G1
for (i=0;i<n;i++)
{
if(a[i]==key)
{
flag=1;
break;
}
}
if(flag==1)
{
User Output
Enter n:
3
369
Enter the element to search:
2
Element not found
Test Case - 2
User Output
Enter n:
3
Page No: 5
369
Enter the element to search:
6
ID: 2100331540014
Element found at pos 2
2021-2025-CSE-DS-A-G1
Raj Kumar Goel Institute Of Technology
Exp. Name: Write a program to sort a given array
S.No: 3 Date: 2023-12-06
using heap sort.
Aim:
Page No: 6
Write a program to sort a given array using heap sort technique.
Source Code:
heapSort.c
ID: 2100331540014
2021-2025-CSE-DS-A-G1
Raj Kumar Goel Institute Of Technology
#include<stdio.h>
void main()
{
int heap[10],num,i,j,c,rootele,tempvar;
printf("Enter no.of elements:");
Page No: 7
scanf("%d",&num);
printf("Enter the elements of an array: ");
for(i=0;i<num;i++)
scanf("%d",&heap[i]);
for (i=1;i<num;i++)
ID: 2100331540014
{
c=i;
do{
rootele=(c-1)/2;
if(heap[rootele]<heap[c])
{
tempvar=heap[rootele];
heap[rootele]=heap[c];
heap[c]=tempvar;
}
c=rootele;
}while(c !=0 );
2021-2025-CSE-DS-A-G1
}
for(j=num-1;j>=0;j--)
{
tempvar=heap[0];
heap[0]=heap[j];
heap[j]=tempvar;
rootele=0;
do
{
c=2*rootele+1;
Page No: 8
Sorted array is
1 3 4 5 6 8
ID: 2100331540014
Test Case - 2
User Output
Enter no.of elements:
6
Enter the elements of an array:
-6 -8 -9 -12 -6 0
Sorted array is
-12 -9 -8 -6 -6 0
2021-2025-CSE-DS-A-G1
Raj Kumar Goel Institute Of Technology
Exp. Name: Write a program to sort a given array
S.No: 4 Date: 2023-12-19
using merge sort.
Aim:
Page No: 9
Given a list of N array elements, apply merge sort on an array to arrange the elements in ascending order.
Constraints:
1 <= N <= 1000
ID: 2100331540014
−1000 <= a[i] <= 1000
Input Format:
The first line contains the integer N, the size of the array.
The next line contains N space-separated integers.
Output Format:
Print the sorted array as a row of space-separated integers.
Source Code:
mergeSort.c
2021-2025-CSE-DS-A-G1
Raj Kumar Goel Institute Of Technology
#include<stdio.h>
#include<stdlib.h>
void Merge(int arr[],int left,int mid,int right)
{
int i,j,k;
Page No: 10
int size1=mid-left+1;
int size2=right-mid;
int Left[size1],Right[size2];
for(i=0;i<size1;i++)
Left[i]=arr[left+i];
ID: 2100331540014
for(j=0;j<size2;j++)
Right[j]=arr[mid+1+j];
j=0;
i=0;
k=left;
while(i<size1 && j<size2)
{
if(Left[i]<=Right[j])
{
arr[k]=Left[i];
i++;
}
2021-2025-CSE-DS-A-G1
else
{
arr[k]=Right[j];
j++;
}
k++;
}
while(i<size1)
{
arr[k]=Left[i];
Page No: 11
}
Merge_Sort(arr, 0, size - 1);
printf("After sorting the elements are : ");
for(int i=0;i<size;i++)
{
ID: 2100331540014
printf("%d ",arr[i]);
}
printf("\n");
return 0;
}
2021-2025-CSE-DS-A-G1
User Output
Enter array size :
5
Enter 5 elements :
48627
After sorting the elements are : 2 4 6 7 8
User Output
Enter array size :
10
Enter 10 elements :
-82 -10 -9 -3 1 2 5 8 9 24
After sorting the elements are : -82 -10 -9 -3 1 2 5 8 9 24
Exp. Name: Write a program to sort a given array
S.No: 5 Date: 2023-12-19
using selection sort.
Aim:
Page No: 12
Given a list of N array elements, apply selection sort on an array to arrange the elements in ascending order.
Constraints:
1 <= N <= 1000
ID: 2100331540014
Input Format:
The first line contains the integer N, the size of the array.
The next line contains N space-separated integers.
Output Format:
Print the sorted array as a row of space-separated integers.
Source Code:
selectionSort.c
#include<stdio.h>
2021-2025-CSE-DS-A-G1
void selection(int arr[],int n)
{
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
arr[i]=arr[i]^arr[j];
arr[j]=arr[i]^arr[j];
User Output
Page No: 13
How many numbers u are going to enter?:
10
Enter 10 elements:
-12 9 1 8 2 -9 -3 5 24 -70
ID: 2100331540014
Sorted elements are: -70 -12 -9 -3 1 2 5 8 9 24
Test Case - 2
User Output
How many numbers u are going to enter?:
8
Enter 8 elements:
8 6 4 123 8 54 9 0
Sorted elements are: 0 4 6 8 8 9 54 123
2021-2025-CSE-DS-A-G1
Raj Kumar Goel Institute Of Technology
Exp. Name: Write a program to sort a given array
S.No: 6 Date: 2023-12-19
using insertion sort.
Aim:
Page No: 14
Given a list of N array elements, apply insertion sort on an array to arrange the elements in ascending order.
Constraints:
1 <= N <= 1000
ID: 2100331540014
Input Format:
The first line contains the integer N, the size of the array.
Enter each element in a new line.
Output Format:
Print the sorted array as a row of space-separated integers.
Source Code:
InsertionSort.c
#include<stdio.h>
2021-2025-CSE-DS-A-G1
void insertion(int arr[], int n)
{
for(int i=1; i<n; i++){
int key=arr[i];
int j=i-1;
while((j>=0) && (arr[j]>key))
{
arr[j + 1]=arr[j];
j--;
}
User Output
n:
5
Page No: 15
Enter the elements:
95143
Sorted array in ascending order:1 3 4 5 9
ID: 2100331540014
Test Case - 2
User Output
n:
7
Enter the elements:
2 9 6 -15 0 -56 -25
Sorted array in ascending order:-56 -25 -15 0 2 6 9
2021-2025-CSE-DS-A-G1
Raj Kumar Goel Institute Of Technology
Exp. Name: Write a program to sort a given array
S.No: 7 Date: 2023-12-19
using quick sort.
Aim:
Page No: 16
Given a list of N array elements, apply quick sort on an array to arrange the elements in ascending order.
Constraints:
1 <= N <= 1000
ID: 2100331540014
Input Format:
The first line contains the integer N, the size of the array.
The next line contains N space-separated integers.
Output Format:
Print the sorted array as a row of space-separated integers.
Source Code:
quickSort.c
2021-2025-CSE-DS-A-G1
Raj Kumar Goel Institute Of Technology
#include<stdio.h>
void quicksort(int num[100], int first, int last)
{
int i, j, pivot, temp;
if(first < last){
Page No: 17
pivot = first;
i = first;
j = last;
while(i<j){
while(num[i] <= num[pivot] && i < last)
ID: 2100331540014
i++;
while(num[j]> num[pivot])
j--;
if(i < j){
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
temp = num[pivot];
num[pivot] = num[j];
num[j] = temp;
2021-2025-CSE-DS-A-G1
quicksort(num, first, j-1);
quicksort(num, j+1, last);
}
}
void main(){
int n, i,o[100];
printf("Enter the no of elements: ");
scanf("%d",&n);
printf("Enter numbers to be sorted: ");
for(i =0; i< n;i++){
User Output
Enter the no of elements:
5
Enter numbers to be sorted:
4 8 2 -3 0
Sorted array is: -3 0 2 4 8
Test Case - 2
User Output
Enter the no of elements:
8
Page No: 18
Enter numbers to be sorted:
-6 -2 -8 -36 0 69 -5 -96
Sorted array is: -96 -36 -8 -6 -5 -2 0 69
ID: 2100331540014
2021-2025-CSE-DS-A-G1
Raj Kumar Goel Institute Of Technology
Exp. Name: Write the code to fill the knapsack in
S.No: 8 such a way that we can get the maximum profit Date: 2023-12-19
and return the value.
Page No: 19
Aim:
Problem Description:
Given is an array of N elements, each of which has a weight and a value denoted by w[ ] and val[ ], respectively.
Moreover, a knapsack with a W weight restriction.
The main objective is to pack the knapsack so that we may make the most money possible. Return the biggest
ID: 2100331540014
sum of money.
Note: The amount of times you can take each thing is unlimited.
Constraints:
1 ≤ N, W ≤ 1000
Input Format:
• The first line represents the size of both the arrays.
• The second line represents the set of elements of wt[ ].
• The third line represents the set of elements of val[ ].
2021-2025-CSE-DS-A-G1
• The next line contains an integer representing the weight limit W.
Output Format:
• An integer representing the maximum profit.
xplanation:
E
1.Pick the 2nd element thrice.
2.Total profit = 1 + 1 + 1 = 3. Also the totalweight = 1 + 1 + 1= 3 which is <= W.
Source Code:
MaxProfit.c
#include<stdio.h>
int max(int a, int b)
{
return (a > b) ? a : b;
}
Page No: 20
int unbounded_knapsack(int wt[], int val[], int n, int W)
{
int dp[n+1][W+1];
for(int i=0; i<=n; i++)
{
ID: 2100331540014
for(int j =0; j<=W; j++)
{
if(i == 0 || j== 0){
dp[i][j]=0;
}
else if(wt[i - 1]>j)
{
dp[i][j] = dp[i-1][j];
}
else
{
dp[i][j]= max(dp[i-1][j],val[i-1] + dp[i][j-wt[i-
2021-2025-CSE-DS-A-G1
1]]);
}
}
}
return dp[n][W];
}
int main() {
int n;
scanf("%d", &n);
int wt[n], val[n];
User Output
3
3
25
325
200
100
5 15 10
5 15 10
65 95 32
10 30 20
User Output
Test Case - 2
Raj Kumar Goel Institute Of Technology 2021-2025-CSE-DS-A-G1 ID: 2100331540014 Page No: 21
S.No: 9 Exp. Name: N Queen problem using backtracking Date: 2023-12-19
Aim:
N-Queens using Backtracking:
Page No: 22
Here is an algorithm for solving the N-Queens problem using backtracking:
1. Start with an empty chessboard of size N x N.
2. Place the first queen in the leftmost column of the first row.
3. Move to the next row and try to place a queen in an empty square in that row that is not attacked by any
ID: 2100331540014
of the queens already placed on the board.
4. If a queen can be placed in the current row, move to the next row and repeat step 3.
5. If a queen cannot be placed in the current row, backtrack to the previous row and try a different square in
that row. If all squares in the previous row have been tried and none of them worked, backtrack again to
the previous row.
6. Repeat steps 3-5 until all N queens have been placed on the board or it is determined that no solution
exists.
The key to this algorithm is the "not attacked" rule. To determine whether a queen is being attacked by any other
queen on the board, we need to check whether any other queen is in the same row, column, or diagonal as the
current queen.
2021-2025-CSE-DS-A-G1
Given an Array, you need to place a queen in the array such that no queen can strike down any other queen. A
queen has the possibility to attack horizontally, vertically, or diagonally. You need to check whether the queen is
placed in the correct position or not.
Input Format:
It contains the number of queens in an Array.
Output Format:
An array in which the queens are placed and print a*symbol when there is no queen in that particular row or
Constraints:
1 <= N <= 6
Source Code:
nQueen.c
#include<stdio.h>
#include<math.h>
int a[30],count=0;
int place(int pos){
int i;
Page No: 23
for(i=1;i<pos;i++)
if((a[i]==a[pos])||((abs(a[i]-a[pos])==abs(i-pos))))
return 0;
return 1;
ID: 2100331540014
}
void print_sol(int n){
int i,j;
count++;
printf("Solution #%d:\n",count);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(a[i]==j)
printf("Q\t");
2021-2025-CSE-DS-A-G1
else
printf("*\t");
}
printf("\n");
}
}
void queen(int n){
int k=1;
a[k]=0;
Page No: 24
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
ID: 2100331540014
4
Solution #1:
* Q * *
* * * Q
Q * * *
* * Q *
Solution #2:
* * Q *
Q * * *
* * * Q
2021-2025-CSE-DS-A-G1
* Q * *
Total solutions:2
Test Case - 2
User Output
3
Total solutions:0
Aim:
Here, we will learn about 0/1 Knapsack problem. In this dynamic programming problem, we have n items each
Page No: 25
with an associated weight and value. The objective is to fill the knapsack with items such that we have a maximum
profit without crossing the weight limit of the knapsack. Since this is a 0/1 knapsack problem hence we can either
take an entire item or reject it completely. We can not break an item and fill the knapsack.
Point to remember:
ID: 2100331540014
• For example, we have a Knapsack that has a weight limit W .
• There are items i ,i , ..., i each having weight w ,w , … w and some value associated with it v , v , ... v
1 2 n 1 2 n 1 2 n
• Our objective is to maximize the benefit such that the total weight inside the knapsack is less than W
• Since this is a 0/1 knapsack problem so, we can either take an entire item or reject it completely. We can
not break an item and fill the knapsack.
Let us consider an example to illustrate this.
The given problem has two integer arrays val [0, 1, . . . n − 1] and w [0, 1, . . . n − 1] which represent values
and weights associated with n items respectively. Also given an integer W which represents the capacity of the
knapsack. Find out the maximum value subset of val [] such that sum of the weights of this subset is smaller than
or equal to W . You cannot break an item, either pick the complete item or don’t pick it .
2021-2025-CSE-DS-A-G1
The solution can be obtained by considering all subsets of items and calculate the total weight and value of all
subsets. Consider the only subsets whose total weight is smaller than W , from all such subsets, pick the maximum
value subset.
,
weight = 10 value = 60
,
weight = 20 value = 100
,
weight = 30 value = 120
,
weight = (20 + 10) value = (100 + 60)
,
weight = (30 + 10) value = (120 + 60)
,
weight = (30 + 20) value = (120 + 100)
• The optimal Solution can be obtained as follows: To consider all subsets of items, there can be two
cases for every item:
7. the item is included in the optimal subset or not included in the optimal set
• Therefore, the maximum value that can be obtained from n items is the max of the following two values
8. Maximum value obtained by n − 1 items and w weight (excluding nth item)Value of nth item plus
maximum value obtained by n − 1 items and W > minus weight of thenth item (including nth item). If
the weight of nth item is greater than W , then the nth item cannot be included and case 1 is the only
possibility
Fill the missing code in the below program, follow the instructions given in the comment lines and print the
output as follows.
Sample Input and Output:
Page No: 26
Enter the no. of items: 4
Enter the weight and price of all items
2 3
3 4
4 5
ID: 2100331540014
5 6
enter the capacity of knapsack: 5
The maximum value of items that can be put into knapsack is 7
Source Code:
dynamicKnapsack.c
#include<stdio.h>
int max(int a, int b) {
return (a > b)? a : b;
}
int knapsack(int w[], int p[], int n, int W) {
2021-2025-CSE-DS-A-G1
if(n-1<0) return 0;
int take=0;
if(w[n-1] <= W)
take=p[n-1]+knapsack(w,p,n-1,W-w[n-1]);
int notake=knapsack(w,p,n-1,W);
return max(take,notake);
int main() {
int w[n];
int p[n];
User Output
Page No: 27
Enter the no. of items:
4
Enter the weight and price of all items
23
ID: 2100331540014
34
45
56
enter the capacity of knapsack:
5
The maximum value of items that can be put into knapsack is 7
Test Case - 2
User Output
2021-2025-CSE-DS-A-G1
Enter the no. of items:
4
Enter the weight and price of all items
3 100
2 20
4 60
1 40
enter the capacity of knapsack:
Aim:
You are given respective weight and profit earned fornitems, put these items in a bag of capacitycto get the
Page No: 28
maximum total profit in the bag. Note: We have onlyone quantity of each item.
In detail: We are given two integer arraysprofit[0..n-1]andweight[0..n-1]which represent profit and weight
associated withnitems respectively. Also given an integer c which represents bags capacity, find out the maximum
profit subset ofprofit[]such that sum of the weights of this subset is smaller than or equal toc.
Note: While putting item in the bag you cannot break an item,either pick the complete item or don't pick
ID: 2100331540014
it.
Input Format:
first line contains an integer n representing the number of items
second line contains an integer c representing the capacity of the bag
third line contains an integer array/list profit[i] containing profit associated with ith item
last line contains an integer array/list weight[i] containing weight associated with ith item
Output Format:
integer: Maximum total profit in the bag.
Sample test cases :
Input-1:
2021-2025-CSE-DS-A-G1
3 // n
4 // c
1 2 3 // profit[]
4 5 1 // weight[]
Output:
3
Input-2:
3 // n
Output:
0
Brief editorial: In input-1 the maximum profit that can be put in the bag of capacity 4 is 3 means only item 3
with weight 1 unit and profit 3,
In input-2 as wights of all items are more than the capacity c = 4 so profit in the bag will be 0.
Constraints:
1 <= n <= 1000
1 <= c <= 1000
1 <= profit[i], weight[i] <= 1000
Instruction : To run your custom test case on the terminal please strictly follow the input and output
layout mentioned in sample test case.
Source Code:
CTC35828.c
#include<stdio.h>
int maximum(int a , int b ){
return (a > b) ? a : b;
}
Page No: 29
int knapsack(int wt[], int val[], int W, int n)
{
int dp[n+ 1][W + 1];
for(int i =0; i <= n; i++){
for(int w = 0; w<= W; w++){
ID: 2100331540014
if(i == 0 || w == 0){
dp[i][w] = 0;
}
else if (wt[i - 1] <= w)
{
dp[i][w] = maximum(val[i - 1] + dp[i -1][w - wt[i -
1]], dp[i - 1][w]);
}
else {
dp[i][w] = dp[i - 1][w];
}
}
2021-2025-CSE-DS-A-G1
}
return dp[n][W];
}
int main()
{
int n, W;
scanf("%d",&n);
scanf("%d",&W);
int profit[n], weight[n];
for(int i = 0; i<n; i++)
User Output
3
4
123
0
3
4
3
567
123
451
User Output
Test Case - 2
Raj Kumar Goel Institute Of Technology 2021-2025-CSE-DS-A-G1 ID: 2100331540014 Page No: 30
S.No: 12 Exp. Name: Dijkstra’s Shortest Path Algorithm Date: 2023-12-19
Aim:
Find the shortest routes between a source vertex and each vertex in the provided graph, given the graph and the
Page No: 31
source vertex.
Input Layout:
The first line contains an integer V represents the number of vertices in the graph.
Next line onwards is the graph's adjacency matrix of size V x V.
ID: 2100331540014
Constraints to be followed:
1<= V <= 1000
2021-2025-CSE-DS-A-G1
0 0 4 14 10 0 2 0 0
000002016
8 11 0 0 0 0 1 0 7
002000670
utput:
O
VertexDistance from Source
00
14
212
Explanation:
Consider the graph (fig -1 )with src=0 (Distance of source vertex from itself is always 0) and we get the following
Shortest Path Tree-SPT (refer fig-2).
Dijkstra’sShortestPath.c
Page No: 32
ID: 2100331540014
2021-2025-CSE-DS-A-G1
Raj Kumar Goel Institute Of Technology
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
#define V 1000
int minDistance(int dist[], int visited[])
Page No: 33
{
int min = INT_MAX, min_index;
for(int v=0; v< V; v++)
{
if(!visited[v] && dist[v] <= min)
ID: 2100331540014
{
min = dist[v];
min_index = v;
}
}
return min_index;
}
void dijkstra(int graph[V][V], int src, int n)
{
int dist[V];
int visited[V];
for(int i = 0; i< V; i++)
2021-2025-CSE-DS-A-G1
{
dist[i] = INT_MAX;
visited[i] = 0;
}
dist[src] = 0;
for(int count = 0; count < V - 1; count++)
{
int u = minDistance(dist, visited);
visited[u] = 1;
for( int v = 0; v< V; v++)
int main()
{
int graph[V][V];
int n;
scanf("%d",&n);
for(int i = 0; i<n; i++)
{
for( int j =0; j<n; j++)
{
}
int source = 0;
dijkstra(graph, source,n);
return 0;
}
Page No: 34
Execution Results - All test cases have succeeded!
ID: 2100331540014
Test Case - 1
User Output
9
040000080
4 0 8 0 0 0 0 11 0
080704002
0 0 7 0 9 14 0 0 0
0 0 0 9 0 10 0 0 0
2021-2025-CSE-DS-A-G1
0 0 4 14 10 0 2 0 0
000002016
8 11 0 0 0 0 1 0 7
002000670
Vertex Distance from Source
0 0
1 4
2 12
Test Case - 2
User Output
3
211
231
341
Vertex Distance from Source
0 0
1 1
2 1
S.No: 14 Exp. Name: Prim’s Minimum Spanning Tree (MST) Date: 2023-12-20
Aim:
Problem Description:
Page No: 35
Given is a graph with V vertices, undirected, and weighted. The goal is to calculate the Minimum Spanning Tree's
edge weights as a whole.
Input Layout:
• The first line contains an integer V represents the number of vertices in the graph.
ID: 2100331540014
• Next line onwards is the graph's adjacency matrix of size V x V.
Constraints to be followed:
2<= V <= 1000
Sample test case 1:
Input:
5
02060
20385
03007
68009
2021-2025-CSE-DS-A-G1
05790
utput:
O
Edge --> Weight
0 - 1 ----> 2
1 - 2 ----> 3
0 - 3 ----> 6
1 - 4 ----> 5
Source Code:
Prim’sMinimumSpanningTree.c
#include<stdio.h>
#include<limits.h>
#include<stdbool.h>
#define V 1000
int minKey(int key[], bool mstSet[])
Page No: 36
{
int min = INT_MAX, min_index;
for(int v = 0; v<V; v++)
{
if(!mstSet[v] && key[v] < min)
ID: 2100331540014
{
min = key[v];
min_index = v;
}
}
return min_index;
}
void primMST(int graph[V][V], int n)
{
int parent[V];
int key[V];
bool mstSet[V];
2021-2025-CSE-DS-A-G1
for(int i = 0; i < V; i++)
{
key[i] = INT_MAX;
mstSet[i] = false;
}
key[0]= 0;
parent[0] = -1;
for(int count = 0; count < V -1; count++)
{
int u = minKey(key, mstSet);
Page No: 37
}
ID: 2100331540014
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
5
02060
20385
03007
2021-2025-CSE-DS-A-G1
68009
05790
Edge Weight
0 - 1 2
1 - 2 3
0 - 3 6
1 - 4 5
User Output
3
648
315
954
Edge Weight
0 - 1 3
1 - 2 5
Test Case - 3
User Output
6
123456
456789
789456
456123
963258
852147
Edge
3 - 5
3 - 4
0 - 3
0 - 2
0 - 1
1
2
4
7
4
Weight
Raj Kumar Goel Institute Of Technology 2021-2025-CSE-DS-A-G1 ID: 2100331540014 Page No: 38
S.No: 15 Exp. Name: Floyd - Warshall's Algorithm Date: 2023-12-19
Aim:
Write a program to implement all pairs of shortest paths using Floyd’s Algorithm.
Page No: 39
Source Code:
Warshall.c
ID: 2100331540014
2021-2025-CSE-DS-A-G1
Raj Kumar Goel Institute Of Technology
#include <stdio.h>
#define INF 99999
#define MAX_N 20 // Maximum value for N
#include <stdio.h>
#define INF 99999
Page No: 40
#define MAX_N 20 // Maximum value for N
void floyd_warshall(int graph[100][100], int num_vertices)
{
int distance_matrix[100][100];
for (int i = 0; i < num_vertices; i++)
ID: 2100331540014
{
for (int j = 0; j < num_vertices; j++)
{
distance_matrix[i][j] = graph[i][j];
}
}
for (int k = 0; k < num_vertices; k++)
{
for (int i = 0; i < num_vertices; i++)
{
for (int j = 0; j < num_vertices; j++)
2021-2025-CSE-DS-A-G1
{
if (distance_matrix[i][k] + distance_matrix[k][j] <
distance_matrix[i][j])
{
distance_matrix[i][j] = distance_matrix[i][k] +
distance_matrix[k][j];
}
}
}
}
printf(" INF");
}
else
{
if(distance_matrix[i][j]<0)
{
printf(" %d",distance_matrix[i][j]);
}
else if(distance_matrix[i][j]>9)
{
else
{
printf(" %d", distance_matrix[i][j]);
}
}
Page No: 41
}
printf("\n");
}
}
int main()
ID: 2100331540014
{
int num_vertices, num_edges;
printf("Enter the number of vertices : ");
scanf("%d", &num_vertices);
printf("Enter the number of edges : ");
scanf("%d", &num_edges);
int graph[100][100];
for (int i = 0; i < num_vertices; i++)
{
for (int j = 0; j < num_vertices; j++)
{
graph[i][j] = INF;
2021-2025-CSE-DS-A-G1
}
}
for (int i = 0; i < num_edges; i++)
{
int src, dest, weight;
printf("Enter source : ");
scanf("%d", &src);
printf("Enter destination : ");
scanf("%d",&dest);
printf("Enter weight : ");
User Output
Enter the number of vertices :
4
Enter the number of edges :
5
Enter source :
1
Enter destination :
2
Enter weight :
4
Enter source :
1
Enter destination :
Page No: 42
4
Enter weight :
10
Enter source :
ID: 2100331540014
1
Enter destination :
3
Enter weight :
6
Enter source :
2
Enter destination :
4
Enter weight :
2021-2025-CSE-DS-A-G1
5
Enter source :
3
Enter destination :
4
Enter weight :
2
The following matrix shows the shortest distances between all pairs of the vertices.
0 4 6 8
Test Case - 2
User Output
Enter the number of vertices :
5
Enter the number of edges :
6
Enter source :
1
Enter destination :
2
Enter weight :
2
Enter source :
1
Enter destination :
5
Enter weight :
3
Enter source :
2
Page No: 43
Enter destination :
4
Enter weight :
4
ID: 2100331540014
Enter source :
2
Enter destination :
3
Enter weight :
7
Enter source :
4
Enter destination :
3
2021-2025-CSE-DS-A-G1
Enter weight :
2
Enter source :
5
Enter destination :
4
Enter weight :
1
Test Case - 3
User Output
Enter the number of vertices :
4
Enter the number of edges :
5
Enter source :
1
Enter destination :
2
Enter weight :
4
Enter source :
3
Enter destination :
2
Enter weight :
5
Page No: 44
Enter source :
4
Enter destination :
1
ID: 2100331540014
Enter weight :
1
Enter source :
4
Enter destination :
2
Enter weight :
3
Enter source :
4
2021-2025-CSE-DS-A-G1
Enter destination :
3
Enter weight :
8
The following matrix shows the shortest distances between all pairs of the vertices.
0 4 INF INF
INF 0 INF INF
INF 5 0 INF
1 3 8 0
User Output
Enter the number of vertices :
4
Enter the number of edges :
6
Enter source :
1
Enter destination :
2
Enter weight :
1
Enter source :
1
Enter destination :
4
Enter weight :
3
Enter source :
2
Enter destination :
3
Enter weight :
Page No: 45
6
Enter source :
3
Enter destination :
ID: 2100331540014
1
Enter weight :
-2
Enter source :
4
Enter destination :
2
Enter weight :
5
Enter source :
2021-2025-CSE-DS-A-G1
4
Enter destination :
3
Enter weight :
10
The following matrix shows the shortest distances between all pairs of the vertices.
0 1 7 3
4 0 6 7
-2 -1 0 1
Aim:
In the sum of the subset problem, we have to find a subset such that the sum of elements in the subset is equal
Page No: 46
to a given number. The backtracking approach generates all permutations in the worst case but in general,
performs better than the recursive approach towards subset sum problem.
Procedural Steps:
9. Start with an empty set
ID: 2100331540014
10. Add the next element from the list to the set
11. If the subset has having sum equal to the given number, then stop with that subset as a solution.
12. If the subset is not possible or if we have reached the end of the set, then backtrack through the subset
until we find the most suitable value.
13. If the subset is possible and the sum of the subset < the given number then go to step 2.
14. If all the elements are visited without finding a suitable subset and if no backtracking is possible then stop
without a solution.
Write a program to count the possible subsets for the given number using backtracking
Input format:
The first line of input contains the number of elements in the set.
2021-2025-CSE-DS-A-G1
The second line of input contains the set of integers.
The third line of input contains the sum.
Output format:
Print the subsets whose sum is equal to the given sum.
Source Code:
CTC35808.c
Page No: 47
for(int i=0;i<subsetsize; i++)
{
printf("%d ", subset[i]);
}
printf("\n");
ID: 2100331540014
return 1;
}
if(n == 0 && sum != 0)
return 0;
int without_last=Sum_subset (array, n-1, sum, subset, subsetsize);
int subset_with_last[100];
for(int i=0;i<subsetsize; i++)
{
subset_with_last[i]=subset[i];
}
subset_with_last[subsetsize]=array[n-1];
int with_last=Sum_subset(array, n-1, sum-array[n-1],subset_with_last, subsetsize+1);
2021-2025-CSE-DS-A-G1
return with_last || without_last;
}
void main()
{
int n, array[50], sum, i, subset[50];
scanf("%d",&n);
for(i=0;i<n; i++)
{
scanf("%d", &array[i]);
}
User Output
3
12 3 2
5
2 3
Test Case - 2
4
500
User Output
100 25 36 54
not possible
Raj Kumar Goel Institute Of Technology 2021-2025-CSE-DS-A-G1 ID: 2100331540014 Page No: 48