greedy method by dr. b. j. mohite

28
Greedy Method The Greedy method is most straightforward 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 option finish 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.

Upload: zeal-education-society-pune

Post on 21-Jan-2018

109 views

Category:

Education


4 download

TRANSCRIPT

Page 1: Greedy method by Dr. B. J. Mohite

Greedy Method • The Greedy method is most straightforward 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 option finish

• 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.

Page 2: Greedy method by Dr. B. J. Mohite

• 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 will result in an infeasible solution, then this input is not added to the partial solution. Otherwise, it is added. The selection procedure itself is based on some optimization measures. This measure may be the objective function. Several different optimization measures may be possible for a given problem. However, will result in algorithm that generate suboptimal solutions is called subset paradigm.

Page 3: Greedy method by Dr. B. J. Mohite

Greedy method control abstraction for the subset paradigm can be written as –

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) thenSolution:=Union(Solution, x);

}return Solution;

}

The above Greedy function describes the essential way that a greedy algorithm will look, once a particular problem is chosen and the function Select, Feasible and Union are properly implemented. The function 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. The function Union combines x with the solution and updates the objective function

Page 4: Greedy method by Dr. B. J. Mohite

Example 1 [Change Making]• A child buys candy valued at less than Rs. 1 and gives an Rs 1 bill to the

cashier. The cashier whishes to return change using the fewest number of coins. Assume that an unlimited supply of 50 paisa, 25 paisa, 20 paisa, 10 paisa and 5 paisa coins are available. The cashier constructs the change in stages. In each stage a coin is added to the change. This coin is selected using the greedy criterion: At each stage increase the total amount of change constructed by as much as possible. To assure feasibility (i.e. the change given exactly equals the desired amount) of the solution, the selected coin should not cause the total amount of change given so far to exceed the final desired amount.

• Suppose that 65 paisa in change is due to child. The first coin selected is 50 paisa, a 25 paisa or 20 paisa coin cannot be selected as a second coin because such a selection results in an infeasible selection of coins (change exceeds 65 paisa). The second coin selected is a 10 paisa coin and then a 5 paisa coin is added to the change.

• Here there were number of other possible sets of coins which formulate 65 paisa but the greedy method implies that with minimum number of inputs or values it is expected to obtain the optimum solution.

Page 5: Greedy method by Dr. B. J. Mohite

Example 2 [Machine Scheduling]• You are given n tasks and an infinite supply of machines

on which these tasks can be performed. Each task has a start time Si and a finish time Fi, Si<Fi. [Si, Fi] is the processing interval for task i. Two tasks i and j overlap iff their processing interval overlap at a point other than the interval start or end. A feasible task-to-machine assignment is an assignment in which no machine is assigned two overlapping tasks. Therefore in a feasible assignment each machine works on the most one task at any time. An optimal assignment is a feasible assignment that utilizes the fewest number of machines to complete the task.

Page 6: Greedy method by Dr. B. J. Mohite

• Suppose we have n=7 tasks labeled from a through g and their start and finish times are as shown bellow.

• The above task-to-machine assignment is a feasible assignment that uses seven machines for seven tasks. E.g. task a assign to machine M1, task b to machine m2 etc. This assignment is not an optimal assignment because other assignment method uses fewer machines as shown in following chart.

M3 _____________c___________________d_________

M2 ________________f________________ _________g________M1 ______a__ ____________b___________________ _________e________________

0 1 2 3 4 5 6 7 8 9 10 11

• Above chart shows you that, we can assign tasks a, b, and e to some machine, task f and g for another machine and task c and d for other machine, reducing the number of utilized machine to three.

Task A b c d e F G

Start 0 3 4 9 7 1 6

Finish 2 7 7 11 10 5 8

Page 7: Greedy method by Dr. B. J. Mohite

• A greedy way to obtain an optimal task assignment is to assign the tasks in stages, one task per stage and in ascending order of task start times. Call a machine old if at least one task has been assigned to it. If machine is not old, it is New.

• For machine selection, use the greedy criterion: If an old machine becomes available by the start time of the task to be assigned, assign the task to this machine; if not, assign it to a new machine.

• Here, first stage has no old machines, so task a is assigned to new machine (Say M1). This machine is now busy from time 0 to 2. In stage 2 task f is considered. Since the only old machine is busy when task f is to start, it is assigned to a new machine (Say M2). When task b is considered in stage 3, we find that the old machine M1 is free at time 3, so b is assigned to M1. In stage 4 task c is considered. Since neither of the old machine is available at time 4, task c is assigned to new machine (Say M3). Task g is considered in stage 5 and assigned to machine M2, which is the first to become available. Task is assigned in stage 6 to machine M1, and finally, in stage 7, task d assigned to machine M2 or M3.

