You don't need both arrays to store this graph.
You can combine them into one array that maps from an index to the "next"
index.

[ g =: nid oid } i. 13

0 2 3 4 5 5 7 12 8 10 11 8 12


If an index points to itself, there's no next index.


Now you can take the "transitive closure" of this relationship by following
each item along the chain until it stops changing:


<@((g{~])^:a:)"0 i=.i.#g

┌─┬─────────┬───────┬─────┬───┬─┬──────┬────┬─┬─────────┬───────┬────┬──┐

│0│1 2 3 4 5│2 3 4 5│3 4 5│4 5│5│6 7 12│7 12│8│9 10 11 8│10 11 8│11 8│12│

└─┴─────────┴───────┴─────┴───┴─┴──────┴────┴─┴─────────┴───────┴────┴──┘


Now group them by the last item in the chain:

>>./ each <@((g{~])^:a:)"0 i=.i.#g

0 5 5 5 5 5 12 12 8 11 11 11 12


and use these as the keys to the original indices:


i </.~ >>./ each <@((g{~])^:a:)"0 i=.i.#g

┌─┬─────────┬──────┬─┬───────┐

│0│1 2 3 4 5│6 7 12│8│9 10 11│

└─┴─────────┴──────┴─┴───────┘


This is almost your answer, but doesn't account for the fact that 11->8.

Since you only care about groups, you could fix this by reversing the
direction of the arrow any time the value is going down.


Or you could use this same basic idea and make a better version of my dumb
algorithm. :D



On Fri, Sep 3, 2021 at 11:21 AM Pablo Landherr <pablo.landh...@gmail.com>
wrote:

> I want to group items that are linked to each other. I tried to use some
> kind of connection matrix
>
> nid =/ oid NB. an example
>
> 0 0 0 1 0 0 0 0 0
>
> 0 0 0 0 1 0 0 0 0
>
> 0 0 0 0 0 1 0 0 0
>
> 0 0 0 0 0 0 1 0 0
>
> 0 0 0 0 0 0 0 1 0
>
> 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 0
>
> 0 0 0 0 0 0 0 0 0
>
>
> to group items linked to each other but I can't figure out how to proceed
> from there. I'm hoping someone has a trick in their toolbox to share with
> me.
>
> oid is the first number of all connections and nid is the second number. So
> in this example 6 is connected to 7, 7 is connected to 12 and 12 has no
> further connection. What should foo be?
>
>
> oid,.nid
>
> 1 2
>
> 9 10
>
> 6 7
>
> 2 3
>
> 10 11
>
> 7 12
>
> 3 4
>
> 11 8
>
> 4 5
>
> oid foo nid
>
> ┌─────────┬─────────┬──────┐
>
> │1 2 3 4 5│9 10 11 8│6 7 12│
>
> └─────────┴─────────┴──────┘
>
>
> Thank you,
>
> Pablo
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to