Greedy_Algorithms(2)
Greedy_Algorithms(2)
https://medium.com/@riya.tendulkar/how-to-solve-the-fractional-knapsack-problem-d2c11b56aa38
Example: Greedy by Price
Example: Greedy by Weight
Why Greedy Fails in 0/1 Knapsack
• We must take whole items (0/1 constraint).
• Greedy approach picks the most valuable item first.
• But it may leave no space for a better combination.
• A combination of smaller items is better than one big item.
Other Problems that Uses Greedy Technique
• Huffman Coding
• Prim’s Algorithm
• Kruskal’s Algorithm
• Dijkstra’s Algorithm
Huffman Coding
• First developed by David Huffman
• Data compression to reduce its size without losing any of the
details
• It assigns shorter binary codes to more frequent characters
and longer codes to less frequent characters to minimize the
total bits needed to store data.
• How does it use Greedy?
• It always chooses the two least frequent characters first to build the
encoding tree.
• Ensuring the total encoding length remains minimal.
Example: Encoding ABRACADABRA
Step 1: Count Frequency Step 2: Build Huffman Tree Step 3: Assign codes
Character Frequency Character Huffman Code
A 5 11 A 0
B 2 0 1 B 10
R 2 A(5) 6 R 111
C 1 0 1 C 1100
D 1 D 1101
B(2)
4
Original (8-bit ASCII): 0 1 Huffman Encoded:
88 bits 01011101100011010101110
2 R(2)
23 bits
0 1
C(1) D(1)
Application: Huffman Encoding
• Data Compression • Compiler Design
• ZIP, RAR, 7-Zip, GZIP, PNG • Token compression, syntax trees
• Text Compression • AI & Machine Learning
• PDF, Emails, HTTP requests • Model compression, feature
selection
• Image & Video Compression
• JPEG, PNG, MPEG • Cryptography
• Steganography, entropy-based
• Network Transmission encryption
• Webpage compression, mobile
data reduction • Bioinformatics
• DNA sequence compression
Prim’s Algorithm
• Finds the Minimum Spanning
Tree (MST) but grows the tree
starting from any node.
• How does it use Greedy?
• It greedily adds the smallest
edge that connects a new node
to the existing tree.
• It builds one tree instead of
merging sets
Example: Prim’s Algorithm
Real World Applications
• Electric grid design
• Power distribution where connections are closely packed.
• Computer network design
• Building the cheapest LAN (Local Area Network) that connects
all computers.
• Navigation systems (GPS)
• Finding the minimum-cost way to lay roads in urban areas with
many intersections.
Kruskal’s Algorithm
• finds the Minimum Spanning
Tree (MST), the smallest total
weight tree connecting all
nodes.
• How does it use Greedy?
• It greedily picks the smallest
edge first.
• It avoids cycles to keep a tree
structure.
Example: Kruskal’s Algorithm
Real World Applications
• Road and pipeline networks
• Choosing the shortest paths between cities in a country with scattered
towns.
• Network clustering
• Finding groups of connected nodes in social networks or machine
learning (hierarchical clustering).
• Optimizing fiber optic networks when cables connect distant
locations.
Difference between Prim and Kruskal
Feature Prim’s Kruskal’s
Approach Start from one node, expand Sort edges, pick the smallest
by picking the smallest edge edge while avoiding cycles
that connects new nodes
Best for? Dense graphs (many edges) Sparse graphs (few edges)
Data Structure used Priority Queue (Min Heap) Disjoint Set (Union-Find)
Graph type Requires a connected graph Works well with disconnected
graphs (can handle multiple
components)
Time Complexity O(E log V) (Using Min Heap) O(E log E) (Sorting edges)
Dijkstra’s Algorithm
• Finds the shortest path from a
single source node to all other
nodes in a weighted graph
(nonnegative only)
• How does it use Greedy?
• It greedily picks the shortest
known path at every step.
• It never revisits nodes with
shorter paths.
Example
Real World Applications
• GPS & Navigation (Google Maps, Waze) • Public Transport (Train & Subway
• Finds the fastest route by calculating the Systems)
shortest travel time. • Determines the quickest route between
• Network Routing (Internet, OSPF stations.
Protocols) • Power Grid Optimization
• Optimizes data packet delivery through the • Reduces energy loss by optimizing electricity
shortest network path. distribution.
• Logistics & Delivery (Lalamove, Grab) • Emergency Services (Ambulances,
• Minimizes travel distance for efficient Firefighters)
deliveries. • Finds the fastest way to accident sites for
• Video Games (Pathfinding AI) quick response.
• Helps NPCs and enemies find the fastest way
to their targets.
Assignment
• Choose 2 problems/algorithms from the ones discussed in our lessons:
• Activity Selection Problem
• Fractional Knapsack Problem
• Prim’s Algorithm (Minimum Spanning Tree - MST)
• Kruskal’s Algorithm (Minimum Spanning Tree - MST)
• Dijkstra’s Algorithm
• Huffman Encoding (or Decoding)
• Write a program to solve each of your chosen problems using the Greedy approach.
• Record a video (5-10 minutes) explaining:
• How the algorithm works (step-by-step explanation).
• How you implemented it in code (walk through your code structure).
• Challenges you faced and how you solved them (if applicable).
• Upload your submission on our NeoLMS, including:
• Source code with comments/documentation
• Video recording