TAOCP Vol 1: Fundamental Algorithms · Week 51

TAOCP 2.2.3 Linked Allocation

Updated

About 9 hours30 exercises, rated 10 to M48

Summary

TAOCP 2.2.3 Linked Allocation: Linked lists, the available-space (free) list, and how to implement stacks and queues with links. Budget about 9 hours; do exercises 1, 7 and 2.

The highlight is topological sorting: putting items in an order consistent with a set of "must come before" relations.

Flashcards 16 cards for Anki and AnkiMobile · all decks

Anki deck.tsv

Step through at your own pace; the arrow keys work too.

TAOCP 2.2.3 Linked stacks and queues Knuth's pointer assignments one at a time: push and pop on a stack, insert and delete on a queue.
  1. P ⇐ AVAIL
  2. INFO(P) ← Y
  3. LINK(P) ← T
  4. T ← P
  5. (pop)
  6. P ← T
  7. T ← LINK(P)
  8. Y ← INFO(P)
  9. AVAIL ⇐ P

Y none

LINK(P) ← T: the new node points to the old top, B.

This week's steps

3 steps
  1. Read §2.2.3 Linked Allocation

    This is a long and very rewarding section.

  2. Work the exercises (8 picked)

    Start with Ex 1, Ex 7, Ex 2; log what you solve below.

  3. Write down one thing you'd explain to someone

    A few sentences in your own words in Your notes: the idea, why it works, an example. 30 words ticks this.

Sign in to track this: tick each step off and it counts toward your week.

Exercises

This section has 30 exercises, rated 10 to M48, and Knuth marks six of them with the triangle for recommended. Big, practical set. Topological sort exercises (11, 17 to 24) are the highlight. 13 and 25 are rated in the high 40s; leave them.

Worth your time

  • Exercise 110▶ Recommended

    Why the linked push never mentions overflow in the text's notation.

    Discuss

  • Exercise 222

    Write a general MIX insertion subroutine with the book's calling conventions.

    Discuss

  • Exercise 723

    Reverse a singly linked list in place. The classic.

    Discuss

  • Exercise 1124▶ Recommended

    List every valid topological order for a small example.

    Discuss

  • Exercise 1721▶ Recommended

    Run the topological sort by hand on the text's input.

    Discuss

  • Exercise 2024▶ Recommended

    Swap the queue inside topological sort for a stack and compare the results.

    Discuss

  • Exercise 2327▶ Recommended

    Make topological sort print an actual cycle when it gets stuck. The stretch, and a feature every build tool needs.

    Discuss

  • Exercise 2921▶ Recommended

    Return a whole list to the free list as fast as possible.

    Discuss

Your log

Sign in to log the exercises you work: number, rating, how far you got.

Your notes

Only you

Sign in to keep private notes on this guide, in Markdown. They stay yours: never on your profile, and you can export them all.

Discussion

0 posts

Questions about the reading go here; each exercise has its own thread below. Hints over answers.

No posts yet. Ask a question or share a tip.

Sign in to postAnyone can read. Posting needs an account with a confirmed email.

Exercise threads

One thread per exercise. Open one to ask for a nudge or check your thinking.

  • Exercise 110No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 222No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 723No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 1124No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 1721No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 2024No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 2327No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 2921No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

Reference

Why it matters

Topological sort runs your build system, package manager and task scheduler. Free lists are the simplest memory allocator and still used in game engines and kernels.

Before you start
  • 2.2.2 Sequential Allocation
  • C pointers (clox)
How to approach it

This is a long and very rewarding section. Implement the free-list allocator in C. Then implement Knuth's topological sort algorithm from the text, run it on a small dependency graph from a real project, and check the output order.

Common traps
  • Losing nodes when you forget to return them to the free list.
  • Topological sort with a cycle in the input: decide what your code should do.

Spot a mistake? Report it

Back to week 51