Page 8: Greedy method by Dr. B. J. Mohite

Example 3: Container Loading

• A large ship is to be loaded with cargo.

• The cargo is containerized, and all containers are the same size.

• Different containers may have different weights.

• We wish to load the ship with maximum number of containers.

Page 9: Greedy method by Dr. B. J. Mohite

This problem can be formulated as an optimization problem in the following way:

• Let Wi be the weight of the ith container, 1 ≤ i ≤ n.

• The cargo capacity of the ship is C.

• Let Xi be a variable whose value can be either 0 or 1. If we set Xi to 0, then container I is not to be loaded. If Xi is 1, then the container is to be loaded in ship.

Page 10: Greedy method by Dr. B. J. Mohite

• We wish to assign the values to the Xi that satisfy the constraints ≤ C and Xi {0, 1}, 1 ≤ i ≤ n. The ∈optimization function is

• Every set of Xi’s that satisfies the constraints is a feasible solution and every feasible solution that maximizes is an optimal solution.

• The ship may be loaded in stages; one container per stage. At each stage we need to select container load. For this decision we may use the greedy criterion: From the remaining containers, select the one with least weight. This order of selection will keep the total weight of the selected container to minimum and hence leave maximum capacity for loading more containers.

Page 11: Greedy method by Dr. B. J. Mohite

Greedy algorithm implies that,

• We first select the container that has least weight, then the one with the next smaller weight, and so on until either all containers have been loaded or there isn’t enough capacity for the next one.

Page 12: Greedy method by Dr. B. J. Mohite

Pseudo-code for Greedy container-loading method can be written as

Algorithm ContainerLoading( C, Capacity, NumberOfContainers, X)

{ Sort(C, NumberOfContainers); //Sort into increasing order of weight.

N:=NumberOfContainsers;

For i:=1 to N do

X[i]:=0; //Initialixe all X to zero (Ship is vacant)

i:=1 // Select containers in ascending order of weight

while(i ≤ N && C[i].weight ≤ Capacity) //enough capacity for container C[i].id

{ X[C[i].id]:=1; // Load container to ship

Capacity -=C[i].weight; // Calculate remaining load capacity of ship

i++; // take next container.

}

}

Page 13: Greedy method by Dr. B. J. Mohite

Suppose we want to load 8 containers of different weights (in tones) [100, 200, 50, 90, 150, 50, 20, 80] in a ship having capacity of 400 tones. Specify How many and which containers can be loaded in to ship.

Here, n=8, C=400 Let weights of container are -W1, W2, W3, W4, W5, W6, W7, W8 Total Total

100, 200, 50, 90, 150, 50, 20, 80 Weight Containers

X1= 1 1 1 0 0 1 0 0 = 400 4

X2= 1 0 1 0 1 0 1 1 = 400 5

X3= 0 1 1 0 1 0 0 0 = 400 3

X4= 1 0 1 1 0 1 1 1 = 390 6

:

:

Page 14: Greedy method by Dr. B. J. Mohite

• When a greedy algorithm is used, the containers are considered for loading in the order 7, 3, 6, 8, 4, 1, 5, 2. Containers 7, 3, 6, 8, 4, and 1 together weight 390 tones and are loaded. The available capacity is now only 10 tones, which is inadequate for any of the remaining containers.

• In the greedy solution we have [X1, X2,…, X8]=[ 1, 0, 1, 1, 0, 1, 1, 1] and = 6. i.e. we can load maximum 6 containers into the ship with identification count 1, 3, 4, 6, 7 and 8.

Page 15: Greedy method by Dr. B. J. Mohite

Example 4: 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.

Page 16: Greedy method by Dr. B. J. Mohite

• Maximize (A)

• Subject to (B)•

• 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.

Page 17: Greedy method by Dr. B. J. Mohite

• Example: Suppose, you have provided a three objects of different weights as (18, 15 and 10) with their corresponding profit values (25, 24 and 15). Also a knapsack having capacity of 20 is provided. Find out the different feasible solution and optimal solution that yield maximum profit.

• Solution– Here, N=3, – M=20, – (P1, P2, P3)=(25, 24, 15) and

– (W1, W2, W3)=(18, 15, 10)

Page 18: Greedy method by Dr. B. J. Mohite

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) 20 31.5

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.

Page 19: Greedy method by Dr. B. J. Mohite

