Computational Thinking and Problem-solving
Advanced computational thinking and algorithm design.
Topics
- Big O Notation
- Divide and Conquer
- Dynamic Programming
- Graph Algorithms
Key terms
19.1 Efficiency and data structures
- Time complexity
- How the time an algorithm takes grows as the size of its input grows.
- Space complexity
- How the memory an algorithm needs grows as the size of its input grows.
- Big O notation
- A way of describing how an algorithm's time or space grows with input size n, ignoring constants.
- O(1) constant time
- Takes the same time however much data there is.
- O(log n) logarithmic time
- Time grows slowly, as the problem is roughly halved at each step. Binary search is an example.
- O(n) linear time
- Time grows in direct proportion to the data. Linear search is an example.
- O(n log n) time
- Typical of efficient sorts such as merge sort.
- O(n²) polynomial time
- Time grows with the square of the data. Bubble sort and insertion sort are examples.
- O(2ⁿ) exponential time
- Time doubles with each extra item, so it quickly becomes impractical.
- Quick sort
- Sorts by choosing a pivot, splitting the list into smaller and larger items, then sorting each part the same way.
- Merge sort
- Sorts by repeatedly splitting the list in half, then merging the sorted halves.
- Binary tree
- A structure where each node has at most two children. In a binary search tree, smaller values go left and larger values go right.
- Tree traversal
- Visiting every node of a tree: in-order, pre-order or post-order.
- Graph
- A set of nodes (vertices) joined by edges. In a weighted graph, each edge has a value such as a distance.
- Hash table
- A structure that stores each item at an address calculated by hashing its key, so items can be found very quickly.
- Dictionary
- An abstract data type that stores key and value pairs, and finds a value by its key.
19.2 Recursion
- Recursion
- A subroutine that solves a problem by calling itself on a smaller version of the problem.
- Base case
- The condition that stops the recursion. Without one the calls never end.
- General (recursive) case
- The part of a recursive subroutine that calls itself with a problem closer to the base case.
- Call stack
- The memory area that keeps track of subroutine calls and where to return to.
- Stack frame
- The memory used by one subroutine call, holding its parameters, local variables and return address.
- Infinite recursion
- Recursion with no reachable base case. It ends when the call stack runs out of memory.
- Recursion compared with iteration
- Recursion can give shorter, clearer code, but it uses more memory because of the stack frames.
19.3 Problem-solving techniques (extension)
- Divide and conquer
- Solving a problem by splitting it into smaller sub-problems, solving those, then combining the answers.
- Dynamic programming
- Solving a problem by storing the answers to overlapping sub-problems so that each is worked out only once.
- Memoisation
- Saving the result of a function call so that it can be reused instead of recalculated.
Practice questions for Computational Thinking and Problem-solving