Professional Documents
Culture Documents
ADA Last3
ADA Last3
ADA Last3
if (amount != 0)
cout << "Cannot make change for the given amount with the available coins." << endl;
else {
cout << "Change using limited coins supply: ";
for (int i = 0; i < numCoins; i++) {
while (change[i] > 0) {
cout << coins[i] << " ";
change[i]--;
}
}
cout << endl;
}
if (amount != 0)
cout << "\nCannot make change for the given amount with the available coins." << endl;
else
int main() {
// Test limited coins scenario
int limitedCoins[] = {1, 5, 10, 20, 50};
int limitedNumCoins = sizeof(limitedCoins) / sizeof(limitedCoins[0]);
int limitedAmount = 63;
limitedCoinsChange(limitedCoins, limitedNumCoins, limitedAmount);
return 0;
}
OUTPUT:
Change using limited coins supply: 1 1 1 10 50
Change using unlimited coins supply: 50 10 1 1 1
2) Write a program to find the Single Source Shortest Path (SSSP) in a Directed
Acyclic Graph (DAG) using Dijkstra's Algorithm
CODE:
// C++ program to find single source shortest
int v;
int weight;
public:
AdjListNode(int _v, int _w) { v = _v; weight = _w;}
int getV() { return v; }
class Graph
{
int V; // No. of vertices'
list<AdjListNode> *adj;
Graph::Graph(int V)
{
this->V = V;
adj = new list<AdjListNode>[V];
}
int dist[V];
visited[i] = false;
int u = Stack.top();
Stack.pop();
if (dist[u] != INF)
{
for (i = adj[u].begin(); i != adj[u].end(); ++i)
if (dist[i->getV()] > dist[u] + i->getWeight())
dist[i->getV()] = dist[u] + i->getWeight();
}
}
(dist[i] == INF)? cout << "INF ": cout << dist[i] << " ";
}
int main()
{
// Create a graph given in the above diagram.
// Here vertex numbers are 0, 1, 2, 3, 4, 5 with
// following mappings: 0=r, 1=s, 2=t, 3=x, 4=y, 5=z
Graph g(6);
g.addEdge(0, 1, 5);
g.addEdge(0, 2, 3);
g.addEdge(1, 3, 6);
g.addEdge(1, 2, 2);
g.addEdge(2, 4, 4);
g.addEdge(2, 5, 2);
g.addEdge(2, 3, 7);
g.addEdge(3, 4, -1);
g.addEdge(4, 5, -2);
int s = 1;
cout << "Following are shortest distances from source " << s <<" n";
g.shortestPath(s);
return 0;
}
OUTPUT:
Following are shortest distances from source 1 nINF 0 2 6 5 3
LAB-11
1) Write a program that efficiently merges a set of sorted files of different
lengths into a single sorted file, aiming to minimize the time complexity of
the process.
CODE:
// C++ program to implement
// Optimal File Merge Pattern
#include <bits/stdc++.h>
using namespace std;
// Function to find minimum computation
int minComputation(int size, int files[])
{
pq.pop();
}
return count;
}
int main()
{
int n = 6;
int files[] = { 2, 3, 4, 5, 6, 7 };
cout << "Minimum Computations = "
<< minComputation(n, files);
return 0;
OUTPUT:
Minimum Computations = 68
ADA LAB 12
1)Write a program to implement Change – making problem using a dynamic
programig when (1) the cashier has only a limited number of coins. (2) the
cashier has an unlimited supply of coins. The value of coins will be like
(1,5,10,20,50…paise)
CODE:
#include<bits/stdc++.h>
using namespace std;
if (coins[j] <= i && dp[i - coins[j]] != INT_MAX && dp[i - coins[j]] + 1 < dp[i]) {
dp[i] = dp[i - coins[j]] + 1;
}
}
}
return dp[amount];
}
// Function to find minimum number of coins required for change with unlimited coins
int unlimited_coins_change(int coins[], int num_coins, int amount) {
int dp[amount + 1];
// Initialize dp array with maximum value
for (int i = 0; i <= amount; i++)
dp[i] = INT_MAX;
// Base case: 0 coins required for amount 0
dp[0] = 0;
// Compute minimum number of coins for each amount from 1 to amount
for (int i = 1; i <= amount; i++) {
}
return dp[amount];
}
int main() {
OUTPUT:
Enter the amount:
27
Minimum number of coins required (limited coins): 5
Minimum number of coins required (unlimited coins): 4