473,385 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

algorithm for brute force an variable lenght array

Hello,

I am trying to find out an alternate way to brute-force a variable
length vector with different variable length contents.
int chromossome[MAX_VECTOR_LENGTH]

int max_value_for_each_chromossome[MAX_VALUES]
The only way I am aware of is to build 'n' for(;;) statements, one
inside the other, but I would then need 1.000 (MAX_VECTOR_LENGTH)
for(;;) inside for(;;).

One key issue is that each chromossome member has different max-
elements, for example:

chromossome[0] may have max_value_for_each_chromossome[0] = 5
(chromossome[0] int will range from 0 to 4)

chromossome[1] may have max_value_for_each_chromossome[1] = 2
(chromossome[1] int will range from 0 to 1)
....

Is there a simpler way to achive this rather than the for(;;) inside
for(;;) scheme?

Thank you

Paulo
Jun 27 '08 #1
11 2769
es******@gmail.com wrote:
Hello,

I am trying to find out an alternate way to brute-force a variable
length vector with different variable length contents.
int chromossome[MAX_VECTOR_LENGTH]

int max_value_for_each_chromossome[MAX_VALUES]
The only way I am aware of is to build 'n' for(;;) statements, one
inside the other, but I would then need 1.000 (MAX_VECTOR_LENGTH)
for(;;) inside for(;;).

One key issue is that each chromossome member has different max-
elements, for example:

chromossome[0] may have max_value_for_each_chromossome[0] = 5
(chromossome[0] int will range from 0 to 4)

chromossome[1] may have max_value_for_each_chromossome[1] = 2
(chromossome[1] int will range from 0 to 1)
...

Is there a simpler way to achive this rather than the for(;;) inside
for(;;) scheme?
I don't understand what you're trying to achieve.

--
pete
Jun 27 '08 #2
On May 17, 3:40*pm, estan...@gmail.com wrote:
Hello,

I am trying to find out an alternate way to brute-force a variable
length vector with different variable length contents.

int chromossome[MAX_VECTOR_LENGTH]

int max_value_for_each_chromossome[MAX_VALUES]
So which of these is variable length? Or do you have a set of
MAX_VECTOR_LENGTH arrays each of which has length set in
max_value_each_chromossome[]?
The only way I am aware of is to build 'n' for(;;) statements, one
inside the other, but I would then need 1.000 (MAX_VECTOR_LENGTH)
for(;;) inside for(;;).
What's n? I would think it unlikely you will ever need for-loops
nested 1000-deep.
>
One key issue is that each chromossome member has different max-
elements, for example:

chromossome[0] may have max_value_for_each_chromossome[0] = 5
(chromossome[0] int will range from 0 to 4)

chromossome[1] may have max_value_for_each_chromossome[1] = 2
(chromossome[1] int will range from 0 to 1)
So [0] ranges from 0..4. [1] ranges from 0..1. There doesn't seem to
be a problem.

What is it you are trying to achieve?

Perhaps give a more fully worked out example using small values then
we can see the pattern.

--
Bartc
Jun 27 '08 #3
es******@gmail.com writes:
int chromossome[MAX_VECTOR_LENGTH]

int max_value_for_each_chromossome[MAX_VALUES]
The only way I am aware of is to build 'n' for(;;) statements, one
inside the other, but I would then need 1.000 (MAX_VECTOR_LENGTH)
for(;;) inside for(;;).
I think you're trying to iterate through all possible value
assignments. I'd do something like this (which is untested):

for (i = 0; i < MAX_VECTOR_LENGTH; i++)
chromossome[i] = 0;
while (next_assignment(chromossome)) {
..do something..
}

/* Increments the values in chromossome to the next logical
value. Returns true if successful, false if all possible
assignments have been exhausted. */
static int
next_assignment(int chromossome[MAX_VECTOR_LENGTH])
{
int i;
for (i = 0; i < MAX_VECTOR_LENGTH; i++) {
if (++chromossome[i] < max_value_for_each_chromossome[i])
return true;
chromossome[i] = 0;
}
return false;
}

Also, you misspelled "chromosome".
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
Jun 27 '08 #4
es******@gmail.com wrote:
Hello,

I am trying to find out an alternate way to brute-force a variable
length vector with different variable length contents.
int chromossome[MAX_VECTOR_LENGTH]

