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

Assignment of Design and Analysis of Algorism

dsadad

Uploaded by

kasutaye192
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Assignment of Design and Analysis of Algorism

dsadad

Uploaded by

kasutaye192
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Gambella University

Faculty of Natural and Computational Science


Department of Computer Science
3rd year II Sem Regular Student
Group Assignment of Design and Analysis of Algorithm
Name of student ID number
Kasahun Taye 0917
Tolasa Girma 0396
Gelane Fufa 0186
Kiber Temesgen 1103

Taidor loul 1136

Summited to Lecturer Degnachew. G (MSc, Software)


Submission date /05June /2024
Gambella, Ethiopia
1. Solve Knapsack problems using greedy method and give clear
examples.
Knapsack Problem Using Greedy Method: The selection of some things, each with profit and
weight values, to be packed into one or more knapsacks with capacity is the fundamental idea
behind all families of knapsack problems. However, there is no greedy algorithm for the 0/1
knapsack problem because we cannot partially take an item; it’s either all or nothing

The fractional knapsack problem is also one of the techniques which are used to solve the
knapsack problem. In fractional knapsack, the items are broken in order to maximize the profit.
The problem in which we break the item is known as a Fractional knapsack problem.

This problem can be solved with the help of using two techniques:

o Brute-force approach: The brute-force approach tries all the possible solutions with all
the different fractions but it is a time-consuming approach.
o Greedy approach: In Greedy approach, we calculate the ratio of profit/weight, and
accordingly, we will select the item. The item with the highest ratio would be selected
first.

1. Fractional Knapsack Problem

The fractional Knapsack problem using the Greedy Method is an efficient method to solve it,
where you need to sort the items according to their ratio of value/weight. In a fractional
knapsack, we can break items to maximize the knapsack’s total value. This problem in which we
can break an item is also called the Fractional knapsack problem. Here, we will see Knapsack
Problem using Greedy method in detail, along with its algorithm and examples.

1 Fractional knapsack problem:


Knapsack Problem
 We are given n objects and knapsack or bag with capacity M. Object ‘i’ has a
weight Wi and profit Pi where i varies from 1 to N.
 The problem is we have to fill the bag with the help of N objects and the resulting
profit has to be maximum.
 Formally the problem can be stated as
Maximize∑1≤𝑖≤𝑛 XiPi subject to XiWi<=M
Where Xi is the fraction of object and it lies between 0 to 1.
 There are so many ways to solve this problem, which will give many feasible
solutions for which we have to find the optimal solution.
 But in this algorithm, it will generate only one solution which is going to be
feasible as well as optimal.
 First, we find the profit & weight rates of each and every object and sort it

1| P a g e
according to the descending order of the ratios.
 Select an object with highest p/w ratio and check whether its height is lesser than
the capacity of the bag.
 If so place 1 unit of the first object and decrement. the capacity of the bag by the
weight of the object you have placed.
 Repeat the above steps until the capacity of the bag becomes less than the weight
of the object you have selected .in this case place a fraction of the object and come
out of the loop.
 Whenever you selected.
The Profits and Weights are positive.

ALGORITHM:
Algorityhm Greedy knapsack (m,n)
//P[1:n] and the w[1:n]contain the profit
 & weight res’.of the n object ordered.
//such that p[i]/w[i] >=p[i+1]/W[i+1]
//n is the Knapsack size and x[1:n] is the solution vertex.
{
for i=1 to n do a[i]=0.0;
U=n;
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: find optimal solution according to fractional knapsack problem
Object 1 2 3 4 5 6 7
W (Weight of the
knapsack): 15
Profit 5 10 15 7 8 9 4
n (no of items): 7
Weight 1 3 5 4 1 3 2

There are basically three approaches to solve fractional knapsack problem:


1st according to maximum profit
2nd according to minimum weight
3rd according to maximum ratio of profit/weight

2| P a g e
a. According to Maximum profit

object profit weight Remaining


weight
3 15 5 15-5=10
2 10 3 10-3=7
6 9 3 7-3=4
5 8 1 4-1=3
4 7*3/4=5.25 3 3-3=0

The total profit would be equal to (15 + 10 + 9 + 8 + 5.25) = = 47.25

b. According to minimum weight

object profit weight Remaining


weight
1 5 1 15-1=14
5 7 1 14-1=13
7 4 2 13-2=11
2 10 3 11-3=8
6 9 3 8-3=5
4 7 4 5-4=1
3 15*1/5=3 1 1-1=0

According to minimum weight the total profit is (5 + 7 + 4 + 10 + 9 + 7 + 3)= 46

c. 3rd according to maximum ratio of profit/weight


Object 1 2 3 4 5 6 7

Profit 5 10 15 7 8 9 4

Weight 1 3 5 4 1 3 2

Profit/ 5 3.33 3 1.75 8 3 2


weight

P:w: 5 3.3 3 1.7 8 3 2

In this approach, we will select the objects based on the maximum profit/weight ratio. Since the
P/W of object 5 is maximum so we select object 5.

3| P a g e
Object Profit Weight Remaining weight

5 8 1 15 - 8 = 7

After object 5, object 1 has the maximum profit/weight ratio, i.e., 5. So, we select object 1 shown
in the below table:

Object Profit Weight Remaining weight

5 8 1 15 - 1 = 14

1 5 1 14 - 1 = 13

After object 1, object 2 has the maximum profit/weight ratio, i.e., 3.3. So, we select object 2
having profit/weight ratio as 3.3.

Object Profit Weight Remaining weight

5 8 1 15 - 1 = 14

1 5 1 14 - 1 = 13

2 10 3 13 - 3 = 10

After object 2, object 3 has the maximum profit/weight ratio, i.e., 3. So, we select object 3
having profit/weight ratio as 3.

Object Profit Weight Remaining weight

5 8 1 15 - 1 = 14

1 5 1 14 - 1 = 13

2 10 3 13 - 3 = 10

3 15 5 10 - 5 = 5

After object 3, object 6 has the maximum profit/weight ratio, i.e., 3. So we select object 6 having
profit/weight ratio as 3.

Object Profit Weight Remaining weight

4| P a g e
5 8 1 15 - 1 = 14

1 5 1 14 - 1 = 13

2 10 3 13 - 3 = 10

3 15 5 10 - 5 = 5

6 9 3 5-3=2

After object 6, object 7 has the maximum profit/weight ratio, i.e., 2. So we select object 7 having
profit/weight ratio as 2.

Object Profit Weight Remaining weight

5 8 1 15 - 1 = 14

1 5 1 14 - 1 = 13

2 10 3 13 - 3 = 10

3 15 5 10 - 5 = 5

6 9 3 5-3=2

7 4 2 2-2=0
According to ratio of profit/weight total profit is (8 + 5 + 10 + 15 + 9 + 4) =51
According to the the above fractional knapsack problem the best solution for profit is maximum
ratio of profit/ weight . this solution is optimal for the given problem instance. In the first approach,
the maximum profit is 47.25. The maximum profit in the second approach is 46. The maximum profit in
the third approach is 51. Therefore, we can say that the third approach, i.e., maximum profit/weight
ratio is the best approach among all the approaches.

5| P a g e

You might also like