Adding properties to a binary operation, one at a time, gives a ladder of structures. The first two steps are the semigroup and the monoid.

Semigroups

An algebraic system (S, ) is a non-empty set S with a binary operation . It is a semigroup if is closed and associative. Examples are (N, +), (Z, ×), and the set of all strings over an alphabet with the operation of concatenation, in which "ab" joined with "cd" gives "abcd". The last example shows why semigroups matter in computer science: strings and the languages built from them form semigroups, and this is the base of automata theory. (Z, −) is not a semigroup, since subtraction is not associative. A semigroup is commutative* if its operation is also commutative.

Definition: Semigroup. An algebraic system (S, ) in which the binary operation is closed and associative.

Monoids

A monoid is a semigroup that has an identity element. (N0, +), where N0 includes 0, is a monoid with identity 0. (N, +) without 0 is a semigroup but not a monoid. The set of strings with concatenation is a monoid whose identity is the empty string ε, since joining ε to any string leaves it unchanged. The set of all n × n matrices under multiplication is a monoid with identity I. The identity of a monoid is unique: if e and f are both identities, then e = e * f = f.

Sub-semigroups and Submonoids

A non-empty subset T of a semigroup S is a sub-semigroup if it is closed under . The even integers form a sub-semigroup of (Z, +). A subset of a monoid is a submonoid* if it is closed and contains the identity.

Homomorphism

A map f from a semigroup (S, ) to a semigroup (T, ∘) is a homomorphism if f(a b) = f(a) ∘ f(b). It preserves the structure. The length of a string is a homomorphism from strings under concatenation to (N0, +), since the length of xy is the length of x plus the length of y. If f is one-to-one and onto, it is an isomorphism, and the two structures are the same in form.

Table 2.2: The ladder of structures

Structure Properties required Example
Algebraic system Closure (Z, −)
Semigroup Closure, associativity (N, +)
Monoid Semigroup with identity (N0, +), strings with ε
Group Monoid with inverses (Z, +)

Example 2.2: Is it a monoid?

Problem: Is (Z, ) with a b = max(a, b) a semigroup? Is it a monoid? What about ({0, 1, 2, …}, max)?

Solution:

  1. max(a, b) is an integer, so it is closed.
  2. max(max(a, b), c) = max(a, b, c) = max(a, max(b, c)), so it is associative. It is a semigroup.
  3. An identity e would need max(a, e) = a for all integers a, so e must be at most every integer. No such integer exists. Not a monoid.
  4. On {0, 1, 2, …}, e = 0 works, since max(a, 0) = a.

Result: (Z, max) is a semigroup but not a monoid, while (N0, max) is a monoid with identity 0.

s = ["ab", "c", "", "de"]
cat = lambda x, y: x + y
print(all(cat(cat(a, b), c) == cat(a, cat(b, c))
          for a in s for b in s for c in s))
print(all(cat(x, "") == x == cat("", x) for x in s))

Output

True
True

Common mistakes

  • Forgetting to check associativity before calling a system a semigroup.
  • Calling (N, +) a monoid when N does not contain 0.
  • Choosing an identity that works only from one side.

Key takeaways

  • A semigroup is closed and associative.
  • A monoid is a semigroup with an identity.
  • Strings under concatenation form a monoid with identity ε.
  • A homomorphism preserves the operation: f(a * b) = f(a) ∘ f(b).

Practice questions

  1. Define a semigroup.
  2. What is the identity of the string monoid?
  3. Is (N, +) without 0 a monoid?

Answers to the practice questions

  1. An algebraic system whose operation is closed and associative.
  2. The empty string ε.
  3. No, since it has no identity element.

Sources

  • Official BCA syllabus Mathematics Foundations to Computer Science II, course code 25BCA402DS01, Unit II