Your approach is more compact than mine, which would be a significant advantage for large collections.
Also, I'd discard the 0 value before grouping (since 0 here is an artifact of the representation and not a part of the original data set). This gives: OID=: 1 9 6 2 10 7 3 11 4 NID=: 2 10 7 3 11 12 4 8 5 G=: NID OID} i.1+>./NID,OID (</. #\)}.{:@({&G^:a:"0)i.#G +---------+------+---------+ |1 2 3 4 5|6 7 12|8 9 10 11| +---------+------+---------+ Or, this could be sorted. That said, note that this only works for tree structured graphs. Which is probably always going to be the case here. Thanks, -- Raul On Fri, Sep 3, 2021 at 12:53 PM Michal Wallace <michal.wall...@gmail.com> wrote: > > Oh, Raul's version using +. (or) on the connection matrix is way nicer than > my version, and gets rid of my bug with directions. Do that instead. :) > > On Fri, Sep 3, 2021 at 12:31 PM Raul Miller <rauldmil...@gmail.com> wrote: > > > I should note that your example connection matrix does not seem to > > match the oid, nid values you displayed. > > > > OID=: 1 9 6 2 10 7 3 11 4 > > NID=: 2 10 7 3 11 12 4 8 5 > > > > Here's the connection matrix I see represented: > > ]CM=: 1 (<:OID,.NID)} 0$~,~>./OID,NID > > 0 1 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 1 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 0 > > 0 0 0 0 0 0 1 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 0 0 0 1 0 0 > > 0 0 0 0 0 0 0 0 0 0 1 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 > > > > Note that I am using a recent J version here. In older versions of J, > > that would have to be 1 (<"1<:OID,.NID)} 0$~,~>./OID,NID > > > > Anyways, its transitive closure would be: > > (+. +./ .*~)^:_ CM > > 0 1 1 1 1 0 0 0 0 0 0 0 > > 0 0 1 1 1 0 0 0 0 0 0 0 > > 0 0 0 1 1 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 0 > > 0 0 0 0 0 0 1 0 0 0 0 1 > > 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 0 1 0 1 1 0 > > 0 0 0 0 0 0 0 1 0 0 1 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 > > > > But that does not match your suggested grouping operation (foo), so > > let's assume that connections work both ways: > > > > ]TC=: (+. +./ .*~)^:_ CM+.|:CM > > 1 1 1 1 1 0 0 0 0 0 0 0 > > 1 1 1 1 1 0 0 0 0 0 0 0 > > 1 1 1 1 1 0 0 0 0 0 0 0 > > 1 1 1 1 1 0 0 0 0 0 0 0 > > 1 1 1 1 1 0 0 0 0 0 0 0 > > 0 0 0 0 0 1 1 0 0 0 0 1 > > 0 0 0 0 0 1 1 0 0 0 0 1 > > 0 0 0 0 0 0 0 1 1 1 1 0 > > 0 0 0 0 0 0 0 1 1 1 1 0 > > 0 0 0 0 0 0 0 1 1 1 1 0 > > 0 0 0 0 0 0 0 1 1 1 1 0 > > 0 0 0 0 0 1 1 0 0 0 0 1 > > > > That gives us something close to the grouping you asked for: > > TC </. 1+i.#TC > > +---------+------+---------+ > > |1 2 3 4 5|6 7 12|8 9 10 11| > > +---------+------+---------+ > > > > The ordering here is different, but if that's important we could try to > > fix it: > > > > (/: {:@>) TC </. 1+i.#TC > > +---------+---------+------+ > > |1 2 3 4 5|8 9 10 11|6 7 12| > > +---------+---------+------+ > > > > I hope this helps, > > > > > > -- > > Raul > > > > 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 > > > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm