0% found this document useful (0 votes)
19 views18 pages

ResearchPaper On Tornament Tree, Binary Tree, 2-3 Tree

The document discusses the 0/1 Knapsack Problem and Fractional Knapsack Problem. It covers their objectives, operations, benefits, and applications. The 0/1 Knapsack Problem involves selecting items entirely, while the Fractional allows partial items. Dynamic programming and greedy algorithms are used to solve them respectively.

Uploaded by

Abhay Thakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views18 pages

ResearchPaper On Tornament Tree, Binary Tree, 2-3 Tree

The document discusses the 0/1 Knapsack Problem and Fractional Knapsack Problem. It covers their objectives, operations, benefits, and applications. The 0/1 Knapsack Problem involves selecting items entirely, while the Fractional allows partial items. Dynamic programming and greedy algorithms are used to solve them respectively.

Uploaded by

Abhay Thakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

ACKNOWLEDGEMENT:

We, the students of group 4 of the esteemed CV Raman Global University , Bhubaneshwar,
branch of Computer Engineering (Software Engineering) , 4th Semester, do hereby
acknowledge our greatfulness towards the people associated in the project of experiential
learning.

First of all , We would like to pay our thanks to my respected associated Professor, Dr. Sukanta
Kishor0 Bisoy for giving us this opportunity. He helped us with his knowledge and sufficient
information without which we would not have been able to complete the experiential learning
project. Secondly , We are greatful for the library facility to gather information needed for the
project. Above all , the grace of God of all creations led me to complete the project successfully.

3|Page
CONTENT:

1. ABSTRACT
2. INTRODUCTION
3. OBJECTIVES OF 0/1 & FRACTIONAL KNAPSACK
4. OPERATIONS IN 0/1 & FRACTIONAL KNAPSACK
5. BENEFITS OF 0/1 & FRACTIONAL KNAPSACK
6. PROBLEM STATEMENT
7. ALGORITHM FOR THE CODE
8. CODE & TEXT FILE FOR 0/1 & FRACTIONAL KNAPSACK
9. OUTPUT OF THE CODE
10. TIME COMPLEXITY
11. SPACE COMPLEXITY
12.CONCLUSION
13.REFERENCES

4|Page
ABSTRACT

The Knapsack Problem is a widely studied combinatorial optimization problem with


applications in various domains, including resource allocation, cryptography, and scheduling.
It involves selecting a subset of items from a given set, each with an associated weight and
value, such that the total weight does not exceed a specified capacity while maximizing the
total value of the selected items.
The 0/1 Knapsack Problem is a variant of the Knapsack Problem in which each item can either
be included entirely or excluded from the knapsack. It is an NP-complete problem, meaning
that it is computationally intractable to find an exact solution for large instances in polynomial
time. However, numerous algorithms have been proposed to solve the 0/1 Knapsack Problem,
including dynamic programming, branch-and-bound, and approximation algorithms.
The Fractional Knapsack Problem, on the other hand, is a continuous relaxation of the 0/1
Knapsack Problem, where items can be fractionally included in the knapsack. This variant can
be solved optimally using a greedy approach, which sorts the items by their value-to-weight
ratio and includes them in the knapsack in decreasing order until the capacity is reached or no
more items can be added.
Both the 0/1 Knapsack Problem and the Fractional Knapsack Problem have been extensively
studied in the literature, and researchers have explored various techniques to improve the
efficiency of existing algorithms, develop new algorithms, and adapt the problems to specific
application domains. These problems serve as fundamental building blocks for many real-
world optimization problems and continue to be actively researched in the field of
combinatorial optimization.

5|Page
INTRODUCTION

