caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Diego Olivier Fernandez Pons <dofp.ocaml@gmail.com>
To: Ly Kim Quyen <lykimq@gmail.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Compute the equivalence classes
Date: Tue, 1 Nov 2011 18:55:24 +0100	[thread overview]
Message-ID: <CAHqiZ-Jg36Tu-4zgXe9M1dUMqQQ-NJwjKt2LO0Oo-N_EFc4-+w@mail.gmail.com> (raw)
In-Reply-To: <CAHqiZ-+T=7nep4TmUuzzYu0qMpO908TmegwaNCg7f--+4PVeGw@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1359 bytes --]

... sorry, sent too early

> You can compute the strongly connected components directly on the
transitive closure. Notice you don't need to transpose it explicitely

// Strong connected components matrix form
let sccmatrix = function matrix ->
  let n = Array.length matrix in
  let result = Array.make_matrix n n 0 in
  for i = 0 to n - 1 do
      for j = 0 to n - 1 do
          result.(i).(j) <- min matrix.(i).(j) matrix.(j).(i)
      done;
  done;
  result

Now you just have to collect the results in a list of lists.
You will need to avoid generating multiple times the same component.

// Output list of lists from matrix
let output = function matrix ->
    let n = Array.length matrix in
    let marked = Array.make n false in
    let result = ref [] in
    for i = 0 to n - 1 do
        if (not marked.(i)) then
        begin
            let component = ref [i] in
            for j = i + 1 to n - 1 do
                if matrix.(i).(j) = 1 then
                begin
                     marked.(j) <- true;
                     component := j :: !component
                end
            done;
            result := !component :: !result
         end
     done;
     !result

Since the code uses for loops, it needs references to store the results.

One can do a nicer code and merge the two functions sccmatrix and output

        Diego Olivier

[-- Attachment #2: Type: text/html, Size: 1868 bytes --]

  reply	other threads:[~2011-11-01 17:55 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-10-31 15:36 Ly Kim Quyen
2011-10-31 16:13 ` Kakadu
2011-10-31 23:59 ` Toby Kelsey
2011-11-01 17:20 ` Diego Olivier Fernandez Pons
2011-11-01 17:55   ` Diego Olivier Fernandez Pons [this message]
2011-11-03  8:10     ` Ly Kim Quyen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAHqiZ-Jg36Tu-4zgXe9M1dUMqQQ-NJwjKt2LO0Oo-N_EFc4-+w@mail.gmail.com \
    --to=dofp.ocaml@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=lykimq@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).