TAOCP Vol 1: Fundamental Algorithms · Week 51
TAOCP 2.2.3 Linked Allocation
Updated
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.
See it
All visualsStep through at your own pace; the arrow keys work too.
- P ⇐ AVAIL
- INFO(P) ← Y
- LINK(P) ← T
- T ← P
- (pop)
- P ← T
- T ← LINK(P)
- Y ← INFO(P)
- AVAIL ⇐ P
Y none
LINK(P) ← T: the new node points to the old top, B.
This week's steps
3 stepsRead §2.2.3 Linked Allocation
This is a long and very rewarding section.
Start with Ex 1, Ex 7, Ex 2; log what you solve below.
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 2024▶ Recommended
Swap the queue inside topological sort for a stack and compare the results.
Exercise 2327▶ Recommended
Make topological sort print an actual cycle when it gets stuck. The stretch, and a feature every build tool needs.
Your log
Sign in to log the exercises you work: number, rating, how far you got.
Your notes
Only youSign 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 postsQuestions 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```spoilerblock.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```spoilerblock.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```spoilerblock.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```spoilerblock.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```spoilerblock.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```spoilerblock.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```spoilerblock.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```spoilerblock.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.