Forums Register Login

Recursive logic questions, any help appreciated

+Pie Number of slices to send: Send
Dear All Professional,
I have a trouble on the recursive logic and try to write it in java.
Let say I have an number array such as [1,2,3,4,5]
and I would like to print out all the combination of this array list such as
1
2
3
4
5
1,2
1,3
1,4
1,5
2,3
2,4
2,5
3,4
3,5
4,5
1,2,3
1,2,4
1,2,5
1,2,6
.....................so on and so on.
Does anyone wrote something similar before.
If you do could you show me the code please.
Thanks for any help.
IceCube
+Pie Number of slices to send: Send
Yo Ice. Loved you in "Friday".

Originally posted by ice cube:
Does anyone wrote something similar before.
If you do could you show me the code please.


Not very likely. One best learns how to program by writing code, especially something tricky like recursion. We will help with syntax or if you have a working program that needs a tweak. To create a recursive program you need to figure out two things:
1. The base case. This is when you've reached the end of whatever you are processing.
2. The recursive case. What do you do with a particular set of data you are given?
In the base case your recursive method should just return. In the recursive case the method should process the input, trim the input and call itself with the trimmed input. Try to do something simple first, like print the array element at position 0. Once you get a feel for it you can add more complex functionality.
+Pie Number of slices to send: Send
Mr. Cube,
We don't have too many rules here at the 'Ranch, but we do have our naming policy, which prohibits names that are obviously fictitious. Yours fails this test (unless you're asking us to believe that the famed rapper-turned-actor is now adding Java Programming to his astonishing array of talents.) Please head over here and change your name, pronto! Note that most folks around these parts use their real names -- keeps us honest.
+Pie Number of slices to send: Send
Dear All Professional,
Just want to share the logic, as you know two heads are better than one.
I'm currently working on a stock delivery system which is about calculate how many cart/truck(s) for delivery a branch of stocks.
Try to utilize the truck capacity and all stock size is same to each other but the weight is different.
Here is my logic.
Step 1) select all the stocks(ID, weight) from DB.
Step 2) generate all the possible combination of the stock order and calculate the total volume * filter out the stock order which is over the CART_CAPACITY_VALUE
Step 3) get the most capacity record(stock order list) first and repeat step 2 recursively.
Am I on the right track?
The way I'm generate the stock order is used: Permutation. Is there any better way to doing this? Because I have trouble when the stock is up to 10. Java seem out of memory.
Any help appreciated.
Dennis S
The follow is some draft code:
// import Java standard package(s) -- BEGIN
import java.util.*;
import java.net.*;
import java.io.*;
// import Java standard package(s) -- END

public class CartEngineV2 {
// public constants -- BEGIN
public CartEngineV2(){
}
public static void init(){
int testSize = 5;
stockID = new String[testSize];
stockID[0] = "1";
stockID[1] = "2";
stockID[2] = "3";
stockID[3] = "4";
stockID[4] = "5";
/*
stockID[5] = "6";
stockID[6] = "7";
stockID[7] = "8";
stockID[8] = "9";
stockID[9] = "10";
*/
weight = new int[testSize];
weight[0] = 550;
weight[1] = 640;
weight[2] = 750;
weight[3] = 610;
weight[4] = 950;
/*
weight[5] = 1060;
weight[6] = 1000;
weight[7] = 900;
weight[8] = 760;
weight[9] = 880;
*/
}

public static void main(String args[]) {
try {
CartEngineV2.init();
Vector cartResultInfo = new Vector();
cartResultInfo = genCart(stockID,stockID.length,0);
} catch (Exception ex) {
ex.printStackTrace(System.err);
}
}
public static Vector genCart(String[] stockIDAry, int endFlag, int numRound) {
Vector resultCartInfo = new Vector();
String templist = null;
try {
for (int i=0; i<stockIDAry.length;i++){
templist=stockIDAry[i];
for (int j=0; j<stockIDAry.length;j++){
if (stockIDAry[i]!=stockIDAry[j]){
templist=templist+","+stockIDAry[j];
System.out.println(templist);
}
}
//resultCartInfo.add(
}
/*
if (endFlag != numRound){
numRound++;
String[] newStockIDAry = new String[stockIDAry.length];
String tempID = null;
int i;
for (i=0; i<stockIDAry.length; i++){
if (i != stockIDAry.length-1){
newStockIDAry[i]=stockIDAry[i+1];
} else {
newStockIDAry[i]=stockIDAry[0];
}
}
resultCartInfo = genCart(newStockIDAry,endFlag,numRound);
}
*/
} catch (Exception ex) {
ex.printStackTrace(System.err);
}
return resultCartInfo;
}
public static Vector vcStr;
public static String[] stockID;
public static int[] weight;
}
+Pie Number of slices to send: Send
 

Originally posted by Dennis Sketagus:

Just want to share the logic, as you know two heads are better than one.
I'm currently working on a stock delivery system which is about calculate how many cart/truck(s) for delivery a branch of stocks.
Try to utilize the truck capacity and all stock size is same to each other but the weight is different.


This sounds like a variation of the classic "Backpack Problem" You have a certain capacity and several loads and you try to optimize the number of loads handled by the given capacity. Yes, it is a Bad Idea to try to figure out all the possible permutations beforehand, simply because it is inefficient, both in time and (obviously) memory use. You can just as easily test each solution as you generate them.
He does not suffer fools gladly. But this tiny ad does:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 938 times.
Similar Threads
Problem with List of files to zipped
Minimum Function(Recursively)
Displaying dice combinations
Why recursion?
An odd question...
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 08:29:18.