Skip to content

Commit aac6414

Browse files
authored
adding Greedy problems
Adding problems related to greedy approach 1. Activity Selection Problem 2.Fractional Knapsack Problem 3.Huffman Algorithm 4.Job Sequencing Problem 5.Minimum Coin Problem
1 parent 6961432 commit aac6414

5 files changed

+268
-0
lines changed

Greedy/HuffmanAlgorithm.cpp

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
// A Huffman tree node
6+
struct Node {
7+
char data;
8+
unsigned freq;
9+
Node *left, *right;
10+
11+
Node(char data, unsigned freq, Node* l = NULL, Node* r = NULL)
12+
13+
{
14+
15+
this->left = l;
16+
this->right = r;
17+
this->data = data;
18+
this->freq = freq;
19+
}
20+
};
21+
22+
// For comparison
23+
struct compare {
24+
25+
bool operator()(Node* l, Node* r)
26+
27+
{
28+
return (l->freq > r->freq);
29+
}
30+
};
31+
32+
// Prints huffman codes from
33+
void printCodes(struct Node* root, string str)
34+
{
35+
36+
if (!root)
37+
return;
38+
39+
if (root->data != '$')
40+
cout << root->data << ": " << str << "\n";
41+
42+
printCodes(root->left, str + "0");
43+
printCodes(root->right, str + "1");
44+
}
45+
46+
void printHcodes(char arr[], int freq[], int size)
47+
{
48+
49+
priority_queue<Node*, vector<Node*>, compare> h;
50+
51+
for (int i = 0; i < size; ++i)
52+
h.push(new Node(arr[i], freq[i]));
53+
54+
while (h.size() > 1) {
55+
56+
Node *l = h.top();h.pop();
57+
58+
Node *r = h.top();h.pop();
59+
60+
61+
Node *top = new Node('$', l->freq + r->freq, l, r);
62+
63+
h.push(top);
64+
}
65+
printCodes(h.top(), "");
66+
}
67+
68+
int main()
69+
{
70+
71+
char arr[] = { 'a', 'd', 'e', 'f' };
72+
int freq[] = { 30, 40, 80, 60 };
73+
74+
int size = sizeof(arr) / sizeof(arr[0]);
75+
76+
printHcodes(arr, freq, size);
77+
78+
return 0;
79+
}
80+

Greedy/activitySelectionProblem.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
bool myCmp(pair <int, int> a, pair <int, int> b)
5+
{
6+
return (a.second < b.second);
7+
}
8+
9+
int maxActivities(pair <int, int> arr[], int n)
10+
{
11+
sort(arr, arr + n, myCmp);
12+
13+
int prev = 0;
14+
int res = 1;
15+
16+
for(int curr = 1; curr < n; curr++)
17+
{
18+
19+
if(arr[curr].first >= arr[prev].second)
20+
{
21+
res++;
22+
23+
prev = curr;
24+
}
25+
}
26+
27+
return res;
28+
}
29+
int main()
30+
{
31+
pair <int, int> arr[] = {make_pair(12, 25), make_pair(10, 20), make_pair(20, 30)};
32+
33+
int n = 3;
34+
35+
cout<<maxActivities(arr, n);
36+
37+
return 0;
38+
}

Greedy/fractionalKnapsack.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
bool myCmp(pair <int, int> a, pair <int, int> b)
5+
{
6+
double r1 = (double)a.first / a.second;
7+
8+
double r2 = (double)b.first / b.second;
9+
10+
return r1 > r2;
11+
}
12+
13+
double fKnapS(int W, pair <int, int> arr[], int n)
14+
{
15+
sort(arr, arr + n, myCmp);
16+
17+
double res = 0.0;
18+
19+
for(int i = 0; i < n; i++)
20+
{
21+
if(arr[i].second <= W)
22+
{
23+
res += arr[i].first;
24+
25+
W = W - arr[i].second;
26+
}
27+
else
28+
{
29+
res += arr[i].first * ((double) W / arr[i].second);
30+
31+
break;
32+
}
33+
}
34+
35+
36+
return res;
37+
}
38+
int main()
39+
{
40+
pair <int, int> arr[] = {make_pair(120, 30), make_pair(100, 20), make_pair(60, 10)};
41+
42+
int n = 3, W = 50;
43+
44+
cout<<fKnapS(W, arr, n);
45+
46+
return 0;
47+
}

Greedy/jobSequencing.cpp

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Program to find the maximum profit job sequence from a given array
2+
// of jobs with deadlines and profits
3+
#include<iostream>
4+
#include<algorithm>
5+
using namespace std;
6+
7+
// A structure to represent a job
8+
struct Job
9+
{
10+
char id;
11+
int dead;
12+
int profit;
13+
};
14+
15+
// sorting all jobs according to profit
16+
bool comparison(Job a, Job b)
17+
{
18+
return (a.profit > b.profit);
19+
}
20+
21+
// Returns minimum number of platforms required
22+
void printJobScheduling(Job arr[], int n)
23+
{
24+
25+
sort(arr, arr+n, comparison);
26+
27+
int result[n];
28+
bool slot[n];
29+
30+
for (int i=0; i<n; i++)
31+
slot[i] = false;
32+
33+
34+
for (int i=0; i<n; i++)
35+
{
36+
37+
for (int j=min(n, arr[i].dead)-1; j>=0; j--)
38+
{
39+
40+
if (slot[j]==false)
41+
{
42+
result[j] = i;
43+
slot[j] = true;
44+
break;
45+
}
46+
}
47+
}
48+
49+
50+
for (int i=0; i<n; i++)
51+
if (slot[i])
52+
cout << arr[result[i]].id << " ";
53+
}
54+
55+
56+
int main()
57+
{
58+
Job arr[] = { {'a', 2, 100}, {'b', 1, 19}, {'c', 2, 27},
59+
{'d', 1, 25}, {'e', 3, 15}};
60+
int n = sizeof(arr)/sizeof(arr[0]);
61+
cout << "Following is maximum profit sequence of jobs \n";
62+
63+
printJobScheduling(arr, n);
64+
return 0;
65+
}

Greedy/minimumCoin.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
5+
int minCoins(int coin[], int n, int amount)
6+
{
7+
sort(coin, coin + n);
8+
9+
int res = 0;
10+
11+
for(int i = n - 1; i >= 0; i--)
12+
{
13+
if(coin[i] <= amount)
14+
{
15+
int c = floor(amount / coin[i]);
16+
17+
res = res + c;
18+
19+
amount = amount - c * coin[i];
20+
}
21+
22+
if(amount == 0)
23+
break;
24+
}
25+
26+
return res;
27+
}
28+
29+
30+
31+
int main() {
32+
33+
int coin[] = {5, 10, 2, 1}, n = 4, amount = 57;
34+
35+
cout<<minCoins(coin, n, amount);
36+
37+
38+
}

0 commit comments

Comments
 (0)