The Knapsack Problem is a fundamental problem in combinatorial optimization that has been
extensively studied and has numerous applications in various fields, including resource
allocation, cryptography, and scheduling. It is a optimization problem that deals with selecting
a subset of items from a given set, each with an associated weight and value, such that the total
weight does not exceed a specified capacity while maximizing the total value of the selected
items.
The 0/1 Knapsack Problem is a variant of the Knapsack Problem in which each item can either
be included entirely or excluded from the knapsack. In this problem, the decision variables are
binary, representing whether an item is included (1) or not (0) in the knapsack. The objective
is to find the subset of items that maximizes the total value while ensuring that the total weight
does not exceed the knapsack's capacity.
The 0/1 Knapsack Problem is an NP-complete problem, meaning that it is computationally
intractable to find an exact solution for large instances in polynomial time. However, numerous
algorithms have been proposed to solve the 0/1 Knapsack Problem, including dynamic
programming, branch-and-bound, and approximation algorithms. These algorithms trade off
between the optimality of the solution and the computational complexity, allowing for efficient
solutions to be found for various problem sizes and constraints.
The Fractional Knapsack Problem can be solved optimally using a greedy approach, which
sorts the items by their value-to-weight ratio and includes them in the knapsack in decreasing
order until the capacity is reached or no more items can be added. This approach provides an
efficient and optimal solution for the Fractional Knapsack Problem, making it a valuable tool
in various resource allocation and optimization scenarios.
Both the 0/1 Knapsack Problem and the Fractional Knapsack Problem have been extensively
studied in the literature, and researchers have explored various techniques to improve the
efficiency of existing algorithms, develop new algorithms, and adapt the problems to specific
application domains. These problems serve as fundamental building blocks for many real-
world optimization problems and continue to be actively researched in the field of
combinatorial optimization.

6|Page
OBJECTIVES OF 0/1 & FRACTIONAL KNAPSACK

The Knapsack Problem is a classic optimization challenge that comes in various forms,
each with unique complexities and applications. Here are some key points from the
sources provided:

- The Knapsack Problem includes variations like the 0/1 Knapsack, Fractional
Knapsack, and Multiple Knapsack scenarios, each with specific characteristics and
objectives.
- The 0/1 Knapsack Problem involves selecting items to maximize total value while
respecting weight constraints, commonly used in scenarios where items cannot be
divided.
- The Fractional Knapsack Problem allows items to be divided, enabling partial
inclusion based on weight or value, providing more flexible solutions for resource
allocation or inventory management.
- The Multiple Knapsack Problem arises when resources need to be distributed across
multiple containers, each with its capacity constraint, requiring optimization of item
allocation across these knapsacks.
- Various algorithms like dynamic programming, greedy algorithms, and evolutionary
approaches are used to tackle the Knapsack Problem, depending on the specific scenario
and constraints.
- The problem's scope extends to real-world applications like financial portfolio
optimization, logistics, and supply chain management, highlighting the relevance of
efficient algorithms in navigating complex resource allocation challenges.

7|Page
OPERATIONS IN 0/1 & FRACTIONAL KNAPSACK

Operations in the 0/1 Knapsack Problem:

1. Initialization: Initialize the problem by defining the set of items, their respective
weights and values, and the capacity of the knapsack.
2. Dynamic Programming: The 0/1 Knapsack Problem can be solved using a dynamic
programming approach, which involves building a table to store the maximum value
that can be achieved for each possible weight and subset of items.
3. Recursion: The dynamic programming solution involves recursively computing the
maximum value that can be achieved by including or excluding each item, based on the
remaining capacity and the values stored in the table.
4. Backtracking: Once the table is filled, backtracking is performed to determine the
actual subset of items that achieves the maximum value while respecting the capacity
constraint.

Operations in the Fractional Knapsack Problem:

1. Initialization: Similar to the 0/1 Knapsack Problem, the problem is initialized by


defining the set of items, their respective weights and values, and the capacity of the
knapsack.
2. Sorting: The items are sorted in descending order based on their value-to-weight ratio
(value/weight).
3. Greedy Selection: A greedy approach is used, where items are included in the
knapsack in the sorted order until the capacity is reached or no more items can be added.
4. Optimal Solution: The greedy approach guarantees an optimal solution for the
Fractional Knapsack Problem.

8|Page
BENEFITS OF 0/1 & FRACTIONAL KNAPSACK

1. 0/1 Knapsack Problem:


- Optimal Resource Allocation: The 0/1 Knapsack Problem is crucial for scenarios where
items cannot be divided, ensuring efficient resource allocation by selecting items entirely based
on their value and weight.
- Simplicity and Clarity: The binary nature of the 0/1 Knapsack Problem simplifies decision-
making processes by restricting item selection to a straightforward "take it" or "leave it" choice.
- Applications in Real-World Scenarios: Widely used in project selection, packing problems,
and financial portfolio optimization, the 0/1 Knapsack Problem offers practical solutions to
resource allocation challenges.

2. Fractional Knapsack Problem:


