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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | //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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | $ 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:
Java Program to Implement Naor-Reingold Pseudo Random Function
Configure a RestTemplate with RestTemplateBuilder
Java Program to Implement Aho-Corasick Algorithm for String Matching
Explain about URL and HTTPS protocol
Spring MVC Setup with Kotlin
Properties with Spring and Spring Boot
Hướng dẫn Java Design Pattern – Adapter
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Java Timer
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
Java Program to Generate a Graph for a Given Fixed Degree Sequence
Flattening Nested Collections in Java
Guide to the Synchronized Keyword in Java
Java Program to Implement Euclid GCD Algorithm
A Guide to Concurrent Queues in Java
Spring Boot - Rest Controller Unit Test
Spring Cloud AWS – EC2
An Introduction to ThreadLocal in Java
Java Program to do a Breadth First Search/Traversal on a graph non-recursively
Java Program to Implement Levenshtein Distance Computing Algorithm
How to Kill a Java Thread
Guide to CopyOnWriteArrayList
Java Program to Check Whether Graph is DAG
Comparing Dates in Java
Reading an HTTP Response Body as a String in Java
Java Program to Compute the Volume of a Tetrahedron Using Determinants
Introduction to Spring Data JPA
Java Program to Optimize Wire Length in Electrical Circuit
Java Program to Find Nearest Neighbor for Dynamic Data Set
LinkedList trong java
Simple Single Sign-On with Spring Security OAuth2
Comparing Long Values in Java