Docsity

Greedy Method(Fractional knapsack), Exams of Design and Analysis of Algorithms

Partial preview of the text

Download Greedy Method(Fractional knapsack) and more Exams Design and Analysis of Algorithms in PDF only on Docsity!

Greedy Method

Introduction

  • When the problem has many feasible solutions with different cost or benefit, finding the best solution is known as optimal solution.
  • Greedy algorithms is that they are very simple in nature. Easy to understand and quick to build.
  • It derives solution step by step by looking at the information available at the current moment. It does not look at future prospects.
  • This method constructs the solution simply by looking at current benefit without exploring future possibilities and hence they are known as greedy.
  • The greedy approach does not ensure the optimal

solution, but in the most cases, it provides a good

approximation.

  • Certain problems, like Prim’s and Kruskal’s algorithm,

greedy always produces an optiml solution.

  • Characteristics of Greedy Method: Greedy choice property Optimal Structure

(Non decreasing means that the values could stay the

same - they don't decrease but they could increase or

stay the same)

Thinks to remember….

  • Greedy algorithm derives solution step by step, by looking at the information available at the current moment.
  • It does not look at future prospects.
  • Decisions are completely locally optimal.
  • The choices that you made under greedy solutions are irrevocable means once we have selected the best solution, it cannot be backtracked.

Introduction

  • Greedy is an algorithmic paradigm that builds up a solution piece by piece, always choosing the next piece that offers the most obvious and immediate benefit.
  • So the problems where choosing locally optimal also leads to global

solution are best fit for Greedy.

  • For example consider the Fractional Knapsack Problem. The local optimal strategy is to choose the item that has maximum value vs weight ratio. This strategy also leads to global optimal solution because we allowed to take fractions of an item.

Problem Scenario

  • 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 i th item is w i and its profit is p i . 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
  • Fractional Knapsack

In this case, items can be broken into smaller

pieces, hence the thief can select fractions of

items.

According to the problem statement,

There are n items in the store

Weight of i

th

item wi>

Profit for i

th

item pi>0 and

Capacity of the Knapsack is W

Complexity of Fractional Knapsack

For one item , there are 2 choices either to select or to reject. For 2 items , there are 4 choices. Brute Force Approach

  • The most basic approach is to try all possible subsets and possible fractions of the given set and find the maximum value among all such fractions.
  • The time complexity will be exponential, as you need to find all possible combinations of the given set.
  • We can improve performance by sorting items in advance. Using merge sort n items can be sorted in O(N*log N) time. The running time is same in best, average and worst case.
  • To select items , we need one scan to this sorted list i.e O(N)
  • T(N)=O(N logN)+O(n)
  • Time Complexity: O(N log N) where N is the size of the array.*