[Edu-sig] Algorithm help

Arthur Siegel ajs@ix.netcom.com
Sat, 2 Mar 2002 11:18:51 -0500


Having inititally thought that asking for advice on algorithms was a bit OT
for edu-sig and it more appropriate to go to the vast web for resources,
I do a Google on "permutations algorithm" and get back as hit #1 a
posting here by Kirby from May 1991. Great.

So I click my ruby (small "r")  slippers.

My geometry meanderings have more and more (and somewhat unexpectedly)
led me into needing algorithms related to permutations and subsets.  E.g., I
have X points in space and want to draw all the lines defined by them -
a line being defined by 2 points.

So I come up with:

def unique2(pointslist):
         P=[]
          s=pointslist[:]
          while len(s):
              r=s.pop(0)
              for x in s:
                    P.append([r,x])
          return P

Or for some number of points > 3, I want to draw all the planes defined by
them - a plane being defined by 3 points.

So,

def unique3(pointslist):
       P=[]
       s=pointslist[:]
       while len(s):
              r=s.pop(0)
              t=s[:]
              while len(t):
                    m=t.pop(0)
                    for x in t:
                       P.append([r,m,x])
       return P


I haven't been able to come up with a generalization - an alogrithm with a
second parameter as the number of elements I want in the returned subset.

I am sure these are well-worn issues - but I haven't found the right
resource for answers.

Anybody have ideas?

Art