Algorithm GreedyKnapsack( M, n) //P[1:n] and W[1:n] contains Profit & Weights

{ respectively of the n objects ordered such that P[i]/W[i] ≥ P[i+1]/ W[i+1].

for i:=1 to n do M is the knapsack size and X[1:n] is the feasible solution vector.

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];

}

Page 20: Greedy method by Dr. B. J. Mohite

Job Sequencing with Deadlines• We are given a set of n jobs. • Di is a deadline given to complete ith job 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.

Page 21: Greedy method by Dr. B. J. Mohite

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 -Here, Number of Jobs (n)= 4

Profit values of four jobs (P1, P2, P3, P4)=(100, 10, 15, 27)

Process deadlines for respective job are (D1, D2, D3, D4) =(2, 1, 2, 1)

Page 22: Greedy method by Dr. B. J. Mohite

(P1, P2, P3, P4)=(100, 10, 15, 27) (D1, D2, D3, D4) =(2, 1, 2, 1)

The feasible solutions and their values are –

Feasible sol. subset Processing 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.

Page 23: Greedy method by Dr. B. J. Mohite

Single-Source Shortest Paths• Path is the way between source and destination. • The source, destination and the path were generally represented with

the help of graph. • Vertices were used to represent Source or Destination and • the lines used to connect sources and destinations are called Edges. • The edges can then be assigned weights which may be either the

distance between two vertices or average time to drive between two cities etc.

• The graphs were generally used to answer the following questions –– Is there a path between source and destination?

– If there are more than one paths from source to destination, which is the shortest path?

• The length of path is the sum of weights of the edges on the path. • The starting vertex of the path is referred as Source, and the last

vertex the destination.

Page 24: Greedy method by Dr. B. J. Mohite

• Consider the directed graph , the number

on the edges are the weights. • The shortest path from node 1 to other

destination node arePath Length

1-4 10

1-5 25

1-2 (45,50)= 45

1-3 (45,50,60) = 45

1-6 No path

If node 1 is the source vertex, then the shortest path from 1 to 2 is 1-4-5-2. The length of this path is 10 + 15 + 20 =45. Even though there are three edges on this path, it is shorter than the path 1-2 which is of length 50.

Page 25: Greedy method by Dr. B. J. Mohite

• The systematic way to generate the shortest paths from vertex V0 to the remaining vertices is to generate these paths in ascending order of path length.

• First, a shortest path to the nearest vertex is generated. • Then a shortest path to the second nearest vertex is

generated, and so on. – For the above graph the nearest vertex to V0=1 is 4

(Cost[1,4]=10). The path 1-4 is the first path generated. – The second nearest vertex to node 1 is 5 and the distance

between 1to5 is 25. Then path 1-4-5 is the next path generated.– The third nearest vertex to node 1 is 2 and the distance

between 1to2 is 45. Then path 1-4-5-2 is the next path generated.

– The forth nearest vertex to node 1 is 3 and the distance between 1to3 is 45. Then path 1-3 is the next path generated.

Page 26: Greedy method by Dr. B. J. Mohite

The Cost adjacency matrix for above problem is as shown bellow

Destination Nodes

1 2 3 4 5 6

1 0 50 45 10

2 0 10 15

3 0 30

4 20 0 15

5 20 35 0

6 3 0

Dijkstra’s algorithm is used to determine the length of the shortest path from vertex V0 to all other vertices in graph G.

Page 27: Greedy method by Dr. B. J. Mohite

• Suppose you have 7 containers whose weights are 50, 10, 30, 20, 70, 60, and 5 and a ship whose capacity is 110. Find an optimal solution to this instance of the container loading problem.

W1, W2, W3, W4, W5, W6, W7, W8 Total Total100, 200, 50, 90, 150, 50, 20, 80 Weight Containers

X1= 1 1 1 0 0 1 0 0 = 400 4

Page 28: Greedy method by Dr. B. J. Mohite

Find an optimal solution to the knapsack instancen=7, m=15, (P1, P2, P3,… P7)=(10, 5, 15, 7, 6, 18, 3), and(W1, W2, W3,…,W7)=(2, 3, 5, 7, 1, 4, 1). Sets Total weight Profit earned1.(0,1,0,1,1,1,0) 15 362.(1,0,0,1,1,1,1) 15 443.(0,1,1,1,0,0,0) 15 274.(1,1,1,0,0,1,1) 15 515.(1,1,1,0,1,1,0) 15 546.(1,2/3,1,0,1,1,1) 15 55.33