This is a java program to implement a standard fractional knapsack problem. It is an algorithmic problem in combinatorial optimization in which the goal is to fill a container (the “knapsack”) with fractional amounts of different materials chosen to maximize the value of the selected materials.
Here is the source code of the Java Program to Solve the Fractional Knapsack Problem. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
//This is a sample program to implement a fractional knapsack problem
import java.io.IOException;
import java.util.Scanner;
class Fractional_Knapsack
{
public static void main(String args[]) throws IOException
{
int i,j=0,max_qty,m,n;
float sum=0,max;
Scanner sc = new Scanner(System.in);
int array[][]=new int[2][20];
System.out.println("Enter no of items");
n=sc.nextInt();
System.out.println("Enter the weights of each items");
for(i=0;i<n;i++)
array[0][i]=sc.nextInt();
System.out.println("Enter the values of each items");
for(i=0;i<n;i++)
array[1][i]=sc.nextInt();
System.out.println("Enter maximum volume of knapsack :");
max_qty=sc.nextInt();
m=max_qty;
while(m>=0)
{
max=0;
for(i=0;i<n;i++)
{
if(((float)array[1][i])/((float)array[0][i])>max)
{
max=((float)array[1][i])/((float)array[0][i]);
j=i;
}
}
if(array[0][j]>m)
{
System.out.println("Quantity of item number: " + (j+1) + " added is " +m);
sum+=m*max;
m=-1;
}
else
{
System.out.println("Quantity of item number: " + (j+1) + " added is " + array[0][j]);
m-=array[0][j];
sum+=(float)array[1][j];
array[1][j]=0;
}
}
System.out.println("The total profit is " + sum);
sc.close();
}
}
Output:
$ javac Fractional_Knapsack.java $ java Fractional_Knapsack Enter no of items 5 Enter the weights of each items 10 20 30 40 50 Enter the values of each items 5 4 3 2 1 Enter maximum volume of knapsack : 80 Quantity of item number: 1 added is 10 Quantity of item number: 2 added is 20 Quantity of item number: 3 added is 30 Quantity of item number: 4 added is 20 The total profit is 13.0
Related posts:
Hướng dẫn Java Design Pattern – Service Locator
How to Remove the Last Character of a String?
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Java Program to Find Strongly Connected Components in Graphs
Java Program to Perform Sorting Using B-Tree
HashSet trong Java hoạt động như thế nào?
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
Java Program to Implement HashMap API
Entity To DTO Conversion for a Spring REST API
How to Return 404 with Spring WebFlux
Java – Byte Array to Reader
Guide to Java OutputStream
Tìm hiểu cơ chế Lazy Evaluation của Stream trong Java 8
Spring Boot - Quick Start
Java Program to Implement Leftist Heap
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Spring Boot - Tomcat Deployment
Guide to the Fork/Join Framework in Java
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
Java equals() and hashCode() Contracts
Ignore Null Fields with Jackson
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
Vector trong Java
Java Program to implement Circular Buffer
Introduction to Spring Cloud Netflix – Eureka
Java Program to Generate Random Numbers Using Multiply with Carry Method
Java Program to Find Hamiltonian Cycle in an UnWeighted Graph
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Spring Cloud AWS – Messaging Support
Java Program to Implement Find all Forward Edges in a Graph
Java Program to Implement ConcurrentHashMap API
Java – Reader to InputStream