You are on page 1of 13

Greedy Algorithm _ Theory

1. Explain in brief characteristics of greedy algorithms.

2. Explain Greedy method in detail with examples and differentiate it with a dynamic
method.
3. Discuss and derive an equation for solving the 0/1 Knapsack problem using
dynamic programming methods.
Design and analyse the algorithm for the same.

The problem statement is as follows: Given a set of items, each with a weight and a
value, determine the maximum value that can be obtained by selecting a subset of
items to fit in a knapsack with a maximum capacity without exceeding the capacity.
Each item can either be included (1) or excluded (0) from the knapsack.

Here's the equation to solve the 0/1 Knapsack problem using dynamic
programming:

Let:

● n be the number of items available.


● j be the maximum capacity of the knapsack.
● weights[1..n] be an array of item weights.
● values[1..n] be an array of item values.
● dp be a 2D array (table) of size (n+1) x (W+1) to store the maximum values
achievable.
The dynamic programming equation is as follows:

dp[i][j] = max(dp[i-1][j],values[i] + dp[i-1][j - weights[i]] )

● dp[i][j] represents the maximum value achievable by considering the first


i items and having a knapsack capacity of w.
● dp[i-1][j] is the maximum value achieved by not including the i-th item in
the knapsack.
● dp[i-1][j - weights[i]] + values[i] is the maximum value achieved by
including the i-th item in the knapsack. This accounts for the value of the
i-th item and reduces the available capacity by its weight.

The dynamic programming algorithm to solve the 0/1 Knapsack problem involves
filling in the dp table using the above equation, starting from dp[0][0] and
progressing to dp[n][W]. The value at dp[n][W] will represent the maximum value
achievable for the given problem instance.

Algo and analysis


Link: https://www.youtube.com/watch?v=yjRrpPejmb4
4. What is a fractional knapsack problem? Design and analyze greedy algorithms to
solve it.
For understanding: https://www.youtube.com/watch?v=M79iHjAG1tg
For theory and algo:

https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_and_anal
ysis_of_algorithms_fractional_knapsack.html

5. Is Selection sorting a greedy algorithm? If so, what are the functions involved?

Yes, selection sort is a greedy algorithm.

6. Give and Explain the Prim’s Algorithm to find out Minimum Spanning Tree with
illustration.Also give itstime complexity.
7. Give and Explain the Kruskal’s Algorithm to find out Minimum Spanning Tree with
illustration. Also give its time complexity.

You might also like