473,656 Members | 2,871 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

All possibilities from datasets? How to?

We have X number of data sets, of Y length each.

For example...

Small, Medium, Large
and
Red, Green, Blue, Yellow

We need to generate a list of all possibilities
Small Red
Small Green
Small Blue
Small Yellow
Medium Red
Medium Green
.... etc.

The catch...
There can be any number of data sets, and they can be of any length.

Any idea on the algorithm to do this?

I've seen some examples where they just have several nested for loops,
but there could be any number of datasets.

Any help is appericated!
Jul 28 '08 #1
9 1936
gardnern wrote:
I've seen some examples where they just have several nested for loops,
but there could be any number of datasets.
Recursivity.

--
----------------------------------
Iván Sánchez Ortega -ivan-algarroba-sanchezortega-punto-es-

Now listening to: SEBRIDER - Heaven and Sea - [4] Whales' Song (6:18)
(97.384598%)
Jul 28 '08 #2
gardnern wrote:
We have X number of data sets, of Y length each.

For example...

Small, Medium, Large
and
Red, Green, Blue, Yellow

We need to generate a list of all possibilities
Small Red
Small Green
Small Blue
Small Yellow
Medium Red
Medium Green
... etc.

The catch...
There can be any number of data sets, and they can be of any length.

Any idea on the algorithm to do this?

I've seen some examples where they just have several nested for loops,
but there could be any number of datasets.

Any help is appericated!
Are these permutations or combinations

In other words, is Small Red different from Red Small?
Jul 28 '08 #3
On Jul 28, 5:26*pm, "Paul Lautman" <paul.laut...@b tinternet.com>
wrote:
gardnern wrote:
We have X number of data sets, of Y length each.
For example...
Small, Medium, Large
and
Red, Green, Blue, Yellow
We need to generate a list of all possibilities
Small Red
Small Green
Small Blue
Small Yellow
Medium Red
Medium Green
... etc.
The catch...
There can be any number of data sets, and they can be of any length.
Any idea on the algorithm to do this?
I've seen some examples where they just have several nested for loops,
but there could be any number of datasets.
Any help is appericated!

Are these permutations or combinations

In other words, is Small Red different from Red Small?
Combinations, not permutations.
Jul 29 '08 #4
On 29 Jul, 15:12, gardnern <nat...@factory 8.comwrote:
On Jul 28, 5:26*pm, "Paul Lautman" <paul.laut...@b tinternet.com>
wrote:


gardnern wrote:
We have X number of data sets, of Y length each.
For example...
Small, Medium, Large
and
Red, Green, Blue, Yellow
We need to generate a list of all possibilities
Small Red
Small Green
Small Blue
Small Yellow
Medium Red
Medium Green
... etc.
The catch...
There can be any number of data sets, and they can be of any length.
Any idea on the algorithm to do this?
I've seen some examples where they just have several nested for loops,
but there could be any number of datasets.
Any help is appericated!
Are these permutations or combinations
In other words, is Small Red different from Red Small?

Combinations, not permutations.- Hide quoted text -

- Show quoted text -
Try this then:

$datasets = array();
$datasets[] = array('Small', 'Medium', 'Large');
$datasets[] = array('Red', 'Green', 'Blue', 'Yellow');

while ($leftset = array_shift($da tasets))
foreach ($leftset as $left)
foreach ($datasets as $rightset)
foreach ($rightset as $right)
echo "{$left} {$right}<br />";
Jul 29 '08 #5
On Jul 29, 9:38*am, Captain Paralytic <paul_laut...@y ahoo.comwrote:
On 29 Jul, 15:12, gardnern <nat...@factory 8.comwrote:
On Jul 28, 5:26*pm, "Paul Lautman" <paul.laut...@b tinternet.com>
wrote:
gardnern wrote:
We have X number of data sets, of Y length each.
For example...
Small, Medium, Large
and
Red, Green, Blue, Yellow
We need to generate a list of all possibilities
Small Red
Small Green
Small Blue
Small Yellow
Medium Red
Medium Green
... etc.
The catch...
There can be any number of data sets, and they can be of any length..
Any idea on the algorithm to do this?
I've seen some examples where they just have several nested for loops,
but there could be any number of datasets.
Any help is appericated!
Are these permutations or combinations
In other words, is Small Red different from Red Small?
Combinations, not permutations.- Hide quoted text -
- Show quoted text -

