As usual, a minor typo:
Since cl finds only the shortest cycles (which are all the same length), I
added and >@
to the first {:”1. That’s why the result is unboxed. However cls deliberately
doesn’t
have this, as its results may be of different lengths.
Here they are again in case I copied the wrong versions in the last message:
cl=: [: >@{:"1 ] ([: ; lp&.>/"1)@]^:(*./@:~: {:@>@{:"1)^:_ lp
lp=: [ ((-. (,: |.)@(_2&{.)) ; ])"2 1 ] ,"1 0 {:"1@[ {~ {:@] I.@:= {."1@[
cls=: [: {:"1 ] ([: ; <@(lp&>/@]`(,:@])@.(= {:@>@{:))"1)^:_ lp
Sorry!
Louis
> On 25 Aug 2016, at 14:34, Louis de Forcrand <[email protected]> wrote:
>
> Got this:
>
> cl=: [: {:"1 ] ([: ; lp&.>/"1)@]^:(*./@:~: {:@>@{:"1)^:_ lp
> lp=: [ ((-. (,: |.)@(_2&{.)) ; ])"2 1 ] ,"1 0 {:"1@[ {~ {:@] I.@:=
> {."1@[
>
> Not particularly elegant or concise, but it works (G -: edges):
>
> ((, |."1) G) cl 1
> 1 2 4 5 6 7 8 9 10 11 12 13 24 25 26 23 16 15 14 22 21 20 19 18
> 17 3 1
> 1 3 17 18 19 20 21 22 14 15 16 23 26 25 24 13 12 11 10 9 8 7 6 5
> 4 2 1
>
> It will find all shortest cycles in the graph x, starting at y. The
> modification of G is to make it undirected; cl works on
> directed graphs as well (I think!).
> I believe this modification will make it less efficient, but it will find
> _all_ cycles in the graph:
>
> cls=: [: {:"1 ] ([: ; <@(lp&>/@]`(,:@])@.(= {:@>@{:))"1)^:_ lp
>
> However I haven’t tested it on another graph yet, so don’t take my word for
> it.
> Nice challenge, thank you.
>
> Cheers,
> Louis
>
>> On 25 Aug 2016, at 12:13, R.E. Boss <[email protected]> wrote:
>>
>> 1 2 4 5 6 7 8 9 10 11 12 13 24 25 26 23 16 15 14 22 21 20 19 18 17 3 1
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm