From 1f86899fc74ec404766a970a1c318605d35c59bd Mon Sep 17 00:00:00 2001 From: ASHMITA DE Date: Thu, 1 Oct 2020 09:15:02 +0530 Subject: [PATCH 1/2] Create Huffman_Coding_474.py Python implementation of Huffman Coding using Greedy Approach --- Huffman_Coding_474.py | 58 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Huffman_Coding_474.py diff --git a/Huffman_Coding_474.py b/Huffman_Coding_474.py new file mode 100644 index 0000000..3273104 --- /dev/null +++ b/Huffman_Coding_474.py @@ -0,0 +1,58 @@ +string = 'BCAADDDCCACACAC' + + +# Creating tree nodes +class NodeTree(object): + + def __init__(self, left=None, right=None): + self.left = left + self.right = right + + def children(self): + return (self.left, self.right) + + def nodes(self): + return (self.left, self.right) + + def __str__(self): + return '%s_%s' % (self.left, self.right) + + +# Main function implementing huffman coding +def huffman_code_tree(node, left=True, binString=''): + if type(node) is str: + return {node: binString} + (l, r) = node.children() + d = dict() + d.update(huffman_code_tree(l, True, binString + '0')) + d.update(huffman_code_tree(r, False, binString + '1')) + return d + + +# Calculating frequency +freq = {} +for c in string: + if c in freq: + freq[c] += 1 + else: + freq[c] = 1 + +freq = sorted(freq.items(), key=lambda x: x[1], reverse=True) + +nodes = freq + +while len(nodes) > 1: + (key1, c1) = nodes[-1] + (key2, c2) = nodes[-2] + nodes = nodes[:-2] + node = NodeTree(key1, key2) + nodes.append((node, c1 + c2)) + + nodes = sorted(nodes, key=lambda x: x[1], reverse=True) + +huffmanCode = huffman_code_tree(nodes[0][0]) + +print(' Char | Huffman code ') +print('----------------------') +for (char, frequency) in freq: + print(' %-4r |%12s' % (char, huffmanCode[char])) From a19039a736b75f1c5f6c74e5430c06d485925444 Mon Sep 17 00:00:00 2001 From: ASHMITA DE Date: Thu, 1 Oct 2020 09:23:25 +0530 Subject: [PATCH 2/2] Create Activity_Selection_474.cpp Implementation of Activity Selection Problem using Greedy Approach --- Activity_Selection_474.cpp | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Activity_Selection_474.cpp diff --git a/Activity_Selection_474.cpp b/Activity_Selection_474.cpp new file mode 100644 index 0000000..c1400f5 --- /dev/null +++ b/Activity_Selection_474.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; + +bool comp(pairi,pairj){ +    return i.second>vec(11); +      cout<<"Start time of activities: "<>v; +      v.push_back(vec[0]); +      paircurrent=vec[0]; +      for(int j=1;j<11;j++){ +          if(vec[j].first > current.second){ +              v.push_back(vec[j]); +              current=vec[j]; +          } +      } +      vector>::iterator i; +      cout<<"\nFollowing activities are selected- "<