Lesson 8.1: Activity Selection

The Activity Selection problem involves choosing the maximum number of non-overlapping activities from a set of activities that each have a start and finish time. This is a classic example of the greedy approach where at each step, we pick the activity that finishes the earliest.

📌 Key Points

🧪 Examples

# Activity selection example activities = [(1, 3), (2, 5), (4, 7), (6, 9), (8, 10)] activities.sort(key=lambda x: x[1]) selected = [activities[0]] for i in range(1, len(activities)): if activities[i][0] >= selected[-1][1]: selected.append(activities[i]) print(selected)
# Output: [(1, 3), (4, 7), (8, 10)]

Lesson 8.2: Huffman Coding

Huffman Coding is a compression algorithm used to reduce the size of data. It assigns variable-length codes to input characters, with shorter codes assigned to more frequent characters. This is widely used in file compression formats such as ZIP and JPEG.

📌 Key Points

🧪 Examples

# Huffman coding skeleton using heapq import heapq from collections import Counter, namedtuple Node = namedtuple("Node", ["freq", "char", "left", "right"]) def huffman(freq_map): heap = [[freq, Node(freq, char, None, None)] for char, freq in freq_map.items()] heapq.heapify(heap) while len(heap) > 1: freq1, left = heapq.heappop(heap) freq2, right = heapq.heappop(heap) merged = Node(freq1 + freq2, None, left, right) heapq.heappush(heap, [freq1 + freq2, merged]) return heap[0][1] freq_map = {'a': 5, 'b': 9, 'c': 12, 'd': 13, 'e': 16, 'f': 45} huffman(freq_map)
# Output is a Huffman tree, traversal gives codes