You are on page 1of 44

Greedy method

By
Abhishek Mukhopadhyay
Concept about Greedy method
It is used to solve problems that have n inputs and require us
to obtain a subset that satisfies some constraints.
Greedy method suggests that one can divide the algorithm
that works in stages.
At each stage a decision is made regarding weather or
particular input is in the optimum solution.
For this purpose all the inputs must be arranged in a
particular order by using a selection process.

Control abstraction for GREEDY
Algorithm Greedy (a,n)

//a*1:n+ contains the n inputs
{ Solution: = 0; //initialize the solution
for i: = 1 to n do
{ x: = select (a);
if feasible (solution, x) then
solution: = Union (solution, x);
}
return solution;
}

Feasible solution and optimal solution
Feasible solution: Any subset of the solutions that
satisfies the constraints of the problem is known as a feasible
solution.
Optimal solution: The feasible solution that maximizes
or minimizes the given objective function is an optimal
solution. Every problem will have a unique optimal solution.
Knapsack problem
Knapsack
problem
0-1 knapsack
Fractional
knapsack
Based on pick-
up of weights
Fractional Knapsack
A knapsack with capacity m is given.
we are required to place certain weights such that the sum of
the weights will not exceed the knapsack capacity.
Associated with each weight we have associated profit which
will be earned by the inclusion of the object in to the
knapsack.
If it is not possible to include an object entirely a fraction of
the object can be included and accordingly a fraction of the
profit is earned.



Mathematical representation
m=capacity of knapsack.
n=no. of objects.
object i has a profit p
i
and weight w
i
associated with it.
If a fraction x
j
(o x, 1) of the object i is placed in the bad. A
profit pi x xi is made.
The knapsack problem can be stated mathematically as follows:

Maximize P
i
x
i,
for 1 i n
S.T.C
w
i
x
i
m
1 u n
o xi 1
1 i n

Example:
Consider 3 objects whose profits and weights are
defined as

(P
1
, P
2
, P
3
) = ( 25, 24, 15 )
(W
1
, W
2
, W
3
) = ( 18, 15, 10 )
n=3 m=20
Consider a knapsack of capasity 20. Determine
the optimum strategy for placing the objects in to
the knapsack.
Example(Cont..)
Greedy about profit(1,2/15,D)
Greedy about weight(D,2/3,1)
Greedy about profit/unit weight (0,1,1/2)

Pseudocode of knapsack problem
Algorithm Greedy knapsack (m, n)
//p (1>n) and w(1:n) contain the profits and weights
//resp. 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.0i// initialize x
u : = m;
for i: = 1 to n do
{ if (w(i) > u) then break;
x(i): = 1.0; u: = u-w(i);
}
if (in) then x(i): = u/w(i);
}


Job Sequencing with Deadlines
We are given a set of n jobs.
Associated with each job there is a integer dead line di0 and
a profit pi>0.
A feasible solution for the problem will be a subset j of jobs
each job in this subset can be completed by its deadline.
An optimum solution is a feasible solution with maximum
value.
Example
Obtain the optimal sequence for the following
jobs.
j
1
j
2
j
3
j
4

(P
1
, P
2
, P
3
, P
4
) = (100, 10, 15, 27)

(d
1
, d
2
, d
3
, d
4
) = (2, 1, 2, 1)
n =4

Find out feasible solutions and optimal solution
Pseudo code for JSD
Algorithm Greedy Job (d,j,n)

// j is a set of jobs that can be completed by
// their dead lines
{ j: = {1};
for i: = 2 to n do
{ if (all jobs in j u {i} can be completed by their
dead lines) then
j: = ju{ i};
}
}

Self-Study
Prims algorithm and
Kruskals algorithm(
Elaborate the algorithm
with suitable example)
Backtracking
Backtracking is a form of recursion.
The usual scenario is that you are faced with a number of
options
you must choose one of these;
After you make your choice you will get a new set of options;
This procedure is repeated over and over until you reach a
final state.
If you made a good sequence of choices, your final state is
a goal state.



