Mapping keywords to pages in SEO Fundamentals - Time & Space Complexity
When mapping keywords to pages, it's important to understand how the effort grows as the number of keywords and pages increases.
We want to know how much work is needed to assign keywords to the right pages as the site grows.
Analyze the time complexity of the following keyword-to-page mapping process.
for each keyword in keywords:
for each page in pages:
if page is relevant to keyword:
assign keyword to page
This code checks every keyword against every page to find the best matches.
Look at what repeats in the code.
- Primary operation: Checking if a page is relevant to a keyword.
- How many times: For every keyword, it checks all pages.
As the number of keywords and pages grows, the number of checks grows quickly.
| Input Size (keywords x pages) | Approx. Operations |
|---|---|
| 10 keywords x 10 pages | 100 checks |
| 100 keywords x 100 pages | 10,000 checks |
| 1000 keywords x 1000 pages | 1,000,000 checks |
Pattern observation: The work grows by the product of the input sizes, meaning doubling keywords and pages quadruples the checks.
Time Complexity: O(n x m)
This means the time needed grows proportionally to the number of keywords times the number of pages.
[X] Wrong: "Checking keywords against pages only takes time proportional to the number of keywords or pages alone."
[OK] Correct: Because each keyword must be compared to every page, the total work depends on both numbers multiplied, not just one.
Understanding how work grows when matching keywords to pages helps you explain how to handle large websites efficiently.
What if we indexed pages by keyword categories first? How would that change the time complexity?