Sets, Functions, and Relations: The Language of CS

A gentle introduction to describing collections, mappings, and relationships precisely.

By Rishipal Singh · · 5 min read

Sets collect elements, while a function maps each input to one output.
Visual guide: Sets collect elements, while a function maps each input to one output.

Computer science uses precise language to describe collections and connections. Sets, relations, and functions provide a compact way to reason about data and behavior.

Sets collect distinct elements

A set is an unordered collection in which an element appears at most once. If A = {1, 2, 3}, then 2 is a member of A, but the order does not matter. The empty set contains no elements. Sets help describe inputs, states, and possible outputs.

Relations connect elements

A relation describes which elements are connected to which others. For example, the “is a prerequisite for” relation connects one course to another. Relations can have properties such as symmetry or transitivity, which are useful when modeling equivalence or ordering.

Functions map inputs to outputs

A function assigns exactly one output to every input in its domain. A function may map several different inputs to the same output, but one input cannot have two outputs at once. In programming, this idea encourages us to define valid inputs and understand what a computation returns.

For example, the absolute-value function maps both -3 and 3 to 3. Its domain can be the integers and its codomain the non-negative integers.

Why this vocabulary matters

Precise definitions help make APIs, database constraints, graph models, and mathematical proofs less ambiguous. When a problem statement feels fuzzy, asking “what is the set of inputs?” and “what mapping or relation is required?” is a useful first step.

Practice: test a function

A relation maps input 2 to both 5 and 7. Can that relation be a function from inputs to outputs?

Reveal the explanation

No. A function assigns exactly one output to each input. Multiple inputs may share an output, but one input cannot have two different outputs.