I would just use (a variant on) transitive closure. 

Utilities for TC are helpfully provided by the "Sample Topics" in the 
Dictionary at 

   http://www.jsoftware.com/help/dictionary/samp20.htm  

and  

   http://www.jsoftware.com/help/dictionary/samp21.htm :


         CM      =:  (#. e.~ [: i. [ , [)~ 1 + >./@:, 
         closure =:  ([ +. +./ .*.)^:a:~
      
         ln      =:  [EMAIL PROTECTED]
   
         dist    =:  [: ln 0 |: closure@:CM
   
         length  =:  <"1@:[ { dist@:]

So:
         r       =:  _2 ]\  0 2  0 1  2 3  2 5  2 7  5 6  1 4
         c       =:  _2 ]\  4 6  0 6  2 3
   
         c length r
      _ 2 0

You can have a little more fun with this:   

         mbp     =:  ({ =)^:_1
         ln      =:  [: ~:&>/`(_ ,: >@:{:) } {:@$ ; mbp
   
         c length  r
      _ 2 0

And:
   
         mbp     =:  i."1&1
      
         c length  r
      _ 2 0

Even if you're not going to define a node to be its own neighbor (i.e. no 
reflexive property), I would still mark such nodes specially:

         dist     =:  (* _1 ^ =@:i.@:$)@:dist f.

         dist r
      __  0  0  1  1  1  2  1
       _ __  _  _  0  _  _  _
       _  _ __  0  _  0  1  0
       _  _  _ __  _  _  _  _
       _  _  _  _ __  _  _  _
       _  _  _  _  _ __  0  _
       _  _  _  _  _  _ __  _
       _  _  _  _  _  _  _ __
      
-Dan

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to