Thursday, September 5, 2024

BCA PART A 8.R Program to Check if a Relation is Symmetric:

 

A symmetric relation is a type of mathematical relation on a set where the order of elements in the pairs does not matter. Specifically, a relation RR on a set AA is called symmetric if, for every pair (a,b)R(a, b) \in R, the reverse pair (b,a)(b, a) is also in RR.

Formal Definition:

A relation RR on a set AA is symmetric if: a,bA,(a,b)R(b,a)R\forall a, b \in A, \, (a, b) \in R \Rightarrow (b, a) \in R This means that if an element aa is related to an element bb, then bb must also be related to aa.

Example:

  • Set AA: {1,2,3}\{1, 2, 3\}
  • Relation RR: {(1,2),(2,1),(3,3)}\{(1, 2), (2, 1), (3, 3)\}

In this case, the relation RR is symmetric because:

  • (1,2)(1, 2) is in RR, and the reverse pair (2,1)(2, 1) is also in RR.
  • (3,3)(3, 3) is its own reverse, so it satisfies the condition.

Non-Symmetric Example:

  • Set AA: {1,2,3}\{1, 2, 3\}
  • Relation RR: {(1,2),(3,3)}\{(1, 2), (3, 3)\}

In this case, the relation RR is not symmetric because:

  • (1,2)(1, 2) is in RR, but the reverse pair (2,1)(2, 1) is not in RR, so the relation is not symmetric.
# Function to check if a relation is symmetric
is_symmetric <- function(relation) {
  # Iterate through each pair in the relation
  for (i in 1:nrow(relation)) {
    # Get the current pair (a, b)
    a <- relation[i, 1]
    b <- relation[i, 2]
   
    # Check if the pair (b, a) is in the relation
    if (!any(relation[,1] == b & relation[,2] == a)) {
      return(FALSE)
    }
  }
  return(TRUE)
}

# Example usage

# Define the relation R as a matrix of pairs
relation <- matrix(c(1, 2,
                     2, 1,
                     3, 3),
                   ncol = 2, byrow = TRUE)

# Check if the relation is symmetric
if (is_symmetric(relation)) {
  print("The relation is symmetric.")
} else {
  print("The relation is not symmetric.")
}

The program checks whether a given relation is symmetric. A relation is symmetric if for every pair (a,b)(a, b), the pair (b,a)(b, a) also exists.

Explanation :

  1. Function is_symmetric:

    • The function takes one argument, relation, which is a matrix of pairs representing the relation.
    • It iterates through each pair (a,b)(a, b) in the matrix using a loop.
    • For each pair (a,b)(a, b), the function checks if the reverse pair (b,a)(b, a) is also present in the relation.
    • If any reverse pair (b,a)(b, a) is missing, the function returns FALSE (indicating the relation is not symmetric).
    • If all pairs have their reverse counterparts, it returns TRUE (indicating the relation is symmetric).
  2. Example Relation:

    • The relation is represented as a matrix, e.g., relation <- matrix(c(1, 2, 2, 1, 3, 3), ncol = 2, byrow = TRUE).
    • This relation contains pairs (1,2)(1, 2), (2,1)(2, 1), and (3,3)(3, 3).
  3. Output:

    • If all pairs have their corresponding reverse pairs (e.g., (1,2)(1, 2) has (2,1)(2, 1)), the program prints "The relation is symmetric.".
    • If any reverse pair is missing, it prints "The relation is not symmetric.".

No comments:

Post a Comment