Bounding box on clusters in a 2D list

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Sun Apr 24 17:43:00 EDT 2005


superprad at gmail.com:
> hi Bearphile! That really gives me an idea.Thanks much for that. Yes
as
> you said the algorithm reaches a maximium recursion depth for larger
> sets i tried.

You can improve the little flood filling function, avoiding the "bad"
Python recursivity.


> Do you see where I am heading

Your problem still looks a bit ill defined to me (see Bengt Richter's
nested boxes case), but you can probably modify this part of my
connected():

.   freq = {}
.   for row in m:
.     for e in row:
.       if e in freq:
.         freq[e] += 1
.       else:
.         freq[e] = 1

For the *values* of the freq dictionary you can use the 4 coordinates
of the bounding box of the key-specific cluster, that is updating its
far left, far right, etc. coordinates, with something like this:

box[e] = ( max(x, box[e][0]), min(x, box[e][1]), max(y, box[e][2]),
min(y, bbox[e][3] )

Or something faster/more correct. It's not difficult, I presume.
(connected() debug: if you want to keep the original matrix m, at the
end of the connected() you can set at 1 all its values different from
0.)

Bear hugs,
Bearophile




More information about the Python-list mailing list