Python Forum
Question on runtime and improving nested for loops
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question on runtime and improving nested for loops
#1
I'm writing code to find all solutions of a commercial, physical puzzle.  This is done by having the script go through all possible configurations/permutations, check to see if it works, and record the results into a text file.  

I've broken down that puzzle into 9 pieces (represented by the list), with each piece having 4 states (NOT represented).  Part of the algorithm is to evaluate all possible permutations of 9 pieces across all 4 states.
For example...
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 2
... and then eventually to
0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 1 2
etc.



The code I have to go through all combinations of all 9 elements, with each element having 4 different states is below.

QUESTION 1)
Are there alternative ways to go about this besides a 9-level deep, nested for loop, with each loop going to a max of 4?

MIDEDIT: I've been made aware of itertools, but will need to come back to it later after I have the chance to look through it
ht  tps://docs.python.org/3/library/itertools.html


l = [a, b, c, d, e, f, g, h, i]
for i1 in range(4):
   for i2 in range(4):
       for i3 in range(4):
           #and then 6 more levels deep
QUESTION 2) What's the runtime?  Are my calculations correct?
Not only do I need to do the above, but I need to do the above for each combination of placements in the list.  For example, I also have to repeat that code with the list as:
[a, b, c, d, e, f, g, i, h]
For this, I'm using Heap's Algorithm, which looks like it's n factorial iterations!
ht  tps://en.wikipedia.org/wiki/Heap%27s_algorithm
I'm not sure how practical it is for me to run through this, as it looks like each check gets done...
9! * (4^9)
362,880 * 262,144
87,869,214,720 total times!

I was going to leave this running overnight, but it looks like this may take a few days, or beyond!  I want to make sure it's something that won't take too much time to solve.
Reply
#2
First, you definitely need to look at itertools, as you said you would. It will be way more optimized than doing the loops yourself. Second, brute force is rarely a good idea. You need to look at optimizing your algorithm before you look at optimizing your program. Ask yourself what the standard human techniques for solving the problem are, and try to make use of that. Ask yourself how you can narrow the search space (even iteratively), and make use of that. Third, if you are looking at running things all night, Python is probably not the correct tool. I would switch to a compiled language at that point, or at least look for the choke points in the program and get compiled solutions for those (which is really what using itertools is doing).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  class and runtime akbarza 4 368 Mar-16-2024, 01:32 PM
Last Post: deanhystad
  reduce nested for-loops Phaze90 11 1,902 Mar-16-2023, 06:28 PM
Last Post: ndc85430
  painfully slow runtime when handling data dadazhu 3 964 Jan-20-2023, 07:11 PM
Last Post: snippsat
  Big O runtime nested for loop and append yarinsh 4 1,392 Dec-31-2022, 11:50 PM
Last Post: stevendaprano
  Nested for loops: Iterating over columns of a DataFrame to plot on subplots dm222 0 1,714 Aug-19-2022, 11:07 AM
Last Post: dm222
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,588 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  breaking out of nested loops Skaperen 3 1,223 Jul-18-2022, 12:59 AM
Last Post: Skaperen
  Reducing runtime memory usage in Cpython interpreter david_the_graower 2 2,221 Oct-18-2021, 09:56 PM
Last Post: david_the_graower
  Break out of nested loops muzikman 11 3,343 Sep-18-2021, 12:59 PM
Last Post: muzikman
  How to break out of nested loops pace 11 5,370 Mar-03-2021, 06:25 PM
Last Post: pace

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020