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

ADA_Greedy Method (1)

The document discusses the Greedy Method, a straightforward algorithm design technique used to solve various problems such as the Knapsack Problem, Job Sequencing with Deadlines, and Single Source Shortest Path. It outlines the algorithm's approach of making optimal choices at each step to reach a feasible solution that maximizes or minimizes an objective function. Additionally, it provides examples and applications of the Greedy Method in different scenarios, including pseudocode for implementing the algorithms.

Uploaded by

Adiba Fatima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

ADA_Greedy Method (1)

The document discusses the Greedy Method, a straightforward algorithm design technique used to solve various problems such as the Knapsack Problem, Job Sequencing with Deadlines, and Single Source Shortest Path. It outlines the algorithm's approach of making optimal choices at each step to reach a feasible solution that maximizes or minimizes an objective function. Additionally, it provides examples and applications of the Greedy Method in different scenarios, including pseudocode for implementing the algorithms.

Uploaded by

Adiba Fatima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

Greedy Method

Objectives
• Greedy Method
• Applications
– Optimal Storage on Tapes
– Job Sequencing with Deadlines
– Knapsack Problem
– Single Source Shortest Path
• Previous Gate Questions
Greedy Method
• The Greedy method is a most straight forward design
technique which can be applied to a wide variety of
problems.
• This algorithm works in steps. In each step it selects the
best available options until all options are finished.
• Most of the problems have n inputs and require us to
obtain a subset that satisfies some constraints.
• Any subset that satisfies these constraints is called as a
feasible solution.
• A feasible solution that either minimizes or maximizes
a given objective function is called as Optimal
Solution. 3
Greedy
Algorithm
• The Greedy method suggest that one can devise an
algorithm that work in stages, considering one input at a
time.
• At each stage, a decision is made regarding whether a
particular input is an optimal solution or not.
• This is done by considering the inputs in an order
determined by some selection procedure.
• If the inclusion of the next input into the partially
constructed optimal solution results sub-
optimal/infeasible solution, then that input is not added to
the partial solution. Otherwise, it is added. The selection
procedure itself is based on some optimization measures.
Control Abstraction for Greedy
Method

Select selects an input from a[] and removes it. The selected input’s
value is assigned to x.
Feasible is a Boolean-valued function that determines whether x can be included
into the solution vector or not.
Union combines x with the solution and updates the objective function.
Types of Greedy Problems
• Subset Paradigm
• To solve a problem (or possibly find the
optimal/best solution), greedy approach generate
subset by selecting one or more available choices.
Eg. includes Knapsack problem, job sequencing
with deadlines. In both of the problems greedy
create a subset of items or jobs which satisfies all
the constraints.
• Ordering Paradigm
• In this, greedy approach generate some
arrangement/order to get the best solution. Eg.
includes: Minimum Spanning tree
Applications
• Fractional knapsack algorithm
• Optimal Storage on tapes
• Job sequencing with deadline
• Single source shortest path
– Dijkstra's SSSP algorithm
• Activity Selection Problem
• Minimum Cost Spanning Tree
• …
Knapsack Problem
• Let, we are given n objects and a Knapsack
or Bag.
• Object i has weight Wi and the Knapsack has
a capacity M.
• if a fraction Xi of object i is placed into
Knapsack, then a profit of PiXi is earned.
• The objective is to obtain a filling of
Knapsack that maximizes the total profit
earned.
• Maximiz (A)
e
• Subject (B)
• to

