0% found this document useful (0 votes)
6 views

Greedy_Algorithms(2)

The document discusses the Greedy Technique Algorithm, which solves optimization problems by making a series of local optimal choices. It includes examples such as the Fractional Knapsack Problem, Huffman Coding, Prim's Algorithm, Kruskal's Algorithm, and Dijkstra's Algorithm, highlighting their applications and how they utilize the greedy approach. Additionally, it outlines an assignment for students to implement and explain two of the discussed algorithms using the greedy method.

Uploaded by

jiieyoh12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Greedy_Algorithms(2)

The document discusses the Greedy Technique Algorithm, which solves optimization problems by making a series of local optimal choices. It includes examples such as the Fractional Knapsack Problem, Huffman Coding, Prim's Algorithm, Kruskal's Algorithm, and Dijkstra's Algorithm, highlighting their applications and how they utilize the greedy approach. Additionally, it outlines an assignment for students to implement and explain two of the discussed algorithms using the greedy method.

Uploaded by

jiieyoh12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Greedy Technique Algorithm

If you are at SM with ₱100 and you're hungry,


how would you spend it?
Buy the cheapest food to get more quantity.

Buy the tastiest food, even if it's expensive.

Plan ahead and buying balanced meal.


What is Greedy Algorithms
• solving an optimization problem which requires the result to be
either maximum or minimum
• typically consist of an iterative procedure that tries to find a local
optimal solution
• a greedy algorithm arrives at a solution by making a sequence of
choices, each of which simply looks the best at the moment.
Optimization Problems
• An objective function that you are maximizing or minimizing
• A set of constraints that have to be satisfied
Example: Selection Problem
Scenario: Problem Statement:
You and your classmates have • There are N assignments, each
multiple assignments with with a deadline and time
different deadlines. Some take a required to complete.
lot of time, while others are • You need to finish as many
quick to finish. You want to assignments as possible
submit the most assignments before deadlines.
on time.
• You can only work on one
assignment at a time.
Greedy Knapsack Problem (Fractional Knapsack)
• Classic optimization problem where you have:
• A knapsack (bag) with a maximum weight capacity.
• A set of items, each with:
• A weight
• A value (profit)
• Goal → maximize the total value while ensuring the total weight does
not exceed the knapsack’s capacity.
• Fractional
• You can take fractions of an item instead of taking it whole.
• The Greedy Strategy is used:
• Sort items by their value-to-weight ratio (profit per weight unit).
• Take as much of the highest ratio item first until the knapsack is full.
• This problem has an optimal greedy solution (always finds the best answer).
Knapsack: Problem Statement
• We will have n objects and each object will have a
value.
• There will be a maximum capacity W of the
knapsack.
• We need to select the objects such that the total
weight of the objects does not exceed the
maximum value W.
• While selecting the objects, we need to select
them in such a way that the total value is
maximized, that is, price is maximized.
• The objects can be divided in fractions, hence the
name is fractional knapsack. As the object is
divided into fractions, the value of the object will
be divided in the same fraction.
Example
Problem Statement:
We have 5 objects having weights {30,50,10,70,40} and the price of the respective weights as
{150,100,90,140,120}. Given the maximum capacity of the knapsack as 150, find the set of
objects to be selected such that the price is maximized.

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

You might also like