Skip to main content

Patterns and Analogies Port Solutions Across Domains

What This Concept Is

Pattern recognition is the observation of repeated structure across different instances. Analogy is the transfer of a known solution from a familiar domain to a new one based on shared structure.

Both rely on abstraction: ignoring surface details (names, numbers, specific objects) and keeping the structural skeleton (how the parts relate, how the problem grows, which operations appear).

When you say "this is just like..." you are using analogy. When you say "every third case behaves the same" you are using pattern. A pattern lives within a problem family; an analogy spans across them. Both are the mechanism by which a solved problem funds future problems instead of disappearing.

Patterns have an internal ladder:

  1. Numeric pattern -- the output sequence matches a known integer sequence.
  2. Structural pattern -- the recurrence, invariant, or decomposition matches.
  3. Categorical pattern -- the problem belongs to a named family (Fibonacci, pigeonhole, convex optimisation, monoid...).

Each level is more powerful and more transferable than the previous. A numeric pattern tells you the answer; a structural pattern tells you why; a categorical pattern tells you where else the same reasoning applies.

Distinguish pattern/analogy from two near-moves:

  • special cases (previous concept) generates the data; pattern recognition reads the data.
  • generalisation (concept 12) extends a specific solution to a broader claim; analogy ports a solution from one problem to another.

Why It Matters Here

Most problems you will face are variants of problems already solved. The cost of learning a new domain drops roughly in half for every good analogy you recognise. Engineers who "see patterns everywhere" are not more intelligent; they have a bigger internal library of structural skeletons.

In CS, pattern recognition underlies:

  • algorithmic paradigm identification (this is a graph problem, that is a DP)
  • data-structure selection (need order statistics $\to$ balanced BST; need nearest neighbour $\to$ kd-tree; need LRU $\to$ doubly linked list + hash map)
  • debugging (this bug pattern looks like an off-by-one; that one looks like a race)
  • architecture (this subsystem looks like a classic producer/consumer; that one is CQRS)
  • code smells (repeated structure across branches $\to$ refactor to a loop or a strategy pattern)

Forward pipeline: Semester 2 (named algorithmic paradigms), Semester 3 (design patterns, code smells, refactoring), Semester 5 (protocol patterns: leader election, consensus, pub/sub), Semester 7 (architectural patterns: layered, microkernel, event-driven). Every one of these is an institutionalised pattern library.

Concrete Examples

Example 1 -- climbing stairs $\Leftrightarrow$ Fibonacci

Problem: Count the number of ways to climb $n$ stairs if you can take 1 or 2 steps at a time.

Small cases: $f(1) = 1, f(2) = 2, f(3) = 3, f(4) = 5, f(5) = 8$.

Numeric pattern. $1, 2, 3, 5, 8, 13, \dots$ -- Fibonacci-shifted.

Structural pattern. $f(n) = f(n-1) + f(n-2)$: last step is 1 (arrived from $n-1$) or 2 (arrived from $n-2$).

Categorical pattern. "Fibonacci-style two-term recurrence". Analogous problems in the same family:

  • counting binary strings of length $n$ with no consecutive 1s
  • tilings of a $2 \times n$ board with $1 \times 2$ dominoes
  • number of paths in a two-state Markov chain where one state forbids self-loops

Recognising the Fibonacci skeleton gives you a closed form (Binet), a logarithmic-time algorithm (fast exponentiation on the $2 \times 2$ matrix $\begin{pmatrix} 1 & 1 \ 1 & 0 \end{pmatrix}$), and a library of extensions -- all for free.

Example 2 -- LRU cache $\Leftrightarrow$ ordered map with recency

Problem: Design a cache that evicts the least-recently-used item when capacity is exceeded.

Analogy scan. "Evict the oldest unused element" is the operation. What data structure supports "mark used" and "find oldest" in $O(1)$?

  • Queue supports find-oldest but not mark-used.
  • Hash map supports lookup but not ordering.
  • Combination: a doubly linked list for ordering + a hash map from key to node for $O(1)$ lookup.

