Sunteți pe pagina 1din 2

Bagian C:

On completion of the Knapsack Problem There are three types of greedy algorithms that can be used,
are :
A. Greedy By Weight
At each step select the object has the lightest weight. Try maximizing the include as many objects to
in a knapsack.
The first thing to do is program sort in ascending objekobjekberdasarkan weightnya. then new
taken one by one object that can be accommodated by knapsack until knapsack
full or object is no longer the can be inserted.
B. Greedy by profit
At each step, select objects has the greatest advantage. Try maximizing the select the object of the most
profitable first.
The first thing to do is program sort descending objects based on profit. Then just taken one by one
object that can be accommodated by knapsack until knapsack full or object is no longer the can be
inserted.
C. Greedy By Density
At every step knapsack filled with objects that have pi / wi the largest, where p is an advantage and w is
heavy goods. Trying to maximize advantage by selecting the object has the greatest density per unit
weight.
The first thing to do is program seeking profit value per weight of each - per unit (density) of each
object. Then the objects were sorted based on its density. then new taken one by one object that can be
accommodated by knapsack until knapsack full or object is no longer thecan be included

EXAMPLES OF APPLICATION IN THE GREEDY


TRANSPORTATION
In table 1 there is a tool transport with the capacity of 100 kg, there are 4 items with the size
as follows :

Barang Weight Profit Density


1 23 67 2.913043478
2 45 86 1.911111111
3 67 139 2.074626866
4 39 42 1.076923077

Then using Greedy algorithms, Table 2 is obtained as follows:


GBW GBP GBD
1 1 1
0 0 0
0 1 1
1 0 0
Keuntungan 109 206 206
Information :
GBW: Greedy By Weight
GBP: Greedy By Profit
GBD: Greedy By Density
0: Goods not transported
1: Goods transported

Flowchart general system for implementation Greedy algorithms in Knapsack problem

Start

Enter the maximum capacity, the weight


and the advantages of each object

Sort objects based on the


False False
Greed by Greed by density of the object in
weighted profit descending order

True True
sort objects based on the Sort objects based on the
weight of the object in profit of the object in
ascending order descending order

enter objects sequentially into the knapsack without exceeding the maximum
capacity

calculate the total profit by summing the advantages of each object that is inserted
into the knapsack

show status object retrieval and total profit

Finish

S-ar putea să vă placă și