Permutation with criteria

BigBadWazza

New Member
Joined
May 21, 2011
Messages
5
Hi there, I have just about done my head in searching for a solution to my problem. I have a list of data similar to the following

AB
BC
CD
AC
AD
BD
I need to be able to create a list of all possible combinations with a criteria of the first character of the second and each subsequent part of the combination needs to match the last character of the previous part of the combination.

So the answers would be
AB BC CD
AB BD
AC CD
AD
BC CD
CD

I have used A, B, C and D in my example but my data can be up to a hundred variables if that makes difference.

I hope I have explained this ok, but please let me know if further information is required.
 
Peter:- Yes ,I also found the codes limitation , & mine!!
PGC,:- That seems exceedingly clever, Can you explain what makes the code loop back at the end of each, new permutation being placed on the sheet, (when you would think the sub should end) and how the code knows the previous "sResult" to start a new search on.
i.e. If the Column "A" data is "AB","BC","CD","CE" then the first "sResult" = "AB,BC,CD" , but when the code loops back in the sub then "sResult" becomes "AB,BC", which starts the next search.
I'm sure you know what I mean,
Regards Mick
 
Last edited:
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Can you explain what makes the code loop back at the end of each, new permutation being placed on the sheet, (when you would think the sub should end) and how the code knows the previous "sResult" to start a new search on.

Hi Mick

The procedure is recursive, which means that the procedures calls itself and executes the same code in a different context, that means executes the same code using a different set of data.

The way I get to control the context is by a judicious choice of how the parameters are passed.

In the case of vArr and lRow you want the code to access the same variable from beginning to end of the program execution. vArr is only read, not written, and so no need to waste time and memory copying it. lRow will have its value incremented each time you write a result and so you want to keep referring to the same variable.
Concusion: you pass vArr and lRow ByRef.

In the case of sResult and lPos you want the opposite. Each time you call Get_Permutations() these are the values that define the context. Each time Get_Permutations() executes it has its own sResult and lPos, it executes the code for those values and when it returns (back to itself) there's not confusion because the calling procedure has a different set of values.
Concusion: you pass sResult and lPos ByVal.


i.e. If the Column "A" data is "AB","BC","CD","CE" then the first "sResult" = "AB,BC,CD" , but when the code loops back in the sub then "sResult" becomes "AB,BC", which starts the next search.
I'm sure you know what I mean,
Regards Mick

I hope now it's clear

Assuming your example, vArr is an array (1 to 4) with the values ("AB","BC","CD","CE")

An example with part of the code execution:

1 - in the middle of the code execution Get_Permutations() is called with the context values sResult="AB,BC" and lPos=3
Let's call this Level 1

2 - it starts looping and finds that at position 3 "CD". Since the last character of sResult and the first character of "CD" are the same then Get_Permutations() calls itself with parameters sResult & "CD" and lPos+1

3 - Get_Permutations() is called (from before) with parameters "AB,BC,CD" and lPos=4
This will be a new context for Get_Permutations(), let's call it Level 2

4 - it starts looping and finds that at position 4 "CE" in which the last character of sResult and the first character of "CD" are not the same. Then the loop ends and since there were no matches this is the end of this thread and so sResult is written to the worksheet after incrementing lRow.
The procedure then ends and returns to Level 1.

5 - We are now back in Level 1. remember that before calling itself in Level 1 its sResult was "AB,BC". This has not changed. The code continues the loop and checks the next element, "CE". Since the last character of sResult and the first character of "CE" are the same then Get_Permutations() calls itself with parameters sResult & "CE" and lPos+1

6 - Get_Permutations() is called (from before) with parameters "AB,BC,CE" and lPos=5
This is again a new context for Get_Permutations(), in Level 2

... and so on

HTH
 
Last edited:
Upvote 0
Hi PCG, Thank you for your in depth explanation, I will study it further.
I have been playing with you code and felt it functioning was some how due to how the variables where passed, so to see the Effect they had I ran the code "as is" and then changed all the "ByVal"s to "ByRef" and the variable (lPos+1) to "2" and ran the code again, but to my surprise the results on your basic data (17 rows) was exactly the same.
So I am a bit unsure what effect they had in the original code.
I really like the idea, but still rather unsure as to How & why.
Regards Mick
 
Upvote 0
Hi Peter, I am having trouble with the code running out of stack space when I increase to a large number of variables (around 1000). Is it possible to define the values for the first 2 characters and the last 2 characters of each string? This way the answers would be considerably refined.
eg if the results currently are
1 AB,BC,CD,DG,GH
2 AB,BC,CD,DH
3 AB,BC,CE,EF,FG,GH
and you define the first 2 characters as "AB" and the last 2 as "DH", then the result would be
1 AB,BC,CD,DH

Even just defining the first 2 characters would help.

Kind regards
Warren
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top