• And 0 ≤ Xi ≤ 1, 1 ≤i ≤n (C)
• The profit and weights are the positive numbers.
• Here, A feasible solution is any set (X1, X2,
…, Xn) satisfying above rules (B) and (C).
• And an optimal solution is feasible solution
for which rule (A) is maximized.
Here, N=3, M=20, (P1, P2, P3)=(25, 24, 15) and (W1, W2, W3)=(18, 15,
10)
Different feasible solutions are:
(X1, X2,
X3)
1. (1/2, 1/3, ¼) 16.5 24.25
2. (1, 2/15, 0) 20 28.2
3. (0, 2/3, 1) 20 31
4. (0, 1, 1/2) wrt 20 31.5
pi/wi
5. (1/2, 2/3, 1/ 10) 20 30
6. (1, 0, 2/10) 20 28
• Of these Six feasible solutions, solution 4 yields the maximum profit.
Therefore solution 4 is optimal for the given problem instance.
• Consideration 1 - In case the sum of all the weights is ≤ M, then
Xi=1, 1 ≤ i ≤n
• is an optimal solution.
Consideration 2 - All optimal solutions will fill the knapsack exactly.
The knapsack algorithm
• The greedy algorithm:
Step 1: Sort pi/wi into nonincreasing order.
Step 2: Put the objects into the knapsack according
to the sorted sequence as possible as we can.
• e. g. p[i]/w[i]≥p[i+1]≥w[i+1]
n = 3, M = 20, (p1, p2, p3) = (25, 24, 15)
(w1, w2, w3) = (18, 15, 10)
Sol: p1/w1 = 25/18 = 1.39
p2/w2 = 24/15 = 1.6
p3/w3 = 15/10 = 1.5
Optimal solution: x1 = 0, x2 =
1, x3 = 1/2
total profit = 24 + 7.5 =
31.5
Algorithm
• Algorithm GreedyKnapsack(m,n)
//order the n objects such that p[i]/w[i]≥p[i+1]≥w[i+1]
{
for i:=1 to n do x[i]:=0.0;
U:=m;
for i:=1 to n do
{
if(w[i] > U) then break;
x[i]:=1.0; U:=U-w[i];
}
if( i ≤ n) then x[i]:=U/w[i];
}
Example problem
• N=7
• M=15(15-1=14-2=12-4=8-5=3-1=2)
• (p1,p2…p7)=(10,5,15,7,6,18,3)
• (w1,w2..w7)=(2,3,5,7,1,4,1)
• The solution vector is (1,2/3,1,0,1,1,1)
• P1/w1=5 p2/w2=1.66 p3/w3=3 p4/w4=1
p5/w5=6 p6/w6=4.5 p7/w7=3
• x5,x1,x6,x3,x7,x2,x4
• Weight is
1+2+4+5+1+2/3*3+0=13+2/3*3=15
• Profit is 6+10+18+15+3+2/3*5+0=55.34
(1,2/3,1,0,1,1,1) 18
Job Sequencing with Deadlines
• We are given a set of n jobs.
• Di is a deadline given to complete ithjob for profit Pi where Pi>0 &
Di ≥ 0.
• For any job i profit Pi is earned iff the job is completed within
its deadline.
• To complete a job, one has to process the job on a machine for
one unit of time.
• Only one machine is available for processing jobs.
• A feasible solution for this problem is a subset J of jobs such
that each job in this subset can be completed by its deadline.
• The value of a feasible solution J is the sum of the profits of the
job in J.
• An optimal solution is a feasible solution with maximum value.
• Example –Suppose on a single machine four jobs
with profit values (100, 10, 15 and 27) and their
respective deadline unit values (2, 1, 2, 1) are
given. Calculate the different feasible solutions to
complete the jobs with optimal solution.
• Solution:
• Number of Jobs (n)= 4
• Profit values of four jobs (P1, P2, P3, P4)=(100, 10, 15,
27)
• Process deadlines for respective jobs are
(D1, D2, D3, D4)=(2, 1, 2, 1)
(P1, P2, P3, P4)=(100, 10, 15, 27) (D1, D2, D3, D4) =(2, 1, 2,
1)
The feasible
Feasible sol. solutions
subset and their values
Processing are –
Sequence Values
(1, 2) (2, 1) 110
(1, 3) (1, 3 or 3, 1) 115
(1, 4) (4, 1) 127
(3, 2) (2, 3) 25
(3, 4) (4, 3) 42
(1) (1) 100
(2) (2) 10
(3) (3) 15
(4) (4) 27
Here, Solution 3 is optimal solution. In this solution only job 1 and 4
are processed and the profit value is 127. These jobs must be
processed in the order Job 4 followed by job 1. Thus the
processing of job 4 begins at time zero and that of job 1 is
completed at time 2.
Algorithm

• The algorithm constructs an optimal set J of jobs that


can be processed by their deadlines. 22
Single-Source Shortest Path
(Dijkstra's algorithm)
Single-Source Shortest Path Problem

Single-Source Shortest Path Problem - The


problem of finding shortest paths from a
source vertex v to all other vertices in
the graph.
Dijkstra's algorithm
Dijkstra's algorithm - is a solution to the single-
source shortest path problem in graph theory.

Works on both directed and undirected


graphs. However, all edges must have
nonnegative weights.

Approach: Greedy

Input: Weighted graph G={E,V} and source


vertex v∈V, such that all edge weights are
nonnegative

Output: Lengths of shortest paths (or the


shortest paths themselves) from a given
source vertex v∈V
to all other vertices
Dijkstra's algorithm - Pseudocode

dist[s] ←0 (distance to source vertex


for all v ∈ V–{s} is zero)
do dist[v] ←∞ (set all other distances to infinity)
S←∅ (S, the set of visited vertices is initially
Q←V empty) (Q, the queue initially contains all
while Q vertices)
≠∅ (while the queue is not empty)
do u ← (select the element of Q with the
S←S∪{u}
mindistance(Q,dist) min.
distance)
Q=Q-{u} (add u to list of visited
vertices) for all v ∈ neighbors[u]
do if dist[v] > dist[u] + (if new shortest path
found) then
w(u, v) d[v] ←d[u] + w(u, v) (set new value of
(if desired,
shortest path) add traceback
code)
return dist
Dijkstra Animated
Example
Dijkstra Animated
Example
Dijkstra Animated
Example
Dijkstra Animated
Example
Dijkstra Animated
Example
Dijkstra Animated
Example
Dijkstra Animated
Example
Dijkstra Animated
Example
Dijkstra Animated
Example
Dijkstra Animated
Example
Implementations and Running
Times
The simplest implementation is to store vertices in
an array or linked list. This will produce a running
time of
O(|V|^2 + |E|)
Where |E| is for search needed to find the minimum
distance node. //A node may be connected to a
maximum of E nodes
For sparse graphs, or graphs with very few edges
and
many nodes, it can be implemented more
efficiently storing
the graph in an adjacency list using a binary
heap or priority queue. This will produce a
running time of
O((|E|+|V|) log |V|) //inner loop is replaced by
a heap
DIJKSTRA'S ALGORITHM - WHY USE
IT?
• As mentioned, Dijkstra’s algorithm calculates
the shortest path to every vertex.
• Therefore, any time we want to know the
optimal path to some other vertex from
a determined origin, we can use
Dijkstra’s algorithm.
Exampl
e
• Vertex 1 is the source

1 2 3 4 5 6 7
0 inf inf inf inf inf Inf
25 inf inf inf inf inf
35 39 inf inf inf
39 inf 51 Inf
inf 51 inf
93 65
93
Previous Gate Questions
Q. No. 1

Queue takes linear time


Q. No. 2

Answer is 16

16

60/10 =6
28/7 =4

20/4 =5

24/2 =12
X4, x1, x3, x2 24+20=44

Vopt= using dynamic programming is 6420


Q. No. 3
Q. No. 4

Answer: None of the Above


Q. No. 5
Q. No. 6
Q. No•. 7 Given below are some algorithms, and some algorithm design paradigms.
List-I
A. Dijkstra’s Shortest Path 3
B.Floyd-Warshall algorithm to compute all pairs shortest path
2 C. Binary search on a sorted array
D. 1 Backtracking search on a graph
List-II 4
1. Divide and Conquer
2. Dynamic Programming
3. Greedy design
4. Depth-first search
5. Breadth-first search
Match the above algorithms on the left to the corresponding design paradigm they follow Codes:
ABCD
(a) 1 3 1 5
(b) 3 3 1 5
(c) 3 2 1 4
(d) 3 2 1 5
Options:
A) a
B) b
C) c
D) D Answer C 48
Q. No. 9
Q. No. 10

You might also like