Module 2B - Greedy - Final
Module 2B - Greedy - Final
(DAA)
Module 2b
(greedy Applications)
1
Table of Content Module 3
• Greedy method:
– definition,
– characteristics , difference with DP,
– Fractional Knapsack algorithm with example,
– job sequencing with deadline algorithm, time complexity, examples
• Spanning tree & Minimal Spanning Tree
– definition,
– kruskal’s algorithm with time complexity, examples
– prim’s algorithm with time complexity, examples
GREEDY TECHNIQUE
• A greedy algorithm, as the name suggests, always makes the choice that
seems to be the best at that moment. This means that it makes a
locally-optimal choice in the hope that this choice will lead to a
globally-optimal solution.
• Assume that we have an objective function that needs to be optimized
(either maximized or minimized) at a given point.
• A Greedy algorithm makes greedy choices at each step to ensure that the
objective function is optimized.
• The Greedy algorithm has only one shot to compute the optimal solution so
that it never goes back and reverses the decision.
Analysis
If the items are already sorted into decreasing order of vi / wi, then the
while-loop takes a time in O(n). Therefore, the total time including the
sort is in O(n log n).
Greedy: Fractional Knapsack problem
• Example 1: Consider 5 items along their respective weights and
values are
wi =(5, 10, 20, 30, 40) and
vi =(30, 20, 100, 90, 160).
The capacity of Knapsack W=60. Find the solution to the
fractional knapsack problem. [Ans: The profit earned is 270]
• Example 2: Consider the following Table that consists of some
items with weights and cost values.
Items I1 I2 I3 I4 I5
Weights 5 10 15 22 25
Cost 30 40 45 77 90
Points to remember
---------------------------------------------------------------------------------------------------------------------
1. In this problem we have n jobs j1, j2, … jn each has an associated deadline d1, d2, … dn
and profit p1, p2, ... pn.
2. Profit will only be awarded or earned if the job is completed on or before the deadline.
3. We assume that each job takes unit time to complete.
4. The objective is to earn maximum profit when only one job can be scheduled or
processed at any given time.
---------------------------------------------------------------------------------------------------------------------
Consider the following 5 jobs and their associated deadline and profit.
job id deadline profit
j1 2 60
j2 1 100
j3 3 20
j4 2 40
j5 1 20
Greedy: Job sequencing Problem
Algorithm: Greedy-Job-sequencing(P, D, n, FS, Profit)
Steps: Analysis: Time complexity of job
1. D[0]🡪FS[0]🡪0 //Initialisation and 1st job inclusion sequencing algorithm is O(n2)
1. FS[1]🡪1
2. K🡪1
3. Profit🡪0
2. For i🡪2 to n Do //For feaseible solution
1. r🡪k
2. While (D[FS[r]]>D[i]) and (D[FS[r]]≠r) Do
1. r🡪r-1
2. If D[FS[r]]≤D[i] and D[i]>r then //Checking deadline violation
1. For m<--k to (r+1) step by -1 Do
1. FS[m+1]🡪FS[m]
2. FS[m+1]🡪I
3. K🡪k+1
3. For i🡪1 to k Do
1. Profit🡪Profit+P[FS[i]]
2. Write FS[i] //Display job sequence
4. Write “Maximum Profit Earned”, Profit. //Display profit
5. Return
Greedy: Job sequencing Problem
Example 1: Find the feasible solution for the following list of jobs
and also find the total profit earned. Assume that each job needs
one unit time in a single machine.
Job No. I II III IV V
Profit 50 30 10 20 15
Deadline 2 1 3 2 4
Max(deadline of all jobs)=Max(2,1,3,2,4)=4. Four times slot will be there i.e. at most 4
jobs will be completed within their deadlines
Greedy: Job sequencing Problem
Now, considering the new job numbering in the above table, jobs are
scheduled as-
Job I is allotted in [0, 1] time slot.
Time Slot 0-1 1-2 2-3 3-4 4-5
New Job No. 1
Deadline 2
Old Job No. 1
Thus, the feasible solution =< I, II, III, V > [new job no.]
Execution sequence = < II, I, III, V > [new job no.]
Profit earned = Profit[II] + Profit[I] + Profit[III] + Profit[V]
= 30 + 50 + 10 + 15
= 105
MCQ
• Fractional knapsack problem is also known as __________
a) 0/1 knapsack problem
b) Continuous knapsack problem
c) Divisible knapsack problem
d) Non continuous knapsack problem
• Fractional knapsack problem is solved most efficiently by which of the
following algorithm?
a) Divide and conquer
b) Dynamic programming
c) Greedy algorithm
d) Backtracking
• What is the objective of the knapsack problem?
a) To get maximum total value in the knapsack
b) To get minimum total value in the knapsack
c) To get maximum weight in the knapsack
d) To get minimum weight in the knapsack
MCQ
• Time complexity of fractional knapsack problem is ____________
a) O(n log n)
b) O(n)
c) O(n2)
d) O(nW)
• Given items as {value,weight} pairs {{40,20},{30,10},{20,5}}. The capacity of
knapsack=20. Find the maximum value output assuming items to be
divisible.
a) 60
b) 80
c) 100
d) 40
• The main time taking step in fractional knapsack problem is ___________
a) Breaking items into fraction
b) Adding items into knapsack
c) Sorting
d) Looping through sorted items
MCQ
• Given items as {value,weight} pairs {{60,20},{50,25},{20,5}}. The capacity of
knapsack=40. Find the maximum value output assuming items to be divisible and
nondivisible respectively.
a) 100, 80
b) 110, 70
c) 130, 110
d) 110, 80
• job sequencing with deadline is based on ____________method
a. greedy method
b. branch and bound
c. dynamic programming
d. divide and conquer
• Greedy job sequencing with deadlines algorithms’ complexity is defined as
a. O(N)
b. Ω( n log n)
c. O (n2 log n)
d. O ( n log n)
Greedy: Spanning Tree
Spanning Tree:
– A spanning tree of a graph is any tree that includes every vertex in the
graph.
– A spanning tree of a graph G is a subgraph of G that is a tree and contains all
the vertices of G. An edge of a spanning tree is called a branch; an edge in
the graph that is not in the spanning tree is called a chord.
– Given a connected undirected graph, a spanning tree of that graph is a
subgraph that is a tree and joined all vertices. A single graph can have many
spanning trees.
Greedy: Spanning Tree
Greedy: Minimum Spanning Tree
A minimum spanning tree (MST) of a weighted graph G is a spanning tree of G
whose edges sum is minimum weight. Let G=(V, E) be a connected, undirected
graph where V is a set of vertices (nodes) and E is the set of edges. Each edge has a
given non negative length.
In other words, a MST is a tree formed from a subset of the edges in a given
undirected graph, with three properties:
✔ It spans the graph, i.e., it includes every vertex of the graph without form of
any cycle.
✔ It is a minimum, i.e., the total weight of all the edges is as minimum as
possible.
✔ Connects all vertices in V
1.Kruskal's Algorithm
2.Prim's Algorithm
Greedy: Prime’s
• Prim's Algorithm is used to find the minimum spanning tree from a graph.
Prim's algorithm finds the subset of edges that includes every vertex of the
graph such that the sum of the weights of the edges can be minimized.
• Prim's algorithm starts with the single node and explore all the adjacent nodes
with all the connecting edges at every step. The edges with the minimal
weights causing no cycles in the graph got selected.
•Prime’s algorithm for finding the minimum
spanning tree of a given weighted graph G.
Steps:
1.Select a starting vertex
2.Select an edge e connecting the tree
vertex and fringe vertex that has
minimum weight
3.Add the selected edge and the vertex
to the minimum spanning tree T
4.Repeat 2 until all vertices are
connected
5.Exit
Greedy: Prime’s Example
Construct a minimum spanning tree of the graph given in the following
figure by using prim's
Solution
Step 1 : Choose a starting vertex B.
• Consider a complete graph G with 4 vertices. The graph G has ____ spanning
trees.
a) 15
b) 8
c) 16
d) 13
References
• https://youtu.be/-Cg-aL1D8CM
• https://youtu.be/MIvWBI8Xlyk
• https://youtu.be/7FtGk9yr66A
• https://youtu.be/M9FMtiCvvhs
Thank you !
H. Tunga 40