Add Lab Manual-2
Add Lab Manual-2
AIM:
To sort an array of integers using the heap sort algorithm, demonstrating the "transform and conquer"
strategy.
ALGORITHM:
1. Set largest to i.
2. Calculate left as 2 * i + 1 and right as 2 * i + 2.
3. If left is within bounds and arr[left] > arr[largest], update largest to left.
4. If right is within bounds and arr[right] > arr[largest], update largest to right.
5. If largest is not equal to i, swap arr[i] and arr[largest].
6. Recursively call heapify(arr, n, largest).
PROGRAM :
#include <iostream>
using namespace std;
void heapify(int arr[], int n, int i) {
int largest = i; // Initialize largest as root
int left = 2 * i + 1; // left = 2*i + 1
int right = 2 * i + 2; // right = 2*i + 2
if (left < n && arr[left] > arr[largest])
largest = left;
if (right < n && arr[right] > arr[largest])
largest = right;
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
void heapSort(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for (int i = n - 1; i > 0; i--) {
swap(arr[0], arr[i]);
heapify(arr, i, 0);
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
cout << endl;
}
int main() {
int arr[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Original array: ";
printArray(arr, n);
heapSort(arr, n);
cout << "Sorted array: ";
printArray(arr, n);
return 0;
}
OUTPUT :
Original array: 3 1 4 1 5 9 2 6 5 3 5
Sorted array: 1 1 2 3 3 4 5 5 5 6 9
RESULT :
Thus,the program for array of integers using the heap sort algorithm, demonstrating the "transformand
conquer" strategy successfully executed and required output is displayed.
EX.NO : 05 DYNAMIC PROGRAMMING – COIN CHANGE PROBLEM, WARSHALL’S AND
DATE : FLOYD’S ALGORITHMS, KNAPSACK PROBLEM
AIM :
To write a c++ program for coin change problem, warshall’s and floyd’s algorithms, knapsack problem
using dynamic programming.
PROGRAM :
#include <iostream>
#include <vector>
#include <limits.h>
using namespace std;
int minCoins(vector<int>& coins, int amount) {
vector<int> dp(amount + 1, INT_MAX);
dp[0] = 0;
for (int coin : coins) {
for (int x = coin; x <= amount; x++) {
if (dp[x - coin] != INT_MAX) {
dp[x] = min(dp[x], dp[x - coin] + 1);
}
}
}
return dp[amount] == INT_MAX ? -1 : dp[amount];
}
int main() {
vector<int> coins = {1, 2, 5};
int amount = 11;
cout << "Minimum coins required: " << minCoins(coins, amount) << endl;
return 0;
}
OUTPUT :
Minimum coins required: 3
2.WARSALL’S ALGORITHM :
ALGORITHM :
1. Create a matrix to represent the graph.
2. For each vertex, update the reachability between every pair of vertices.
PROGRAM :
#include <iostream>
#include <vector>
using namespace std;
void warshall(vector<vector<int>>& graph) {
int n = graph.size();
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
graph[i][j] = graph[i][j] || (graph[i][k] && graph[k][j]);
}
}
}
}
int main() {
vector<vector<int>> graph = {
{0, 1, 0},
{0, 0, 1},
{1, 0, 0}
};
warshall(graph);
cout << "Transitive closure matrix:\n";
for (const auto& row : graph) {
for (int val : row) {
cout << val << " ";
}
cout << endl;
}
return 0;
}
OUTPUT :
Transitive closure matrix:
111
111
111
3.FLOYD’S ALGORITHM :
ALGORITHM :
1. Initialize a distance matrix with the edge weights.
2. For each vertex, update the distances based on whether a shorter path exists through another vertex.
PROGRAM :
#include <iostream>
#include <vector>
#include <limits.h>
using namespace std;
void floydWarshall(vector<vector<int>>& dist) {
int n = dist.size();
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (dist[i][k] + dist[k][j] < dist[i][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
}
int main() {
vector<vector<int>> dist = {
{0, 3, INT_MAX, 7},
{8, 0, 2, INT_MAX},
{5, INT_MAX, 0, 1},
{2, INT_MAX, INT_MAX, 0}
};
floydWarshall(dist);
cout << "Shortest path matrix:\n";
for (const auto& row : dist) {
for (int val : row) {
cout << (val == INT_MAX ? "INF" : to_string(val)) << " ";
}
cout << endl;
}
return 0;
}
OUTPUT :
Shortest path matrix:
0323
5023
5801
2530
4.KNAPSACK PROBLEM :
ALGORITHM :
1. Create a 2D array to store maximum value achievable with different weights and items.
2.Fill the array based on whether to include each item or not.
PROGRAM :
#include <iostream>
#include <vector>
using namespace std;
int knapsack(vector<int>& weights, vector<int>& values, int capacity) {
int n = weights.size();
vector<vector<int>> dp(n + 1, vector<int>(capacity + 1, 0));
for (int i = 1; i <= n; i++) {
for (int w = 0; w <= capacity; w++) {
if (weights[i - 1] <= w) {
dp[i][w] = max(dp[i - 1][w], dp[i - 1][w - weights[i - 1]] + values[i - 1]);
} else {
dp[i][w] = dp[i - 1][w];
}
}
}
return dp[n][capacity];
}
int main() {
vector<int> weights = {1, 2, 3};
vector<int> values = {60, 100, 120};
int capacity = 5;
cout << "Maximum value in knapsack: " << knapsack(weights, values, capacity) << endl;
return 0;
}
OUTPUT :
Maximum value in knapsack: 220
RESULT :
Thus, the above programs dynamic programming – coin change problem, warshall’s and
floyd’s algorithms, knapsack problem was successfully executed and output is verified.