The same combination is the answer to a string of other problems:

  • LFU cache (swap "oldest" for "least-frequently-used" with a frequency bucket)
  • deque with random-access deletion
  • insertion-order-preserving dict

Analogy: "ordering structure + lookup structure" is a reusable compound pattern. Recognising it once means you never re-derive it.

Common Confusion / Misconceptions

Surface vs structural similarity. "This problem has integers and so does that problem" is not useful. Real analogy is structural: same relationships, same growth rate, same constraint shape. The word integer is surface; the recurrence $f(n) = f(n-1) + f(n-2)$ is structural.

Premature pattern commitment. If you see a rule after 2 examples, it might be coincidence. Verify with a third and fourth example before committing. A classical trap: $f(n) = n^2 - n + 41$ is prime for $n = 0, 1, \dots, 40$ and composite at $n = 41$. Four data points are not enough.

Memorised vs understood patterns. If you can name the pattern ("this is Fibonacci") but cannot derive the result from it, you do not yet own the pattern. Recognition without derivation is mere vocabulary.

Analogy as substitute for verification. Even when the analogy is correct, the port of the solution needs to be verified on the new domain. Surface names match can be misleading -- the new problem may have an extra constraint the old one did not.

How To Use It

Pattern-hunting protocol:

  1. Compute 3-5 small cases by hand (previous concept does this).
  2. Tabulate results as a sequence or table.
  3. Look for differences, ratios, or recurrences between consecutive terms.
  4. Consult external catalogues (OEIS for integer sequences) or your own transfer-notes file.
  5. Form a hypothesis, test it on a new case, then prove it.

Analogy protocol:

  1. Strip surface features: replace names with roles (source, sink, input, state, resource, actor).
  2. Restate the problem abstractly.
  3. Ask: "which known problem has the same abstract shape?"
  4. Port the solution, adapting details, and list the differences.
  5. Verify on a concrete instance of the new problem before committing.
  6. Note the analogy in your transfer-notes file for future recognition.

Transfer / Where This Shows Up Later

  • Semester 2 (algorithmic paradigms). Every chapter has a name ("greedy", "DP", "flow") because the whole subject is patterned.
  • Semester 3 (design patterns). GoF patterns are canonicalised analogies: Observer, Strategy, Factory, Visitor.
  • Semester 3 (refactoring). Code smells are patterns ("long method", "feature envy") with named remedies.
  • Semester 5 (protocol design). Consensus, leader election, pub/sub, and gossip are canonical protocol patterns.
  • Semester 7 (architecture patterns). Layered, hexagonal, event-sourced architectures are the macro-level analogy library.
  • Semester 10 (capstone). Recognising "this subsystem is a classic X" saves weeks of rediscovery.

Check Yourself

  1. What is the difference between surface similarity and structural analogy? Give an example of each.
  2. How many small cases should you compute before committing to a pattern? Is four always enough?
  3. Give a problem where the right analogy was not obvious until the problem was stripped of its surface names.
  4. Why is it important to verify an analogical port on a concrete instance, even when the analogy feels clean?
  5. Name one numeric pattern, one structural pattern, and one categorical pattern for the sequence $1, 2, 4, 7, 11, 16, \dots$.

Mini Drill or Application

Drill A. Consider: How many ways can $n$ ordered items be split into two non-empty groups? Compute for $n = 2, 3, 4, 5$ by hand. Identify the numeric pattern. Identify a combinatorial analogy (hint: think of binary choices). Verify against $2^n - 2$ and explain why in two sentences.

Drill B. Given the sequence $1, 1, 2, 5, 14, 42, \dots$, identify the pattern and the categorical family (Catalan numbers). Name three problems in the Catalan family (balanced parentheses, binary trees, Dyck paths) and explain the structural isomorphism between any two of them in one paragraph.

Drill C (unseen). Problem: You have an $8 \times 8$ grid. Place as many kings as possible so that none attacks another. Try small grids ($2 \times 2$, $3 \times 3$, $4 \times 4$) and recognise the pattern. Port the structural argument to an $n \times n$ grid. State the closed form.

Read This Only If Stuck