- Flexibility in Item Selection: The Fractional Knapsack Problem allows for partial inclusion
of items, providing flexibility in selecting fractions of items based on their weight or value,
leading to more optimized solutions.
- Enhanced Resource Utilization: By enabling fractional selections, the Fractional Knapsack
Problem maximizes resource utilization, especially in contexts where items can be utilized in
fractional amounts.
- Applications in Diverse Fields: The Fractional Knapsack Problem finds applications in
resource allocation, inventory management, and other scenarios where items can be divided
and utilized partially, showcasing its versatility and efficiency.

Both the 0/1 Knapsack and Fractional Knapsack problems offer unique advantages tailored to
different optimization requirements, making them valuable tools in various decision-making
processes and resource allocation challenges.

9|Page
PROBLEM STATEMENT

The 0/1 Knapsack Problem and the Fractional Knapsack Problem are two variations of the
Knapsack Problem, which is a combinatorial optimization problem. The Knapsack Problem
deals with selecting a set of items from a given set, where each item has a certain weight and
value, to maximize the total value while ensuring that the total weight does not exceed a
specified capacity.

The 0/1 Knapsack Problem is a variant where items can only be taken in their entirety or not
taken at all. This problem is solved using dynamic programming, where the state space is
defined by the remaining capacity and the items taken so far. The optimal solution for a given
state is calculated by considering the optimal solutions for the previous states and the current
item.

The Fractional Knapsack Problem, on the other hand, allows items to be taken in fractions,
meaning that the items can be divided into smaller parts. This problem is solved using the
greedy approach, where the items are sorted according to their value-to-weight ratio, and the
items are added to the knapsack in that order until the knapsack is full.

The main differences between the two problems are:

1. Item Selection: In the 0/1 Knapsack Problem, items are selected in their entirety, while in
the Fractional Knapsack Problem, items can be divided into smaller parts.

2. Solution Approach: The 0/1 Knapsack Problem is solved using dynamic programming, while
the Fractional Knapsack Problem is solved using the greedy approach.

3. Complexity: The 0/1 Knapsack Problem has a time complexity of O(N*W), where N is the
number of items and W is the capacity, while the Fractional Knapsack Problem can be solved
in O(NlogN) time using sorting.

10 | P a g e
ALGORITHM FOR THE CODE

1. Define Data Structure:


