Foundations
Discrete Mathematics Basics
Difficulty: Intermediate
Almost every conditional you have ever written is a tiny piece of propositional logic, and almost every collection you have ever stored is, mathematically, a set. Discrete Mathematics Basics makes that connection explicit, giving you the...
Discrete Mathematics Basics
Almost every conditional you have ever written is a tiny piece of propositional logic, and almost every collection you have ever stored is, mathematically, a set. Discrete Mathematics Basics makes that connection explicit, giving you the formal vocabulary that data structures, graph algorithms, database queries, and correctness arguments all sit on top of.
This lesson walks you through three pillars of discrete math used throughout DSA. First, sets and the operations on them: union, intersection, difference, complement, subset, and membership, the same ideas baked into hash sets and array problems. Second, propositional logic: the operators AND, OR, NOT, and implication, plus truth tables for evaluating compound expressions; this is the math behind every Boolean condition in your code. Third, binary relations and their properties (reflexive, symmetric, transitive), which generalize the way edges connect nodes in graphs and rows connect in databases. Along the way you will get a first taste of formal proof: how to argue that two sets are equal or that a logical equivalence always holds.
This lesson assumes only basic programming familiarity, so no specific DSA prerequisite is required. If you have written if (x && !y) or used a hash set, you already have informal intuition for everything we will formalize here.
From here you will move into Combinatorics Basics, where you will use these set and counting foundations to count permutations, combinations, and arrangements (the math behind backtracking and many complexity proofs).
Topics:
What You'll Learn
By the end of this lesson, you will be able to:
Define sets and perform union, intersection, difference, and complement operations.
Determine set membership, subsets, and equality, and apply these concepts to programming scenarios.
Evaluate propositional logic expressions using AND, OR, NOT, and implication operators.
Construct and interpret truth tables for compound propositions.
Describe binary relations and identify reflexive, symmetric, and transitive properties.
Explain how discrete math concepts connect to data structures and algorithm analysis.
Why This Matters
01
Sets are the foundation of data structures like hash sets, arrays, and graphs -- understanding set operations helps you reason about unions, intersections, and membership tests.
02
Propositional logic directly maps to Boolean expressions in every programming language -- mastering AND, OR, NOT, and implications makes you write correct conditionals and guards.
03
Graph theory, which is critical for many algorithms, is built on relations and set theory -- you cannot formalize graphs without these concepts.
04
Proof techniques (even at an introductory level) give you the tools to argue that an algorithm is correct and that your reasoning is sound.
Key Terms
8 terms you'll encounter in this lesson
Set
An unordered collection of distinct elements. No duplicates are allowed, and the order does not matter. Written with curly braces: {1, 2, 3}.
Union
The set of all elements that belong to either set A or set B (or both). Written A ∪ B.
Intersection
The set of elements that belong to both set A and set B. Written A ∩ B.
Complement
The set of all elements in the universal set that are not in set A. Written A' or A^c.
Proposition
A declarative statement that is either true or false, but not both. For example, '5 > 3' is a proposition (true).
Implication
A logical statement of the form 'if P then Q' (P → Q). It is false only when P is true and Q is false.
Binary relation
A relationship between elements of two sets. Formally, a subset of the Cartesian product A × B. For example, 'less than' is a relation on numbers.
Truth table
A table that lists all possible combinations of truth values for the input propositions and the resulting value of a compound expression.
Related Problems
Coding problems that put this lesson's concepts into practice
Heads Up
Common misconceptions to watch out for
"Sets can contain duplicates"
By definition, a set contains only distinct elements. {1, 2, 2, 3} is the same as {1, 2, 3}. If you need duplicates, you need a multiset (bag) or a list.
"P → Q means P causes Q"
Implication (P → Q) is a logical relationship, not a causal one. It simply means 'it is not the case that P is true and Q is false.' A false antecedent makes the implication vacuously true.
"NOT (A AND B) is the same as (NOT A) AND (NOT B)"
This is a common error. De Morgan's Law states that NOT (A AND B) = (NOT A) OR (NOT B). The operator flips from AND to OR when you distribute the NOT.
"The empty set is not a subset of any set"
The empty set {} is a subset of every set, including itself. This is because the statement 'every element of {} is in S' is vacuously true -- there are no elements to violate it.
