Click here to Skip to main content
15,357,967 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Given a array of String values split array with all possible combibantions of group of subset's as 
given inputs:-

 String[] items = {"a","b","c","d"};  asume items array elements are an cart item ids like "a" might be equal to in real senorio "ab2fdsde23d2$sad"	

I have to fulfil orders from FCs(any store or warehouse) so I wanted to split items with all subset combinations(as Given below) to get fulfilment centres with less cost where I can find all items.   

so please help me to get optimum solution to get this output
//output:- 
[ [a, b, c, d] ,
[a, b, c] [d] ,
[a, b, d] [c] ,
[a, b] [c, d] ,
[a, c, d] [b] ,
[a, c] [b, d] ,
[a, d] [b, c] ,
[a] [b, c, d] ,
[a, b] [c] [d] ,
[a, c] [b] [d] ,
[a] [b, c] [d] ,
[a, d] [b] [c] ,
[a] [b, d] [c] ,
[a] [b] [c, d] ,
[a] [b] [c] [d]  ]

example2 :-

String[] arr ={"a","b","c"};

output :- [
        [[a], [b], [c] ] ,
        [[a,b],[c]] ,
        [[a,c],[b] ] ,
        [[b,c],[a] ]
        [a,b,c,d]
      ] ;

For more reference, I can tell you  if you have 4 items in the cart like a,b,c,d so to fulfil these items from any of fulfilment centres (FCs) by splitting product in any combinations


please check the below-given solution and optimise it if you guys can.


What I have tried:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.model.Item;

public class TestMain2 {

	public static void main(String[] args) {
		// first param itemid and second quantity
		Item item1 = new Item("a", 1);
		Item item2 = new Item("b", 18);
		Item item3 = new Item("c", 3);
		Item item4 = new Item("d", 1);
		List<Item> items = new ArrayList<>();
		items.add(item1);
		items.add(item2);
		items.add(item3);
		items.add(item4);
		
		int itemsCount = items.size();
		String[] arr = new String[itemsCount] ;
		for(int i=0 ; i<items.size() ; i++) {
			arr[i] = items.get(i).getItemId();
		}
		
		List<List<List<String>>> resItemSubsetGroup = new ArrayList<>();
		for(int i=1 ; i<=items.size() ; i++) {
			partitionsItemListInKSubsets(arr, itemsCount, i,resItemSubsetGroup);
		}
		
		for(List<List<String>> subsetGroup:resItemSubsetGroup) {
			
			for(List<String> ls:subsetGroup) {
				System.out.print(ls+" ");
			}
			System.out.println();
		}
		
	}
	
	 static void PartitionSub(String arr[], int i,
             int N, int K, int nos,
             List<List<String>> v , List<List<List<String>>> resItemSubsetGroup)
		{
		

		if (i >= N)
		{
		
		if (nos == K)
		{
		List<List<String>> lls = new ArrayList<>();
		
		for(List<String> ls:v) {
		
		List<String> lsnew = new ArrayList<>();
		
		for(String l:ls) {
			  lsnew.add(l);
		}
		lls.add(lsnew);
		}
		
		resItemSubsetGroup.add(lls);
		}
		return ;
		}
		
		for (int j = 0; j < K; j++)
		{
		
		if (v.get(j).size() > 0)
		{
		v.get(j).add(arr[i]);
		
		PartitionSub(arr, i + 1, N, K, nos, v ,resItemSubsetGroup);
		v.get(j).remove(v.get(j).size()-1);
		}
		
		else
		{
		
		v.get(j).add(arr[i]);
		PartitionSub(arr, i + 1, N, K, nos + 1, v , resItemSubsetGroup);
		v.get(j).remove(v.get(j).size()-1);
		
		break;
		}
		}
		}
		
		static List<List<List<String>>> partitionsItemListInKSubsets(String arr[], int N, int K ,List<List<List<String>>> resItemSubsetGroup)
		{
		List<List<String>> v = new ArrayList<>();
		
		for(int i = 0; i < K; i++)
		v.add(new ArrayList<>());
		
		if (K == 0 || K > N)
		{
		System.out.println("Not possible");
		}
		else
		{
		PartitionSub(arr, 0, N, K, 0, v,resItemSubsetGroup);
		}
		
		return resItemSubsetGroup;
		}
}
Posted
Updated 25-May-22 6:33am
v4
Comments
Dave Kreskowiak 25-May-22 0:09am
   
And you had a question?
OriginalGriff 25-May-22 0:48am
   
And?
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
Patrice T 25-May-22 3:06am
   
And you have a question ?

Hello Member numbered,

you're looking for a 'fuzzing' method.

from a strings array, your project require an exhaustive output of all existing combinations.

I note your output are mutliple types of array : one dimension/n-dimension.


start at ground.

take 0 and 1 as basis.

it could be binary / integer / or strings [ at end all symbols and types ]

* in a loop to fill 'output array' for all combination :
divide your fuzzing method in 2 steps ( all capitals )
- soft cases : allocation is direct. output[slot] with slot under dictionary array length
else :
- hard cases : a concatenation is required before allocation in the output array.



** easy path for understanding, by another method
a bitset ( c++ ) with length of 64 owns billions or more solutions for you.
each 0 or 1 stand for the symbol to put or not. ( CharAt / indexof( array(i),position ) )
then substitute the 0||1 by the slot in the array dictionnary.
you'll get all the pattern required for your _first step_ BIG list of combination.

create a bitset
loop on bitset++;
fill an array with all those.=> it's equal to an exhaustive _binary tree_ with a depth of 64

as your outputs are multiple types of array,
take your 'exhaustive solutions tree' as a start point to build all your output.
it makes few more functions to write filling your arrays/structure with all row from your 'solution tree'.

sure, it's not the more effiscient method but a first and fixed solution.
as it's readable and understandable easily.


UPDATE :

from the all vars as inputs of your function, you're trying to use 'recursive style' to write this function.
work on 'recursion' first.

or land your code to write as usual. ( it's not shorter, but hard to read and write in recursive style )


UPDATE :

about your output, hardcode it
each output needed is a structure or map of your datas
make a function for every output pattern you need.
go as simple as possible.

declare last_ouput before the loop

for each row in big_solution_tree
 try
   return_structure_A(row);
 end try
 try
   return_structure_B(row);
end try
try
   return_structure_C(row);
end try
try
   return structure_D(row);
end try
 ..............................
 try
  return_structure_M(row);
  end try
  
 end try // with a global try/catch , on a mistake , the fetch continue for the current iteration.
next each

void return_structure_A( row ){

last_output.Add( = ................... the pattern of output );
last_output.next();
}

void return_structure_B( row ){

last_output[].Add( ................... the pattern of output B  );
last_output.next();
}
   
v3
If you have n elements (n=4 in your case), how many combinations of those elements are there if a combination consists of 0 to n elements? Does that give you an idea as to how you could generate each of them?
   
v2
Comments
Member 15627495 25-May-22 6:18am
   
the length of the wanted output will define the number of combinations.

Basically you're on :
N = dictionary.length (-1)
S = pow(N, N)

belonging to output and filtering action on purposes, this count of solution will change. Good stuff !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900