Assignment 2
Assignment 2
ASSIGNMENT-2 REPORT
Submitted by
P Varun 4SF22CI067
In partial fulfilment of the requirements for the IV semester
of
BACHELOR OF ENGINEERING
in
COMPUTER SCIENCE AND ENGINEERING
( ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING)
at
SAHYADRI
COLLEGE OF ENGINEERING & MANAGEMENT
An Autonomous Institution
Total
Marks Distribution
(10)
Working Timely
Q.
Program Submissio
No Questions Tracing
(Use of Valid n
. Output and
Concepts + (1)
(2) Report
Originality +
(2)
Innovativenes
s (5)
Write a program to determine the last
1
person standing in a circle after
eliminating every k-th person until only
one person is left.
GRAND TOTAL
Faculty Incharge
Program-1: Write a program to determine the last person standing in a circle after
eliminating every k-th person until only one person is left.
Program:
#include <stdio.h>
int position = 0;
position = (position + k) % i;
return position + 1;
if (n == 1)
return 1;
return (josephusRecursive(n - 1, k) + k - 1) % n + 1;
int main()
{ int n = 95;
int k = 5;
return 0;
}
Output:
Program-2: Design a program to optimize delivery routes using Prim's algorithm for a
courier service company.
Program:
#include <stdio.h>
int total_distance = 0;
printf("Edge \tWeight\n");
total_distance += graph[i][parent[i]];
min = key[v];
min_index = v;
return min_index;
int parent[n];
int key[n];
int mstSet[n];
key[i] = INF;
mstSet[i] = 0;
key[0] = 0;
parent[0] = -1;
mstSet[u] = 1;
for (int v = 0; v < n; v++)
parent[v] = u;
key[v] = graph[u][v];
printMST(parent, n, graph);
int main()
{5, 0, 1, 3, 999},
{999, 1, 0, 4, 6},
{6, 3, 4, 0, 2},
printf("\nPrim's algorithm\n");
primMST(5, graph);
return 0;
Output:
Program-3: Implement Kruskal's algorithm to solve a specific problem involving finding
the Minimum Spanning Tree (MST) of a connected graph.
Program:
#include <stdio.h>
#include <stdlib.h>
typedef struct
int u, v, cost;
} Edge;
Edge edges[MAX];
int parent[MAX];
int find(int i)
while (i != parent[i])
i = parent[i];
return i;
parent[y] = x;
}
int main()
int N, M;
printf("Kruskal's Algorithm\n");
scanf("%d", &N);
scanf("%d", &M);
parent[i] = i;
int u = edges[i].u;
int v = edges[i].v;
if (find(u) != find(v))
minCost += cost;
unionSet(find(u), find(v));
return 0;
Output: