Anda di halaman 1dari 11

Session 3

Session Name: Greedy Technique


Author Name: G.Kanagasabapathy

Session Objectives
At the end of this session, the learner will be able to:
Explain the control abstraction with respect to greedy Algorithms.
Identify and classify the various problems under greedy design technique.
Clearly distinguish the greedy method with the divide and conquer
method.
Apply the greedy methodology to the identified problems.

Teaching Learning Material


Board
Presentation
Bins and Paper Slips

Page 2 Ver 1:.01

Greedy Technique
G.Kanagasabapathy AAMCE

Session Plan
Time
(in min)

10

10

10

Content

Greedy Method

Greedy method-an
Introduction

Identifying the
Problems Under
Greedy Method

An example for
greedy method

10

Comparison between
divide and conquer
and greedy algorithm

10

Conclusion and
summary

Greedy Technique
G.Kanagasabapathy AAMCE:

Learning Aid
and
Methodology

Analogy

Faculty
Approach

Narrates

Board Activity
Presentation

Explains

PPT

Facilitates
Monitors

Board Activity
Presentation

Board Activity

Pick and place

Explains

Explains

Listen
Facilitates

Typical Student
Activity

Listens
Understands

Listens
Participates

Participates

Listen
participates

Listen
participates

Participates
Identifies

Skill and
Competency
Developed

Knowledge
Comprehension

Knowledge
Comprehension
Intrapersonal
Interpersonal
Visual
Spatial
Linguistic

Knowledge
Comprehension
Intrapersonal
Interpersonal

Knowledge
Comprehension
Intrapersonal
Interpersonal
Linguistic

Knowledge
Comprehension
Intrapersonal
Interpersonal
Linguistic

Knowledge
Comprehension
Intrapersonal
Interpersonal

Page 3

Session Inputs
Greedy Method
Since the learners are already familiar with the concept, the best
way is to start the session by giving analogy.
A dog was going towards the forest holding a piece of bread in
his mouth. On his way, while he was crossing a pond of water, he
saw his own reflection. He thought that there was another dog in
the water, who holding another piece of bread in his mouth. He
thought, "If I grab his piece of bread, I would have more bread".
So when he opened his mouth to grab the piece of bread, his
piece, dropped into the water. He felt very sad. The one, who
wants the whole, leaving the half, gets nothing.
We are not debating if the greedy method is good or bad, but it
is true that it is an important algorithm design technique which
scans the list of inputs and adds to the existing solution, only if the
input satisfies the problem constraints. Thus, it will have a greedy
grab on every possible input.

Greedy Method: An Introduction


Having explained the analogy, we can now discuss and
introduce the greedy technique with the help of a blackboard or
through a presentation.
We can show the following presentation while introducing the Greedy
Method.

D:\Thanjavur\Anjalai
Ammal Mahalingam\kanaga Sabhapathy\greedy.ppt

Page 4 Ver 1:.01

Greedy method is one of the algorithm design techniques.

Greedy Technique
G.Kanagasabapathy AAMCE

A problem with n inputs requires us to obtain a subset that


satisfies some constraints. Any subset that satisfies these
constraints is called feasible solution.
We need to find a feasible solution that either maximizes or
minimizes a given objective function.
The greedy method suggests that one can devise an algorithm
that works in stages, considering one input at a time.
At each stage, a decision is made regarding whether a
particular input is in an optimal solution.
If the inclusion of the next input into partially constructed
optimal solution will result in infeasible solution, then this input is
not added to the partial solution. Otherwise it is added.

Greedy Method Control Abstraction for the Subset Paradigm


We can now explain the Greedy Method control abstraction for
the subset paradigm.

Algorithm Greedy(a,n)
// a[1:n] contains the n inputs
{
solution :=0;
for i:= 1 to n do
{
x:= Select(a);
if Feasible ( solution, x) then
solution:=Union(solution,x);
}
return solution;
}
The function Select selects an input a[] and removes it.

The selected inputs value is assigned to x.


Feasible is a Boolean valued function that determines
whether x can be included into the solution vector.
The function Union combines x with the solution and
updates the objective function.

Greedy Technique
G.Kanagasabapathy AAMCE:

Page 5

An Example for Greedy Method: 1 Knapsack Problem


Among the examples cited, we can choose for explanation the
problem that has more clarity for understanding. A simple
example may be finding solution for knapsack problem using
greedy technique.

A knapsack is a sack. We are given n objects. Object i has a


weight wi and the knapsack has a capacity m. If a fraction xi ,
0<=xi<=1, of object is placed into the knapsack, then a profit
of pixi is earned.
The objective is to obtain a filling of the knapsack that
maximizes the total profit earned. Since the knapsack
capacity is m, we require the total weight of all chosen objects
to be atmost m.
Formally, the problem can be stated as follows
Maximize pixi
Subject to wixi

i ranges from 1 to n
I ranges from 1 to n

The profits and weights are positive numbers.

The following pseudo code contains solving the knapsack problem under
greedy method. This control structure forms the basis of solving most of the
problems under greedy method.
Algorithm GreedyKnapsack( m,n)
// P[1:n] and w[1:n] contain the profits and weights respectively.
// of the n objects ordered such that p[i]/w[i] >= p[i+1]/w[i+1].
// m is the knapsack size and x[1:n] is the solution vector.
{
for i:= 1 to n do x[i]=0.0;
U:= m;
for i:=1 to m do
{

Page 6 Ver 1:.01

Greedy Technique
G.Kanagasabapathy AAMCE

if (w[i] > U) then break;


x[i]:=1.0; U=U-w[i];
}
if(i<=n) then x[i]:=U/w[i];
}
2: Coin Change problem:

C:\
wipromission10Xworshop\greedy method_1.ppt

Comparison Between Greedy Technique and Divide and Conquer


Method
We can now make the comparisons between greedy methods
against divide and conquer algorithm.

The greedy approach suggest constructing a solution through a


sequence of steps, each expanding a partially constructed
solution obtained so far, until a complete solution to the problem
is reached. On each step, the choice must be:
Feasible, i.e, it has to satisfy the problems constraints.
Locally optimal i.e, it has to be the best local choice among all

possible choices available on that step.


Irrevocable, i.e, once made, it cannot be changed on
subsequent steps of the algorithm.
Divide and conquer algorithm, on the other hand divides the
problem into several smaller instances of the same problem,
ideally of about the same size. The smaller instances are solved
and combined together to get the final solution for the problem.

Greedy Technique
G.Kanagasabapathy AAMCE:

Page 7

Conclusion
After learning two techniques in algorithm namely greedy
method and divide and conquer. Now, it would be a right time to
conduct a pick and place activity to understand the knowledge
level of the learners.
Suggested Activity: Pick and Place
Place three bins. The bins are named as greedy, divide and conquer and
others. Place list of problems in a common bin. Divide the learners into
three teams A, B and C. Within 10 minutes each team should pick as many
problems from the common bin and place them into the appropriate bin
after identifying the correct category.
The problems would be:

Quick sort
Merge sort
Selection sort
Shortest path
Minimum scanning tree
Huffman code
Trapezoidal rule

Page 8 Ver 1:.01

Greedy Technique
G.Kanagasabapathy AAMCE

Summary
In this session, we learnt to:
Explain the control abstraction of greedy algorithms.
Identify and classify the problems in the pretext of greedy algorithms.
Clearly identify the differences between greedy methods and divide and
conquer algorithm.
Apply the greedy methodology for the problems identified.

Greedy Technique
G.Kanagasabapathy AAMCE:

Page 9

Assignment
1. Design a greedy algorithm for Tree vertex splitting (TVS) problem.
Implement your algorithm on a language of your choice.
2. Design a greedy algorithm for the construction of minimum spanning tree
(MST). Implement your algorithm in a language of your choice.
3. Design a greedy algorithm for computing the shortest path from a single
source to multiple destinations (Dijkstras algorithm). Implement your
algorithm in a language of your choice.

Page 10 Ver 1:.01

Greedy Technique
G.Kanagasabapathy AAMCE

References
Introduction to The Design & Analysis of Algorithms Anany Levitin,
pearson Education.
Fundamentals of Computer Algorithms- Ellis Horowitz,Sartaj Sahani
,Sanguthevar Rajasekaran-Galgotia publications.
Introduction to Algorithms- Thomas H.Cormen,Charles E. Leiserson,Ronald
L.Rivest- Prentice Hall India.

Greedy Technique
G.Kanagasabapathy AAMCE:

Page 11

Anda mungkin juga menyukai