Greedy Method in Algorithm Design
Greedy Method in Algorithm Design
Chapter -3
Greedy Method
Outline
• Introduction
• Job Sequencing with Deadlines
• Optimal Merge Pattern
• Minimum Spanning Trees
• Single Source Shortest Pattern
Introduction
• The greedy method is an algorithmic paradigm that builds up a solution piece by
piece, always choosing the next piece that offers the most immediate benefit (the
solution — but it doesn’t always work unless the problem has certain properties.
• The greedy method is perhaps the most straightforward design technique, and it
can be applied to a wide variety of problems. Most of these problems have n - inputs
and require us to obtain a subset that satisfies some constraints. Any subset that
❑ You have a bag with limited capacity, and several items with different weights
and values.
1 2
Knapsack Problem
4
3
Knapsack Problem…
❑ Fractional Knapsack (Greedy Method)
Knapsack Problem…
Fractional Knapsack (Greedy Method) – Step by Step
❑ Exercise Step 1: Given Data
Step 2: Compute Profit-to-Weight Ratio (p/w)
Step 3: Sort Items (Descending p/w)
Step 4: Fill the Knapsack
Step 5: Calculate Total Profit
Final Answer (Fractional Knapsack)
Maximum profit
Item 2: is not added ➔ 0 (When the bag is full, no item weight can be selected )
Item 3 is added with the full weight of the complete item, not partially ➔1
Item 4 is added with the full weight of the complete item, not partially ➔1.
Knapsack Problem…
Exercise -2
Obtain the optimal solution for the knapsack problem using the greedy method, given the
Following
M=40 capacity of Knapsack
N=3 number of objects
(𝑤1 = 20, 𝑤2 = 25, 𝑤3 = 10) represents the weights of 3 objects
(𝑝1 = 30, 𝑝2 = 40, 𝑝3 = 35) represents the profits of 3 objects
Knapsack Problem…
Solution
Step 2 – Calculate profit-to-weight ratio
𝑝1 30
Step 1 – Understand the problem 𝑟1 = = = 1.5
𝑤1 20
We have: 𝑝2 40
𝑟2 = = = 1.6
Knapsack capacity 𝑀 = 40 𝑤2 25
𝑝3 35
Number of objects 𝑛 = 3 𝑟3 = = = 3.5
𝑤3 10
Weights: 𝑤1 = 20, 𝑤2 = 25, 𝑤3 = 10 Step 3 – Sort objects in non-increasing order of 𝑟𝑖
𝑟3 = 3.5 𝑤3 = 10𝑝3 = 35 Rank 1
Profits: 𝑝1 = 30, 𝑝2 = 40, 𝑝3 = 35 𝑟2 = 1.6 𝑤2 = 25𝑝2 = 40 Rank 2
Greedy method for knapsack problem usually means profit per 𝑟1 = 1.5 𝑤1 = 20𝑝1 = 30 Rank 3
Order: Object 3 → Object 2 → Object 1 by 𝑟𝑖 .
unit weight (not simply pick highest profit).
Knapsack Problem
Step 4 – Fill knapsack greedily (fractional knapsack allowed)
Step 5 – Total profit
Remaining capacity = 40
Pick Object 3 (w=10, p=35) fully
Total profit = 35 + 40 + 7.5 = 82.5
Taken: 10 units weight
Remaining capacity = 40 − 10 = 30
Step 6 – Optimal solution (fractional knapsack)
Profit so far = 35
We took:
Pick Object 2 (w=25, p=40) fully All of object 3 (10 kg)
Taken: 25 units weight All of object 2 (25 kg)
5
Remaining capacity = 30 − 25 = 5 = 0.25 fraction of object 1 (5 kg)
20
Profit so far = 35 + 40 = 75 Max profit = 82.5
Pick Object 1 (w=20, p=30)
Remaining capacity = 5
5
Can’t take fully. Take fraction: of object 1.
20
5
Fraction profit = × 30 = 7.5
20
Weight taken = 5
Remaining capacity = 0
Knapsack Problem…
❑ Exercise
Knapsack Problem…
❑ Exercise
Job Sequencing with Deadlines
✓ The problem is stated as below. Given an array of jobs where every job has a deadline
and an associated profit if the job is finished before the
There are n jobs to be processed on a machine.
deadline.
Each job i has a deadline dj>0 and profit pi>0 .
Every job takes a single unit of time. So the minimum
pi is earned if the job is completed by its deadline.
deadline for any job is 1.
The job is completed if it is processed on a machine for
a unit time.
Every job takes a single unit of time. So the minimum deadline for any job is 1.
Job Sequencing with Deadlines…
Job Sequencing with Deadlines…
Job Sequencing with Deadlines…
Job Sequencing with Deadlines Problem
Exercise
Obtain the optimal solution for the Job sequencing problem with deadlines where n=6([Link] jobs), Profit(P1, P2, P3, P4, P5, P6)=(200,180,190,300,120,120), and
deadlines(d1,d2,d3,d4,d5,d6)=(5,3,3,2,4,2).
Job Sequencing with Deadlines Problem
Step 7: Select Job 5
▪ Since both Job5 and Job6 have the same profit, they are considered equally desirable. However, Job6's deadline is 2,
and both slots 1 and 2 are already occupied, so Job6 cannot be scheduled.
▪ Job5 has a later deadline of 4, meaning it can be scheduled in any available slot from time 1 through time 4. But if
slots 1 and 2 are full, then the earliest available slot for Job5 would be slot 4, within its deadline.
▪ Result
o Optimal Solution of Job Sequence = 300+200+190+180+120=990
o Total Profit= {J2,J4,J3,J5,J1}
Job Sequencing with Deadlines Problem
Exercise
For the following sequence of jobs, provide a snapshot of the execution that will achieve the
maximum profit.
Given Job J1 J2 J3 J4 J5 J6 J7
Profit 3 5 20 18 0 6 30
Deadline 1 3 4 3 2 1 2
Step 1:Sort/Arrange the jobs in decreasing order of profits
Job J7 J3 J4 J6 J2 J1 J5
Result= ????
Profit 30 20 18 6 5 3 0
Deadline 2 4 3 1 3 1 2
Exercise
1. Obtain the optimal solution for the Job sequencing problem with deadlines where n=4([Link] jobs), Profit(P1, P2, P3, P4)=(100,10,15,27),
and deadlines(d1,d2,d3,d4)=(2,1,2,1).
2. Obtain the optimal solution for the Job sequencing problem with deadlines as given in the table.
Job Profit Deadline
J₁ 35 3
J₂ 30 4
J₃ 25 4
J₄ 20 2
J₅ 15 3
J₆ 12 1
J₇ 5 1
Optimal Merge Pattern Using Greedy Method
▪ It relates to the merging of 2 or more sorted files into a single sorted file.
Algorithm
1. Sort the file in increasing order of length
2. Merge the first 2 file, replace them with the resulting files in the list.
3. Repeat from step 1 till list has only one file.
4. Exit Algorithm: TREE (n)
▪ Take the list of file sizes (or merge costs). 2. [Link]:= least (list) [Link]:= least (list)
▪ Repeat until only one file remains:
3. node. Weight:=(([Link]).weight) + (([Link]).weight) insert (list, node);
Pick the two smallest files.
4. return least (list);
Merge them (cost = sum of their sizes).
Insert the new merged file size back into the list.
3. Challenge:
Files: 50, 20, 10, 15, 25, 40, 5
• Would the cost change if we started merging the largest files first? Justify.
Minimum Spanning Trees(MST)
▪ Tree: A connected graph without cycles. A cycle is a path that starts and ends at the same vertex.
▪ A spanning tree is a subset of Graph G, which has all the vertices covered with the minimum possible number of
edges.
o we conclude that every connected and undirected Graph G has at least one spanning tree.
▪ Let G=(V, E) be an undirected connected graph . A sub graph t=(V, E’) of G is a spanning tree of G iff t is a tree.
Spanning tree is sub-graph of a graph and it will take all vertices of graph G.
Any connected graph with n-vertices must have at least n-1 edges and all are tree.
The cost of spanning tree is the cost of sum of the edges in that tree.
In each case, the MST gives the lowest-cost set of connections that links all required points
Minimum Spanning Trees(MST)
▪ Example
List a Possible Spanning tree of the following
Minimum Spanning Trees(MST)
▪ Example
List a Possible Spanning tree of the following
Minimum Spanning Trees(MST)
▪ Example
List a Possible Spanning tree of the following
Prim’s Algorithm
Prim’s Algorithm
Key Algorithms
[Link]’s Algorithm
Step-6
Exercise
Apply Prim’s Algorithm
Graph Data
[Link] the weighted graph with vertices P1–P6 and edges labeled with distances.
Vertices: P1, P2, P3, P4, P5, P6
[Link] Prim’s Algorithm starting from P1.
Edges with distances (in meters):
[Link] each step:
▪ P1 – P2 = 150 [Link] vertices in MST
▪ P1 – P3 = 200 [Link] edges (connecting MST to outside)
▪ P1 – P4 = 100 [Link] edge (minimum weight)
▪ P2 – P3 = 120 [Link] MST
▪ P2 – P5 = 180 [Link] the final chosen paths and the total minimized distance.
▪ P3 – P4 = 80 [Link] the final MST (connection map of PoliStops).
▪ P3 – P5 = 90
➢ Your task is to choose paths so that all PoliStops are connected, and the total
▪ P4 – P5 = 130
walking distance is minimized.
▪ P4 – P6 = 160
▪ P5 – P6 = 110
Kruskal’s Algorithm
▪ It is faster than Prim’s algorithm.
▪ It generates a minimum cost spanning tree for every connected undirected graph.
▪ It says that always select the minimum cost edges.
▪ Kruskal's Algorithm is used to find the minimum spanning tree for a connected weighted
graph.
▪ The main target of the algorithm is to find a subset of edges by using which we can
traverse every vertex of the graph.
▪ It follows a greedy approach, which finds an optimum solution at every stage instead of
focusing on a total optimum.
Kruskal’s Algorithm Examples
Here is the list of applications where Kruskal's algorithm is commonly used:
[Link]’s Algorithm
[Link] the smallest edge that doesn’t form a cycle (use Union-Find)
Example
Construct a minimum spanning tree of the given graph
Using Kruskal’s Algorithm
Given
[Link] construct the Minimum Spanning Tree using Kruskal’s Algorithm, first draw
all the vertices of the graph.
[Link] these vertices by adding edges in order of increasing weight, ensuring
that no cycle is formed at any step.
➢ Remember: the edge 7 to 4 and edge 7 to 5 form a cycle, so reject it and move to the
next minimum weight.
▪ Kruskal's algorithm is an algorithm that produces a Minimum Spanning Tree (MST) of a given
connected, weighted graph.
▪ It is a greedy MST algorithm.
❑ The main difference from Prim's algorithm is that :
▪ Prim's algorithm grows a single tree from a starting vertex.
▪ In contrast, Kruskal's algorithm grows a collection of trees (a forest) and gradually merges them
until a single spanning tree remains.
Single Source Shortest Pattern (SSSP)
[Link] initial node as current and mark all other nodes as unvisited.
[Link] current node, consider all unvisited nodes and calculate tentative distance. Compare
current distance with calculated distance and assign the small value.
[Link] all the neighbors are considered of the current node, mark it visited, ‘visited node’ is
never checked again
[Link]
Dijkstra's Algorithm
Dijkstra's Algorithm
▪ Given that node A is the source node, what are the distances of the shortest paths
from the source node to all other nodes using Dijkstra’s shortest path algorithm.
Step -01
The source node has a distance of 0.
Step -02
➢ If we have found a new shorter distance to one of the unvisited nodes. So B and D are unvisited nodes, and
the current distance is infinity. So we can update the distance from A to B to 2. Store A as a previous node in
the table and A to D to 8.
➢ For the Current node, calculate the distance to all unvisited neighbors
▪ Update the shortest distance if the new distance is shorter than the old distance.
➢ For the Current node, calculate the distance to all unvisited neighbors
▪ Update the shortest distance if the new distance is shorter than the old distance.
Step -08 Choose the new current node from the unvisited nodes with the minimal distance.
➢This means E is the current node
Dijkstra's Algorithm…
Step -09
➢ For the Current node, calculate the distance to all unvisited neighbors
▪ Update the shortest distance if the new distance is shorter than the old distance.
Dijkstra's Algorithm…
Step -09 Choose the new current node from the unvisited nodes with the minimal distance.
➢This means F is the current node
Dijkstra's Algorithm…
Step -09 Choose the new current node from the unvisited nodes with the minimal distance.
➢This means F is the current node
Dijkstra's Algorithm…
Step -09 Choose the new current node from the unvisited nodes with the minimal distance.
➢This means C is the current node
Dijkstra's Algorithm…
Step -09
➢There is no unvisited node of neighbor of C
Dijkstra's Algorithm…
Step -09
➢The shortest path from A to C is :
A➔B➔D➔F➔C
Exercise
Find the shortest path from vertex A to every other vertex Using Dijkstra's Algorithm…
Floydwrashall
Floyd Warshall: The advantage of this algorithm is used to handle negative weighted graphs.
✓The all pairs shortest path problem is to determine a matrix A such that A (i, j) is the
length of a shortest path from i to j.
✓The matrix A may be obtained by solving n single source problems using the procedure
shortest_paths.
✓Another alternate solution will require a weaker restriction on edge costs than required by
shortest _paths.
✓It uses negative weights without negative-weight cycles. The general formula:
Floydwrashall
▪ The main idea behind the Floyd-Warshall algorithm is to gradually build up all
intermediate routes between nodes i and j to find the optimal path.
▪ The Floyd-Warshall algorithm is a dynamic programming algorithm (not purely greedy) used to find
the shortest paths between all pairs of vertices in a weighted graph. It works for both directed and
undirected graphs, and can handle negative weights
l solution from i to j is simply the distance in the adjacency matrix.
𝑑𝑝 𝑘 𝑖 𝑗 = 𝑚 𝑖 𝑗 𝑖𝑓 𝑘 = 0
Find the shortest path of all edge using Floydwrashall
Step 1: The diagonal of the matrix is set to 0, and the rest is put according to the adjacency.
Step 4: Copy Row 3 and Column 3 from D(2). Step 5: Copy Row 4 and Column 4 from D(3).
0 3 5 7 0 3 5 6 0 3 5 6
8 0 2 15 7 0 2 3 5 0 2 3
A2= A3= A4=
5 8 0 1 5 8 0 1 3 6 0 1
2 5 7 0 2 5 7 0 2 7 5 0
1 0 3 8 − 4 0 3 8 − 4
7
2 0 1 7
0 1
4 0
4 0
3 2 5 −5 0 − 2
2 −5 0
4
6 0
0
5 6
1st row and 1st column are the same with A(0) and A(1)
A(3)=
75
0 3 8 4 − 4
0 3 8 4 − 4 0 1 7
0 1 7
A(2)= 4 0 5 11
4 0 5 11
−5
− 2 2 −1 −5 0 − 2
2 5 0
6 0
6 0
Note: if A(1) there is ∞ signKin the 1st row and k-1 same with A(0) for all rows.
col then thek-1value is thek-1
Use formula: A [i, j] =min {A [i, j], A [i, k] +A [k, j]}
Cont’d
0 3 −1 4 − 4 0 1 −3 2 − 4
3 3 0 −4 1
− 1
0 −4 1 − 1
7 4 0 5 3 A(5)= 7 4 0 5 3
A(4)=
2 −1 −5 0 − 2 2 −1 −5 0 − 2
8 5 1 6 0
8 5 1 6 0
76
1-Jan-26
Cont’d
Algorithm:
Note: The advantage of this algorithm is used to handle the negative weighted graphs
77
THANK YOU