Thread: how to check a realations ? (Math not real life)

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    111

    [Tutorial] how to check a realations ? (Math not real life)

    Hello,...

    im going over over course that was tought by Dr. David Tankus : "Logic and formal Language".

    one of the things that distinct this lecturar (i don't know how to call him in the right form) is that any mathimatical question should be solved by c programming (wonder why ... ).

    any why
    one of the ways to check relations is using a binary matrix (the value can be 0 or 1).

    The matrix is created when the lines are the domain and coulmuns are the range.

    for example the relation :

    A = {1,2,3,4}
    B = {1,2,4,6}

    R = A toward B

    R = {(1,1) , (2,2),(3,4),(4,6)}

    then the domain is : (1,2,3,4)
    and range is (1,2,4,6).

    so lets go to the issues :
    Relation is Reflexive if aRa (each pair is in relation with it self) by defention it means that any object (i don't how to call this in english).

    so all diagnel in the matrix should be ones
    so:
    i = lines.
    J = coulmuns.
    Code:
    #define true 1
    #define false 0
    
    int check_for_reflexive(int **matrix)
    {
     for (i =0 ; i < lines;i++)
     {
        if (matrix[i][i] == 0)
        {
           return false;
         }
     }
     return true;
    }
    Ireflexive:
    a not R a: (object not in relation with it self)
    the diagnnel should be all 0.
    Code:
    [CODE]
    int check_for_Ireflexive(int **matrix)
    {
     for (i =0 ; i < lines;i++)
     {
        if (matrix[i][i] == 1) //see the deference ? 
        {
           return false;
         }
     }
     return true;
    }
    [/CODE]
    Semtric:
    aRb and bRa dlarrow semtric:
    (1,2) in R and (2,1) in R
    so:

    Code:
    [CODE]
    int check_for_semtric(int **matrix)
    {
     for (i =0 ; i < lines;i++)
     { 
       for (j =0;j <colmuns;j++)
       {
         if (matrix[i][j] == 1))
           if (matirx[j][i] == 0)
              return false;
       }
     }
     return true;
    }
    [/CODE]
    antisemtric:
    aRb and bRa dlrarrow a = b.
    so ...

    Code:
    int check_for_antisemtric(int **matrix)
    {
     for (i =0 ; i < lines;i++)
      for (j =0;j <colmuns;j++)
      {
         if ((matrix[i][j] == 1)) && (matrix[j][i] ==1))
         {
              if (i != j)
                 return false;
         }
    
      }
     return true;
    }
    Asemtirc:
    if aRb then b not R a
    Code:
    int check_for_antisemtric(int **matrix)
    {
     for (i =0 ; i < lines;i++)
      for (j =0;j <colmuns;j++)
      {
         if ((matrix[i][j] == 1)) && (matrix[j][i] ==1))
         {
               return false;
         }
    
      }
     return true;
    }


    Transtive:
    if aRb and bRc then aRc example : 1 <3<4 -> 1<4
    Code:
    int check_for_trastive(int **matrix)
    {
     for (i =0 ; i < lines;i++)
      for (j =0;j <colmuns;j++)
       for(k = 0;k < colmuns;k++)
       {
         if ((matrix[i][k] == 1)) &&  (matrix[k][j] ==1)) ** (matrix[i][j] == 0)
         {
               return false;
         }
    
      }
     return true;
    }
    Last edited by jabka; 09-12-2007 at 01:41 PM.
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    So what's your question? Can you elaborate?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    111
    soory wrong title it should be [HOWTO] ....
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Ah, I see. In that case, I'd add an 'i' here:
    Code:
    int check_for_antisemtric(int **matrix)
    {
     for (i =0 ; i < lines;i++)
      for (j =0;j <colmuns;j++)
      {
         if ((matrix[i][j] == 1)) and (matrix[j][i] ==1))
         {
               return false;
         }
    
      }
     return true;
    }
    And you might want to use && instead of and. and works in C++ and in C when you have iso646.h included, but not in ordinary C. At least mention this if you're going to use and.

    Finally, if you're posting code for others to read, consider using consistent indentation, and perhaps code colouring. Which looks better? This?
    Code:
    int check_for_Ireflexive(int **matrix)
    {
     for (i =0 ; i < lines;i++)
     {
        if (matrix[i][i] == 1) //see the defrence
        {
           return false;
         }
     }
     return true;
    }
    Or this?
    Code:
    int check_for_Ireflexive(int **matrix)
    {
        for (i =0 ; i < lines;i++)
        {
            if (matrix[i][i] == 1) //see the defrence
            {
                return false;
            }
        }
        return true;
    }
    I syntax highlighted that code with codeform, a program I wrote. Feel free to use it yourself here: http://dwks.theprogrammingsite.com/myprogs/cfonline
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    111

    Format for rrelation files

    Hello,..

    Today i had the exam (secend attempt) and i realized that i didn't posted the relation formats that are used so :
    all numbers are Natural numbers(integer or duble) and between two tokens are allways spaces
    Code:
    amount_of_pairs x1 y1 x2 y2 x3 y3 ...
    Code:
    amount_of_pairs (x1,y1) (x2,y2) (x3,y3) ...
    and the hardest one
    Code:
    (x1,x2) (y1,y2) (x3,y3) ..
    The first should be loaded into an array (two dimentional or one) but be if you use boolean matirx (int **Mr) you should be awere that pairs won't be allways as 1,2 2,3 etc but they can be very large numbers so don't use the next//I acidantly did it :'-( :
    Code:
    for (i = 0; i < amount_of_pairs ;i++)
      for (j=0;j< amount_of_pairs;j++)
      {
        fscanf(fp,"(%d,%d)",&k,&s);
        Mr[k-1][s-1] = 1;
       }
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 09-03-2002, 09:17 PM
  2. check out my first real c++ program
    By Klinerr1 in forum C++ Programming
    Replies: 1
    Last Post: 06-25-2002, 03:06 AM
  3. any real l33t people wanna check this out for me
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 06-06-2002, 10:55 PM
  4. Programming Puns
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 44
    Last Post: 03-23-2002, 04:38 PM
  5. real life...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 35
    Last Post: 11-17-2001, 07:30 PM