Sets, Relations, and Functions JavaScript with examples

Let's break down the concepts of sets, relations, and functions with examples in JavaScript.

Sets, Relations, and Functions:

1. Sets:

A set is a collection of well-defined objects or elements. These objects can be anything like numbers, letters, or even other sets. Sets are usually denoted by curly braces {}.

Example of a Set:

let A = {1, 2, 3, 4}; // Set A containing four elements
let B = {a, b, c}; // Set B containing three elements

Operations on Sets:

  1. Union (A ∪ B): Combines elements from both sets.

    let A = {1, 2, 3};
    let B = {3, 4, 5}; let union = new Set([...A, ...B]); // Union of A and B console.log(union); // Output: {1, 2, 3, 4, 5}
  2. Intersection (A ∩ B): Finds elements common to both sets.

    let intersection = new Set([...A].filter(x => B.has(x)));
    console.log(intersection); // Output: {3}
  3. Difference (A - B): Elements in A that are not in B.

    let difference = new Set([...A].filter(x => !B.has(x)));
    console.log(difference); // Output: {1, 2}

2. Relations:

A relation between two sets is a collection of ordered pairs where the first element is from the first set, and the second element is from the second set. In JavaScript, a relation can be represented as a set of pairs.

Example of a Relation:

Consider two sets:

  • Set A = {1, 2, 3}
  • Set B = {a, b, c}

A relation R from A to B can be represented as a set of ordered pairs:

let R = new Set([[1, 'a'], [2, 'b'], [3, 'c']]);
console.log(R); // Output: Set { [ 1, 'a' ], [ 2, 'b' ], [ 3, 'c' ] }

Types of Relations:

  1. Reflexive Relation: A relation R is reflexive if every element of set A is related to itself. For example, if A = {1, 2, 3}, then R = {(1, 1), (2, 2), (3, 3)} is reflexive.

  2. Symmetric Relation: A relation R is symmetric if for every pair (a, b) in R, the pair (b, a) is also in R. Example: If (1, 2) is in R, then (2, 1) must also be in R.

  3. Transitive Relation: A relation R is transitive if for all a, b, c ∈ A, whenever (a, b) and (b, c) are in R, then (a, c) must also be in R.

3. Functions:

A function is a special type of relation where each element in the domain (the first set) is related to exactly one element in the codomain (the second set). A function can be denoted as f: A → B, where A is the domain, and B is the codomain.

Example of a Function:

Let’s say:

  • Set A = {1, 2, 3}
  • Set B = {a, b, c}

A function f: A → B could be:


let f = new Map([ [1, 'a'], // f(1) = 'a' [2, 'b'], // f(2) = 'b' [3, 'c'] // f(3) = 'c' ]); console.log(f); // Output: Map { 1 => 'a', 2 => 'b', 3 => 'c' }

Types of Functions:

  1. One-to-One (Injective): A function is one-to-one if different elements of the domain map to different elements of the codomain. In our example, f is one-to-one because no two elements in A map to the same element in B.

  2. Onto (Surjective): A function is onto if every element of the codomain has at least one element from the domain mapping to it. For instance, if B = {a, b, c} and f = {1 → a, 2 → b, 3 → c}, the function is onto because every element in B has a corresponding element in A.

  3. One-to-One Correspondence (Bijective): A function is bijective if it is both one-to-one and onto, meaning there is a perfect pairing between the elements of the domain and codomain.

Example of a Bijective Function:

Let’s say:

  • Set A = {1, 2, 3}
  • Set B = {a, b, c}

A bijective function f: A → B could be:


let f = new Map([ [1, 'a'], [2, 'b'], [3, 'c'] ]); console.log(f); // Output: Map { 1 => 'a', 2 => 'b', 3 => 'c' }

In this case, the function is both injective and surjective, making it bijective.

Summary of Examples:

  1. Set Operations (Union, Intersection, Difference)
  2. Relation (Set of ordered pairs)
  3. Function (Mapping of domain to codomain)


Sets, Relations, and Functions 


No comments:

Post a Comment

Angular's new httpResource and a bonus hidden feature

  The first   release candidate   for   Angular 19.2   is out, and it includes the new experimental   httpResource . This is essentially a s...

Best for you