473,402 Members | 2,046 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,402 software developers and data experts.

Best way to do multiply nested loops

I'm working on a project, and I have an arbitrarily high number of dimensions N. I want to create a loop over all combinations for each of these N dimensions from say 0 to M-1. So something like:

for(i1=0;i1<M;++i1){
for(i2=0;i2<M;++i2){
.
.
.
for(iN=0;iN<M;++iN){
check for some condition, if condition satsified, we add vector
[i1, ..., iN] to a list
}
.
.
.
}
}

Right now I am doing this with recursion, but N may get to be VERY large, so I do not want to have to worry about recursion limits on different machines and whatnot. Anyone know a good way to do this looping? I know that this type of loop for arbitrarily large N,M will have more operations than are reasonable, but there is other types of compression and whatnot, so don't really worry about that issue. Thank you for all of the help.
Apr 18 '07 #1
3 2532
JosAH
11,448 Expert 8TB
So you want C(n, k) for, say, n == 4 and k == 2 so you want the tuples
01, 02, 03, 12, 13 and 23, right?

Note that the number at position i is less than the number at position i+1 and
that the number at position j must be <= n-(k-j). If you start at the first
combination (here: 01) and start at the rightmost position, do an increment until
you can satisfy both of the above conditions, you can enumerate all
combinations iteratively in a single loop.

kind regards,

Jos
Apr 18 '07 #2
Thanks for the reply, but not quite what I am doing. I'm not taking combinations nor permutations or anything like that. Essentially, I just want a good way to have an arbitrarily large loop. Just like the code I wrote. I want N successive nested for loops, each of which goes from from 0 to M-1 (so N^M total times in the inside loop).

So, for N=2 I'd have

for(i1=0;i1<M;i1++)
for(i2=0;i2<M;i2++)

and for N=3 I'd have
for(i1=0;i1<M;i1++)
for(i2=0;i2<M;i2++)
for(i3=0;i3<M;i3++)

etc.

My N is a command line parameter, since my dimensions my change from one run to the next. So I cannot "hard code" this set of loops.

It's a relatively easy recursion... but I was hoping someone might know a "trick" that my be more "C++ optimized" since recursion for large N may run into some issues. Thanks.

So you want C(n, k) for, say, n == 4 and k == 2 so you want the tuples
01, 02, 03, 12, 13 and 23, right?

Note that the number at position i is less than the number at position i+1 and
that the number at position j must be <= n-(k-j). If you start at the first
combination (here: 01) and start at the rightmost position, do an increment until
you can satisfy both of the above conditions, you can enumerate all
combinations iteratively in a single loop.

kind regards,

Jos
Apr 18 '07 #3
AdrianH
1,251 Expert 1GB
Thanks for the reply, but not quite what I am doing. I'm not taking combinations nor permutations or anything like that. Essentially, I just want a good way to have an arbitrarily large loop. Just like the code I wrote. I want N successive nested for loops, each of which goes from from 0 to M-1 (so N^M total times in the inside loop).

So, for N=2 I'd have

for(i1=0;i1<M;i1++)
for(i2=0;i2<M;i2++)

and for N=3 I'd have
for(i1=0;i1<M;i1++)
for(i2=0;i2<M;i2++)
for(i3=0;i3<M;i3++)

etc.

My N is a command line parameter, since my dimensions my change from one run to the next. So I cannot "hard code" this set of loops.

It's a relatively easy recursion... but I was hoping someone might know a "trick" that my be more "C++ optimized" since recursion for large N may run into some issues. Thanks.
Recursion is not a bad thing. For most compilers, this is not a problem. In fact many compiler optimisers will take something that is recursive and may be able to unwind it to somewhat, and if not, your recursion depth is limited only to your stack size which you can increase if you want to. Since you are using an arbitrarily nesting depth, recursion is probably the best way to go for its dynamic ability to handle the situation.

To explicitly unwind the recursion would require an upper bound, and to go beyond it would require recursion. If this is really what you want, try BOOST’s MPL preprocessor language. I’m working on an update for it currently to speed it up (it is supposed to be quite fast already) and make it more readable, but it won’t be complete for at least a month. BOOST’s current version is capable of writing many (close to) arbitrary things for you, given that you write the preprocessor code to do it.

In all honesty though, I really don’t think that it is worth it. What issues do you see that could occur? Blowing the stack a little sooner? Making it slightly slower? Remember, optimisers are quite powerful now. You may be just wasting your time.


Adrian
Apr 18 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

25
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
0
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # David Eppstein of the Geometry Junkyard fame gave this elegant # version for returing all possible pairs from a range of n numbers. def combo2(n): return...
4
by: dw | last post by:
Hello all. We're doing a site with teams and their members. We've got a page where we need to display people according to who belongs to a which team. I've heard that nested loops are bad, but...
9
by: Pushkar Pradhan | last post by:
I need to time my blocked matrix multiply code to decide the optimal block size. I know two ways to do it: time() and clock() functions. In order to get time is usec clock() must be divided by...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
5
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
Could someone give me a simple example of nested scope in C#, please? I've searched Google for this but have not come up with anything that makes it clear. I am looking at the ECMA guide and...
4
by: toddlahman | last post by:
I am using two while loops that are nested. The first loop (post name) returns the full column of results, but the second (post modified) only returns the first row of the column. Is there another...
13
by: Fredrik Lundh | last post by:
Patrol Sun wrote: so why exactly are you trying to nest 20 or 100 for-in loops? </F>
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...
0
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...

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.