Study CKC111 one week at a time.
Your lectures and tutorials, turned into short lessons, interactive tools, and practice questions that check your answers straight away.
Your weeks
Pick a week to open it. The bar on each card shows how many of its practice questions you have tried.
How to study with this guide
- Read and play. Each week starts with a short lesson. Use the interactive tools to test your understanding before moving on.
- Recall, then practise. Answer the recall questions from memory, then try the practice set. Hints come one at a time, and a solution unlocks only after you have tried.
- Drill before the exam. Use the Exam Lab for unlimited new questions, then take a timed mock exam. The cheat sheet collects everything on one page.
Learn to read logic like a contract.
Your slides cover propositions, connectives, truth tables and translation. Week 2 continues with logical equivalence. This page teaches them in that order. Where I add something that is not on your slides, I label it "Beyond the slides".
Objectives. By the end you can: decide if a sentence is a proposition; use ¬ ∧ ∨ ⊕ → ↔ and build truth tables; translate English into logic; write the converse, inverse and contrapositive; and use precedence and brackets correctly.
Try it. The promise is: "If you get 100% on the final, then you get an A." That is p → q.
Flip the switches. Only one combination breaks the promise. That is the whole idea behind implication, and the rest of the chapter builds on it.
1. Propositions
A proposition is a declarative sentence that is either true or false. It does not have to be true. It only has to have a truth value.
Tap each sentence to see the verdict.
Propositional variables are p, q, r, … The proposition that is always true is T, and the one that is always false is F.
2. The connectives
A compound proposition is built from smaller propositions with connectives. Choose a connective, flip p and q, and watch which row of the table you are in.
Inclusive vs exclusive or. "Students who have taken CS202 or Math120 may take this class" allows both, so it is ∨. "Soup or salad comes with this entrée" allows only one, so it is ⊕.
3. Implication
p → q is read "if p, then q". Here p is the hypothesis and q is the conclusion. Using the promise from the top of the page:
- Got 100% and got an A: promise kept.
- Got 100% and no A: promise broken. This is the only way to break it.
- Did not get 100%: the promise says nothing about this case, so it cannot be broken. The implication is true. We call this vacuously true.
So p → q is false in exactly one row: p true and q false. This is why a sentence like "If 1 + 1 = 3, then your grandma wears combat boots" counts as true in logic.
Different ways to say p → q
| English | Meaning |
|---|---|
| if p, then q · q if p · q whenever p · p implies q | p → q |
| p is sufficient for q | p → q |
| q is necessary for p | p → q |
| p only if q | p → q |
| q unless ¬p | p → q |
Biconditional
p ↔ q ("p if and only if q") is true when p and q have the same truth value. Try it in the connectives table above. It means the same as (p → q) ∧ (q → p).
4. Converse, inverse, contrapositive
Start from p → q. You can form three related statements.
Look at the columns. The contrapositive matches the original in every row. The converse and inverse do not, although they match each other.
Example. "If it rains, the ground is wet." The converse is "if the ground is wet, it rained", which can be false because a sprinkler might be on. The contrapositive, "if the ground is not wet, it did not rain", is always valid.
Slide example. "It raining is a sufficient condition for my not going to town" is r → ¬t. Converse: if I do not go to town, then it is raining. Inverse: if it is not raining, then I will go to town. Contrapositive: if I go to town, then it is not raining.
5. Truth-table lab
With n variables you need 2n rows, and one column for every sub-expression. The lab does that for you. Type an expression, or use the symbol buttons. Single lowercase letters are variables, and T and F are constants.
The lab also names each result as a tautology, a contradiction or a contingency. Week 2 explains those names and how to compare two statements.
6. Precedence and translation
Precedence
From highest to lowest: ¬, ∧, ∨, →, ↔. So p ∨ q → ¬r means (p ∨ q) → ¬r. If you want p ∨ (q → ¬r), you must write the brackets. The lab above shows you the fully bracketed form of whatever you type.
Translating English
Two steps from your slides: (1) identify the atomic propositions and give each a variable, (2) choose the connectives.
"If I go to Harry's or to the country, I will not go shopping."
p: I go to Harry's. q: I go to the country. r: I go shopping.
(p ∨ q) → ¬r
"You can access the Internet from campus only if you are a computer science major or you are not a freshman."
a: you can access the Internet from campus. c: you are a CS major. f: you are a freshman.
a → (c ∨ ¬f)
"The automated reply cannot be sent when the file system is full."
p: the automated reply can be sent. q: the file system is full.
q → ¬p
7. Common mistakes
- Thinking p → q is false when p is false. It is true.
- Reading "p only if q" as q → p.
- Claiming the converse or inverse is equivalent to the original. Only the contrapositive is.
- Forgetting that ∨ is inclusive unless ⊕ is stated.
- Leaving out brackets when precedence changes the meaning.
- Skipping rows. Always list all 2n rows systematically.
Where this goes next. Week 2 turns to logical equivalence: tautologies, De Morgan's Laws and proofs with named laws. Everything you learned here, especially the truth tables and p → q, is the machinery for it.
8. Active recall
Answer from memory in the box, then check. The check button unlocks once you have typed something.
9. Practice set
Three easy, four medium, three challenging. Write your attempt first. Hints come in three levels, and the full solution unlocks only after you have written an attempt. Use the truth-table lab to check your tables, but do the reasoning yourself.
10. Weekly checkpoint
Rate how you feel about each objective, then build your checkpoint.
Two statements, one truth table: are they the same?
Week 2 uses the second half of your Chapter 1 slides: tautologies, logical equivalence, De Morgan's Laws and the key equivalences, then proofs by laws. It builds directly on the connectives from Week 1. Anything I add that is not on your slides is labelled "Beyond the slides".
Start with the equivalence checker. Type two statements, or press an example. It builds the truth table and tells you whether they always agree, and if not, the first row where they differ.
Tip: the symbol buttons type into whichever box you used last. You can also type ASCII: ~ or ! for ¬, & for ∧, | for ∨, -> for →, <-> for ↔, xor for ⊕. Single lowercase letters are variables; T and F are constants.
Objectives. By the end you can: classify a statement as a tautology, contradiction or contingency; show two statements are equivalent (or not) with a truth table; state De Morgan's Laws and name the key equivalences; prove an equivalence with a chain of named laws; and rewrite conditionals as p → q ≡ ¬p ∨ q ≡ ¬q → ¬p.
1. Tautologies, contradictions, contingencies
Once you can build truth tables, look at the final column. Its pattern gives the statement a name.
- A tautology is always true, like p ∨ ¬p. Every row of the final column is T.
- A contradiction is always false, like p ∧ ¬p. Every row is F.
- A contingency is neither: sometimes true, sometimes false, like p.
Your slide's table for p: when p is T, ¬p is F, so p ∨ ¬p is T and p ∧ ¬p is F; the same happens when p is F. "Either it is raining or it is not" can never be false, so it is a tautology.
Classifier
Type any statement. The lab shows its truth table and names it.
2. Logical equivalence
Idea. Two statements that look different can say exactly the same thing. If they are always true together and false together, they are logically equivalent.
Slide example. ¬p ∨ q is equivalent to p → q:
| p | q | ¬p | ¬p ∨ q | p → q |
|---|---|---|---|---|
| T | T | F | T | T |
| T | F | F | F | F |
| F | T | T | T | T |
| F | F | T | T | T |
The two highlighted columns match in all four rows, so the statements are equivalent.
Showing that statements are NOT equivalent
Your slides show that the converse and the inverse of p → q are not equivalent to p → q, while the contrapositive is. You only need one row where the columns differ to prove non-equivalence. Press the "vs converse" and "vs inverse" buttons in the checker at the top: the first differing row is highlighted.
3. De Morgan's Laws
Named after Augustus De Morgan (1806 to 1871). They tell you how to push a negation inside brackets.
¬(p ∨ q) ≡ ¬p ∧ ¬q
Easy way to remember. When the ¬ moves inside the brackets, the connective flips: ∧ becomes ∨, and ∨ becomes ∧. Each part gets its own ¬.
In words (beyond the slides). "It is not true that I have both a pen and a ruler" means I am missing the pen or I am missing the ruler. "It is not true that I have a pen or a ruler" means I have neither: no pen and no ruler.
Your slide shows the second law in a truth table:
| p | q | ¬p | ¬q | p ∨ q | ¬(p ∨ q) | ¬p ∧ ¬q |
|---|---|---|---|---|---|---|
| T | T | F | F | T | F | F |
| T | F | F | T | T | F | F |
| F | T | T | F | T | F | F |
| F | F | T | T | F | T | T |
4. The key equivalences
These are the building blocks for proofs. A variable in any law can be replaced by an arbitrarily complex proposition. The right-hand column is a plain-words reading of each law (my wording, not from the slides).
| Law | Statement | In plain words |
|---|---|---|
| Identity | p ∧ T ≡ p p ∨ F ≡ p | Adding something always true to an "and", or always false to an "or", changes nothing. |
| Domination | p ∨ T ≡ T p ∧ F ≡ F | One always-true option makes an "or" always true. One always-false requirement makes an "and" always false. |
| Idempotent | p ∨ p ≡ p p ∧ p ≡ p | Saying it twice is the same as saying it once. |
| Double negation | ¬(¬p) ≡ p | Two negatives cancel. |
| Negation | p ∨ ¬p ≡ T p ∧ ¬p ≡ F | Either it is or it is not. It is never both. |
| Commutative | p ∨ q ≡ q ∨ p p ∧ q ≡ q ∧ p | The order of an "and" or an "or" does not matter. |
| Associative | (p ∧ q) ∧ r ≡ p ∧ (q ∧ r) (p ∨ q) ∨ r ≡ p ∨ (q ∨ r) | In a chain of all ∧ or all ∨, the grouping does not matter. |
| Distributive | p ∨ (q ∧ r) ≡ (p ∨ q) ∧ (p ∨ r) p ∧ (q ∨ r) ≡ (p ∧ q) ∨ (p ∧ r) | Like multiplying out brackets. Unlike ordinary algebra, it works in both directions: ∨ over ∧ as well as ∧ over ∨. |
| Absorption | p ∨ (p ∧ q) ≡ p p ∧ (p ∨ q) ≡ p | The extra part is "absorbed" because p already decides the result. |
| De Morgan | ¬(p ∧ q) ≡ ¬p ∨ ¬q ¬(p ∨ q) ≡ ¬p ∧ ¬q | Push ¬ inside and flip the connective. |
Conditional statements (Table 7)
| Equivalence |
|---|
| p → q ≡ ¬p ∨ q |
| p → q ≡ ¬q → ¬p |
| p ∨ q ≡ ¬p → q |
| p ∧ q ≡ ¬(p → ¬q) |
| ¬(p → q) ≡ p ∧ ¬q |
| (p → q) ∧ (p → r) ≡ p → (q ∧ r) |
| (p → r) ∧ (q → r) ≡ (p ∨ q) → r |
| (p → q) ∨ (p → r) ≡ p → (q ∨ r) |
| (p → r) ∨ (q → r) ≡ (p ∧ q) → r |
Biconditional statements (Table 8)
| Equivalence |
|---|
| p ↔ q ≡ (p → q) ∧ (q → p) |
| p ↔ q ≡ ¬p ↔ ¬q |
| p ↔ q ≡ (p ∧ q) ∨ (¬p ∧ ¬q) |
| ¬(p ↔ q) ≡ p ↔ ¬q |
5. Proving equivalences with laws
Truth tables get huge (10 variables means 1,024 rows). Instead, show A ≡ B by writing a chain A ≡ A1 ≡ … ≡ An ≡ B, where each step uses one named law.
Worked example 1 (from your slides). Show that ¬(p ∨ (¬p ∧ q)) is logically equivalent to ¬p ∧ ¬q.
- ¬(p ∨ (¬p ∧ q))start
- ¬p ∧ ¬(¬p ∧ q)second De Morgan law
- ¬p ∧ (¬(¬p) ∨ ¬q)first De Morgan law
- ¬p ∧ (p ∨ ¬q)double negation law
- (¬p ∧ p) ∨ (¬p ∧ ¬q)second distributive law
- F ∨ (¬p ∧ ¬q)negation law (¬p ∧ p ≡ F)
- (¬p ∧ ¬q) ∨ Fcommutative law for ∨
- ¬p ∧ ¬qidentity law for F
Worked example 2 (from your slides). Show that (p ∧ q) → (p ∨ q) is a tautology.
- (p ∧ q) → (p ∨ q)start
- ¬(p ∧ q) ∨ (p ∨ q)p → q ≡ ¬p ∨ q (the slide says "by truth table for →")
- (¬p ∨ ¬q) ∨ (p ∨ q)first De Morgan law
- (¬p ∨ p) ∨ (¬q ∨ q)associative and commutative laws
- T ∨ Tnegation law
- Tdomination law (or idempotent law)
Reading a proof. At every step ask: which law turned the line above into this line? If you cannot name it, you have not understood the step yet.
6. Common mistakes
- Pushing ¬ inside brackets without flipping ∧ and ∨.
- Claiming the converse or inverse is equivalent to p → q. Only the contrapositive is.
- Concluding "equivalent" after checking only some rows of a truth table.
- Using a law in a step without being able to name it.
- Applying a law to only part of a line and forgetting the rest of it.
- Writing ≡ between statements that are merely similar. Equivalent means equal in every row.
- Forgetting that T and F are propositions too: p ∧ T ≡ p, but p ∧ F ≡ F.
Where this goes next. The Chapter 1 summary slide lists predicate logic (the language of quantifiers) and proofs (rules of inference, proof methods, proof strategy). Rules of inference are tautologies written as arguments, so everything in this week is the machinery for them. I do not have those slides yet.
7. Active recall
Answer from memory in the box, then check. The check button unlocks once you have typed something.
8. Practice set
Three easy, four medium, three challenging. Write your attempt first. Hints come in three levels, and the solution unlocks only after you have written an attempt. Use the checker and classifier above to verify an answer, but do the reasoning yourself.
9. Weekly checkpoint
Rate how you feel about each objective, then build your checkpoint.
Watch an algorithm run, one step at a time.
This page follows your Chapter 2 slides (Section 2.1). You will learn what an algorithm is, how to read pseudocode, then searching, sorting and greedy methods. Anything I add that is not on your slides is labelled "Beyond the slides".
Start with the player below. It finds the largest number in a list. Press Next step and watch the highlighted line, the variables and the explanation change together.
Objectives. By the end you can: define an algorithm and list its properties; read and write pseudocode with for, while and if; trace max, linear search and binary search; trace bubble sort and insertion sort; run the greedy change algorithm and explain when greedy can fail.
1. What is an algorithm?
Every algorithm begins with a clearly stated problem: a valid input goes in, and the desired output comes out. The first job is to state the problem precisely: what the input is, and what the output must be.
Example: the input is a finite list of integers, and the output is the largest integer in it.
Easy way to think about it (beyond the slides): an algorithm is like a recipe written so carefully that someone who cannot think, only obey, still gets the right dish. "Add salt to taste" is not precise enough. "Add 1 teaspoon of salt" is.
Three ways to write an algorithm down
- Structured English: numbered steps in plain sentences.
- Pseudocode: code-like steps that are not tied to any programming language. This is what we use most.
- Flowchart: boxes and arrows.
2. Reading pseudocode
Your pseudocode needs only three ideas: assignments, conditional statements and loops, all wrapped in a procedure.
| Idea | Example | Plain meaning |
|---|---|---|
| Assignment | a := 10 i := i + 1 | Put a value into a variable. := means "becomes". The second line adds 1 to the old value of i. |
| if…then | if a = 1 then count := count + 1 | Do the action only when the test is true. Here = is a test, not an assignment. |
| if…then…else | if a > 0 then pc := pc + 1 else nc := nc + 1 | Do one action or the other. |
| for loop | for i := 1 to n sum := sum + i | Repeat for i = 1, 2, …, n. The loop counts for you. |
| while loop | i := 1 while i ≤ n i := i + 1 | Repeat as long as the test is true. You must change i yourself. |
| procedure | procedure max(a1, …, an: integers) … return max | A named algorithm with its inputs, a body, and a returned answer. |
3. Worked example: the maximum of a list
Idea. Keep a "champion so far". Start with the first number. Look at the next number. If it is bigger, it becomes the new champion. Repeat until the list ends.
Structured English
- Set the temporary maximum equal to the first integer in the sequence.
- Compare the next integer with the temporary maximum. If it is larger, set the temporary maximum equal to this integer.
- Repeat the previous step if there are more integers. If not, stop.
- When the algorithm stops, the temporary maximum is the largest integer.
Pseudocode, with a for loop and with a while loop
procedure max(a1, a2, …, an: integers) procedure max(a1, a2, …, an: integers)
max := a1 max := a1
for i := 2 to n i := 2
if max < ai then max := ai while i ≤ n
return max {max is the largest element} if max < ai then max := ai
i := i + 1
return max {max is the largest element}
The trace from your slide: (10, 6, 8, 20, 2)
| i | Test | Result | max afterwards |
|---|---|---|---|
| (start) | 10 | ||
| 2 | 10 < 6 | false | 10 |
| 3 | 10 < 8 | false | 10 |
| 4 | 10 < 20 | true | 20 |
| 5 | 20 < 2 | false | 20 |
| 6 | i ≤ n is false, loop ends | return 20 | |
Try it. In the player at the top of the page, type a new list and predict each step before you press Next. That is the fastest way to learn tracing.
4. Properties of algorithms
A procedure only counts as an algorithm if it has these properties.
| Property | In plain words | For the max procedure |
|---|---|---|
| Input | It takes values from a specified set. | A sequence of integers. |
| Output | It produces values from a specified set. These are the solution. | The largest integer in the sequence. |
| Correctness | It gives the right output for every valid input. | When it stops, max equals the maximum of all the terms. |
| Finiteness | It stops after a finite number of steps, for any input. | It stops once every integer has been examined. |
| Effectiveness | Each step can really be carried out, in finite time. | Every step is a comparison or an assignment, and each takes finite time. |
| Generality | It works for all problems of the desired form. | It works for any finite sequence of integers. |
Definiteness also appears in your slide's max example and answers: each step must be precisely defined. For max, this holds because only assignments, a finite loop and conditional statements occur.
Property detective (from your slides)
Each procedure below is broken. Decide which property it lacks. If you get one wrong, I will ask you a smaller question first.
5. Three kinds of problem
- Searching: find the position of a particular element in a list.
- Sorting: put the elements of a list into increasing order.
- Optimization: find the best (maximum or minimum) value of some quantity over all possible inputs.
The rest of this page takes them in order.
6. Searching
The problem. Locate an element x in a list of distinct elements a1, a2, …, an, or decide that it is not there. The answer is the location i where x = ai, or 0 if x is not in the list. Positions start at 1, so 0 can never be a real position, which is why it safely means "not found".
Our focus is two methods: linear search and binary search.
Linear search
Idea. Look at the first element, then the second, then the third, until you find x or run out of list. It is like scanning a class attendance list from the top to find one name.
procedure linear search(x: integer, a1, a2, …, an: distinct integers)
i := 1
while (i ≤ n and x ≠ ai)
i := i + 1
if i ≤ n then location := i
else location := 0
return location {location is the subscript of the term that equals x, or 0 if x is not found}
From your slide: x = 30 in (10, 50, 30, 70) gives location 3. x = 40 in the same list gives location 0. Run both:
Binary search
Idea. This needs a list already in increasing order. Look at the middle element. If x is bigger, it can only be in the right half. Otherwise it is at the middle or in the left half. Keep halving. It is the "guess my number" game: guessing 50 out of 100 and being told higher or lower.
procedure binary search(x: integer, a1, a2, …, an: increasing integers)
i := 1 {i is the left endpoint of the interval}
j := n {j is the right endpoint of the interval}
while i < j
m := ⌊(i + j)/2⌋
if x > am then i := m + 1
else j := m
if x = ai then location := i
else location := 0
return location
The symbol ⌊ ⌋ is the floor: round down to a whole number. For example ⌊(1 + 16)/2⌋ = ⌊8.5⌋ = 8.
Your slide searches for 19 in a 16-element list. Step through it, and watch the grey cells (outside the interval) fade out.
7. Sorting
To sort a list is to put its elements in increasing order. There are many sorting algorithms (binary, insertion, bubble, selection, merge, quick, tournament). Your slides work through two: bubble sort and insertion sort.
Bubble sort
Idea. Make several passes through the list. Whenever two neighbours are in the wrong order, swap them. Big values "bubble" to the right, so after pass 1 the largest is at the end, after pass 2 the two largest are at the end, and so on.
procedure bubblesort(a1, …, an: real numbers with n ≥ 2)
for i := 1 to n − 1
for j := 1 to n − i
if aj > aj+1 then interchange aj and aj+1
{a1, …, an is now in increasing order}
Why does the inner loop stop at n − i? After i − 1 passes the last i − 1 elements are already in place, so there is no need to compare them again. Try the slide example (3, 2, 4, 1, 5):
Insertion sort
Idea. Sort a hand of playing cards: pick up the next card and slide it into the right place among the cards you already hold. Your sorted section starts as just the first element and grows by one each round. Your slide notes that insertion sort uses a linear search to find the correct position.
procedure insertion sort(a1, …, an: real numbers with n ≥ 2)
for j := 2 to n
i := 1
while aj > ai
i := i + 1
m := aj
for k := 0 to j − i − 1
aj−k := aj−k−1
ai := m
{Now a1, …, an is in increasing order}
Read it in three parts: (1) the while finds the position i, (2) m := aj saves the new element, (3) the inner for shifts elements right to make a gap, then ai := m drops the saved value into the gap.
8. Greedy algorithms
An optimization problem asks you to minimize or maximize something over all possible inputs. Examples from your slides: find the route between two cities with the smallest total mileage; encode messages using the fewest bits; connect network nodes using the least fibre.
A greedy algorithm solves such a problem by making the best-looking choice at each step. That does not always produce an optimal solution overall, but often it does. So after you pick a greedy rule, you either prove it is always optimal, or find a counterexample.
Making change
Problem. Make change for n cents with quarters (25), dimes (10), nickels (5) and pennies (1), using the fewest coins. Greedy rule: at each step take the largest coin that does not exceed what is left.
Slide example, n = 67: quarter (left 42), quarter (left 17), dime (left 7), nickel (left 2), penny (left 1), penny (left 0). That is 6 coins.
procedure change(c1, c2, …, cr: values of coins, where c1 > c2 > … > cr; n: a positive integer)
for i := 1 to r
di := 0 {di counts the coins of denomination ci}
while n ≥ ci
di := di + 1 {add a coin of denomination ci}
n := n − ci
{di counts the coins ci}
Greedy lab
Type any coin values and an amount. The lab runs the greedy algorithm and also searches for the true minimum.
9. Common mistakes
- Mixing up := (assign) and = (test).
- Starting the loop at the wrong place. In max, max := a1 and the loop starts at i := 2.
- Forgetting i := i + 1 inside a while loop, so it never ends.
- Running binary search on a list that is not sorted.
- Forgetting to round the midpoint down: m := ⌊(i + j)/2⌋.
- In bubble sort, running the inner loop to n instead of n − i.
- In linear search, forgetting that "not found" returns 0.
- Believing a greedy algorithm is always optimal.
- Tracing too fast. Write down the variables after every step.
Connection to Week 1. Every if test and every loop condition is a proposition that is true or false. Reading i ≤ n and x ≠ ai uses ∧ and ¬ exactly as you learned in propositional logic.
10. Active recall
Answer from memory in the box, then check. The check button unlocks once you have typed something.
11. Practice set
Three easy, four medium, three challenging. Write your attempt first. Hints come in three levels, and the solution unlocks only after you have written an attempt. You can use the players above to check a trace, but do the tracing yourself first.
12. Weekly checkpoint
Rate how you feel about each objective, then build your checkpoint.
A relation is a set of pairs. Build one and see what it is.
This week follows Section 1 of your Chapter 3 slides: relations and their properties (reflexive, symmetric, antisymmetric, transitive), then combining and composing relations. Anything I add that is not on your slides is labelled "Beyond the slides".
Start with the explorer below. Click cells to add or remove pairs. The four properties update instantly, and when one fails, it shows the exact pair that breaks it.
Row = first element, column = second element. The shaded diagonal holds the pairs (a, a).
R =
Objectives. By the end you can: define a binary relation and list its pairs; test a relation for reflexive, symmetric, antisymmetric and transitive; combine relations with ∪, ∩ and −; and compute the composition R2 ∘ R1.
1. What is a relation?
Big picture. A relation says which things are connected to which. "Student s is enrolled in course c", "x divides y" and "x ≤ y" are all relations. In maths we store the connection as a set of ordered pairs.
An ordered pair (a, b) has an order: (1, 2) is not the same pair as (2, 1). The set of all possible pairs from A to B is A × B. A relation just picks some of them.
Slide example. Let A = {0, 1, 2} and B = {a, b}. Then R = {(0, a), (0, b), (1, a), (2, b)} is a relation from A to B. As a table, a × means "related":
| R | a | b |
|---|---|---|
| 0 | × | × |
| 1 | × | |
| 2 | × |
Relations on a set
When A = B we say R is a relation on A: a subset of A × A. All four properties in this chapter are about relations on a set.
Slide example. Let A = {1, 2, 3, 4} and R = {(a, b) | a divides b}. The pairs are (1,1), (1,2), (1,3), (1,4), (2,2), (2,4), (3,3), (4,4). Press a divides b in the explorer to see this exact relation.
2. Reflexive
Idea. Everything is related to itself. Think of "has the same age as": you are always the same age as yourself.
How to check. In the grid, the whole diagonal must be filled. Write down every pair (a, a) for the set, then tick each one off. One missing pair is enough to fail.
Slide example. R = {(a, a), (b, b)} on X = {a, b} is reflexive.
3. Symmetric and antisymmetric
Symmetric: a two-way street
Idea. If a is related to b, then b is related to a. "Is a classmate of" works that way.
How to check. For every pair (x, y), look for its reverse (y, x). In the grid, the picture must be a mirror image across the diagonal. A loop (a, a) is its own reverse, so loops never break symmetry.
Slide example. R = {(1,2), (2,1), (3,2), (2,3)} on {1, 2, 3} is symmetric. (The explorer has a button for it.)
Antisymmetric: no two-way streets between different elements
Idea. Two different elements are never related in both directions. "Is at most as tall as" works that way: if you are at most as tall as me and I am at most as tall as you, we must be the same height.
How to check. Ignore the loops. For every pair (x, y) with x ≠ y, the reverse (y, x) must not be in R.
Slide example. R = {(x, y) ∈ N | x ≤ y} is antisymmetric, because x ≤ y and y ≤ x imply x = y.
| On {1, 2, 3} | Example | Symmetric? | Antisymmetric? |
|---|---|---|---|
| Symmetric only | {(1,2), (2,1)} | yes | no |
| Antisymmetric only | {(1,2)} | no | yes |
| Both | {(1,1), (2,2)} | yes | yes |
| Neither | {(1,2), (2,1), (1,3)} | no | no |
These four examples are mine, not from your slides. Type each into the explorer to check.
4. Transitive
Idea. "A friend of a friend is a friend": if you can get from a to b and from b to c, you can get from a to c directly. "Is taller than" works that way.
How to check, step by step.
- Take a pair (a, b) in R.
- Find every pair that starts with b, say (b, c).
- For each one, the shortcut (a, c) must be in R. If it is missing, R is not transitive.
- Repeat for every pair in R.
Slide example. R = {(1,2), (2,3), (1,3)} on {1, 2, 3} is transitive. The only chain is (1,2),(2,3), and its shortcut (1,3) is there.
procedure is symmetric(R: a set of pairs)
for each pair (a, b) in R
if (b, a) is not in R then return false
return true5. Combining and composing relations
Combining
A relation is a set, so you can combine two relations with set operations: R1 ∪ R2 (in either), R1 ∩ R2 (in both), R1 − R2 (in R1 but not R2) and R2 − R1.
Slide example. R1 = {(1,1), (2,2), (3,3)} and R2 = {(1,1), (1,2), (1,3), (1,4)} give:
Edit the boxes to try your own. Write pairs like (1,2), (2,3).
Composition
Idea. Composition chains two relations into a two-step journey. First take a step with R1, then a step with R2. Whenever that journey is possible from x to z, the pair (x, z) is in the composite.
Reading order. R2 ∘ R1 is read right to left: R1 acts first. (The slide's figure labels the answer R2 ∘ R1 = {(b, x), (b, z)}, while the slide's text layer shows R1 ∘ R2. Follow the figure and the definition: first R1, then R2.)
How to compute. For each pair (x, y) in R1, find every pair in R2 that starts with that same y, say (y, z). Each match gives (x, z). The middle element y is used up and does not appear in the answer.
6. Common mistakes
- Calling a relation reflexive when it has loops only for the elements that appear. Check every element of A.
- Thinking antisymmetric means "not symmetric". They are different questions.
- Saying (a, a) breaks antisymmetry. It does not: a = a.
- Checking only some chains for transitivity. Check every pair against every pair that starts where it ends.
- Treating (a, b) and (b, a) as the same pair. They are different.
- Forgetting vacuous truth: with no chains, a relation is transitive.
- Reading R2 ∘ R1 left to right.
Connections. Every definition above is an implication (Week 1). Antisymmetry has the form (p ∧ q) → r, and its contrapositive says: if x ≠ y then not both (x, y) and (y, x) are in R. Checking a property is an algorithm (Week 3), and next week you will see these properties in matrices and graphs.
7. Active recall
Answer from memory in the box, then check. The check button unlocks once you have typed something.
8. Practice set
Three easy, four medium, three challenging. Many come straight from your slide exercises. Write your attempt first. Hints come in three levels, and the solution unlocks only after you have written an attempt. You can use the explorer and calculators above to check an answer, but work it out yourself first.
9. Weekly checkpoint
Rate how you feel about each objective, then build your checkpoint.
One relation, three pictures.
This week covers Sections 2 and 3 of your Chapter 3 slides: representing relations with matrices and digraphs, then equivalence relations, equivalence classes and partitions. Anything I add that is not on your slides is labelled "Beyond the slides".
The tool below takes one relation (a list of pairs) and shows it as a zero-one matrix, a digraph and a list of properties. Edit the pairs, or press a preset.
Matrix MR
Digraph
Properties
Objectives. By the end you can: build the zero-one matrix of a relation and read a relation back from a matrix; draw and read a digraph; spot reflexive, symmetric and antisymmetric relations from a matrix or digraph; define an equivalence relation and find its equivalence classes; and decide whether a collection of sets is a partition.
1. Representing relations with matrices
Big picture. A table of 1s and 0s can hold a whole relation. A 1 means "related", a 0 means "not related". It is the grid from Week 4, written as numbers.
Reading it. Row i is the first element, column j is the second element. The entry in row i, column j answers: is ai related to bj? The elements can be listed in any order, but when A = B use the same order for rows and columns.
Slide example 1. A = {1, 2, 3}, B = {1, 2}, and R holds (a, b) when a > b. Then R = {(2,1), (3,1), (3,2)} and:
| MR | 1 | 2 |
|---|---|---|
| 1 | 0 | 0 |
| 2 | 1 | 0 |
| 3 | 1 | 1 |
Going the other way: matrix to pairs
Slide example 2 gives a matrix and asks for the pairs. The tool below does that. Rows are separated by new lines, entries by spaces.
Properties straight from the matrix
For a relation on a set (a square matrix), your slides give three quick tests:
- Reflexive if all entries on the main diagonal are 1.
- Symmetric if and only if mij = 1 whenever mji = 1. Picture: the matrix is a mirror image across the diagonal.
- Antisymmetric if and only if mij = 0 or mji = 0 whenever i ≠ j. Picture: no pair of 1s in mirror positions off the diagonal.
Slide example 3. MR = [[1,1,0],[1,1,1],[0,1,1]]. The diagonal is all 1, so R is reflexive. The matrix equals its mirror image, so R is symmetric. And m12 and m21 are both 1, so R is not antisymmetric. (Press "Slide matrix example 3" in the tool at the top.)
2. Representing relations with digraphs
Big picture. Draw each element as a dot and each pair (a, b) as an arrow from a to b. It is the most visual way to see a relation.
Slide example 1. The digraph with vertices a, b, c, d and edges (a,b), (a,d), (b,b), (b,d), (c,a), (c,b), (d,b) is what the tool at the top shows when it loads. Notice the loop at b, and the two-way pair b ↔ d.
Slide example 2. The ordered pairs of the slide's second digraph are (1,3), (1,4), (2,1), (2,2), (2,3), (3,1), (3,3), (4,1), (4,3). Press "Slide digraph example 2" to draw them.
Reading properties from a digraph
| Property | What to look for |
|---|---|
| Reflexive | A loop at every vertex. |
| Symmetric | If (x, y) is an edge, so is (y, x). Every arrow has a return arrow. |
| Antisymmetric | If (x, y) with x ≠ y is an edge, then (y, x) is not an edge. No return arrows (loops are fine). |
| Transitive | If (x, y) and (y, z) are edges, so is (x, z). Every two-step route has a direct shortcut arrow. |
3. Equivalence relations
Big picture. An equivalence relation is a precise version of "is the same kind as". "Has the same birthday month as", "lives in the same city as" and "has the same remainder when divided by 3" all sort things into groups of look-alikes.
Why these three? Reflexive: everything is the same kind as itself. Symmetric: if a is the same kind as b, then b is the same kind as a. Transitive: if a matches b and b matches c, then a matches c.
Try it. Press "Same remainder mod 3" in the tool at the top. It shows the relation a ∼ b if a and b leave the same remainder when divided by 3 on {1, …, 6}: the matrix, the digraph, and a verdict that it is an equivalence relation, with its classes listed. Then press "a divides b". It fails: which property breaks?
Equivalence classes
Let A be a nonempty set and ∼ an equivalence relation on A. The equivalence class of a, written [a] and read "bracket a", is the set of all elements of A that are equivalent to a. (The formula did not show in my copy of your slide, so this is the standard definition.) Your slide lists three facts:
- For each a ∈ A, a ∈ [a]. (Because ∼ is reflexive.)
- For each a, b ∈ A, a ∼ b if and only if [a] = [b].
- For each a, b ∈ A, [a] = [b] or [a] ∩ [b] = ∅. Two classes are either identical or completely separate.
In the mod 3 example, [1] = [4] = {1, 4}, [2] = [5] = {2, 5} and [3] = [6] = {3, 6}. Three classes, no overlaps.
Partitions
- Ai ≠ ∅ for every i ∈ I,
- Ai ∩ Aj = ∅ whenever i ≠ j,
- and the union of all the Ai is S.
Picture. Cut a cake into pieces: no piece is empty, no two pieces overlap, and together they make the whole cake. The equivalence classes of an equivalence relation on A form a partition of A.
Partition checker
Type a set and a collection of subsets in braces. The checker tests the three conditions one by one.
4. Common mistakes
- Reading a matrix with rows and columns swapped. Row = first element.
- Calling a matrix antisymmetric because it is not symmetric. Test the mirror positions.
- Forgetting that the diagonal must be all 1s for reflexive.
- In a digraph, forgetting a loop at one vertex when checking reflexive.
- Checking transitivity by eye and missing a two-step path.
- Claiming an equivalence relation when one of the three properties fails. Name the pair that breaks it.
- Listing a class twice. [1] and [4] in the mod 3 example are the same class, {1, 4}.
- Calling something a partition when a block is empty, two blocks overlap, an element is missing, or an extra element appears.
Connections. Matrices and digraphs are two pictures of the pairs from Week 4. Equivalence relations combine three of the Week 4 properties. The partition conditions use set operations (∩, ∪, ∅) you already know, and "if and only if" is the biconditional from Week 1.
5. Active recall
Answer from memory in the box, then check. The check button unlocks once you have typed something.
6. Practice set
Three easy, four medium, three challenging. Several come from your slide exercises. Write your attempt first. Hints come in three levels, and the solution unlocks only after you have written an attempt. Use the tools above to check an answer, but work it out yourself first.
7. Weekly checkpoint
Rate how you feel about each objective, then build your checkpoint.
Dots, lines, and the shortest way between them.
This week covers your Chapter 4 slides on graphs: graph terminology, special graphs, representing graphs, connectivity and Dijkstra's shortest-path algorithm. Tutorial 5 is in the practice set. Anything I add that is not on your slides is labelled "Beyond the slides".
The graph lab below draws any graph you type and works out the degrees, the neighbourhoods, the type of graph, the components, and the adjacency list and matrices. Type edges like a-b, b-c, c-c (type an edge twice for a multiple edge), or press a slide example.
Objectives. By the end you can: name the type of a graph (simple, multigraph, pseudograph, directed); find degrees, neighbourhoods, in-degrees and out-degrees; recognise Kn, Cn, Wn, bipartite graphs and Km,n; represent a graph with an adjacency list, an adjacency matrix and an incidence matrix; read paths, circuits and connected components; and find shortest paths with Dijkstra's algorithm.
1. Graphs and graph terminology
Big picture. A graph is a set of dots (vertices) joined by lines (edges). Road maps, social networks and computer networks are all graphs. Last week a digraph pictured a relation; this week graphs are the main object.
Kinds of graph
- A simple graph: each edge connects two different vertices, and no two edges connect the same pair.
- A multigraph may have multiple edges between the same two vertices. If m edges connect u and v, the edge {u, v} has multiplicity m.
- A loop is an edge from a vertex to itself. A pseudograph may have loops as well as multiple edges.
- A simple directed graph has no loops and no multiple directed edges. A directed multigraph may have multiple directed edges (m edges from u to v: (u, v) has multiplicity m).
| Type | Edges | Multiple edges? | Loops? |
|---|---|---|---|
| Simple graph | Undirected | No | No |
| Multigraph | Undirected | Yes | No |
| Pseudograph | Undirected | Yes | Yes |
| Simple directed graph | Directed | No | No |
| Directed multigraph | Directed | Yes | Yes |
| Mixed graph | Directed and undirected | Yes | Yes |
Adjacent, neighbourhood, degree
- Adjacent (neighbours): u and v are adjacent if an edge e joins them. The edge e is incident with u and v.
- Neighbourhood N(v): the set of all neighbours of v. For a set A of vertices, N(A) is the union of N(v) over all v in A.
- Degree deg(v): the number of edges incident with v, where a loop counts two.
Slide example. Press "Slide graph G" in the lab: deg(a) = 2, deg(b) = deg(c) = deg(f) = 4, deg(d) = 1, deg(e) = 3, deg(g) = 0, and N(g) = ∅ (g is isolated). Then press "Slide graph H": the loop at b adds 2, so deg(b) = 6, and b is in its own neighbourhood: N(b) = {a, b, c, d, e}.
In-degree and out-degree
Try it. Press "Slide adjacency list (directed)" in the lab. It has 12 edges, so the in-degrees add up to 12 and so do the out-degrees.
2. Special types of graphs
- Complete graph Kn: n vertices with exactly one edge between each pair of different vertices.
- Cycle Cn (n ≥ 3): vertices v1, …, vn and edges {v1, v2}, {v2, v3}, …, {vn−1, vn}, {vn, v1}.
- Wheel Wn (n ≥ 3): a cycle Cn plus one extra vertex joined to each of the n vertices.
- Bipartite graph: a simple graph whose vertices split into two disjoint sets V1 and V2 so that every edge joins a vertex of V1 to a vertex of V2. Same thing: you can colour the vertices red and blue with no edge joining two vertices of the same colour.
- Complete bipartite graph Km,n: V1 has m vertices, V2 has n vertices, and every vertex of V1 is joined to every vertex of V2.
- Union G1 ∪ G2: the simple graph with vertex set V1 ∪ V2 and edge set E1 ∪ E2.
Slide example (bipartite). To show a graph is not bipartite, try the colouring and find a clash. On the slide, colouring a red forces its neighbours f and b to be blue, but f and b are adjacent. Any triangle does this, so K3 is not bipartite.
Special graph drawer
3. Representing graphs
Big picture. A computer cannot store a drawing, so we store the graph as a list or a table. You should be able to go from a drawing to each representation and back.
Adjacency lists
An adjacency list (for a graph with no multiple edges) lists, for each vertex, the vertices adjacent to it. For a directed graph, list each initial vertex with its terminal vertices (where its arrows go).
| Vertex | Adjacent vertices |
|---|---|
| a | b, c, e |
| b | a |
| c | a, d, e |
| d | c, e |
| e | a, c, d |
| Initial vertex | Terminal vertices |
|---|---|
| a | b, c, d, e |
| b | b, d |
| c | a, c, e |
| d | |
| e | b, c, d |
Your slide tables. Press the two "Slide adjacency list" buttons in the lab to see the graphs they describe.
Adjacency matrices
- For a simple graph the matrix is symmetric (aij = aji) and the diagonal is all 0 (no loops).
- With multiple edges, aij is the number of edges between vi and vj. A loop at vi puts a 1 at position (i, i) (more loops, a bigger number).
- For a directed graph, aij is the number of edges from vi to vj: row = start, column = end. It need not be symmetric.
- The matrix depends on the order of the vertices. Use the order the question gives (often alphabetical).
Slide example. Press "Slide pseudograph matrix". With order a, b, c, d the matrix is [[0,3,0,2],[3,0,1,1],[0,1,1,2],[2,1,2,0]]: three edges between a and b, one loop at c.
Incidence matrices
Rows are vertices, columns are edges. Each column of a normal edge has exactly two 1s; a loop's column has a single 1; multiple edges give identical columns. The lab numbers the edges in the order you type them and shows the incidence matrix for undirected graphs.
4. Connectivity
Big picture. A path is a walk along edges from vertex to vertex. Connectivity asks: can you get from any vertex to any other?
- The length is the number of edges (one less than the number of vertices in the list).
- A circuit begins and ends at the same vertex (u = v) and has length greater than 0.
- A path or circuit is simple if it does not use the same edge more than once.
Slide examples (graph "Slide paths example"): a, d, c, f, e is a simple path of length 4; d, e, c, a is not a path because e and c are not adjacent; b, c, f, e, b is a circuit of length 4; a, b, e, d, a, b is a path of length 5 that is not simple (it uses {a, b} twice). Check them below.
Path checker
Connected components
The lab at the top lists the components. "Slide graph G" has two: {a, b, c, d, e, f} and the lonely {g}.
5. Shortest paths: Dijkstra's algorithm
Big picture. In a weighted graph each edge has a number (a distance, a cost, a time). A shortest-path algorithm finds the minimum total cost from a source vertex to the other vertices. Dijkstra's algorithm finds the shortest paths from the source to all the other vertices.
The method by hand
- Give the source distance 0 and every other vertex distance ∞.
- Pick the unfinished vertex with the smallest distance and finalise it: its distance will not change again.
- For each neighbour v of that vertex u that is not finalised, if dist(u) + w(u, v) < dist(v), replace dist(v) and remember that the best way to v comes from u.
- Repeat steps 2 and 3 until every vertex is finalised. Follow the "comes from" links backwards to read each path.
Slide example. The tool below starts with the slide graph (vertices 0 to 8, source 0). Your slide's answer is 0 4 12 19 21 11 9 8 14, for example 0 → 7 → 6 → 5 → 4 = 8 + 1 + 2 + 10 = 21. The edge from 3 to 4 has weight 9, which does not show in the slide's picture.
6. Common mistakes
- Counting a loop as 1 in an undirected degree. It counts 2. (In a directed graph a loop adds 1 to in-degree and 1 to out-degree.)
- Mixing up in-degree and out-degree. In = arrows arriving, out = arrows leaving.
- Writing a 1 instead of the number of edges in the adjacency matrix of a multigraph.
- Writing a 2 on the diagonal for one loop. In the adjacency matrix a single loop is a 1.
- Reading a directed adjacency matrix the wrong way round. Row = start of the arrow, column = end.
- Using a different vertex order from the question. Keep the order you are given.
- Calling a path's length the number of vertices. Length = number of edges.
- In Dijkstra, finalising a vertex that is not the smallest, or forgetting to update a neighbour when a shorter route appears.
- Calling a graph bipartite without showing the two sets, or calling it not bipartite without pointing to the clash.
Connections. A directed graph is the digraph of a relation from Week 5, and the adjacency matrix of a simple directed graph is the relation's zero-one matrix. The neighbourhood N(A) is a union of sets, and Dijkstra is a greedy algorithm like those in Week 3: it always takes the closest unfinished vertex.
7. Active recall
Answer from memory in the box, then check. The check button unlocks once you have typed something.
8. Practice set
Three easy, four medium, three challenging. Most are your Tutorial 5 questions. Write your attempt first. Hints come in three levels, and the solution unlocks only after you have written an attempt. You can check an answer by typing the edges into the graph lab or the Dijkstra tool, but work it out yourself first.
9. Weekly checkpoint
Rate how you feel about each objective, then build your checkpoint.
Practice until the pattern is automatic.
This lab makes fresh exam-style questions in the same styles as your tutorials: logic, algorithms, relations and graphs. Every question is new (new numbers, new relations, new graphs), each answer is checked for you, and you can ask for up to three hints before you look at a solution.
I wrote these as new questions, not copies of your tutorial or coursework questions. They test the same skills, so the coursework itself stays yours to do.
1. How each tutorial question type works
Your tutorial and coursework sheets keep reusing a small set of question types. Here is the method for each one, in a few steps. Open a card, then try the matching question type above.
Logic
Translate a sentence into logic
- Name the two simple statements: p and q.
- Spot the key word: "only if", "if", "necessary", "sufficient", "unless", "but", "neither … nor", "not both".
- Write the connective, then test the row p = T, q = F against the sentence.
| English | Formula |
|---|---|
| P only if Q · Q is necessary for P · P is sufficient for Q · whenever P, Q | p → q |
| P if Q · P whenever Q | q → p |
| P unless Q | ¬q → p |
| P if and only if Q · necessary and sufficient | p ↔ q |
| P but not Q | p ∧ ¬q |
| neither P nor Q | ¬p ∧ ¬q |
| P or Q but not both | p ⊕ q |
Common slip: reversing "only if" and "if".
Construct a truth table
- List the variables and write 2n rows (TTT, TTF, … , FFF).
- Add a column for each sub-expression, starting with the innermost brackets and negations.
- Fill column by column, not row by row.
- The last column is the answer.
Common slip: forgetting precedence (¬ first, then ∧, ∨, →, ↔). Use brackets to be safe.
Tautology, contradiction or contingency
- Build the truth table.
- All T: tautology. All F: contradiction. Mixed: contingency.
Shortcut: to show a conditional is a tautology, ask whether the hypothesis can be true while the conclusion is false. If never, it is a tautology.
Show a tautology or an equivalence using laws (no truth table)
- Rewrite → as ¬ and ∨ using p → q ≡ ¬p ∨ q.
- Push negations inward with De Morgan.
- Regroup so that p and ¬p sit together (associative, commutative).
- Finish with the negation law (p ∨ ¬p ≡ T), then domination or identity.
- Write the name of the law beside every line.
Common slip: a line without a named law. Marks are given for the reason.
Verify a law with a truth table
- Build one column for each side of the law.
- If the two columns agree in all rows, the law is verified.
Algorithms
Properties of an algorithm (the Goldbach-style question)
- Go through the properties one at a time: input, output, precision (definiteness), finiteness, correctness, generality.
- For each, ask: "what could go wrong with this procedure?"
- Then ask which of your answers depend on an unproved fact. If the algorithm's stopping depends on whether a conjecture is true, finiteness cannot be guaranteed.
Linear and binary search steps
Linear: write i, the test x ≠ ai, and the new i at each step, then the location.
Binary: write a table with columns i, j, m, am, and the comparison x > am. Update i := m + 1 or j := m, and stop when i = j. Finally test x = ai.
Common slip: forgetting the floor, or using binary search on an unsorted list.
Sorting: describe or trace
Insertion sort in words: keep the left part sorted; take the next element, find its place among the sorted ones, shift the larger ones right, and drop it in. Repeat until the end.
Bubble sort: repeatedly compare neighbours and swap when out of order; after pass k the k largest are in place at the right.
Write min, max or sum (the "write an algorithm" question)
- Start with the first element: min := a1 (or sum := a1).
- for i := 2 to n, update with ai.
- For min: if min > ai then min := ai. For sum: sum := sum + ai.
- return the variable.
Greedy change
Take as many of the largest coin as fit, then move to the next coin with what is left. To see if greedy is optimal, try swapping the largest coin for smaller ones.
Relations
Union, intersection and difference
Treat each relation as a set of pairs. Write both sets, tick which pairs are shared, then combine: ∪ all pairs in either, ∩ shared pairs, R1 − R2 pairs in R1 that are not in R2.
Composition S ∘ R
Make a three-column table: pair (x, y) in R, pairs (y, z) in S that start with the same y, resulting pair (x, z). R is applied first. Remove duplicates.
Relation to matrix, matrix to relation
Rows are first elements, columns second elements. Put 1 where the pair is in R, else 0. To go back, each 1 in row i, column j gives (i, j).
Draw or read a digraph
Each pair (a, b) is an arrow from a to b; (a, a) is a loop. To read one, go vertex by vertex and list every arrow that leaves it.
Reflexive, symmetric (and antisymmetric) from a matrix
Reflexive: whole diagonal is 1. Symmetric: the matrix equals its mirror image. Antisymmetric: no mirrored pair of 1s off the diagonal.
Is it a partition?
Check three things: every subset is nonempty; no element is in two subsets; the union is exactly the whole set. Repeats inside one subset do not matter, and an element not in the set spoils the union.
Equivalence relation from a partition
Two elements are related exactly when they are in the same subset. For each subset, write every ordered pair of its elements (including (a, a)). A subset with k elements gives k × k pairs.
Graphs (see the next section)
In-degree and out-degree
For each vertex count arrows arriving (in) and leaving (out). Parallel arrows count each time; a loop adds 1 to both. The sum of in-degrees equals the sum of out-degrees equals the number of edges.
Adjacency list and adjacency matrix
List: for each vertex, the vertices it has an edge to. Matrix: aij is the number of edges from vertex i to vertex j, with the vertices in the stated order.
Draw a graph from an adjacency matrix
Draw one dot per row. For each entry aij draw that many edges between i and j (undirected: use the entries above the diagonal; directed: draw arrows from i to j). A 1 on the diagonal is a loop.
Dijkstra's shortest path
- Distance 0 at the start, ∞ everywhere else.
- Finalise the unvisited vertex with the smallest distance.
- For each neighbour, new distance = (this distance) + (edge weight). Keep it if smaller.
- Repeat until all vertices are finalised, then read paths backwards from the predecessors.
2. Graph basics and tools
For the full lessons on graphs (Chapter 4), open Week 6 · Graphs. This is a quick summary with the two tools.
- A graph has vertices and edges. In a directed graph each edge is an arrow from one vertex to another. A multigraph allows several edges between the same two vertices, and a loop is an edge from a vertex to itself.
- Undirected degree: the number of edge ends at the vertex, so a loop counts 2. The degrees add up to 2 × (number of edges).
- Directed: in-degree is the number of edges arriving, out-degree the number leaving, and a loop counts 1 for each. Both sums equal the number of edges.
- Adjacency list: each vertex with the vertices it is joined to. Adjacency matrix: row i, column j holds the number of edges from i to j (for an undirected graph the matrix is symmetric, and a loop gives a 1 on the diagonal).
Graph lab
Type edges like a-b, b-c, c-c. Tick "directed" to read each edge as an arrow from the first vertex to the second. Parallel edges are allowed (type them twice). The lab draws the graph and gives the degrees, the adjacency list and the adjacency matrix.
Dijkstra tool
Type weighted edges like A-B:4, A-D:3 and a start vertex. The tool shows the table you would write by hand: one row per step, with the vertex being finalised.
3. Mock exam
A mixed set from all four topics, with no feedback until you submit, like the real thing. A timer runs while you work. After submitting you see the marks per topic, every solution, and your weakest topic.
4. Exam cheat sheet
One page of everything you need to recall, collected from all the weeks.
Logic
- p → q is false only when p = T and q = F. p → q ≡ ¬p ∨ q ≡ ¬q → ¬p. Converse (q → p) and inverse (¬p → ¬q) are not equivalent to it.
- De Morgan: ¬(p ∧ q) ≡ ¬p ∨ ¬q, ¬(p ∨ q) ≡ ¬p ∧ ¬q.
- Laws: identity (p ∧ T ≡ p), domination (p ∨ T ≡ T), idempotent, double negation, negation (p ∨ ¬p ≡ T, p ∧ ¬p ≡ F), commutative, associative, distributive, absorption.
- Precedence: ¬, ∧, ∨, →, ↔. A truth table with n variables has 2n rows.
- Tautology: all T. Contradiction: all F. Contingency: mixed.
Algorithms
- Properties: input, output, definiteness, correctness, finiteness, effectiveness, generality.
- Linear search: i := 1; while (i ≤ n and x ≠ ai) i := i + 1; location is i or 0.
- Binary search (sorted list): i := 1, j := n; while i < j: m := ⌊(i + j)/2⌋; if x > am then i := m + 1 else j := m; then test x = ai.
- Bubble sort: pass i compares j = 1 to n − i. Insertion sort: insert aj into the sorted first j − 1 items.
- Greedy change: largest coin that fits, repeatedly. Not always optimal (coins 4, 3, 1 and n = 6).
Relations
- Reflexive: (a, a) for every a. Symmetric: (a, b) gives (b, a). Antisymmetric: (a, b) and (b, a) give a = b. Transitive: (a, b), (b, c) give (a, c).
- Matrix: mij = 1 iff (ai, bj) ∈ R. Reflexive: diagonal all 1. Symmetric: mirror image. Antisymmetric: mij = 0 or mji = 0 for i ≠ j.
- Composition S ∘ R: (x, z) when (x, y) ∈ R and (y, z) ∈ S. R acts first.
- Equivalence relation = reflexive + symmetric + transitive. Classes [a]: either equal or disjoint. Partition: nonempty, disjoint, union is the whole set.
Graphs
- Undirected: sum of degrees = 2 × edges (loop counts 2). Directed: sum of in-degrees = sum of out-degrees = edges (loop counts 1 each).
- Adjacency matrix aij = number of edges from i to j. Adjacency list = neighbours of each vertex.
- Dijkstra: finalise the smallest unvisited distance, update neighbours with (distance + weight) if smaller, repeat.
5. Your progress
Every question you answer is counted once (the first time you check it). Use the Practice buttons to drill your weak types.
Test 1 mock paper: 60 minutes, full marking scheme.
A timed paper on Chapters 1, 2 and 3, built around the topics your lecturer said to focus on. Do it by hand on paper, like the real test, then mark yourself with the scheme.
The format
- Section A: 6 shorter questions, 30 marks, about 18 minutes.
- Section B: 5 longer questions, 60 marks, about 32 minutes.
- Total: 90 marks. That is about 50 minutes of writing, so you have roughly 10 minutes left in the hour to check your work. (The real paper may weight marks differently, so treat the percentage as a guide.)
What is covered
- Logic: naming laws (De Morgan, identity, and so on) and proofs where you must write the law beside each step.
- Algorithms: bubble sort step by step; coding max, min and sum; location and calculation for linear and binary search.
- Relations: equivalence classes and partitions; digraph to relation and relation to digraph (the two free-mark questions, A2 and A3: aim for full marks); matrices.
Difficulty goes from medium to hard. Section B contains the hardest parts (a proof that needs the biconditional law, a full equivalence-relation question, and the binary search table).
The paper
Press "Start the timed attempt" above to open the paper and start the clock.