int max_value_for_each_chromossome[MAX_VALUES]
The only way I am aware of is to build 'n' for(;;) statements, one
inside the other, but I would then need 1.000 (MAX_VECTOR_LENGTH)
for(;;) inside for(;;).

One key issue is that each chromossome member has different max-
elements, for example:

chromossome[0] may have max_value_for_each_chromossome[0] = 5
(chromossome[0] int will range from 0 to 4)
Okay. chromossome[n] is an int which can hold all values from INT_MIN to
INT_MAX. So unless a max_value_for_each_chromossome[n] is likely to be
beyond these bounds then you can safely use chromossome[n].
chromossome[1] may have max_value_for_each_chromossome[1] = 2
(chromossome[1] int will range from 0 to 1)
...

Is there a simpler way to achive this rather than the for(;;) inside
for(;;) scheme?
It's not entirely clear what you want to do. Can you elaborate?

Jun 27 '08 #5

<es******@gmail.comwrote in message
The only way I am aware of is to build 'n' for(;;) statements, one
inside the other, but I would then need 1.000 (MAX_VECTOR_LENGTH)
for(;;) inside for(;;).
I somehow doubt you need a N^N algorithm. N^2, or two nested fors, is quite
common.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jun 27 '08 #6
On 17 May 2008 at 14:40, es******@gmail.com wrote:
I am trying to find out an alternate way to brute-force a variable
length vector with different variable length contents.

int chromossome[MAX_VECTOR_LENGTH]
int max_value_for_each_chromossome[MAX_VALUES]

The only way I am aware of is to build 'n' for(;;) statements, one
inside the other, but I would then need 1.000 (MAX_VECTOR_LENGTH)
for(;;) inside for(;;).
If 1000 really is what you're looking at, then even if each
"chromossome" only takes values 0 or 1, your brute-forcing will still
be so far from finishing when oblivion takes the earth that you may as
well not bother.
One key issue is that each chromossome member has different max-
elements, for example:
That makes things awkward. If the max-elements is fixed, let's say you
have vectors with n components each taking values 0,...,k-1, then you can
use a single loop counter i from 0 to k^n-1. At each iteration of the
loop, regard i as an integer base k; treat the jth base-k-digit of i as
the value to assign to the jth component on that iteration.

With variable max-elements, I can't off-hand think of a better way than
doing this with k=max(max-elements) and putting tests into the loop to
ignore invalid assignments.

Jun 27 '08 #7
In article <e2**********************************@w7g2000hsa.g ooglegroups.com>
>The only way I am aware of is to build 'n' for(;;) statements, one
inside the other ... One key issue is that each chromossome member
has different max-elements ...

chromossome[0] may have max_value_for_each_chromossome[0] = 5
(chromossome[0] int will range from 0 to 4)

chromossome[1] may have max_value_for_each_chromossome[1] = 2
(chromossome[1] int will range from 0 to 1)
...
As others have said, it is not entirely clear what you are really
trying to achieve here.

My best guess at what you actually want is what I like to call an
"odometer algorithm".

In an odometer, there are a series of wheels that count up from 0
to 9, and when one wheel clicks from 9 to 0, the "next-one-over"
wheel counts up. We can do this trivially for a fixed number of
digits (say, 3) that count 0-to-9 with:

for (a[0] = 0; a[0] < 10; a[0]++) {
for (a[1] = 0; a[1] < 10; a[1]++) {
for (a[2] = 0; a[2] < 10; a[2]++) {
... now the three digits are in a[0] a[1] a[2] ...
}
}
}

So far, everything is pretty obvious, but what if we want our
"odometer" to read, e.g.:

000, 001, 002,
010, 011, 012,
020, 021, 022,
030, 031, 032,
040, 041, 042,
100, 101, 102,
110, 111, 112,
...
940, 941, 942

That is, the last digit only counts 0..2 and the middle digit only
counts 0..5? Well, again, this is pretty obvious:

for (a[0] = 0; a[0] < 10; a[0]++) {
for (a[1] = 0; a[1] < 5; a[1]++) {
for (a[2] = 0; a[2] < 3; a[2]++) {
... now the three digits are in a[0] a[1] a[2] ...
}
}
}