- Define a `struct` named `Item` to represent items that can be placed in the knapsack. Each
item contains attributes such as name, cost, weight, and category.
2. Comparison Function:
- Define a comparison function `compare_items` to sort items based on their value-to-weight
ratio (`cost/weight`). This function is used by the `qsort` function to sort items in descending
order of their value-to-weight ratio.
3. Read Items from File:
- Implement a function `read_items` to read items from a file named "items.txt". Parse each
line of the file to extract item attributes (name, cost, weight, category) and store them in an
array of `Item` structures.
4. Knapsack Algorithm:
- Implement a function `knapsack` to solve the knapsack problem. This function takes the
array of items, the number of items, and the capacity of the knapsack as input parameters.
- Sort the items in descending order of their value-to-weight ratio using the `qsort` function
and the `compare_items` comparison function.
- Iterate through the sorted items and select them one by one if they fit into the knapsack (i.e.,
their weight doesn't exceed the remaining capacity).
- Keep track of the total cost and weight of the selected items.
- Write the optimal items set along with their attributes (name, quantity, cost, weight,
category), total cost, and total weight to a file named "Optimal_items_set.txt".
- Print the optimal items set and its attributes along with the total cost and total weight to the
console.
5. Main Function:
- In the `main` function, prompt the user to enter the capacity of the knapsack.
- Read the items from the file using the `read_items` function.
- Call the `knapsack` function with the array of items, the number of items, and the knapsack
capacity.
6. Exit Program:
- Return 0 from the `main` function to indicate successful execution of the program.

11 | P a g e
CODE FOR 0/1 & FRACTIONAL KNAPSACK

12 | P a g e
13 | P a g e
TEXT FILE

ITEMS PRICE WEIGHT CATEGORY

14 | P a g e
15 | P a g e
OUTPUT

TERMINAL OUTPUT :

TEXT FILE OUTPUT :

16 | P a g e
TIME COMPLEXITY

1. Reading items from the file:


- The `read_items` function reads the items from the file using a loop.
- The time complexity of this step is O(n), where n is the number of items in the file.

2. Sorting items based on cost-to-weight ratio:


- The `qsort` function is used to sort the items in descending order of their cost-to-weight
ratio.
- The time complexity of the `qsort` function is typically O(n log n), where n is the number
of items to be sorted.

3. Selecting items for the knapsack:


- The `knapsack` function iterates through the sorted items and selects the items that can be
included in the knapsack without exceeding the capacity.
- The time complexity of this step is O(n), where n is the number of items.

4. Writing the output to a file and printing to the console:


- The time complexity of writing the output to a file and printing to the console is O(k), where
k is the number of selected items.
The overall time complexity of the code is dominated by the sorting step, which has a time
complexity of O(n log n). Therefore, the time complexity of the given code is O(n log n).

- Time Complexity: O(n log n)

17 | P a g e
SPACE COMPLEXITY

1. Array to store items:


- An array of `Item` structures with a maximum size of `MAX_ITEMS` (defined as 100) is
used to store the items.
- The space complexity of this array is O(n), where n is the number of items.

2. Array to store indices of selected items:


- An integer array `selected_items` with a maximum size of `MAX_ITEMS` (defined as 100)
is used to store the indices of the selected items.
- The space complexity of this array is O(n), where n is the number of selected items.

3. Other variables:
- The code uses a few other variables like `total_cost`, `total_weight`, `num_selected_items`,
etc.
- The space complexity of these variables is constant, O(1).

The overall space complexity of the code is O(n), where n is the maximum of the number of
items and the number of selected items.

- Space Complexity: O(n)

18 | P a g e
CONCLUSION

For the 0/1 knapsack problem:

- Each item can be selected either completely (0) or not at all (1).
- The goal is to maximize the total value of the selected items while not exceeding the capacity
of the knapsack.
- It is typically solved using dynamic programming or greedy algorithms.
- Dynamic programming ensures an optimal solution but has a higher time complexity,
especially for large instances.
- Greedy algorithms, such as sorting items based on their value-to-weight ratio and selecting
them greedily, provide a fast but not always optimal solution.
- The problem is NP-hard, meaning there is no known polynomial-time algorithm that solves
it optimally for all instances.

For the fractional knapsack problem:

- Items can be selected partially, allowing fractions of items to be taken.


- The goal is to maximize the total value of the selected items while not exceeding the capacity
of the knapsack.
- It can be efficiently solved using a greedy algorithm that sorts items based on their value-to-
weight ratio and selects them greedily until the knapsack is full.
- Unlike the 0/1 knapsack problem, the fractional knapsack problem always has an optimal
solution provided by the greedy algorithm.
- The time complexity of the greedy algorithm is O(n log n), where n is the number of items.

19 | P a g e
REFERENCES

1. Martello, Silvano, and Paolo Toth. "Knapsack problems: algorithms and computer
implementations." John Wiley & Sons, 1990.

2. Hochbaum, Dorit S. "Approximation algorithms for the subset-sum and knapsack problems."
SIAM Journal on Computing 15.2 (1986): 396-410.

3. Resende, Mauricio GC, and Celso CR Lyra. "A GRASP for the multiple knapsack problem."
INFORMS Journal on Computing 15.4 (2003): 349-365.

4. Orlova, Olga, et al. "On knapsack problems with two dimensional vectors." European
Journal of Operational Research 270.3 (2018): 918-930.

5. Kellerer, Hans, Ulrich Pferschy, and David Pisinger. "Knapsack problems." Springer Berlin
Heidelberg, 2004.

6. Vazirani, Vijay V. "Approximation algorithms." Springer Science & Business Media, 2013.

7. Garey, Michael R., and David S. Johnson. "Computers and intractability: A guide to the
theory of NP-completeness." W. H. Freeman, 1979.

8. Nair, Radhakrishnan, and Sunitha C. R. "Greedy algorithms for the knapsack problems–a
survey." Proceedings of the International Conference on Computing, Communication and
Automation (ICCCA). IEEE, 2016.

9. Liu, Li, and Mengqi Liu. "A hybrid differential evolution algorithm for solving 0-1 knapsack
problems." Proceedings of the 2010 International Conference on E-product E-service and E-
entertainment (ICEEE). IEEE, 2010.

10. Iori, Manuel, et al. "An exact approach for the capacitated vehicle routing problem based
on a two-commodity network flow formulation." Networks 56.1 (2010): 43-59.

20 | P a g e

You might also like