Chapter 18 of 22 Part IV: Information Processing

Finding a Needle in a Digital Universe

Each project below is tailored to a specific algorithm and scientific question from Chapter 18, with concrete inputs, verifiable numerical outputs, boundary cases, and links to the related lab and pioneer.

Project 1 of 3 · v1-c18-p01 Foundational · ~20 min

Linear vs. Binary Search Complexity Benchmark

Lab activity: 1. Find a record fast · Related code: block-01.js, block-04.js · Pioneer: View profile

Real-World Problem: Searching 10 million sorted IDs one by one is slow, while halving the interval finds any ID in 24 steps.
Scientific Question: How does the comparison count of binary search grow when array size increases 1,000×?

Objective: Implement linear and binary search, count exact comparisons across sizes 16 to 65,536, and verify O(log2 n).

Hypothesis to Test: Doubling array size adds at most 1 extra comparison to binary search, whereas it doubles worst-case linear search.

Step-by-Step Implementation

  1. Write linearSearch(arr, target) and binarySearch(arr, target) returning both index and comparison count.
  2. Run both on sorted arrays of size 16, 256, 1024, and 65536.
  3. Verify binary search fails on an unsorted array (precondition violation).
  4. Compare worst-case comparisons against Math.ceil(Math.log2(n)).
Concrete Input: Sorted arrays of size 16, 1024, and 65536; search for worst-case missing key.
Expected Output: N=1024: Linear=1024 vs. Binary=10; N=65536: Linear=65536 vs. Binary=16.

Test Cases: Found first/middle/last element -> exact index; Missing element -> -1 in <= ceil(log2(n))+1 steps.

Boundary Condition: Empty array [] must return -1 in 0 comparisons without infinite loop.

Common Mistake to Avoid: Using (lo + hi) / 2 without Math.floor, or setting hi = mid instead of mid - 1 causing infinite loops.

Interpreting Results: Logarithmic algorithms scale effortlessly from classroom lists to planetary databases.

Show Hint, Extension Challenge & Reflection

Hint: Review the worked example and equation in Chapter 18 before testing the boundary case: Empty array [] must return -1 in 0 comparisons without infinite loop.

Extension Challenge: Implement lowerBound() to find the first occurrence when duplicate keys exist.

Reflection Question: Why is sorting data once worth the upfront cost if you will search it thousands of times?

Project 2 of 3 · v1-c18-p02 Intermediate · ~25 min

Mini Inverted-Index Search Engine

Lab activity: 2. Search a tiny index · Related code: block-02.js · Pioneer: View profile

Real-World Problem: Full-text web search cannot scan every word of every webpage for each user query.
Scientific Question: How does an inverted index answer multi-word AND queries in milliseconds?

Objective: Tokenize a miniature document collection, build an inverted index Map, and intersect posting lists for multi-word queries.

Hypothesis to Test: Looking up each query term in a hash map and intersecting posting sets visits only matching documents.

Step-by-Step Implementation

  1. Normalize text to lowercase, strip punctuation, and split into tokens.
  2. Build Map<word, Set<docId>> mapping each unique word to documents containing it.
  3. For a multi-word query, intersect the posting sets of all query words.
  4. Rank results by term frequency (TF) inside matching documents.
Concrete Input: 6 short science articles; query "binary parity" (AND) vs. "quantum" (missing).
Expected Output: "binary parity" -> [Doc 1, Doc 5]; "quantum" -> [] (0 matches).

Test Cases: Single word -> matching IDs; Two words -> exact intersection; Stop/missing word -> empty array.

Boundary Condition: Empty query string should return [] cleanly rather than throwing.

Common Mistake to Avoid: Forgetting to lowercase both indexed tokens AND query tokens.

Interpreting Results: Inverted indexes (like Pagefind powering this very website!) make instant static search possible.

Show Hint, Extension Challenge & Reflection

Hint: Review the worked example and equation in Chapter 18 before testing the boundary case: Empty query string should return [] cleanly rather than throwing.

Extension Challenge: Add an OR query mode and snippet highlighting using safe DOM textContent (no innerHTML).

Reflection Question: How did Alan Emtage's Archie (1990) pioneer automated indexing before the modern web?

Project 3 of 3 · v1-c18-p03 Applied · ~30 min

PageRank Power-Iteration with Dangling Nodes

Lab activity: 3. Rank a network · Related code: block-03.js, block-04.js · Pioneer: View profile

Real-World Problem: Matching keywords is not enough when thousands of pages contain the same words; search needs authority ranking.
Scientific Question: Why does PageRank require both a damping factor d and dangling-node redistribution to conserve total probability 1.0?

Objective: Iterate PageRank on a 5-node directed graph containing a dangling node (out-degree 0) and verify probability conservation.

Hypothesis to Test: Redistributing dangling-node rank evenly keeps Σ PR(u) = 1.000000 across every iteration until convergence.

Step-by-Step Implementation

  1. Initialize every node's rank to 1/N.
  2. In each iteration, accumulate dangling-node rank and distribute d * PR(v) / outDegree(v) across outgoing edges.
  3. Add teleport baseline (1 − d)/N + d * danglingSum / N to every node.
  4. Verify Σ PR(u) === 1.0 within 1e-10 and measure max delta until < 1e-6.
Concrete Input: Graph: A->[B,C], B->[C], C->[A], D->[] (dangling), damping d = 0.85, 25 iterations.
Expected Output: Ranks converge with C highest (most incoming links) and total sum = 1.000000.

Test Cases: Sum of ranks always 1.0; Node with more high-rank in-links scores higher than isolated node.

Boundary Condition: Self-loops and parallel edges should be normalized consistently.

Common Mistake to Avoid: Dropping dangling-node mass, which causes total PageRank to decay toward 0.

Interpreting Results: Larry Page, Sergey Brin, and Jon Kleinberg showed that graph structure carries rich collective signal.

Show Hint, Extension Challenge & Reflection

Hint: Review the worked example and equation in Chapter 18 before testing the boundary case: Self-loops and parallel edges should be normalized consistently.

Extension Challenge: Add a spam link farm pointing to Node D and test how damping limits artificial rank inflation.

Reflection Question: Why do modern search engines combine hundreds of signals rather than relying solely on PageRank?