What if the odometer does not have exactly *three* digits, but
rather some variable number of digits? (Let us call this n and
assume it is no more than MAX_N.)

Here is where we take advantage of the fact that the outermost loop
runs over a[0], the next loop runs over a[1], the next over a[2],
and so on. Then we simply start by zeroing-out the entire odometer
(let me write this as a general-purpose function):

/* we will see what these are for in a moment */
#define NO_OVERFLOW 0
#define OVERFLOW 1

void odo_init(int *odo, int n_digits) {
int i;
for (i = 0; i < n_digits; i++)
odo[i] = 0;
}

and then run a loop that repeats until our odometer "overflows"
(back to all-zeros if it is a traditional car-odometer):

int a[MAX_N];
... find n ...
assert(n <= MAX_N);
odo_init(a);
do {
... work with odometer in a ...
} while (odo_increment(a, n) == NO_OVERFLOW);

Now we need only write the "increment" function. If the odometer
were a traditional car odometer, with all the digits running from
0 to 9 inclusive, this would look like:

/*
* Increment an odometer by "clicking the digits". Return
* OVERFLOW if the odometer overflows, or NO_OVERFLOW if not.
*/
int odo_increment(int *odo, int n_digits) {
int i;

/*
* Click the right-most (least-significant) digit first,
* and stop (and return NO_OVERFLOW) as soon as we can
* increment a digit without having to reset it to 0.
* If we have to reset the digit to 0, do that and continue
* the loop to increment the next-more-significant digit.
*/
for (i = n_digits - 1; i >= 0; i--) {
if (odo[i] < 9) {
odo[i]++;
return NO_OVERFLOW;
}
odo[i] = 0;
}

/* If we got here, we cranked everything all the way to 0 again. */
return OVERFLOW;
}

It should be pretty obvious how to modify odo_increment() to take
a second array of "range for each odometer digit" -- which can of
course vary per "digit" -- instead of just assuming [0..9].

It should be equally obvious how to rearrange the "odometer digits"
if "rightmost-counts-fastest" is not what is desired. The key work
all happens in the odo_increment() function.

(Note that there are other ways to solve this problem. For the
most trivial cases -- an odometer that reads 000 to 999 for instance
-- we can just do:

for (i = 0; i < 1000; i++) {
a[0] = i / 100;
a[1] = (i / 10) % 10;
a[2] = (i / 100) % 10;
... a[] holds the three digits ...
}

and we can usually eliminate the array "a" entirely. But calling
it an "odometer" makes things much clearer, I think.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: gmail (figure it out) http://web.torek.net/torek/index.html
Jun 27 '08 #8
It's not entirely clear what you want to do. Can you elaborate?- Hide quoted text -

Hi,

Thanks all for the replies. I think the best way is to show a numeric
example:

for a given max_chromosome = 3 (I will not need to go upto
MAX_VECTOR_LENGHT, which is 1000):

max_value_for_each_chromossome[0] = 2 (0, 1)
max_value_for_each_chromossome[1] = 1 (0)
max_value_for_each_chromossome[2] = 3 (0, 1 and 2)

I need to get the following sequence of valid cobinations:

0, 0, 0
0, 0, 1
0, 0, 2
1, 0, 0
1, 0, 1
1, 0, 2

In a 3-number combination is easy to build a 3-level for(;;)
statement, but this value can be upto a thousand (variable).

Thank you all for the help, I appreciate it.

Paulo
Jun 27 '08 #9
On May 18, 2:58*am, estan...@gmail.com wrote:
It's not entirely clear what you want to do. Can you elaborate?- Hide quoted text -

Hi,

Thanks all for the replies. I think the best way is to show a numeric
example:

for a given max_chromosome = 3 (I will not need to go upto
MAX_VECTOR_LENGHT, which is 1000):

max_value_for_each_chromossome[0] = 2 (0, 1)
max_value_for_each_chromossome[1] = 1 (0)
max_value_for_each_chromossome[2] = 3 (0, 1 and 2)

I need to get the following sequence of valid cobinations:

0, 0, 0
0, 0, 1
0, 0, 2
1, 0, 0
1, 0, 1
1, 0, 2

In a 3-number combination is easy to build a 3-level for(;;)
statement, but this value can be upto a thousand (variable).
I used this code:

#include <stdio.h>
#include <stdlib.h>

#define n 3

int main(void) {

int a[n] = {2,1,3};
int b[n] = {0,0,0};
int i,j,k;

while(1) {

for (i=0; i<n; ++i) printf(" %d",b[i]); puts("");

j=n-1;

while(1) {
++b[j];
if (b[j]==a[j]) {
b[j]=0;
if (j==0) exit(0);
--j;
}
else
break;
};
};

}

Array a corresponds to your max_value vector. Array b is an auxilliary
counting vector.

Probably other replies have suggested similar.

Note that for bigger values of n, and larger values in your max_value
vector (a above) the task may take a long time to finish. As has also
been noted.

--
Bartc
Jun 27 '08 #10
On May 17, 6:59 pm, Chris Torek <nos...@torek.netwrote:
What if the odometer does not have exactly *three* digits, but
rather some variable number of digits? (Let us call this n and
assume it is no more than MAX_N.)
This is the case.
It should be pretty obvious how to modify odo_increment() to take
a second array of "range for each odometer digit" -- which can of
course vary per "digit" -- instead of just assuming [0..9].
Not for me :) ...
-- we can just do:

