Module 37 of 39

Sets, relations, and functions for algorithms

Use sets, relations, functions, graphs, and trees with the meanings algorithms rely on.

A set is a collection defined by membership. Order and duplicates do not change the set: {2, 3, 3} and {3, 2} name the same set. A list is different. It has positions, and repeated values may matter. Many algorithm mistakes begin by treating those two kinds of collection as though they made the same promises.

Background for this page. A set is defined by membership. A list additionally records positions and repetitions. A function assigns exactly one output to each input in its domain. Related explanations: Hash tables: collisions, load factor, and lookup · Counting, probability, and expectation for algorithms.

We write x ∈ S when x belongs to set S. The union A ∪ B contains anything in either set. The intersection A ∩ B contains things in both. The difference of A and B contains things in A but not B. A subset relation A ⊆ B says every member of A is also in B. These ideas appear in graph reachability, search results, and proofs about which cases an algorithm has covered.

A set ignores order and repeated entries.

A relation says which ordered pairs of items satisfy a rule. “Is connected to” and “is less than” are relations with different properties. An equivalence relation is reflexive, symmetric, and transitive; it divides items into disjoint equivalence classes. Disjoint-set structures maintain such classes as connections are added. An order relation lets us compare items, but a sorting algorithm needs a consistent comparison rule to produce a meaningful result.

A function maps each allowed input to one output. Its domain says which inputs are allowed. A hash function maps keys to hash values; collisions mean different keys may share an output. An injective function does not map two distinct inputs to the same output. A surjective function reaches every output in its stated codomain. A bijection is both; it gives a one-to-one correspondence useful for counting.

Graphs and trees are also mathematical structures. A graph has vertices and edges. A path follows edges; a cycle returns to a previous vertex. A tree is a connected undirected graph with no cycle. Choosing a root gives it parent and child relationships, but the root is an added choice, not a property of every unrooted tree. A directed acyclic graph is not necessarily a tree; a vertex may have several incoming paths.

Use the pigeonhole principle on hashing

Suppose possible keys are {a,b,c,d} and table positions are {0,1,2}. Any function assigning each key a position must assign at least two keys to the same position. Four distinct keys cannot all occupy three distinct positions. This proves collisions are unavoidable for that mapping, regardless of the hash function’s cleverness.

A one-to-one function never maps two different inputs to the same output. An onto function reaches every member of its stated output set. A bijection has both properties. These definitions concern the whole stated domain and codomain; observing unique hashes for a small sample does not prove injectivity across all possible keys.

Practice. Is [2, 2, 3] the same list as [2, 3]? Is it the same set of values? Why does a hash function with more possible keys than table positions need collisions?

Answer. The lists differ in length and positions. Their sets of values are both {2, 3}. Mapping more possible keys into fewer positions forces at least two keys to share a position.

Try another case. If a list contains five items but only three distinct values, what is the size of its corresponding set?

Reasoning. Three. Converting to a set discards repeated membership, which can change a problem that depends on frequencies.

View the full course and choose another topic.

Image preview

Fit