Try this then:

$datasets = array();
$datasets[] = array('Small', 'Medium', 'Large');
$datasets[] = array('Red', 'Green', 'Blue', 'Yellow');

while ($leftset = array_shift($da tasets))
* foreach ($leftset as $left)
* * foreach ($datasets as $rightset)
* * * *foreach ($rightset as $right)
* * * * *echo "{$left} {$right}<br />";

thats fine if theres only 2 datasets, but what if theres 20 of them?
nested foreach loops wont work. I know I have to use recursion, but
not sure how...
Jul 29 '08 #6
On Jul 29, 3:56 pm, gardnern <nat...@factory 8.comwrote:
On Jul 29, 9:38 am, Captain Paralytic <paul_laut...@y ahoo.comwrote:
On 29 Jul, 15:12, gardnern <nat...@factory 8.comwrote:
On Jul 28, 5:26 pm, "Paul Lautman" <paul.laut...@b tinternet.com>
wrote:
gardnern wrote:
We have X number of data sets, of Y length each.
For example...
Small, Medium, Large
and
Red, Green, Blue, Yellow
We need to generate a list of all possibilities
Small Red
Small Green
Small Blue
Small Yellow
Medium Red
Medium Green
... etc.
The catch...
There can be any number of data sets, and they can be of any length.
Any idea on the algorithm to do this?
I've seen some examples where they just have several nested for loops,
but there could be any number of datasets.
Any help is appericated!
Are these permutations or combinations
In other words, is Small Red different from Red Small?
Combinations, not permutations.- Hide quoted text -
- Show quoted text -
Try this then:
$datasets = array();
$datasets[] = array('Small', 'Medium', 'Large');
$datasets[] = array('Red', 'Green', 'Blue', 'Yellow');
while ($leftset = array_shift($da tasets))
foreach ($leftset as $left)
foreach ($datasets as $rightset)
foreach ($rightset as $right)
echo "{$left} {$right}<br />";

