Chapter 1.5: Computation and Computational Thinking
Introduction
Learning Outcomes
Explain computation as processing data according to rules.
Trace a Turing machine step by step.
Distinguish declarative from imperative knowledge; trace Euclid's algorithm.
Apply decomposition, pattern recognition, abstraction and algorithm design.
Introduction
Computation
Introduction
The Building Blocks of Computation
Operations on quantities and relations.
Evaluating conditions as true or false.
Encoded inputs and intermediate results of a problem.
A finite, ordered sequence of operations.
The system that physically carries out the steps.
A way to express an algorithm to a computer.
Computer Science
Computer Science
Computer science is not just about using tools; it studies how information is represented and how problems can be solved.
Hiding unnecessary detail to focus on the essential structure.
Designing and analyzing the steps of a solution.
Understanding how hardware and software work together.
Weighing technology's impact, security and ethics.
Computer Science
Technology and Responsibility
Equal access to technology for different users.
Using personal data securely and only for its stated purpose.
Data and algorithms should not produce discriminatory outcomes.
Accounting for energy, hardware lifetime and e-waste.
History
Milestones in the History of Computing
History
Fixed-Program vs. Stored-Program Machines
| Approach | How is the program defined? | Flexibility |
|---|---|---|
| Fixed-program | By the machine's physical layout or a special mechanism | A specific task |
| Stored-program | Instructions are stored in memory like data | Different tasks by changing the program |
| General-purpose | An architecture that can run very different programs | Broad range of uses |
Models of Computation
The Universal Turing Machine
A Turing machine is an abstract model used to explain algorithmic computation.
- The head reads a symbol, may write a new one, and moves left or right.
- A transition rule picks the next step from the current state and symbol.
- A universal Turing machine reads the rules of another machine from its tape and simulates it: the theoretical basis of the stored-program idea.
Models of Computation · Step by step
A Turing Machine Step by Step: 111 + 1
Start: the tape holds 111 (binary 7), the head is on the rightmost digit, and the state is q0. Goal: add 1.
Reads 1 in state q0 → rule 1: write 0, move the head left (the carry of 1 continues).
Reads 1 in state q0 → rule 1: write 0, move the head left (the carry of 1 continues).
Reads 1 in state q0 → rule 1: write 0, move the head left (the carry of 1 continues).
Reads □ in state q0 → rule 3: write 1, and the machine halts. Tape: 1000 = 8. Every step was determined only by (state, symbol read).
Models of Computation
The Limits of Computers
Large inputs can make some algorithms impractical.
Data and intermediate results use finite storage.
Every operation and data transfer consumes energy.
Some problems have no algorithm that always gives an answer (e.g. the halting problem).
Missing or wrong input prevents a correct result.
A program represents only selected features of the real world.
Knowledge and Algorithms
Kinds of Knowledge
States what is true or what a concept is.
The angles of a triangle add up to 180°.Explains in steps how to reach a result.
Approximate the square root of a number.Knowledge and Algorithms · Step by step
Imperative Knowledge: Approximating a Square Root (x = 25)
We want the square root of x = 25.
A rough starting guess: guess = 1.
Error |1² − 25| = 24 → > 0.01: continue.
Pass 1: guess = (1 + 25 / 1) / 2 = 13. Average the guess with x/guess.
Error |13² − 25| = 144 → > 0.01: continue.
Pass 2: guess = (13 + 25 / 13) / 2 ≈ 7.462. Average the guess with x/guess.
Error |7.462² − 25| ≈ 30.67 → > 0.01: continue.
Pass 3: guess = (7.462 + 25 / 7.462) / 2 ≈ 5.406. Average the guess with x/guess.
Error |5.406² − 25| ≈ 4.225 → > 0.01: continue.
Pass 4: guess = (5.406 + 25 / 5.406) / 2 ≈ 5.015. Average the guess with x/guess.
Error |5.015² − 25| ≈ 0.1527 → > 0.01: continue.
Pass 5: guess = (5.015 + 25 / 5.015) / 2 ≈ 5.00002. Average the guess with x/guess.
Error |5.00002² − 25| ≈ 0.00023 → ≤ 0.01: close enough, stop.
Output: ≈ 5.0000. In 5 passes it got very close to the true value (5): knowing “how to find it” is imperative knowledge.
x = 25
guess = 1
while |guess * guess - x| > 0.01
guess = (guess + x / guess) / 2
print(guess)Knowledge and Algorithms
Euclid's Algorithm
An algorithm is a method that solves a problem in finite, unambiguous steps. One of the oldest known is Euclid's (around 300 BC): it finds the greatest common divisor of two positive integers by repeatedly taking remainders.
GCD(a, b)
while b ≠ 0
r = a mod b
a = b
b = r
return aKnowledge and Algorithms · Step by step
Euclid Step by Step: GCD(48, 18)
GCD(48, 18) is called: a = 48, b = 18. The bars show the numbers as lengths.
b ≠ 0 → 18 ≠ 0 → true: pass 1 begins.
r = a mod b = 48 mod 18 = 12: 2 pieces of length b fit into a, leaving 12.
a = b → a = 18. The smaller number becomes the dividend.
b = r → b = 12. A common divisor also divides the remainder; the problem got smaller.
b ≠ 0 → 12 ≠ 0 → true: pass 2 begins.
r = a mod b = 18 mod 12 = 6: 1 piece of length b fits into a, leaving 6.
a = b → a = 12. The smaller number becomes the dividend.
b = r → b = 6. A common divisor also divides the remainder; the problem got smaller.
b ≠ 0 → 6 ≠ 0 → true: pass 3 begins.
r = a mod b = 12 mod 6 = 0: 2 pieces of length b fit into a, leaving 0.
a = b → a = 6. The smaller number becomes the dividend.
b = r → b = 0. A common divisor also divides the remainder; the problem got smaller.
b ≠ 0 → 0 ≠ 0 → false: the loop ends.
return a → 6. GCD(48, 18) = 6: both 48 and 18 are measured exactly by pieces of length 6. Done in 3 passes.
GCD(a, b) while b ≠ 0 r = a mod b a = b b = r return a
Computational Thinking
Computational Thinking
Computational thinking is the practice of turning problems into forms that a computer or a person can solve systematically.
Computational Thinking
Decomposition
The big problem: an online registration system. Too hard to tackle in one piece.
It is split into main subtasks, each with its own clearly defined inputs and outputs.
Split further if needed. Each leaf is a small job that can be written and tested on its own.
- Each subproblem can be developed and tested separately.
- The inputs and outputs of subproblems are clearly defined.
- Dependencies show up early.
Computational Thinking
Patterns and Abstraction
Three different record types: student, staff and visitor.
Pattern recognition: the name, national ID and email fields repeat in all three.
Abstraction: the shared fields go into a single Person model; the differences stay only in the types that need them.
Computational Thinking
Algorithm Design and Testing
- Consider normal, smallest, largest and invalid inputs.
- Check that each step produces the expected state.
- When you find a bug, fix the root cause, not just the symptom.
Summary
Chapter Summary
- Computation is processing data according to rules; it is not limited to arithmetic.
- The stored-program idea, keeping instructions in memory like data, made the general-purpose computer possible.
- The Turing machine is an abstract model of algorithmic computation; each step is determined by the state and the symbol read.
- Imperative knowledge tells how to reach a result; Euclid's algorithm is a historical example.
- Computational thinking combines decomposition, pattern recognition, abstraction and algorithm design.
- Time, memory, energy and computability set the limits of computers.
Summary
Review Questions
- Why is computation not just arithmetic?
- Give an example each of declarative and imperative knowledge.
- How does a fixed-program machine differ from a stored-program machine?
- What determines the next step of a Turing machine?
- Why does Euclid's algorithm always halt?
- How would you approach a problem with the four components of computational thinking?
Summary
Exercises
- Trace GCD(84, 36) with Euclid's algorithm; record a, b and r for each pass in a table.
- Classify a calculator, a washing machine controller and a smartphone as fixed-program or stored-program, and justify your answer.
- Which details of a real city does a subway map hide, and which does it keep? Why does it work as an abstraction?
- Run the add-one Turing machine with 1011 on the tape (head on the rightmost digit); what value is on the tape when it halts?
- Do three passes of the square-root method with x = 10 and guess = 1; what value is the guess approaching?
- Decompose a library lending system: at least four subtasks with their inputs/outputs; which fields do member and staff records share?
Summary
References
- Wing, Computational Thinking, Communications of the ACM, 2006.
- Cormen et al., Introduction to Algorithms.
- Downey, Think Java.
- Sipser, Introduction to the Theory of Computation.