You are on page 1of 7

PRACTICAL-7

a)
AIM: Write a program to implement 0/1 Knapsack Problem.

CODE:
#include <iostream>
using namespace std;
int max(int x, int y) {
return (x > y) ? x : y;
}
int knapSack(int W, int w[], int v[], int n) {
int i, wt;
int K[n + 1][W + 1];
for (i = 0; i <= n; i++) {
for (wt = 0; wt <= W; wt++) {
if (i == 0 || wt == 0)
K[i][wt] = 0;
else if (w[i - 1] <= wt)
K[i][wt] = max(v[i - 1] + K[i - 1][wt - w[i - 1]], K[i - 1][wt]);
else
K[i][wt] = K[i - 1][wt];
}
}
return K[n][W];
}
int main() {
cout << "Enter the number of items in a Knapsack:";
int n, W;
cin >> n;
int v[n], w[n];
for (int i = 0; i < n; i++) {
cout << "Enter value and weight for item " << i << ":";
cin >> v[i];
cin >> w[i];
}
cout << "Enter the capacity of knapsack: ";
cin >> W;
cout << knapSack(W, w, v, n);
return 0;
}
OUTPUT:

b)
AIM: Write a program to solve All pairs shortest path problem using Floyd-Warshall
Algorithm.

CODE:
#include <iostream>
#include <climits>
using namespace std;

#define V 4
#define INFINITY INT_MAX

void printSolution(int dist[][V]);

void floydWarshall(int dist[][V]) {


for (int k = 0; k < V; k++) {
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][j] > (dist[i][k] + dist[k][j]) &&
(dist[k][j] != INFINITY && dist[i][k] != INFINITY))
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
printSolution(dist);
}

void printSolution(int dist[][V]) {


cout << "The shortest distances between every pair of vertices \n";
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][j] == INFINITY)
cout << "INFINITY"
<< " ";
else
cout << dist[i][j] << " ";
}
cout << endl;
}
}

int main() {
int graph[V][V] = {
{0, 7, INFINITY, 9},
{INFINITY, 0, 5, INFINITY},
{INFINITY, INFINITY, 0, 3},
{INFINITY, INFINITY, INFINITY, 0}
};

floydWarshall(graph);
return 0;
}
OUTPUT:
PRACTICAL-8
a)
AIM: Write a program to solve 8-queen problem using backtracking approach

CODE:
#include <bits/stdc++.h>
using namespace std;
int countt=0;
void print(int board[][4]){
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
cout<<board[i][j]<<" ";
}
cout<<endl;
}
cout<<"-----------------\n";
}
bool isValid(int board[][4],int row,int col){

for(int i=col;i>=0;i--){
if(board[row][i])
return false;
}
int i=row,j=col;
while(i>=0&&j>=0){
if(board[i][j])
return false;
i--;
j--;
}
i=row;
j=col;

while(i<4&&j>=0){
if(board[i][j])
return false;
i++;
j--;
}
return true;
}

void ninjaQueens(int board[][4],int currentColumn){


if(currentColumn>=4)
return;

for(int i=0;i<4;i++){
if(isValid(board,i,currentColumn)){
board[i][currentColumn]=1;
if(currentColumn==3){
print(board);
countt++;
}

ninjaQueens(board,currentColumn+1);

board[i][currentColumn]=0;
}

}
}
int main() {

int board[4][4]={{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0}};
ninjaQueens(board,0);

cout<<countt<<endl;
return 0;
}

OUTPUT:
b)
AIM: Write a program to solve Sum of Subsets problem using backtracking
approach.

CODE:
#include <iostream>
#include <vector>
using namespace std;
bool isSubsetSum(vector<int>& set, int n, int targetSum, vector<int>& subset) {
if (targetSum == 0) {
for (int i = 0; i < subset.size(); i++) {
cout << subset[i] << " ";
}
cout << endl;
return true;
}
if (n == 0 || targetSum < 0) {
return false;
}
if (isSubsetSum(set, n - 1, targetSum, subset)) {
return true;
}
subset.push_back(set[n - 1]);
if (isSubsetSum(set, n - 1, targetSum - set[n - 1], subset)) {
return true;
}
subset.pop_back();

return false;
}

int main() {
vector<int> set = {3, 34, 4, 12, 5, 2};
int targetSum;
cout<<"Enter the targetSum: ";
cin>>targetSum;

vector<int> subset;

if (isSubsetSum(set, set.size(), targetSum, subset)) {


cout << "Subset with the given sum exists." << endl;
} else {
cout << "No subset with the given sum exists." << endl;
}

return 0;
}

OUTPUT:

You might also like