thats fine if theres only 2 datasets, but what if theres 20 of them?
nested foreach loops wont work. I know I have to use recursion, but
not sure how...
If you're dealing with 20 datasets then the odds are your script will
time out long before the operation completes. The problem you're
describing is of a kind that has exponential run time. Lets say you
have 2 sets of 256 elements each. To get all possible combinations
will take 65536 iterations. Quite a lot but managable on modern
hardware. Now, lets say you have 3 sets of 256 elements. You're now
talking about 16777216 iterations. Adding another set of 256 elements
will result in 4294967296 iterations, 5 sets will mean 1099511627776
iterations, 6 sets will mean 281474976710656 iterations, 7 sets will
mean 720575940379279 36 iterations... When you hit 20 sets the number
my calculator spits out is so big it needs to be written in scientific
notation (1.461501637330 902918203684832 7163e+48, if you're curious).

Even with much smaller sets, the number of sets will cause the
possible permutations to grow exponentially, for example even with
only 8 elements per set the result is 115292150460684 6976 possible
permutations of 20 sets.

At the risk of sounding rude (which is honestly not my intent), I
think you might need to take a careful look at what it is you're
actually trying to achieve, as it looks like there could be something
wrong with your design, or at least with your planned implementation
of it. You need to be thinking "How can I achieve what I want without
having to do it by brute force?" instead of "How do I implement the
brute force way of doing this?"
Jul 29 '08 #7
On Jul 29, 11:41*am, Gordon <gordon.mc...@n tlworld.comwrot e:
On Jul 29, 3:56 pm, gardnern <nat...@factory 8.comwrote:
On Jul 29, 9:38 am, Captain Paralytic <paul_laut...@y ahoo.comwrote:
On 29 Jul, 15:12, gardnern <nat...@factory 8.comwrote:
On Jul 28, 5:26 pm, "Paul Lautman" <paul.laut...@b tinternet.com>
wrote:
gardnern wrote:
We have X number of data sets, of Y length each.
For example...
Small, Medium, Large
and
Red, Green, Blue, Yellow
We need to generate a list of all possibilities
Small Red
Small Green
Small Blue
Small Yellow
Medium Red
Medium Green
... etc.
The catch...
There can be any number of data sets, and they can be of any length.
Any idea on the algorithm to do this?
I've seen some examples where they just have several nested forloops,
but there could be any number of datasets.
Any help is appericated!
Are these permutations or combinations
In other words, is Small Red different from Red Small?
Combinations, not permutations.- Hide quoted text -
- Show quoted text -
Try this then:
$datasets = array();
$datasets[] = array('Small', 'Medium', 'Large');
$datasets[] = array('Red', 'Green', 'Blue', 'Yellow');
while ($leftset = array_shift($da tasets))
* foreach ($leftset as $left)
* * foreach ($datasets as $rightset)
* * * *foreach ($rightset as $right)
* * * * *echo "{$left} {$right}<br />";
thats fine if theres only 2 datasets, but what if theres 20 of them?
nested foreach loops wont work. I know I have to use recursion, but
not sure how...

If you're dealing with 20 datasets then the odds are your script will
time out long before the operation completes. *The problem you're
describing is of a kind that has exponential run time. *Lets say you
have 2 sets of 256 elements each. *To get all possible combinations
will take 65536 iterations. *Quite a lot but managable on modern
hardware. *Now, lets say you have 3 sets of 256 elements. *You're now
talking about 16777216 iterations. *Adding another set of 256 elements
will result in 4294967296 iterations, 5 sets will mean 1099511627776
iterations, 6 sets will mean 281474976710656 iterations, 7 sets will
mean 720575940379279 36 iterations... When you hit 20 sets the number
my calculator spits out is so big it needs to be written in scientific
notation (1.461501637330 902918203684832 7163e+48, if you're curious).

Even with much smaller sets, the number of sets will cause the
possible permutations to grow exponentially, for example even with
only 8 elements per set the result is 115292150460684 6976 possible
permutations of 20 sets.

At the risk of sounding rude (which is honestly not my intent), I
think you might need to take a careful look at what it is you're
actually trying to achieve, as it looks like there could be something
wrong with your design, or at least with your planned implementation
of it. *You need to be thinking "How can I achieve what I want without
having to do it by brute force?" instead of "How do I implement the
brute force way of doing this?"
This isn't something that will be running often, its used to generate
all possible SKUs for products with every combination of its options.
I found a solution and its working great. Even with 3 (lengths
100,5,9) it only takes 0.23 seconds on a slow machine.

Heres the solution, for reference....

<?php

$startTime = microtime(true) ;

function arrays_mix(){

// This is an array that adds a new
// element each time we are starting
// with a new parent element in the hierarchy
static $parents;
global $mixedArrays;
$combinations = array();
$args = func_get_args() ;
$values = array_shift($ar gs);

if(!empty($args )){

foreach($values as $value){

$parents[] = $value;
call_user_func_ array("arrays_m ix", $args);

// Remove this parent from the array. We need
// a reorganized index so we use array_pop()
array_pop($pare nts);

}

} else {

foreach($values as $value){

$mixedArrays[] = implode(", ",$parents) .", ".$value;

}

}

return $mixedArrays;

}

$sizes = range(0,100);
$widths = array('SS','S', 'W','M','XW');
$colors =
array('red','gr een','blue','ye llow','orange', 'black','brown' ,'white','trans parent');
$result = arrays_mix($siz es,$widths,$col ors);

echo '<pre>';
print_r($result );
echo '</pre>';

$endTime = microtime(true) ;

echo 'Took '.number_format ($endTime - $startTime,2).' seconds, and php
used '.((memory_get_ peak_usage(true )/1024)/1024).'MB of memory';

?>
Jul 29 '08 #8
..oO(gardnern)
>On Jul 29, 9:38*am, Captain Paralytic <paul_laut...@y ahoo.comwrote:
>>
$datasets = array();
$datasets[] = array('Small', 'Medium', 'Large');
$datasets[] = array('Red', 'Green', 'Blue', 'Yellow');

while ($leftset = array_shift($da tasets))
* foreach ($leftset as $left)
* * foreach ($datasets as $rightset)
* * * *foreach ($rightset as $right)
* * * * *echo "{$left} {$right}<br />";


thats fine if theres only 2 datasets, but what if theres 20 of them?
nested foreach loops wont work. I know I have to use recursion, but
not sure how...
If you have a function to combine two datasets A and B:

R1 = A + B

just call it again to add another one:

R2 = R1 + C = (A + B) + C

Nothing special. Of course the combine function should return the result
in a way so that it can be used as an input again.

Micha
Jul 29 '08 #9
On Jul 29, 6:05*pm, gardnern <nat...@factory 8.comwrote:
On Jul 29, 11:41*am, Gordon <gordon.mc...@n tlworld.comwrot e:
On Jul 29, 3:56 pm, gardnern <nat...@factory 8.comwrote:
On Jul 29, 9:38 am, Captain Paralytic <paul_laut...@y ahoo.comwrote:
On 29 Jul, 15:12, gardnern <nat...@factory 8.comwrote:
On Jul 28, 5:26 pm, "Paul Lautman" <paul.laut...@b tinternet.com>
wrote:
gardnern wrote:
We have X number of data sets, of Y length each.
For example...
Small, Medium, Large
and
Red, Green, Blue, Yellow
We need to generate a list of all possibilities
Small Red
Small Green
Small Blue
Small Yellow
Medium Red
Medium Green
... etc.
The catch...
There can be any number of data sets, and they can be of any length.
Any idea on the algorithm to do this?
I've seen some examples where they just have several nested for loops,
but there could be any number of datasets.
Any help is appericated!
Are these permutations or combinations
In other words, is Small Red different from Red Small?
Combinations, not permutations.- Hide quoted text -
- Show quoted text -
Try this then:
$datasets = array();
$datasets[] = array('Small', 'Medium', 'Large');
$datasets[] = array('Red', 'Green', 'Blue', 'Yellow');
while ($leftset = array_shift($da tasets))
* foreach ($leftset as $left)
* * foreach ($datasets as $rightset)
* * * *foreach ($rightset as $right)
* * * * *echo "{$left} {$right}<br />";
thats fine if theres only 2 datasets, but what if theres 20 of them?
nested foreach loops wont work. I know I have to use recursion, but
not sure how...
If you're dealing with 20 datasets then the odds are your script will
time out long before the operation completes. *The problem you're
describing is of a kind that has exponential run time. *Lets say you
have 2 sets of 256 elements each. *To get all possible combinations
will take 65536 iterations. *Quite a lot but managable on modern
hardware. *Now, lets say you have 3 sets of 256 elements. *You're now
talking about 16777216 iterations. *Adding another set of 256 elements
will result in 4294967296 iterations, 5 sets will mean 1099511627776
iterations, 6 sets will mean 281474976710656 iterations, 7 sets will
mean 720575940379279 36 iterations... When you hit 20 sets the number
my calculator spits out is so big it needs to be written in scientific
notation (1.461501637330 902918203684832 7163e+48, if you're curious).
Even with much smaller sets, the number of sets will cause the
possible permutations to grow exponentially, for example even with
only 8 elements per set the result is 115292150460684 6976 possible
permutations of 20 sets.
At the risk of sounding rude (which is honestly not my intent), I
think you might need to take a careful look at what it is you're
actually trying to achieve, as it looks like there could be something
wrong with your design, or at least with your planned implementation
of it. *You need to be thinking "How can I achieve what I want without
having to do it by brute force?" instead of "How do I implement the
brute force way of doing this?"

This isn't something that will be running often, its used to generate
all possible SKUs for products with every combination of its options.
I found a solution and its working great. Even with 3 (lengths
100,5,9) it only takes 0.23 seconds on a slow machine.

Heres the solution, for reference....

<?php

$startTime = microtime(true) ;

function arrays_mix(){

* * * * // This is an array that adds a new
* * * * // element each time we are starting
* * * * // with a new parent element in the hierarchy
* * * * static $parents;
* * * * global $mixedArrays;
* * * * $combinations = array();
* * * * $args = func_get_args() ;
* * * * $values = array_shift($ar gs);

* * * * if(!empty($args )){

* * * * * * * * foreach($values as $value){

* * * * * * * * * * * * $parents[] = $value;
* * * * * * * * * * * * call_user_func_ array("arrays_m ix", $args);

* * * * * * * * * * * * // Remove this parent from the array. We need
* * * * * * * * * * * * // a reorganized index sowe use array_pop()
* * * * * * * * * * * * array_pop($pare nts);

* * * * * * * * }

* * * * } else {

* * * * * * * * foreach($values as $value){

* * * * * * * * * * * * $mixedArrays[] = implode(", ",$parents) .", ".$value;

* * * * * * * * }

* * * * }

* * * * return $mixedArrays;

}

$sizes = range(0,100);
$widths = array('SS','S', 'W','M','XW');
$colors =
array('red','gr een','blue','ye llow','orange', 'black','brown' ,'white','trans parent');
$result = arrays_mix($siz es,$widths,$col ors);

echo '<pre>';
print_r($result );
echo '</pre>';

$endTime = microtime(true) ;

echo 'Took '.number_format ($endTime - $startTime,2).' seconds, and php
used '.((memory_get_ peak_usage(true )/1024)/1024).'MB of memory';

?>
Glanced at your code quickly. Seems that it's working over the data
structures recursively. I noticed you mentioned datasets of 100, 5
and 9 elements. The sums say the result of processing that amount of
state is 4500 iterations.

If this works okay for you then fair enough. :) Just be careful about
the data you feed into this function, as simply adding a 4th array
with 2 elements will double the runtime, or adding a 4th array with 10
elements will increase it 10 fold. Taking the run time you quoted this
would result in 2.3 seconds of run time. A 5th 10 element array will
push this up to 23 seconds, dangerously close to the PHP 30 seconds
default runtime limit.
Jul 30 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2686
by: William Ryan | last post by:
At the risk of sounding like a Big 5 consultant, "It depends". 1) Strongly typed datasets rock, they are faster than untyped, use intellisense... but your reason for wanting to use them is important. I use them every day of my life, but I also don't use them every day of my life and there are reasons for both. 2) How much of your process is dependent on reads vs.
4
1730
by: Alpha | last post by:
I have a small Window application and through out the different forms I create a different dataset. At the begining I used the Tools to drag and drop the SqlDataAdapter, connection and dataset objects to the frist few forms but then later I removed those and created these objects in my code. I now see 3 datasets in the Solution Explorer panel part but not all the datasets that I have in my codes. Are these 3 datasets leftover from the...
9
2900
by: GaryDean | last post by:
We have been noticing that questions on vs.2005/2.0 don't appear to get much in answers so I'm reposting some questions posted by some of the programmers here in our organization that never got answered... There are articles on the new TableAdapters where it says that a key new advantage is that a single TableAdapter, which can have multiple queries, can be used on multiple forms. Now that was in an article on using TableAdapters with...
16
1926
by: Luqman | last post by:
Is it recommended to use datasets in ASP.Net 2.0 / VS.Net 2005 ? Best Regards, Luqman
4
9915
by: Ronald S. Cook | last post by:
I've always used untyped datasets. In a Microsoft course, it walks through creating typed datasets and harps on the benefits. It has you drag all these things around ..wizard, wizard, wizard... code gen, code gen, code gen. What's at the end looks slick, but then there's a ton of generated code that I'm going to have to maintain now. I.e. I like typing things myself (don't like wizards) so I can know exactly what I've done.
25
2764
by: Penelope Dramas | last post by:
Hello, I'm in a front of very serious .net redesign/rewrite of an old VB6 application. I had been asked to make it .NET 2.0 and would like to ask couple of questions regarding data access as this application is heavily data-centric around MSDE database. Would it be better to use custom business objects or extend
2
1363
by: S.Tedeschi | last post by:
Hi all gurus. I'm trying to switch to VS 2005, from VS 2003. I've an ASP.NET 1.1 app heavily relying on StronglyTyped DataSets, with lots of FindByKey..., dataSet.Tablename, and similar methods. Converting to ASP.NET 2.0 leaves dataSets' classes unconverted, as stated by M$; what's worse, I'm not able to use converted dataSets, nor new ones, but just non- typed dataSets declared directly in pages (or code-behind), and so I'm not allowed...
0
1216
by: S.Tedeschi | last post by:
Hi all; as posted some days ago, I'm converting an on-line app; I used to heavily rely on strongly-typed DataSets directly dropped onto pages, and so viewed by code(-behind) as well. In the next two weeks I discovered that such objects are no more directly usable in pages, namely in DataGrid which don't see them any more. Even if rebuilt in Component Designer, and so visible in code, DataSets are invisible in Page Designer, so now it's...
12
3581
by: BillE | last post by:
I'm trying to decide if it is better to use typed datasets or business objects, so I would appreciate any thoughts from someone with more experience. When I use a business object to populate a gridview, for example, I loop through a datareader, populating an array list with instances of a custom class in the middle tier, and then send the array list up to the presentation layer and bind the gridview to it. If I use a typed dataset, I...
0
8297
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8717
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8498
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8600
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1930
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.