Chapter 1: Foundations

Algorithms

What is an algorithm? (Cormen et al., 2022, p. 3)
18:49

Question

What is an algorithm?

Definition (Algorithm)

Sequence of computational steps that transform an input into an output

Example: Insertion sort (Cormen et al., 2022, p. 17)
18:49

Question

When playing cards, how would you sort your hand in ascending order?

Loading

Insertion sort
18:49

Here is a more standard implementation of insertion sort

Loading

Analysis of an algorithm
18:49

For every algorithm, we shall study the following properties.

  • Termination: Does the algorithm terminate?
  • Correctness: Does it yield the desired result?
  • Running time: How fast is the algorithm?

Correctess and Loop invariants of insertion sort (Cormen et al., 2022, pp. 19-21)
18:49

Definition (Loop invariant)

Property which is true before each iteration of a loop.

Loop invariants are important tools to show correctness.

Proposition (Insertion sort's loop invariant)

The subarray A[:i] contains the first i elements of the original array in sorted order.

Running time
18:49

Question (Running time?)

Assume that has elements. If the cost of the -th line is , what is total running cost?

Let's consider the best and worst case scenarios

Computing time
18:49

We are interested in , where is the time an algorithm takes to solve a problem of size .

Loading

We shall only keep the asymptotic term:

-notation (Cormen et al., 2022, p. 54-55)
18:49

Let and be positive functions.

Definition ()

if there exist positive constants and such that

As an abuse of notation, we shall write .

Loading

-notation (Cormen et al., 2022, p. 54-55)
18:49

Definition ()

if there exist positive constants and such that

As an abuse of notation, we shall write .

Loading

Algorithmic families
18:49

Constant
Logarithmic
Linear
Quadratic
Polynomial
Exponential
Factorial
Big-O Complexity Chart

Exercises: -notation
18:49

Programming tasks
18:49

Exercise

  • Given an array A of sorted elements, write an algorithm that would search for a given value. What's the running time?
  • Assume A is an array of elements which are increasing then decreasing. Write an algorithm that finds the maximum value. What's its running time?
  • Given an array of strictly the characters R, G, and B, segregate the values of the array so that all the Rs come first, the Gs come second, and the Bs come last. You can only swap elements of the array. Do this in linear time and in-place.
Loading

Divide et impera
18:49

A common stategy is divide-and-conquer.

  • Divide into similar subproblems
  • Conquer by solving them recursively
  • Combine the subproblem solutions to solve the original problem
Merge sort example

Merge sort: example
18:49

Example

Sort the following array: [11, 6, 3, 24, 46, 22, 7]

Merge sort
18:49

Loading

Running time of merge sort (Cormen et al., 2022, pp. 42-43)
18:49

Proposition (Running time of merge sort)

  1. Guess an expression for
  2. Rigorous proof by induction

Sorting olympics
18:49

Insertion sort

Loading

Merge sort

Loading

Solving recurrences (Cormen et al., 2022, p. 90)
18:49

Information (Substitution method)

  • Guess the form of the solution using symbolic constants
  • Proof by induction

Example

Show that the recurrencehas solution .

Exercises: solving recurrences (Cormen et al., 2022, pp. 94-95)
18:49

Exercise

Use the substitution method to show that each of the following recurrences has the asymptotic solution specified:

  1. has solution
  2. has solution
  3. has solution
  4. has solution
  5. has solution
  6. has solution

Exercise (Lower-order term trick)

Show that is the solution to the recurrence .

Exercises: solving recurrences part II (Cormen et al., 2022, p. 101)
18:49

Exercise

For each of the following recurrences, sketch its recursion tree, and guess a good asymptotic upper bound on its solution. Then use the substitution method to verify your answer.

Exercise (Master theorem)

Assume that , with and being integers. If for some , then .

Recurrences: Avoiding pitfalls (Cormen et al., 2022, pp. 93-94)
18:49

Exercise

Find the mistake in the reasoning below:

We can check that satisfies the recurrence relation, as

Comparison of sorting algorithms
18:49

Insertion sortMerge sortQuicksort
In placeYesNoYes
ComparisonYesYesYes
StableYesYesNo
Worst case runtime
Average case runtime
Best case runtime
Comparison sort
Only uses comparisons to chose between different permutations
Stable sort
Maintain the order of identical elements

We shall see later that is the best we can do for comparison sorts.

Quick sort: example
18:49

Example

Sort the following array: [11, 6, 3, 24, 46, 22, 7]

Exercise

Apply (by hand) the quicksort algorithm to:

A first implementation of quicksort
18:49

The real quicksort is in place. Here, we use the first element as pivot.

Loading

Hoare partition's scheme (Cormen et al., 2022, pp. 183-184)
18:49

Example (Hoare partition's scheme)

Apply the Hoare partition scheme on [11, 6, 3, 24, 46, 22, 7]

Exercise

Illustrate the partition operation on the array

In-place partitioning (Cormen et al., 2022, p. 184)
18:49

Loading

Proposition (Invariant)

  • In , the elements are .
  • In , the elements are .

Exercises
18:49

Exercise

  1. Describe the worst case for quicksort. What's the runtime?
  2. Describe the best case for quicksort. What's the runtime?
  3. What is the runtime of the algorithm if all the elements are equal?

Exercise

Without sorting, implement an algorithm that finds the -th smallest element.

The algorithm must be in place and on average.

Loading

Runtime of quicksort
18:49

Proposition (Runtime of quicksort)

  • The worst-case runtime is
  • The average case runtime is
  • The best case runtime is

Remark (Choice of pivot)

The algorithm may perform poorly. One idea would be to choose a random pivot.

Exercise

Implement Quicksort with a random pivot.

Multiplication algorithm
18:49

Fast multiplication of large numbers is essential in CAS, cryptography, etc.

For a long time, people thought that the multiplication algorithm we learn in primary school was optimal.

Question

Assume we have two numbers with digits.

  1. What is the runtime of the traditional multiplication algorithm?
  2. What is the runtime when you sum them?

Towards the Карацу́ба (Karatsuba) algorithm
18:49

Exercise

Assume are digits and calculate the product Compare the result with

Hence, use this to calculate using 3 multiplications.

Exercise (Карацу́ба's trick)

Use Карацу́ба's trick to calculate in 3 multiplications.

Карацу́ба's algorithm in Python
18:49

Loading

Karatsuba's runtime
18:49

Loading

Exercise

Calculate the runtime of Karatsuba's algorithm.

What about if we only did divide-and-conquer without applying Karatsuba's trick?

Hint:

Closest pair of points
18:49

Problem (Closest pair of points)

Given distinct points , find such that

Loading

Question

  • What is the runtime of the brute-force algorithm?
  • Could you think of a faster algorithm using divide-and-conquer?

Closest pair: divide and conquer
18:49

  1. Divide points into two halves using a vertical line
  2. Conquer: Find closest pair in each side recursively
  3. Combine: Find closest amongst pairs crossing the line

Question (Complexity of Step III)

Is it possible to do better than for Step III?

Idea

If the closest pairs in each sides have distances , we only need to look at a distance from the line.

Closest pair: Step III
18:49

Idea (Ordering)

Sort these points by their -coordinate (). We now need only check each pair against their immediate neighbours.

Proposition

Assume are sorted according to their coordinate. If then .

Closest pair: runtime
18:49

Theorem (Runtime of closest pair)

The runtime of our closest pair algorithm is

Optimality of for comparison sorts
18:49

Theorem

The running time of a comparison sorting algorithm is at best .

Hint:

Counting sort: an example
18:49

Example

Sort the list
3227246181
  1. Keep a running tally
    0123456789
              
  2. Recontruct the sorted list

Counting sort: Python implementation
18:49

Loading

Counting sort: running time
18:49

Proposition

The running time of counting sort is , where is the number of admissible elements.

Stability
18:49

Definition

A sorting algorithm is called stable if it preserves the order of records with equal keys.

Exercise

Which of the following sorting algorithms are stable: insertion sort, merge sort, and quicksort? Give a simple scheme that makes any comparison sort stable. How much additional time and space does your scheme entail?

Exercise

How would you transform any comparison algorithm into a stable algorithm?

Stability and counting sort
18:49

Exercise

Write a stable algorithm that sorts integers on their last digit.

Loading

Is counting sort actually good?
18:49

An active open problem is the conjecture that a list of integers can be sorted in . Currently, the best known algorithm sorts in

Counting sort is far from that conjecture. It is when .

Stable counting sort
18:49

Loading

Towards Radix sort
18:49

Idea

Use a stable counting sort to sort on the last digit, then on the penultimate, etc.

Example

Sort the list

Exercise

Use radix sort to sort the following words: COW, DOG, SEA, RUG, ROW, MOB, BOX, TAB, BAR, EAR, TAR, DIG, BIG, TEA, NOW, FOX.

Implementing Radix sort
18:49

Exercise

Implement Radix sort in Python. What is the invariant? Prove the algorithm is correct. What is its runtime?

Loading

Exercise

How would you sort integers in the range to in time.

Counting and Radix sort
18:49

Runtime of counting sort on

Runtime of radix sort on :

Observe that for a general base , radix sort works in

We can check that this is optimas as .

Correctness
18:49

Exercise (Correctness)

Prove the correctness of radix sort.

(Non-examinable) Towards Fourier analysis
18:49

Exercise

Calculate

Discuss in terms of .

(Non-examinable) Fourier and Fast Fourier Transform
18:49

Question

Signals often take the shape:

How could we find the values

Idea (Averaging)

What happens if we integrate both sides?

Discrete Fourier Transform
18:49

Definition (Discrete Fourier Transform)

Instead of an actual function, we'll often have a discretization, so that the most common definition is

Periodicity
18:49

Proposition

Complexity of the DFT
18:49

Proposition

Calculating all the discrete Fourier coefficients has a runtime of

The FFT
18:49

Idea

  • Divide into two sets:
  • Calculate the DFTs on both subsets:
  • Conquer! They are enough to calculate for .

FFT in Python
18:49

Theorem

Calculate the running time of the FFT.

Revision
18:49

Revision
18:49

Revision
18:49