0% found this document useful (0 votes)
7 views

Assignment 2

Uploaded by

Varun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Assignment 2

Uploaded by

Varun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

VISVESVARAYA TECHNOLOGICAL UNIVERSITY

“JNANA SANGAMA”, BELAGAVI - 590 018

DESIGN AND ANALYSIS OF ALGORITHMS ( CS422T4C)

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

Adyar, Mangaluru - 575 007

Academic Year: 2023 - 24


Evaluation Sheet

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.

Design a program to optimize delivery


2 routes using Prim's algorithm for a
courier service company.

Implement Kruskal's algorithm to solve a


3
specific problem involving finding the
Minimum Spanning Tree (MST) of a
connected graph.

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 josephusIterative(int n, int k)

int position = 0;

for (int i = 2; i <= n; i++)

position = (position + k) % i;

return position + 1;

int josephusRecursive(int n, int k)

if (n == 1)

return 1;

return (josephusRecursive(n - 1, k) + k - 1) % n + 1;

int main()

{ int n = 95;

int k = 5;

printf("P Varun - 4SF22CI067\n");

printf("\nn:%d , k:%d", n, k);

printf("\nIterative approach: %d\n", josephusIterative(n, k));

printf("Recursive approach: %d\n", josephusRecursive(n, k));

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>

#define INF 999

void printMST(int parent[], int n, int graph[n][n])

int total_distance = 0;

printf("Edge \tWeight\n");

for (int i = 1; i < n; i++)

printf("%d - %d \t%d \n", parent[i], i, graph[i][parent[i]]);

total_distance += graph[i][parent[i]];

printf("Total distance traveled: %d\n", total_distance);

int minKey(int key[], int mstSet[], int n)

int min = INF;


int min_index = -1;

for (int v = 0; v < n; v++)

if (mstSet[v] == 0 && key[v] < min)

min = key[v];

min_index = v;

return min_index;

void primMST(int n, int graph[n][n])

int parent[n];

int key[n];

int mstSet[n];

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

key[i] = INF;

mstSet[i] = 0;

key[0] = 0;

parent[0] = -1;

for (int count = 0; count < n - 1; count++)

int u = minKey(key, mstSet, n);

mstSet[u] = 1;
for (int v = 0; v < n; v++)

if (graph[u][v] && mstSet[v] == 0 && graph[u][v] < key[v])

parent[v] = u;

key[v] = graph[u][v];

printMST(parent, n, graph);

int main()

int graph[5][5] = {{0, 5, 999, 6, 999},

{5, 0, 1, 3, 999},

{999, 1, 0, 4, 6},

{6, 3, 4, 0, 2},

{999, 999, 6, 2, 0}};

printf("P Varun - 4SF22CI067\n");

printf("\nPrim's algorithm\n");

printf("Optimal delivery path and total distance traveled:\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>

#define MAX 1000

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;

void unionSet(int x, int y)

parent[y] = x;
}

int compare(const void *a, const void *b)

Edge edgeA = *(Edge *)a;

Edge edgeB = *(Edge *)b;

return edgeA.cost - edgeB.cost;

int main()

int N, M;

printf("P Varun - 4SF22CI067\n");

printf("Kruskal's Algorithm\n");

printf("Enter number of cities (N):\n");

scanf("%d", &N);

printf("Enter number of roads (M):\n");

scanf("%d", &M);

for (int i = 0; i < M; i++)

printf("Enter road %d details (city1 city2 cost): ", i + 1);

scanf("%d %d %d", &edges[i].u, &edges[i].v, &edges[i].cost);

for (int i = 1; i <= N; i++)

parent[i] = i;

qsort(edges, M, sizeof(Edge), compare);


int minCost = 0;

for (int i = 0; i < M; i++)

int u = edges[i].u;

int v = edges[i].v;

int cost = edges[i].cost;

if (find(u) != find(v))

minCost += cost;

unionSet(find(u), find(v));

printf("Minimum cost to repair all roads: %d\n", minCost);

return 0;

Output:

You might also like