Let L
be a finite set of pair of integers.
I want to partition L
according to connectivity
The connectivity is intuitive :
Rule1
(l1,l2)
,(l3,l4)
is connected if
(l1-l3, l2-l4)
equals to one of (0,1)
,(0,-1)
,(1,0)
,(-1,0)
.
Rule2
If (l1,l2)
,(l3,l4)
is connected and (l3,l4)
,(l5,l6)
is connected, then (l1,l2)
,(l5,l6)
is connected
(auxiliary rule : Only consider Rule1, Rule2.)
I know two methods, but I am not satisfied perfectly for both methods.
method 1)
MorphologicalComponents
As far as I know the method is a graphical approach.
So to apply it to following example :
Connected components of {{1,1},{1,2},{1234512345,1234512345},{1234512345,1234512346}} ?
The answer is of course very easy :
Answer : {{{1,1},{1,2}},{{1234512345,1234512345},{1234512345,1234512346}}}
but to use MorphologicalComponents
, we should introduce a rectangle of dimension 1234512345*1234512346. We start with so many unnecessary pixels. That's the point I am not satisfied.
method 2) ConnectedComponents
ConnectedComponents[{1 <-> 2, 1 <-> 3, 4 <-> 5}]
{{2, 1, 3}, {5, 4}}
To apply this method to solve my problem(mentioned on the beginning of this post), we should assert propositions such as followings, one by one. It will put a lot of strain on the RAM.
{50,50} and {50,51} is connected
{50,51} and {50,52} is connected
{50,52} and {50,53} is connected
...
{60,21} and {60,22} is connected
{60,22} and {60,23} is connected
...
{20,20} and {20,21} is connected
{20,21} and {20,22} is connected
...
Also the kernel of mathematica would look for this Connectivity Table everytime to get the job done. There is no way to explain mathematica kernel what is connectivity?, other then offering this bunch of table.
So I need yet another method. Can you suggest a good method ?
I am almost completely new to this kind of field (connectivity, graph theory, MorphologicalComponents
)
If possible, you can also answer for the case when diagonal connectivity is also allowed. Thank you!
+-+-+-+-+-+-+- After Henrik Schumacher's answer +-+-+-+-+-+-+-
It works well. Thank you!
You can get the picture of Euglena in mathematica help file ref/ MorphologicalComponents.
But one a little sad thing is it is fairly slower then MorphologicalComponents
. For example,
MorphologicalComponents
takes 0. seconds
Schumacher's method takes 0.15625 seconds
I will update the post soon!