Question

How to recursive solve a fractional Knapsack problem in Java with log(n) time.

Note!! And return a boolean array of each being take(T) or not take(F)

0-1 Knapsack Problem Weight 10; Value 60; Weight 20; Value 100; Weight = 30; Value = 120; Weight = (20+10): Value = (100+60); Weight = (30+10): Value = (120+60); Weight = (30+20): Value = (120+100); Weight = (30+20+10) > 50 value1 = {60, 100, 120); weigh 3(10, 20, 30); W = 50; Solution: 220

this output shoud be: take=[0,1,1]

static double[] fractionalKnap(double[] values, int[] weights, int k) {

double[] take = new double[values.length]; ​

return take;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

code:

import java.util.Random;

class Knapsack {

   public static int TotalValueknapSack(int Bag, int weight[], int value[], int N) {

       if (N == 0 || Bag == 0)
           return 0;

       if (weight[N - 1] > Bag)
           return TotalValueknapSack(Bag, weight, value, N - 1);

       else
           return Math.max(value[N - 1] + TotalValueknapSack(Bag - weight[N - 1], weight, value, N - 1),
                  
                   TotalValueknapSack(Bag, weight, value, N - 1));
   }

   public static void main(String args[]) {
       Random r = new Random();
       int value[] = new int[5];
       int weight[] = new int[5];
       for (int i = 0; i < 5; ++i) {
           value[i] = r.nextInt(100)+1; // values is between 50 to 200
           weight[i] = r.nextInt(19) + 1; // wight is between 1 to 20
       }
      
       for (int i = 0; i < 5; ++i)
           System.out.print(value[i] + " ");
           System.out.println();
       for (int i = 0; i < 5; ++i)
           System.out.print(weight[i] + " ");
       System.out.println();
      
       int bag = 20;// Bag is of weight 20
       System.out.println("The total Value of knapsack is " + TotalValueknapSack(bag, weight, value, weight.length));
   }
}

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)

Add a comment
Know the answer?
Add Answer to:
How to recursive solve a fractional Knapsack problem in Java with log(n) time. Note!! And return...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I'm having trouble with my Java Homework in which my professor wants us to solve the...

    I'm having trouble with my Java Homework in which my professor wants us to solve the 0-1 Knapsack problem with Dynamic Programming. The code below is what she provided and she requested that we not change any of her existing code but simply add to it. As you can see she gave us the stub file for the knapsack class and the Item class. You are a thief with a knapsack with a carrying capacity of knapsackCapacity pounds. You want...

  • In C Write a couple of functions to process arrays. Note that from the description of...

    In C Write a couple of functions to process arrays. Note that from the description of the function you have to identify what would be the return type and what would be part of the parameter. display(): The function takes an int array and it’s size and prints the data in the array. sumArray(): It takes an int array and size, and returns the sum of the elements of the array. findMax(): It takes an int array and size, and...

  • Important Note: • It is required to solve each problem in separate sheet. • You are...

    Important Note: • It is required to solve each problem in separate sheet. • You are requested to solve problems 1, 3, and 5 using AutoCAD. Problem (1) In the mechanism shown, link 2 is rotating CW at the constant rate of 4 rad/s. In the position shown, is 53. Determine: vc, 03, 04. Link Lengths: AB = 100 mm, BC = 160 mm, CD = 200 mm 160 mm 220 mm DO Problem (2) In the mechanism shown, link...

  • computer architecture and organization Figure Q20 shows a space time diagram to execute n instructions by...

    computer architecture and organization Figure Q20 shows a space time diagram to execute n instructions by CAOTM processor The instruction cycle comprises 4 steps; fetch (F), decode (D), execute (E), and write back (W). Assume 1 clock cycle= 10 ns. 10 20 30 40 50 60 70 80 90 100 110 120 130 Time, ns Cycle Instruction- 1 2 3 4 6 7 8 9 10 11 13 1 F D E E W 2 F E E W D...

  • Can somone show me how to do the 1st problem? Need to find the LS and...

    Can somone show me how to do the 1st problem? Need to find the LS and SS for the fit and the LH and SH for the hole. Fits are all SHAFT BASIS METRIC but the shaft and hole diameters can not be used right out of the table. This is because the 3mm shaft tolerance does not match. You will need to lookup the "Fit" from the table, and then use the LS (Largest Shaft) and SS (Smallest Shaft)...

  • Java Programming .zip or .jar with 4 files: Shape.java, Square.java, TSquare.java, Triangle.java In this homework, you w...

    Java Programming .zip or .jar with 4 files: Shape.java, Square.java, TSquare.java, Triangle.java In this homework, you will build the below hierarchy: Overview: You will implement the supplier code: Shape, Square, TSquare, and Triangle, which will be used by the Homework7Main program. This program will use the DrawingPanel to draw these objects onto a canvas. Specification: You will take advantage of inheritance with using a super class, Shape.java. (below) Shape is an abstract class, which means it can be extended, but...

  • Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and...

    Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and implementation HighArray class and note the attributes and its methods.    Create findAll method which uses linear search algorithm to return all number of occurrences of specified element. /** * find an element from array and returns all number of occurrences of the specified element, returns 0 if the element does not exit. * * @param foundElement   Element to be found */ int findAll(int...

  • How do I solve this? Problem 2(50 points) The roof of the Edwin A. Stevens building...

    How do I solve this? Problem 2(50 points) The roof of the Edwin A. Stevens building is to be redesigned to accommodate a billboard for Stevens Institute of Technology. The new billboard will weigh 40 kips and be supported equally by two columns located 9 ft from the rear of the building. The beams supporting the new roof will be simply supported as shown. (Note there is a 5 ft overhang at the front of the building). For the grade...

  • In Java Burdell and the Buzz Problem Description As an aspiring band manager, you need to...

    In Java Burdell and the Buzz Problem Description As an aspiring band manager, you need to promote your band and expand publicity. Being hip with the times, you know that social media can make or break a band’s popularity. So, you decide to bring some ‘lucky’ Georgia Tech students to spread the good word! Solution Description Write the Concert, Musician, and Fan classes to guide your band on their rock star journey. You will design these classes from scratch following...

  • use the code and change it Task This assignment is very straight-forward and quick to do...

    use the code and change it Task This assignment is very straight-forward and quick to do since BigInteger supports integer operations just as long does! Start by downloading the code below (scroll down). (Certainly compile and run the code to see what it does.) Add "import java.math.BigInteger;" to the top of the file. Replace all "Long" types with "BigInteger" in the entire file. (This works because long was intentionally only used for computed values.) Replace all "long" types with "BigInteger"...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT