Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Greedy Approach and Fractional Knapsack Problem, Schemes and Mind Maps of Commercial Law

An overview of the greedy approach, an algorithmic paradigm that follows local optimal choices at each step with the hope of finding a global optimal solution. It discusses the properties of greedy algorithms, such as the greedy choice property and optimal substructure, and presents the fractional knapsack problem as an example where a greedy algorithm can be applied. The problem statement, the algorithm, and the analysis of the time complexity. It covers key concepts like feasibility, local optimal choice, and unalterability in the context of greedy algorithms. This information could be useful for students studying algorithms, optimization problems, and problem-solving techniques in computer science or related fields.

Typology: Schemes and Mind Maps

2023/2024

Uploaded on 02/08/2024

ganesh-gummadi
ganesh-gummadi 🇮🇳

1 document

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Greedy Approach
In greedy approach, the decision is taken on the basis of current available information
without worrying about the effect of the current decision in future.
Greedy algorithms build a solution part by part, choosing the next part in such a way, that it
gives an immediate benefit. This approach never reconsiders the choices taken previously.
This approach is mainly used to solve optimization problems. Greedy method is easy to
implement and quite efficient in most of the cases. Hence, we can say that Greedy algorithm
is an algorithmic paradigm based on heuristic that follows local optimal choice at each step
with the hope of finding global optimal solution.
In many problems, it does not produce an optimal solution though it gives an approximate
(near optimal) solution in a reasonable time.
Greedy Method finds out of many options, but you have to choose the best option."
In this method, we have to find out the best method/option out of many present ways.
In this approach/method we focus on the first stage and decide the output, don't think about
the future.
This method may or may not give the best output.
Greedy Algorithm solves problems by making the best choice that seems best at the particular
moment. Many optimization problems can be determined using a greedy algorithm. Some
issues have no efficient solution, but a greedy algorithm may provide a solution that is close
to optimal. A greedy algorithm works if a problem exhibits the following two properties:
1. Greedy Choice Property:+A globally optimal solution can be reached at by creating
a locally optimal solution. In other words, an optimal solution can be obtained by
creating "greedy" choices.
2. Optimal substructure:+Optimal solutions contain optimal subsolutions. In other
words, answers to subproblems of an optimal solution are optimal.
Example:
1. machine scheduling
pf3
pf4
pf5

Partial preview of the text

Download Greedy Approach and Fractional Knapsack Problem and more Schemes and Mind Maps Commercial Law in PDF only on Docsity!

Greedy Approach

In greedy approach, the decision is taken on the basis of current available information without worrying about the effect of the current decision in future. Greedy algorithms build a solution part by part, choosing the next part in such a way, that it gives an immediate benefit. This approach never reconsiders the choices taken previously. This approach is mainly used to solve optimization problems. Greedy method is easy to implement and quite efficient in most of the cases. Hence, we can say that Greedy algorithm is an algorithmic paradigm based on heuristic that follows local optimal choice at each step with the hope of finding global optimal solution. In many problems, it does not produce an optimal solution though it gives an approximate (near optimal) solution in a reasonable time. Greedy Method finds out of many options, but you have to choose the best option." In this method, we have to find out the best method/option out of many present ways. In this approach/method we focus on the first stage and decide the output, don't think about the future. This method may or may not give the best output. Greedy Algorithm solves problems by making the best choice that seems best at the particular moment. Many optimization problems can be determined using a greedy algorithm. Some issues have no efficient solution, but a greedy algorithm may provide a solution that is close to optimal. A greedy algorithm works if a problem exhibits the following two properties:

  1. Greedy Choice Property: A globally optimal solution can be reached at by creating a locally optimal solution. In other words, an optimal solution can be obtained by creating "greedy" choices.
  2. Optimal substructure: Optimal solutions contain optimal subsolutions. In other words, answers to subproblems of an optimal solution are optimal. Example:
  3. machine scheduling
  1. Fractional Knapsack Problem
  2. Minimum Spanning Tree
  3. Huffman Code
  4. Job Sequencing
  5. Activity Selection Problem Steps for achieving a Greedy Algorithm are:
  6. Feasible: Here we check whether it satisfies all possible constraints or not, to obtain at least one solution to our problems.
  7. Local Optimal Choice: In this, the choice should be the optimum which is selected from the currently available
  8. Unalterable: Once the decision is made, at any subsequence step that option is not altered.

PROBLEM STATEMENT

A thief is robbing a store and can carry a maximal weight of W into his knapsack. There are n items available in the store and weight of ith^ item is wi and its profit is pi. What items should the thief take? In this context, the items should be selected in such a way that the thief will carry those items for which he will gain maximum profit. Hence, the objective of the thief is to maximize the profit. Based on the nature of the items, Knapsack problems are categorized as  Fractional Knapsack  Knapsack

As the provided items are not sorted based on pi/wi. After sorting, the items are as shown in the following table. Item B A C D Profit 100 280 120 120 Weight 10 40 20 24 Ratio (pi/wi) 10 7 6 5 Solution After sorting all the items according to pi/wi. First all of B is chosen as weight of B is less than the capacity of the knapsack. Next, item A is chosen, as the available capacity of the knapsack is greater than the weight of A. Now, C is chosen as the next item. However, the whole item cannot be chosen as the remaining capacity of the knapsack is less than the weight of C. Hence, fraction of C (i.e. (60 − 50)/20) is chosen. Now, the capacity of the Knapsack is equal to the selected items. Hence, no more item can be selected. The total weight of the selected items is 10 + 40 + 20 * (10/20) = 60 And the total profit is 100 + 280 + 120 * (10/20) = 380 + 60 = 440 This is the optimal solution. We cannot gain more profit selecting any different combination of items.

Algorithm: Greedy-Fractional-Knapsack (w[1..n], p[1..n], W)

tp=

for i = 1 to n

do x[i] = 0

weight = 0

for i = 1 to n

if weight + w[i] ≤ W then

x[i] = 1

weight = weight + w[i]

tp = tp + p[i]

else

x[i] = (W - weight) / w[i] //fraction

weight = W

tp = tp +( x[i]*profit[i])

break

print tp

return x

Analysis If the provided items are already sorted into a decreasing order of pi/wi, then the while loop takes a time in O(n) ; Therefore, the total time including the sort is in O(n logn).