1

This question came up for me when thinking about an answer to this: https://stackoverflow.com/questions/16326318/finding-blocks-in-arrays. I had the idea of listing the 1's, for example:

example1 =
  [[0,1,0,0]
  ,[0,1,0,0]
  ,[0,1,1,0]
  ,[0,0,0,0]
  ,[0,1,1,0]]

1's => [(0,1),(1,1),(2,1),(2,2),(4,1),(4,2)]

example2 =
  [[0,1,0,1]
  ,[0,1,0,1]
  ,[0,1,1,1]
  ,[0,0,0,0]
  ,[0,1,1,0]]

1's => [(0,1),(0,3),(1,1),(1,3),(2,1),(2,2),(2,3),(4,1),(4,2)]

My next idea was to sort the 1's so as to arrange them in a sequence that would group the objects (an object includes all 1's that are connected, meaning adjacent either vertically or horizontally).

Others and myself had difficulty coming up with an Ordering definition in Haskell that would work, and since my math skills are limited, I thought I would ask more knowledgeable folk: Is it possible to create a sequence that would list the objects sequentially by defining an Ordering relation between the tuples of 1's? (As I understand, an Ordering relationship can be defined between any two elements in the set.)

My intuitive ordering:

Example 1 (seems already sorted): [(0,1),(1,1),(2,1),(2,2),(4,1),(4,2)]
Example 1 grouped:                [[(0,1),(1,1),(2,1),(2,2)],[(4,1),(4,2)]]

 Example 2 sorted: [(0,1),(1,1),(2,1),(2,2),(2,3),(1,3),(0,3),(4,1),(4,2)]
 Example 1 grouped: [[(0,1),(1,1),(2,1),(2,2),(2,3),(1,3),(0,3)],[(4,1),(4,2)]]

P.S. Suggestions for more appropriate tags for this question are welcome.

0 Answers0