for (i = 0; i < 1000; i++) {
a[0] = i / 100;
a[1] = (i / 10) % 10;
a[2] = (i / 100) % 10;
... a[] holds the three digits ...
}
If I understood you correclty, I will only one nested for(;;). I still
have to think and read your explanation a few more times to see if I
can adapt your idea to my code.

Thank you

Paulo
Jun 27 '08 #11

<es******@gmail.comwrote in message
I need to get the following sequence of valid cobinations:

0, 0, 0
0, 0, 1
0, 0, 2
1, 0, 0
1, 0, 1
1, 0, 2

In a 3-number combination is easy to build a 3-level for(;;)
statement, but this value can be upto a thousand (variable).
Do you think maybe Chris Torek can read minds?

The odometer is a bit fiddly to code, but if you say

/*
advance odometer 1 step
Params: numvals - number of values in each wheel
Nvals - number of wheels
vals - current wheel positions
Returns: 0 if running, 1 if clocked over
*/
int tick(const int *numvals, int Nvals, int *vals)
{
int i = Nvals - 1;

while(i >= 0)
{
vals[i]++;
if(vals[i] < maxvals[i])
return 0;
vals[i] = 0;
i--;
}
return 1;
}

Remember that a thousand wheels is too many. Assming two values per wheel,
the program will start to run into severe time difficulties at about Nvals =
32.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jun 27 '08 #12

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

Similar topics

16
by: cody | last post by:
I have to write an algorithm with must ensure that objects are put in buckets (which are always 4 in size). The objects have two properties: A and B. It is not allowed that in a bucket are objects...
28
by: joshc | last post by:
If I have an array of data that I know to be sorted in increasing order, and the array is less than 50 elements, and I want to find the first element greater than a certain value, is a simple...
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
29
by: Roy Gourgi | last post by:
Hi, I am new to C#. I have the same time scheduling program written in C++ and it is 5 times faster than my version in C#. Why is it so slow as I thought that C# was only a little slower than...
2
by: Peter Schmitz | last post by:
Hi again, I just need to write the following function to search in a binary buffer for a given pattern: BOOL CheckBufferForPattern(BYTE *buffer,int bufferlen,BYTE *pattern, int patternlen,...
16
by: a | last post by:
We are writing an app that assigns people to teams based on their curent score. Teams are 8 people, there are 2 teams. (i would like it to be flexible, but this is a start). I need an algorithm...
2
by: The One We Call 'Dave' | last post by:
I have a list of DateTime objects stored in a collection: SortedList<DateTime,Object> MyDates=new SortedList<DateTime,Object>(); The dates, which can be accessed via MyDates.Keys, are stored in...
4
prometheuzz
by: prometheuzz | last post by:
Hello (Java) enthusiasts, In this article I’d like to tell you a little bit about graphs and how you can search a graph using the BFS (breadth first search) algorithm. I’ll address, and...
38
by: Boon | last post by:
Hello group, I've been toying with a simple sudoku solver. I meant for the code to be short and easy to understand. I figured "brute force is simple" -- who needs finesse, when you've got...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.