C Programming Algorithms: Coin Change, Knapsack, MST, Shortest Path
Problem Statement 16: Coin Change
Given a set of coins and a value, find the minimum number of coins to satisfy the given value.
Test Case 1:
Coins: {25, 20, 10, 5}, Value: 50 cents
Used coin: 25 cents
Used coin: 25 cents
Total number of coins: 2
Test Case 2:
Coins: {25, 20, 10, 5}, Value: 73 cents
Used coin: 25 cents
Used coin: 25 cents
Used coin: 20 cents
Used coin: 3 cents
Total number of coins: 4
Code:
-
#include int main { int amount =
50; int coins[] = {25, 20, 10, 5}; int
numCoins = 0;
printf("Amount: %d cents\n", amount);
for (int i = 0; i while (amount >= coins[i]){
amount -= coins 1;
numCoins++;
printf("Used
coin: %d cents\n", coins[i]);
}
printf("Total number of coins: %din", numCoins); return 0; }
Problem Statement 19: -#include #include int i,j,k,a,b,u,v,n,ne=1; int
min,mincost=0,cost[9][9],parent[9];
int find(int); int uni(int,int); int
printf("n\tImplementation of Kruskal's Algorithm\n"); printf(" nEnter
the no. of vertices:"); scanf("d",&n); printf("|nEnter the cost
adjacency matrix:\n"); for(i=1;i
scanf("d",&cost[i]b]);
if(cost[ib]==0)
cost[ib]=999;
printf("The edges of Minimum Cost Spanning Tree are\n");
for(i=1,min=999;i
for(j=1;j
if(cost[i][]
min-=cost[iGiJ;
a=u=1;
b=v-j;}}
u-find(u);
v=find(v);
(%d,%d) =%din",ne + + ,a,b,min);
}
if(uni(u,v)){
mincostt=min;
cost[a][b]=cost[b][a]=999;
printf("n\tMinimum cost = %dn",mincost); return 0; ; int
find(int i){ while(parent[i)
i-parent[i]; return i; }
int uni(int i,int j& ifi!=i){
parentfjl=i;return 1;return O;}while(ne
Problem Statement 20: - #include
int
visited[101=(0),cost{10][10],min,mincost-0; void prims(int num); int main int num,ij; printf"ntltPrim's Algorithm");
print"ninEnter the number of nodes=*); scanf("d", &enum);
printf("nEnter the adjacency matrixin\n"); for(i=1;iprintf" value of cost{⅚d]%d] " i,j;
scanf("d", &cost[iüD);
if(costFiJ6]==0)
cost[i10]=999;
}
printf("n The cost of adjacency matrixinin");
for(i=1;iprintf("d t", cost[ilil);
printf("\n");
}
prims(num); return 0;
}
void prims(int num){
ijj,a,b,u,v,min,num_edges=1;
while(num_edges for(i=l;ifor(j=l;jcost[ill|a-u=i;
b=v-j;
int visited[1]=1; min=999;
if(visitedfil)
if!visitedli] &&
min=costfi]il;
}
if(tvisited[vi
%d\n",num edges+ +,a,b,min);
cost[a][b]=cost[b][a]=999;
printf"d edge (%d,%d) =
mincost+=min;
visited[b]=1;
}
printf("'nMinimum cost = %dIn",mincost);
Problem Statement 21: -#include #define V 6
#define INF 999
int minDistance(int dist[], int visited[|) int min = INF,
min_index = -1; for (int v = 0; v (!visited[v] && dist[v] min = dist[v];
min_index = v;
return min_index;
void printSolution(int distl]) {
printf("Vertex It Distance from Sourceln"); for (int i = 0; i
}
void dijkstra(int graph[V][V], int sic) {
visited [V];
int dist[V],
INF;
for (int i = 0; i visited [i] = 0;
dist [i] =
dist[src] = 0;
for (int count = 0; count minDistance(dist, visited);
visited u] = 1;
int u =
for (int v = 0; v dist[u] != INF &&
= dist[u] + graph[u][v];
if (!visited[v] && graph[u][v] &&
dist[u] + graph[u][v] dist[v]
}
printSolution(dist);
} int main 40, 7, 9, 0, 0, 14},
{7, 0, 10, 15, 0, 03,
19, 10, 0, 11, 0, 27,
{0, 15, 11, 0, 6, 03,
10, 0, 0, 6, 0, 97,
{14, 0, 2, 0, 9, 03 dijkstra(graph, 0);
return 0;
}
Problem Statement 18: - You are given a set of N jobs, where each job is having a deadline and a profit associated with it. You can only earn a profit if you complete the job within its given deadline. You can perform the job either before the deadline or strictly on the deadline, not after that. Find the maximum profit achieved.
Rules:
1. Each job takes a single unit of time to execute
2. Only one job can be performed at a given time #include void jobScheduling(int profitll, int deadline[], int n, int maxDeadline); int main printf"nJob Scheduling using Greedy\n"); int profit[20], deadline[20], n, maxDeadline; printf("nEnter number of jobs: "); scanf("d", &n); printf("InEnter profit of each job in descending
order: \n"); for (int i = 0; i scanf("%d", &profit[i]);
printf("InEnter deadline of each job according to the profit order: \n"); for (int i = 0; i
scanf("%d", &deadline[i]);
printf(" nEnter maximum deadline: "); scanf("d", &maxDeadline); jobScheduling(profit, deadline, n, maxDeadline);
return 0;
void jobScheduling(int profit|], int deadline[], int n, int maxDeadline) totalProfit = 0; for (int i = 0; i result [1] = 0;
for (int i = 0; i > 0; j-- {
if (result[] = 0) {
profit[i];
totalProfit += profit[i];}
for (int j = deadline[i] - 1; j
break;}
printf("InScheduled Job Profits (slot-wise): ");
maxDeadline; i++) {
printf("d ", result[i]);
, printf("InTotal Profit: %dIn", totalProfit);
for (int 1 = 0; i}
Problem Statement 23:
Write a program in C language to compute the nth Fibonacci number using the Dynamic Programming approach. Also, explain why this method improves time complexity compared to the traditional recursive approach.
#include
int Fibonacci(int n) { if (n == 0){
if (n = 1){
return 1;
// Create an array to store Fibonacci numbers int dp[n + 1];
// Initialize the first two Fibonacci numbers dp[0] = 0;
dp[l|= 1;
// Fill the array iteratively
for (int i = 2; i
// Return the nth Fibonacci number return dp[n];
int main ‹ int n = 5:
int result = Fibonacci(n); printf"%d\n", result); return 0;
OUTPUT:
Problem Statement 22: - Design and implement a program for Floyd-Warshall Algorithm in C language to find shortest distance between all pair of vertices. #include
#define V 4
#define INF 999
void printMatrixint matrix[][V]);
void floyd Warshall(int graph[|[V) {
int matrix[V][V], i, j, k;
for (i =0;i for (j = 0; jmatrix[7]b] = graph[10];
for (k = 0; k for (i = 0; i for 0 = 0;j if (matrix*][k] + matrix[k]Ö] matrix[i]0] = matrix[i][k] + matrix[k]Б];
printMatrix(matrix);
void printMatrixint matrix|][V]) ‹
for (int i = 0; i for (int j = 0; j if (matrix (1]0] == INF)
printf("%4s", "INF");
else
printf("%4d", matrix[i]Б]);
}
printf("\n");
}
}
int main) {
int graph[V][V] = {
{0, 3,
, INF, 5},
{2, 0,
), INF, 4},
{INF, 1, 0, INF}, {INF, INF, 2, 0}
};
floydWarshall(graph);
return 0;
Problem Statement 24:
#include
int max(int a, int b) {
return (a > b)? a : b;
int knapsack(int values[], int weights], int n, int capacity) { int K[n + 1][capacity + 1];
for (int i = 0; i
for (int w = 0; w
K[i][w] = 0;
} else if (weights [1 - 1]
K[i][w] = max(values[i - 1] + K[i - 1][w - weights[i - 1]1, K[i - 1][w]);
} else {
K[i][w] = K[i - 1][w];}
}}
return K[n][capacity];
int main {
int n, capacity;
printf("Enter the number of items: "); scanf("d",
*, &n);
int values[n], weights[n];
printf("Enter the values/profits of items: In"); for (int i = 0; i
scanf("%d", &values[i);
printf"Enter the weights of items:\n"); for (int i = 0; i
scanf("d", &weights[i]);
} printf("Enter the total capacity of the knapsack: "); scanf("d", &capacity);
int result = knapsack(values, weights, n, capacity);
printf("Maximum value/profit in the knapsack: %d\n", result); return 0;