Chapter 1.5: Computation and Computational Thinking

Computer Fundamentals and Introduction to Programming
ComputationHistory of computingThe Turing machineKnowledge and algorithmsComputational thinking
Sercan KÜLCÜ · Giresun University

Introduction

Learning Outcomes

Computation

Explain computation as processing data according to rules.

Models of computation

Trace a Turing machine step by step.

Knowledge and algorithms

Distinguish declarative from imperative knowledge; trace Euclid's algorithm.

Computational thinking

Apply decomposition, pattern recognition, abstraction and algorithm design.

Introduction

Computation

Inputdata to processe.g. 70, 85, 60, 75Processrule-based stepse.g. add, divide by 4Outputthe resulte.g. average 72.5Resources: time · memory · energy · communication
Computation is more than arithmetic. Searching, sorting, deciding, transforming and communicating are computation too.

Introduction

The Building Blocks of Computation

Mathematics

Operations on quantities and relations.

Logic

Evaluating conditions as true or false.

Data

Encoded inputs and intermediate results of a problem.

Algorithm

A finite, ordered sequence of operations.

Hardware

The system that physically carries out the steps.

Programming language

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.

Abstraction

Hiding unnecessary detail to focus on the essential structure.

Algorithms

Designing and analyzing the steps of a solution.

Systems

Understanding how hardware and software work together.

People and society

Weighing technology's impact, security and ethics.

Computer Science

Technology and Responsibility

Accessibility

Equal access to technology for different users.

Privacy

Using personal data securely and only for its stated purpose.

Fairness

Data and algorithms should not produce discriminatory outcomes.

Sustainability

Accounting for energy, hardware lifetime and e-waste.

History

Milestones in the History of Computing

1800sBabbage: the AnalyticalEngine design;Lovelace: notes onan algorithm1930sTuring: an abstractmodel of computation;first electronicdigital experiments1940sColossus and ENIAC:electroniccomputingmachines1950sStored programsbecome common;Fortran (1957)TodayMobile, cloud,AI anddistributed systems
As hardware got faster and smaller, the same core ideas (algorithms, stored programs) found ever wider use.

History

Fixed-Program vs. Stored-Program Machines

Fixed-programMachinewiring / gear layout= the programone taskStored-programinstr: read xinstr: addinstr: writedata: 42data: 17MemoryCPUfetch instruction
ApproachHow is the program defined?Flexibility
Fixed-programBy the machine's physical layout or a special mechanismA specific task
Stored-programInstructions are stored in memory like dataDifferent tasks by changing the program
General-purposeAn architecture that can run very different programsBroad range of uses

Models of Computation

The Universal Turing Machine

A Turing machine is an abstract model used to explain algorithmic computation.

tape□□1011□□□……state: q0read/write head
  • 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

tape□□111□……state: q0read/write headTransition rules(state, read) → write, move, new state1) q0, 1 → 0, left, q02) q0, 0 → 1, stop, HALT3) q0, □ → 1, stop, HALTOn the tape: 111 (binary) = 7

Start: the tape holds 111 (binary 7), the head is on the rightmost digit, and the state is q0. Goal: add 1.

tape□□110□……state: q0read/write headTransition rules(state, read) → write, move, new state1) q0, 1 → 0, left, q02) q0, 0 → 1, stop, HALT3) q0, □ → 1, stop, HALTOn the tape: 110 (binary) = 6

Reads 1 in state q0 → rule 1: write 0, move the head left (the carry of 1 continues).

tape□□100□……state: q0read/write headTransition rules(state, read) → write, move, new state1) q0, 1 → 0, left, q02) q0, 0 → 1, stop, HALT3) q0, □ → 1, stop, HALTOn the tape: 100 (binary) = 4

Reads 1 in state q0 → rule 1: write 0, move the head left (the carry of 1 continues).

tape□□000□……state: q0read/write headTransition rules(state, read) → write, move, new state1) q0, 1 → 0, left, q02) q0, 0 → 1, stop, HALT3) q0, □ → 1, stop, HALTOn the tape: 000 (binary) = 0

Reads 1 in state q0 → rule 1: write 0, move the head left (the carry of 1 continues).

tape□1000□……HALTread/write headTransition rules(state, read) → write, move, new state1) q0, 1 → 0, left, q02) q0, 0 → 1, stop, HALT3) q0, □ → 1, stop, HALTOn the tape: 1000 (binary) = 8

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

