0% found this document useful (0 votes)
16 views14 pages

ADA Last3

Uploaded by

poori.2819.hapo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views14 pages

ADA Last3

Uploaded by

poori.2819.hapo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

LAB-10

1)Write a program to implement Change – making problem using a greedy


algorithm 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 <iostream>
using namespace std;

// Function to make change with limited coins supply


void limitedCoinsChange(int coins[], int numCoins, int amount) {
int change[numCoins];

// Initialize change array with zeros

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


change[i] = 0;

// Iterate through coins in reverse order


for (int i = numCoins - 1; i >= 0; i--) {

while (amount >= coins[i] && coins[i] > 0) {


amount -= coins[i];
change[i]++;
}
}

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;
}

// Function to make change with unlimited coins supply


void unlimitedCoinsChange(int coins[], int numCoins, int amount) {
// Iterate through coins in reverse order

for (int i = numCoins - 1; i >= 0; i--) {


while (amount >= coins[i]) {
amount -= coins[i];
cout << coins[i] << " ";
}

if (amount != 0)
cout << "\nCannot make change for the given amount with the available coins." << endl;
else

cout << endl;


}

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);

// Test unlimited coins scenario


int unlimitedCoins[] = {1, 5, 10, 20, 50};
int unlimitedNumCoins = sizeof(unlimitedCoins) / sizeof(unlimitedCoins[0]);
int unlimitedAmount = 63;

cout << "Change using unlimited coins supply: ";


unlimitedCoinsChange(unlimitedCoins, unlimitedNumCoins, unlimitedAmount);

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

// paths for Directed Acyclic Graphs


#include<iostream>
#include <bits/stdc++.h>
#define INF INT_MAX
using namespace std;

// Graph is represented using adjacency list. Every node


// of adjacency list contains vertex number of the vertex
// to which edge connects. It also
// contains weight of the edge
class AdjListNode
{

int v;
int weight;
public:
AdjListNode(int _v, int _w) { v = _v; weight = _w;}
int getV() { return v; }

int getWeight() { return weight; }


};

// Class to represent a graph using adjacency


// list representation

class Graph
{
int V; // No. of vertices'

// Pointer to an array containing adjacency lists

list<AdjListNode> *adj;

// A function used by shortestPath


void topologicalSortUtil(int v, bool visited[], stack<int> &Stack);
public:

Graph(int V); // Constructor

// function to add an edge to graph


void addEdge(int u, int v, int weight);

// Finds shortest paths from given source vertex


void shortestPath(int s);
};

Graph::Graph(int V)
{
this->V = V;
adj = new list<AdjListNode>[V];
}

void Graph::addEdge(int u, int v, int weight)


{
AdjListNode node(v, weight);
adj[u].push_back(node); // Add v to u's list

// A recursive function used by shortestPath.


// See below link for details
// https://www.geeksforgeeks.org/topological-sorting/

void Graph::topologicalSortUtil(int v, bool visited[], stack<int> &Stack)


{
// Mark the current node as visited
visited[v] = true;

// Recur for all the vertices adjacent to this vertex


list<AdjListNode>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
{
AdjListNode node = *i;
if (!visited[node.getV()])
topologicalSortUtil(node.getV(), visited, Stack);
}

// Push current vertex to stack which stores topological sort


Stack.push(v);
}

// The function to find shortest paths from given vertex.

// It uses recursive topologicalSortUtil() to get topological


// sorting of given graph.
void Graph::shortestPath(int s)
{
stack<int> Stack;

int dist[V];

// Mark all the vertices as not visited


bool *visited = new bool[V];
for (int i = 0; i < V; i++)

visited[i] = false;

// Call the recursive helper function to store


// Topological Sort starting from all vertices
// one by one

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


if (visited[i] == false)
topologicalSortUtil(i, visited, Stack);

// Initialize distances to all vertices as


// infinite and distance to source as 0
for (int i = 0; i < V; i++)
dist[i] = INF;
dist[s] = 0;

// Process vertices in topological order


while (Stack.empty() == false)
{
// Get the next vertex from topological order

int u = Stack.top();
Stack.pop();

// Update distances of all adjacent vertices


list<AdjListNode>::iterator i;

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();

}
}

// Print the calculated shortest distances


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

(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[])
{

// Create a min heap


priority_queue<int, vector<int>, greater<int> > pq;

for (int i = 0; i < size; i++) {

// Add sizes to priorityQueue


pq.push(files[i]);
}

// Variable to count total Computation


int count = 0;

while (pq.size() > 1) {


// pop two smallest size element
// from the min heap
int first_smallest = pq.top();
pq.pop();
int second_smallest = pq.top();
pq.pop();

int temp = first_smallest + second_smallest;

// Add the current computations


// with the previous one's
count += temp;
// Add new combined file size
// to priority queue or min heap
pq.push(temp);
}
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;

int limited_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++) {

for (int j = 0; j < num_coins; j++) {


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++) {


for (int j = 0; j < num_coins; j++) {
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];
}

int main() {
int limited_coins[] = {1, 5, 10, 20, 50}; // Denominations of limited coins
int unlimited_coins[] = {1, 5, 10, 20, 50}; // Denominations of unlimited coins
int amount;
cout << "Enter the amount: " << endl;

cin >> amount;


// Calculate minimum number of coins required for limited coins scenario
int min_coins_limited = limited_coins_change(limited_coins, sizeof(limited_coins) /
sizeof(limited_coins[0]), amount);
cout << "Minimum number of coins required (limited coins): " << min_coins_limited <<
endl;
// Calculate minimum number of coins required for unlimited coins scenario
int min_coins_unlimited = unlimited_coins_change(unlimited_coins,
sizeof(unlimited_coins) / sizeof(unlimited_coins[0]), amount);
cout << "Minimum number of coins required (unlimited coins): " << min_coins_unlimited
<< endl;
return 0;
}

OUTPUT:
Enter the amount:
27
Minimum number of coins required (limited coins): 5
Minimum number of coins required (unlimited coins): 4

You might also like