-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 18c8f52
Showing
9 changed files
with
613 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Assignment 1 | ||
// Write a program to calculate Fibonacci numbers and find its step count. | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int Counter = 0; | ||
// Recursive Function for Fibonacci Series | ||
int recUtilityFunction(int n) { | ||
if (n <= 1) { | ||
return n; | ||
} | ||
Counter++; | ||
return recUtilityFunction(n - 1) + recUtilityFunction(n - 2); | ||
} | ||
|
||
void recursiveFibonacci(int n) { | ||
int i = 0; | ||
cout << "Fibonacci Series: "; | ||
while (i < n) { | ||
cout << recUtilityFunction(i) << " "; | ||
i++; | ||
} | ||
cout << endl; | ||
cout << "Step Count: " << Counter << endl; | ||
Counter = 0; | ||
} | ||
|
||
// Iterative Function for Fibonacci Series | ||
void iterativeFibonacci(int n) { | ||
int F1 = 1, F2 = 0, nextTerm = 0; | ||
cout << "Fibonacci Series: "; | ||
for (int i = 1; i <= n; ++i) { | ||
nextTerm = F1 + F2; | ||
F1 = F2; | ||
F2 = nextTerm; | ||
cout << nextTerm << " "; | ||
Counter++; | ||
} | ||
cout << endl; | ||
|
||
cout << "Step Count: " << Counter << endl; | ||
Counter = 0; | ||
} | ||
|
||
|
||
int main() { | ||
int n; | ||
cout << "Enter the length of series: "; | ||
cin >> n; | ||
int choice = 1; | ||
do { | ||
cout << "***Enter your choice***" << endl; | ||
cout << "[1] Recursive" << endl; | ||
cout << "[2] Iterative" << endl; | ||
cout << "[3] ESC" << endl; | ||
cin >> choice; | ||
switch (choice) { | ||
case 1: { | ||
recursiveFibonacci(n); | ||
break; | ||
} | ||
case 2: { | ||
iterativeFibonacci(n); | ||
break; | ||
} | ||
default: break; | ||
} | ||
} while (choice != 3); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Assignment 2 | ||
// Implement job sequencing with deadlines using a greedy method. | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
#include <string> | ||
#include <queue> | ||
|
||
using namespace std; | ||
|
||
struct Job { | ||
string id; | ||
int dead; | ||
int profit; | ||
}; | ||
|
||
class jobProfit { | ||
public: | ||
bool operator()(Job A, Job B) { | ||
return (A.profit < B.profit); | ||
} | ||
}; | ||
|
||
bool jobDeadline(Job A, Job B) { return (A.dead < B.dead); } | ||
|
||
void jobschedule(vector<Job> Jobs, int n) { | ||
vector<Job> schedule; | ||
sort(Jobs.begin(), Jobs.end(), jobDeadline); | ||
|
||
priority_queue<Job, vector<Job>, jobProfit> jobQueue; | ||
|
||
for(int i = n-1; i >= 0; i--) { | ||
int slot_available = 0; | ||
|
||
if(i == 0) { | ||
slot_available = Jobs[i].dead; | ||
} | ||
else { | ||
slot_available = Jobs[i].dead - Jobs[i-1].dead; | ||
} | ||
|
||
jobQueue.push(Jobs[i]); | ||
|
||
if(slot_available > 0 && !jobQueue.empty()) { | ||
Job newJob = jobQueue.top(); | ||
jobQueue.pop(); | ||
|
||
slot_available--; | ||
schedule.push_back(newJob); | ||
} | ||
} | ||
|
||
sort(schedule.begin(), schedule.end(), jobDeadline); | ||
for(auto& item : schedule) { | ||
cout << item.id << ' '; | ||
} | ||
cout << endl; | ||
return; | ||
} | ||
|
||
int main() { | ||
int n; | ||
cout << "Enter the no. of jobs : "; | ||
cin >> n; | ||
|
||
vector<Job> Jobs(n); | ||
for(int i=0; i<n; i++) { | ||
cout << "Enter job details [Id Deadline Profit] : " << endl; | ||
cin >> Jobs[i].id; | ||
cin >> Jobs[i].dead; | ||
cin >> Jobs[i].profit; | ||
} | ||
|
||
cout << "Schedule : "; | ||
jobschedule(Jobs, n); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Asisgnment 3 | ||
// Write a program to solve a fractional Knapsack problem using a greedy method | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
double fracKnapsack(vector<pair<double, double>> Items, int n, int weight) { | ||
double maxProfit = 0; | ||
sort(Items.begin(), Items.end(), [](pair<int, int> A, pair<int, int> B){ return (A.second < B.second);}); | ||
for(int i = 0; i < n; i++) { | ||
if(Items[i].first < weight) { | ||
maxProfit += Items[i].second; | ||
weight -= Items[i].first; | ||
} | ||
else { | ||
maxProfit += Items[i].second * (weight / Items[i].first); | ||
weight -= weight / Items[i].first; | ||
} | ||
} | ||
return maxProfit; | ||
} | ||
|
||
int main() { | ||
int n; | ||
cout << "Enter no of items: "; | ||
cin >> n; | ||
|
||
vector<pair<double, double>> Items(n); | ||
for(int i = 0; i < n; i++) { | ||
cout << "Enter Weight and Profit for each Item: "; | ||
cin>>Items[i].first>>Items[i].second; | ||
} | ||
|
||
int weight; | ||
cout << "Enter Capacity of Knapsack: "; | ||
cin >> weight; | ||
|
||
|
||
cout << "Maximum Profit Possible: " << fracKnapsack(Items, n, weight); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Assignment 4 | ||
// Write a program to solve a 0-1 Knapsack problem using dynamic programming or branch and bound strategy. | ||
|
||
#include <iostream> | ||
#include <algorithm> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
int dynamicKnapsack(vector<pair<int, int>> Items, int n, int weight) { | ||
vector<int> dp(weight + 1, 0); | ||
|
||
for(int i = 0; i < n; i++) { | ||
for(int j = weight; j >=0; j--) { | ||
if(Items[i].first <= j) { | ||
dp[j] = max(dp[j], dp[j - Items[i].first] + Items[i].second); | ||
} | ||
} | ||
} | ||
|
||
return dp[weight]; | ||
} | ||
|
||
int main() { | ||
int n; | ||
cout << "Enter no of items: "; | ||
cin >> n; | ||
|
||
vector<pair<int, int>> Items(n); | ||
for(int i = 0; i < n; i++) { | ||
cout << "Enter Weight and Profit for each Item: "; | ||
cin>>Items[i].first>>Items[i].second; | ||
} | ||
|
||
int weight; | ||
cout << "Enter Capacity of Knapsack: "; | ||
cin >> weight; | ||
|
||
cout << "Maximum Profit Possible: " << dynamicKnapsack(Items, n, weight); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Assignment 5 | ||
// Write a program to generate binomial coefficients using dynamic programming. | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
int binomialCoefficient(int n, int k) { | ||
vector<int> C(k + 1, 0); | ||
C[0] = 1; | ||
|
||
for(int i = 1; i <= n; i++) { | ||
for(int j = min(i, k); j >0; j--) { | ||
C[j] = C[j] + C[j - 1]; | ||
} | ||
} | ||
|
||
return C[k]; | ||
} | ||
|
||
int main() { | ||
int n, k; | ||
cout << "Enter the value of N and K : "; | ||
cin >> n >> k; | ||
|
||
cout << "Value of C(" << n << "," << k << ")"<< " is " << binomialCoefficient(n, k); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Assignment 6 | ||
// Design 8-Queens matrix having first Queen placed. Use backtracking to place remaining Queens to generate the final 8-queen’s matrix. | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
void printSolution(vector<vector<int>> board, int N) | ||
{ | ||
for (int i = 0; i < N; i++) { | ||
for (int j = 0; j < N; j++) { | ||
if(board[i][j]) { | ||
cout << "Q "; | ||
} | ||
else { | ||
cout << "_ "; | ||
} | ||
} | ||
|
||
cout << "\n"; | ||
} | ||
cout << "\n\n"; | ||
} | ||
|
||
bool isSafe(vector<vector<int>> board, int row, int col, int N) | ||
{ | ||
int i, j; | ||
for (i = 0; i < col; i++) | ||
if (board[row][i]) | ||
return false; | ||
for (i = row, j = col; i >= 0 && j >= 0; i--, j--) | ||
if (board[i][j]) | ||
return false; | ||
for (i = row, j = col; j >= 0 && i < N; i++, j--) | ||
if (board[i][j]) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
bool solveNQueen(vector<vector<int>> board, int col, int N) | ||
{ | ||
if (col >= N) { | ||
printSolution(board, N); | ||
} | ||
|
||
for (int i = 0; i < N; i++) { | ||
if (isSafe(board, i, col, N)) { | ||
|
||
board[i][col] = 1; | ||
|
||
solveNQueen(board, col + 1, N); | ||
|
||
board[i][col] = 0; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
int main() | ||
{ | ||
int n; | ||
cout << "Enter size of board: "; | ||
cin >> n; | ||
|
||
vector<vector<int>> board(n, vector<int>(n, 0)); | ||
|
||
solveNQueen(board, 0, n); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.