Time

Large inputs can make some algorithms impractical.

Memory

Data and intermediate results use finite storage.

Energy

Every operation and data transfer consumes energy.

Computability

Some problems have no algorithm that always gives an answer (e.g. the halting problem).

Data quality

Missing or wrong input prevents a correct result.

Model limits

A program represents only selected features of the real world.

Knowledge and Algorithms

Kinds of Knowledge

Declarative knowledge

States what is true or what a concept is.

The angles of a triangle add up to 180°.
Imperative knowledge

Explains in steps how to reach a result.

Approximate the square root of a number.
60°50°70°Declarative: what is true?60° + 70° + 50° = 180°states a fact; gives no methodImperative: how to find it?g = 1g = (g + x/g) / 2g² ≈ x ? stopotherwise repeatx = 25: 1 → 13 → 7.46 → 5.41 → 5.02 → 5.00
Algorithms turn imperative knowledge into clear, executable, finite steps.

Knowledge and Algorithms · Step by step

Imperative Knowledge: Approximating a Square Root (x = 25)

01234567891011121314√25 = 5Faded dots: earlier guesses. Each round moves the guess closer to √x.

We want the square root of x = 25.

01234567891011121314√25 = 5guess ≈ 1Faded dots: earlier guesses. Each round moves the guess closer to √x.

A rough starting guess: guess = 1.

01234567891011121314√25 = 5guess ≈ 1Faded dots: earlier guesses. Each round moves the guess closer to √x.

Error |1² − 25| = 24 → > 0.01: continue.

01234567891011121314√25 = 5guess ≈ 13Faded dots: earlier guesses. Each round moves the guess closer to √x.

Pass 1: guess = (1 + 25 / 1) / 2 = 13. Average the guess with x/guess.

01234567891011121314√25 = 5guess ≈ 13Faded dots: earlier guesses. Each round moves the guess closer to √x.

Error |13² − 25| = 144 → > 0.01: continue.

01234567891011121314√25 = 5guess ≈ 7.462Faded dots: earlier guesses. Each round moves the guess closer to √x.

Pass 2: guess = (13 + 25 / 13) / 2 ≈ 7.462. Average the guess with x/guess.

01234567891011121314√25 = 5guess ≈ 7.462Faded dots: earlier guesses. Each round moves the guess closer to √x.

Error |7.462² − 25| ≈ 30.67 → > 0.01: continue.

01234567891011121314√25 = 5guess ≈ 5.406Faded dots: earlier guesses. Each round moves the guess closer to √x.

Pass 3: guess = (7.462 + 25 / 7.462) / 2 ≈ 5.406. Average the guess with x/guess.

01234567891011121314√25 = 5guess ≈ 5.406Faded dots: earlier guesses. Each round moves the guess closer to √x.

Error |5.406² − 25| ≈ 4.225 → > 0.01: continue.

01234567891011121314√25 = 5guess ≈ 5.015Faded dots: earlier guesses. Each round moves the guess closer to √x.

Pass 4: guess = (5.406 + 25 / 5.406) / 2 ≈ 5.015. Average the guess with x/guess.

01234567891011121314√25 = 5guess ≈ 5.015Faded dots: earlier guesses. Each round moves the guess closer to √x.

Error |5.015² − 25| ≈ 0.1527 → > 0.01: continue.

01234567891011121314√25 = 5guess ≈ 5Faded dots: earlier guesses. Each round moves the guess closer to √x.

Pass 5: guess = (5.015 + 25 / 5.015) / 2 ≈ 5.00002. Average the guess with x/guess.

01234567891011121314√25 = 5guess ≈ 5Faded dots: earlier guesses. Each round moves the guess closer to √x.

Error |5.00002² − 25| ≈ 0.00023 → ≤ 0.01: close enough, stop.

01234567891011121314√25 = 5guess ≈ 5Faded dots: earlier guesses. Each round moves the guess closer to √x.

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 a
18×1818×1812×1266481848 = 2×18 + 12 ; 18 = 1×12 + 6 ; 12 = 2×6Largest square that tiles exactly: 6×6
GCD(48, 18): 48 mod 18 = 12 · 18 mod 12 = 6 · 12 mod 6 = 0 · result 6

Knowledge and Algorithms · Step by step

Euclid Step by Step: GCD(48, 18)

a = 48b = 184818

GCD(48, 18) is called: a = 48, b = 18. The bars show the numbers as lengths.

a = 48b = 184818

b ≠ 0 → 18 ≠ 0 → true: pass 1 begins.

a = 48b = 181818r=1248 = 2 × 18 + 12 → remainder r = 1218

r = a mod b = 48 mod 18 = 12: 2 pieces of length b fit into a, leaving 12.

a = 18b = 181818

a = b → a = 18. The smaller number becomes the dividend.

a = 18b = 121812

b = r → b = 12. A common divisor also divides the remainder; the problem got smaller.

a = 18b = 121812

b ≠ 0 → 12 ≠ 0 → true: pass 2 begins.

a = 18b = 1212r=618 = 1 × 12 + 6 → remainder r = 612

r = a mod b = 18 mod 12 = 6: 1 piece of length b fits into a, leaving 6.

a = 12b = 121212

a = b → a = 12. The smaller number becomes the dividend.

a = 12b = 6126

b = r → b = 6. A common divisor also divides the remainder; the problem got smaller.

a = 12b = 6126

b ≠ 0 → 6 ≠ 0 → true: pass 3 begins.

a = 12b = 66612 = 2 × 6 + 0 → remainder r = 06

r = a mod b = 12 mod 6 = 0: 2 pieces of length b fit into a, leaving 0.

a = 6b = 666

a = b → a = 6. The smaller number becomes the dividend.

a = 6b = 06(0: nothing left to divide)

b = r → b = 0. A common divisor also divides the remainder; the problem got smaller.

a = 6b = 06(0: nothing left to divide)

b ≠ 0 → 0 ≠ 0 → false: the loop ends.

a = 6b = 06(0: nothing left to divide)

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.

Problem→ solution1 · Decompositionsplit a big probleminto subproblems2 · Pattern recognitionfind similaritiesand repetition3 · Abstractionfocus on what matters,hide the rest4 · Algorithm designwrite the solution inclear, ordered steps

Computational Thinking

Decomposition

Online registration system

The big problem: an online registration system. Too hard to tackle in one piece.

Online registration systemUser loginForm validationRegistrationNotification

It is split into main subtasks, each with its own clearly defined inputs and outputs.

Online registration systemUser loginemailpasswordForm validationempty fieldformatRegistrationdatabaseNotificationconfirmation email

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

Studentnamenational IDemailstudent IDStaffnamenational IDemaildepartmentVisitornamenational IDemailvisit date

Three different record types: student, staff and visitor.

Studentnamenational IDemailstudent IDStaffnamenational IDemaildepartmentVisitornamenational IDemailvisit date

Pattern recognition: the name, national ID and email fields repeat in all three.

Studentnamenational IDemailstudent IDStaffnamenational IDemaildepartmentVisitornamenational IDemailvisit datePersonnamenational IDemail+ each type's own field

Abstraction: the shared fields go into a single Person model; the differences stay only in the types that need them.

Good abstraction factors out repeated structure without losing the important differences.

Computational Thinking

Algorithm Design and Testing

Make examplesinput–outputWrite the stepspseudocodeTrace by handstep by stepTry the edge casessmallest, empty…Fixthe root causeif a bug is found, rewrite and trace again
  • 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

  1. Why is computation not just arithmetic?
  2. Give an example each of declarative and imperative knowledge.
  3. How does a fixed-program machine differ from a stored-program machine?
  4. What determines the next step of a Turing machine?
  5. Why does Euclid's algorithm always halt?
  6. How would you approach a problem with the four components of computational thinking?

Summary

Exercises

  1. Trace GCD(84, 36) with Euclid's algorithm; record a, b and r for each pass in a table.
  2. Classify a calculator, a washing machine controller and a smartphone as fixed-program or stored-program, and justify your answer.
  3. Which details of a real city does a subway map hide, and which does it keep? Why does it work as an abstraction?
  4. 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?
  5. Do three passes of the square-root method with x = 10 and guess = 1; what value is the guess approaching?
  6. 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.

The End

Chapter 1.5: Computation and Computational Thinking
Previous: Chapter 1.4: The Internet · Next: Chapter 2.1: Software and Operating Systems
1 / 1Sercan KÜLCÜ. All rights reserved.