caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Toby Kelsey <toby.kelsey@gmail.com>
To: caml-list@inria.fr
Cc: lykimq@gmail.com
Subject: Re: [Caml-list] Compute the equivalence classes
Date: Mon, 31 Oct 2011 23:59:51 +0000	[thread overview]
Message-ID: <4EAF3677.1050301@gmail.com> (raw)
In-Reply-To: <CAHrFk+vEoS+1PoKm=PR4EBko0Ot8osuNVpk9EO4JLL-fu7mm6w@mail.gmail.com>

On 31/10/11 15:36, Ly Kim Quyen wrote:
> Dear group,
> 
> I have an question about data structure and types. I have a function calculates
> transitive closure of relation represented as an adjacency matrix
> 
> let trans_closure (m: 'a array array) : 'a array array =
>   let last_cols = length m - 1 in
>     for k = 0 to last_cols do
>       for i = 0 to last_cols do
> for j = 0 to last_cols do
>   m.(i).(j) <- m.(i).(j) || (m.(i).(k) && m.(k).(j))
> done;
>       done;
>   done;
>   m
> ;;

I may have misunderstood how you are using this, but I'm not sure this finds the transitive closure since you iterate over k just once in one direction. You might need to repeat this until no more relations are found.
Since you're using logical operators on elements, your 'a type must be boolean (unless you override the operators).

> (* transpose matrix A is the matrix A' formed by turning rows into
>    columns and vice versa: (A')i,j = Aj,i.*)
> let transpose (m: 'a array array) : 'a array array =
>   let tc = trans_closure m in
>   let last_cols = length m - 1 in
>   for i = 0 to last_cols do
>     for j = 0 to last_cols do
>       tc.(j).(i) <- tc.(i).(j)
>     done;
>   done;
>   tc
> ;;

This visits each position twice and is not symmetric, so for example
(a b)
(c d)
becomes
(a b)
(b d)

To transpose you want to visit each non-diagonal entry once and swap, I.E. (untested):
  for i = 0 to last_cols-1 do
    for j = i+1 to last_cols do
      let tmp = tc.(i).(j) in
      begin
        tc.(i).(j) <- tc.(j).(i);
        tc.(j).(i) <- tmp
      end
    done;
  done;

> I would like to compute equivalence classes, it is disjoint between matrix
> transitive closure  and matrix transpose. I would like it returns for me a
> new list with the type: list of list [[]]
> 
> I have a function convert 'a array array to 'a list list
> 
> let to_list (m : 'a array array) : 'a list list =
>   List.map to_list (to_list m)
> 
> let eq_class (m: 'a array array) : 'a list list =
>   let lst = length m - 1 in
>       for i = 0 to lst do
> for j = 0 to lst do
>   let a = tc.(i).(j) && trans.(i).(j) in
>             if a
>             then
>            ???
> 
> I'm stuck here, I don't know how I can add the result "a" into the new list
> of list. I think I should create a new list with type 'a list list, but I
> don't know where I should write it?

How do you want to represent your equivalence class?  You could have a list of (i,j) pairs for each class, or perhaps a more complex data structure. Once you choose the data structure that will help determine what you need to do.
In any case looping once over the matrix probably can't compute it. You will have to follow the equivalence relations to find all equivalent entries, so you might need a recursive 'flood-fill' style algorithm to find all the entries.

> Thank you for helping me understand and give me some advises.
> 
> Best regards,
> Gwen

Regards,
Toby


  parent reply	other threads:[~2011-11-01  0:00 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 [this message]
2011-11-01 17:20 ` Diego Olivier Fernandez Pons
2011-11-01 17:55   ` Diego Olivier Fernandez Pons
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=4EAF3677.1050301@gmail.com \
    --to=toby.kelsey@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).