The Lawfare Academy

Lawfare Group → Knowledge Institute → Lawfare Academy

← Back to program

← CS 201 — Data Structures, Algorithms and Systems

Weeks 1–2

Specifications, correctness and growth rates

Learning objectives

  • Explain and trace the mechanisms in specifications, correctness and growth rates.
  • Apply the method to the worked example and changed-condition exercise.
  • Identify assumptions, failure cases and the evidence needed to justify a result.

Original on-site study sequence

Study this preserved lesson with the expanded original reader linked in Further reading. It supplies mathematical preparation, advanced representations, complete original and changed models, the Cedar cumulative investigation and a 24-point rubric. Use the resource-safe laboratory edition for optional local execution. All older publisher and textbook links are optional references. Current whole-course internal review remains pending.

An algorithm is a finite, precise procedure for a specified family of inputs. Correctness and efficiency are separate questions: a fast wrong answer is still wrong. State the precondition, postcondition and input-size measure before comparing methods. For a list search, n can be the number of records; for a graph, both vertices and edges matter.

Asymptotic analysis describes how resource use grows, suppressing constant factors and smaller-order terms. O(n) is an upper-bound description, not an exact time measurement or a claim that an algorithm always scans all records. A linear search can finish immediately when the target is first and require n comparisons when absent. Worst-case, average-case and best-case claims therefore need labels. Timing a single small input cannot establish a growth law. Hardware, caching and interpreter overhead affect measured times; use counting and measurement together.

Correctness arguments often use invariants. Before each search iteration, every earlier position has been checked and does not match. Returning the current index gives a match; reaching the end establishes absence under the equality rule. The rule itself matters: case-insensitive names, unique identifiers and approximate numeric matches are different specifications.

Worked reasoning The implementation returns the first matching index, not every occurrence. None distinguishes absence from index zero. Testing if find(...) would mistakenly treat a valid zero index as false. An absent target requires examining the whole finite list. Doubling a long absent-search input roughly doubles comparisons, though measured runtime may fluctuate.

Extended teaching and laboratory

A correctness argument has initialization, preservation and termination obligations. An invariant true initially but broken by an update proves nothing about the final result. Conversely, a useful invariant need not mention every implementation variable: it must be strong enough to connect the termination condition to the postcondition. Time complexity is a separate argument about the number and cost of operations.

For a first-match search, the processed prefix contains no match. Returning the first encountered equality is justified because every earlier candidate has already been ruled out. An all-match search needs a different invariant: the output contains exactly the matching indices in the processed prefix. Confusing the specifications leads to incorrect early termination.

Worked problem Specify and justify a scan that returns all indices of 7 in [7,2,7,7]. State an invariant before processing index i.

Step-by-step reasoning 1. Before i=0 the result is empty, exactly matching the empty prefix.

  1. At iteration i, append i if and only if items[i] equals 7. This extends the exact-match description from the prefix ending before i to the prefix ending at i.
  1. At termination i=4, the output [0,2,3] covers the entire list. No early return is allowed after the first match.
  1. The scan takes linear comparisons. Output storage can also be linear if every element matches; that is required output, not an accidental leak.

Optional preparation

Apply the idea

Specify and justify a scan that returns all indices of 7 in [7,2,7,7]. State an invariant before processing index i.

Change the postcondition to return only the last matching index or None. Give an update rule, invariant and result on an empty list.

Read the worked reasoning

Keep last=None initially. After processing a prefix, last is the greatest matching index in that prefix, or None if none exists. Update last on every match and return it only after the scan. Empty input returns None. The algorithm uses constant auxiliary state and linear comparisons; no list of all matches is needed.

Check understanding

What is wrong with treating an O(n) bound as exactly n milliseconds?

  1. Only sorted inputs can use big O
    Feedback

    Sorted and unsorted algorithms can both be analyzed.

  2. It confuses an asymptotic bound with measured time
    Feedback

    Growth rates suppress units and implementation constants.

  3. Nothing; big O includes the computer speed
    Feedback

    Big O does not encode hardware or milliseconds.

Further reading

Study the original native lesson and expanded on-site reader. Work through the relevant mathematical and representation mechanisms, then the complete Cedar original/changed investigation and rubric. The retained workbook and weighted-path model provide further original practice. All practice and source references are optional; no external chapter or exercise is required.

My learning →