Backtracking algorithm
boolean solve(Node n)
{
if n is a leaf node
{
if the leaf is a goal node, return true
else return false
}
else
{
for each child c of n
{
if solve(c) succeeds, return true
}
return false
}
}
N-Queens
The object is to place queens on a chess board
in such as way as no queen can capture
another one in a single move
Recall that a queen can move horz, vert, or
diagonally an infinite distance
This implies that no two queens can be on the same
row, col, or diagonal
We usually want to know how many different
placements there are
4-Queens
Lets take a look at the simple problem of placing
queens 4 queens on a 4x4 board
The brute-force solution is to place the first queen,
then the second, third, and forth
After all are placed we determine if they are placed
legally
There are 16 spots for the first queen, 15 for the
second, etc.
Leading to 16*15*14*13 = 43,680 different combinations
Obviously this isnt a good way to solve the problem
4-Queens
First lets use the fact that no two queens can be in
the same col to help us
That means we get to place a queen in each col
So we can place the first queen into the first col, the
second into the second, etc.
This cuts down on the amount of work
Now there are 4 spots for the first queen, 4 spots for the
second, etc.
4*4*4*4 = 256 different combinations
4-Queens
However, we can still do better because as we
place each queen we can look at the previous
queens we have placed to make sure our new
queen is not in the same row or diagonal as a
previously place queen
Then we could use a Greedy-like strategy to
select the next valid position for each col
4-Queens
Q Q Q
Q
Q x
Q
x
Q x
Q
x
Q x Q
Q
x Q
Q x x
4-Queens
Q Q
x x
Q x x
Q
Q x
x x
Q x x
4-Queens
So now what do we do?
Well, this is very much like solving a maze
As you walk though the maze you have to make a series of
choices
If one of your choices leads to a dead end, you need to
back up to the last choice you made and take a different
route
That is, you need to change one of your earlier selections
Eventually you will find your way out of the maze
4-Queens
Q
Q x
x x
Q x x
Q
x
x
Q x
Q
x
x
Q x Q
Q
x
x Q
Q x x
Q
x
x Q
Q x x Q
Q
x
x Q Q
Q x x x
4-Queens
Q
x Q
x Q x
Q x x x
Q Q
x x
x Q x
Q x x x
Q
x Q
x x
Q x x
Q Q
x x
x x
Q x x
Q
x
x
Q x
Q
x
4-Queens
Q
x
Q
x Q
Q Q
x x
Q
Q x
x x
Q
x
Q x
x x
Q
x
Q x
x x Q
4-Queens
Q
x
Q x
x x Q Q
Q
x
Q x Q
x x Q x
Q
x Q
Q x x
x x Q x
4-Queens
This type of problem is often viewed as a
state-space tree
A tree of all the states that the problem can be in
We start with an empty board state at the
root and try to work our way down to a leaf
node
Leaf nodes are completed boards
4-Queens
1
1 2 3 4
2
1 2 3 4
1 2 4 3
1 2 4 3
1 2 3 4
1
1 2 3
Graph Coloring
Graph coloring is the problem of coloring each
vertex in a graph such that no two adjacent
vertices are the same color
Some direct examples:
Map coloring
Register assignment
Graph Coloring
The same issues apply as in N-Queens
We dont want to simply pick all subsets
Way too many
We want to prune the state-space tree as soon as we find
something that wont work
This implies that we need a sequence of vertices to color
As we color the next vertex we need to make sure it doesnt conflict
with any of its previously colored neighbors
We may need to backtrack
Graph Coloring
C
A
F B
E
D
As an example:
The vertices are
enumerated in order A-F
The colors are given in
order: R, G, B
Graph Coloring
C
A
F B
E
D
Graph Coloring
C
A
F B
E
D
Graph Coloring
C
A
F B
E
D
Graph Coloring
C
A
F B
E
D
Graph Coloring
C
A
F B
E
D
Graph Coloring
C
A
F B
E
D
Graph Coloring
C
A
F B
E
D
Graph Coloring
C
A
F B
E
D
Graph Coloring
C
A
F B
E
D
Graph Coloring
C
A
F B
E
D
Graph Coloring
C
A
F B
E
D
Algorithm RCA (Recursive Color
Assignment)

Input: Vertex i, Set of Vertices V
1 For each color c Li, if c is not assigned to any neighbour of i
1.1 Assign c to i
1.2 If i is the last vertex in V
1.2.1 If x(G) > M
1.2.1.1 x(G) = M
1.2.1.2 Save the current coloring
1.2.1.3 If M = LB then stop
1.2.1.4 Otherwise UB = M 1
1.3 Otherwise execute RCA for the next vertex in V

You might also like