brain

tamnd's digital brain — notes, problems, research

41641 notes

TAOCP 2.2.3 Exercise 23

Algorithm $T$ performs a topological sort on a directed graph with vertices $1,2,\dots,n$, using the array $\text{COUNT}$ and the queue controlled by $F$ and $R$.

taocpmathematicsalgorithmsvolume-1hard
TAOCP 2.2.3 Exercise 21

Let Algorithm $T$ process an input consisting of relations of the form $j \prec k$.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.2.3 Exercise 22

Program $T$ becomes unsafe when an input value is used as a subscript without verification, since a negative or excessively large value of $k$ causes an access such as $X[k]$ or $\text{COUNT}[k]$ to r...

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.2.3 Exercise 24

The flaw in the previous solution is the introduction of an auxiliary walk that is not part of Program $T$’s state.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.2.3 Exercise 18

Algorithm $T$ in §2.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.2.3 Exercise 20

Algorithm $T$ maintains a collection of nodes whose $\text{COUNT}$ field has just become $0$ but whose successor arcs have not yet been fully processed.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.2.3 Exercise 19

In Algorithm `T`, step `T5` accesses the front element of the queue via the pointer `F` without changing the queue structure, while step `T7` performs the structural update that removes that front ele...

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.2.3 Exercise 14

Let $(S,\preceq)$ be a finite partially ordered set, and let a _topological sort_ mean a linear extension of $\preceq$, i.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 2.2.3 Exercise 15

Let $S$ be a finite partially ordered set with order relation $\preceq$.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 2.2.3 Exercise 16

Let $S = {x_1,\ldots,x_n}$ be a finite set with a partial ordering $\preceq$.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 2.2.3 Exercise 17

We restart the solution from first principles and explicitly execute Algorithm $T$ on the given input.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.2.3 Exercise 12

Let $S$ be a set of $n$ elements.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 2.2.3 Exercise 10

Define the relation $\preceq$ by x \preceq y \iff (x = y \ \text{or}\ x \subset y).

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 2.2.3 Exercise 11

Correcting the argument requires first fixing the fundamental mistake in the previous write-up: it is not valid to replace the given instance with a generic discussion.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.2.3 Exercise 13

The reviewer’s objections remove the entire structural collapse used in the previous solution.

taocpmathematicsalgorithmsvolume-1math-research
TAOCP 2.2.3 Exercise 7

Let $P$ be a pointer to the current node being processed, initially set to $FIRST$, let $Q$ be a pointer to the already reversed portion of the list, initially set to $\Lambda$, and let $R$ be an auxi...

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.2.3 Exercise 9

A relation $\preceq$ is a partial ordering on $S$ if it is reflexive, antisymmetric, and transitive.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.2.3 Exercise 8

The algorithm is correct (standard in-place reversal of a singly linked list).

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.2.3 Exercise 6

The flaw in the previous solution is that it still implicitly relies on evaluating $\text{LINK}(P)$ in the case where $P$ is the last node.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.2.3 Exercise 3

Use the standard representation employed in §2.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.2.3 Exercise 4

The previous solution fails at the only critical requirement of the exercise: it never actually changes the return address used by `JMP 0,3`, so it cannot produce return to $rJ - 2$.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.2.3 Exercise 5

Let the linked representation be the standard TAOCP structure with node fields $\mathrm{INFO}$ and $\mathrm{LINK}$, and with pointers $F$ and $R$ to the front and rear of the queue.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.2.2 Exercise 19

The stack part of the proposed solution is already correct under 0-origin indexing.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.2.3 Exercise 2

The correct solution must be rebuilt from the MIX calling convention of Section 1.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.2.3 Exercise 1

Operation (8) uses the allocation primitive $P \Leftarrow \text{AVAIL}$, which is defined in (6).

taocpmathematicsalgorithmsvolume-1simple
TAOCP 2.2.2 Exercise 16

Let the memory consist of locations $L_0 < L \le L_\infty$.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.2.2 Exercise 17

A correct proof must introduce a coupling invariant that controls how the two executions can differ.

taocpmathematicsalgorithmsvolume-1hard
TAOCP 2.2.2 Exercise 18

The error in the previous solution is exactly the attempt to control the additive $O(n)$ per repacking by bounding the number of repackings.

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 2.2.2 Exercise 14

For fixed $n$, the multinomial vector satisfies the multivariate central limit theorem.

taocpmathematicsalgorithmsvolume-1hm-project
TAOCP 2.2.2 Exercise 15

We restart from the definitions and construct a **valid Monte Carlo experiment** that faithfully simulates both Algorithm $G$ and the earlier **one-by-one shifting algorithm**, then describe how to es...

taocpmathematicsalgorithmsvolume-1project
TAOCP 2.2.2 Exercise 13

Let $S_n=k_1(n)+k_2(n)$ and $D_n=k_1(n)-k_2(n)$.

taocpmathematicsalgorithmsvolume-1hm-project
TAOCP 2.2.2 Exercise 12

Let $a_1,\ldots,a_m$ be independent choices with $\Pr{a_j=1}=\Pr{a_j=2}=1/2$.

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 2.2.2 Exercise 9

Let the model in the text generate a random sequence a_1,a_2,\dots,a_n, where each $a_i$ is chosen independently and uniformly from $\{1,2,\dots,m\}$.

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 2.2.2 Exercise 10

Let $m$ be the number of items $a_1, a_2, \ldots, a_m$, where each $a_j \in {1,2,\ldots,n}$.

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 2.2.2 Exercise 11

The mathematical model of Exercise 9 assigns a random sequence of stack operations and defines the total number of moves as a sum of contributions from individual insertions, where each insertion may...

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 2.2.2 Exercise 8

The key point of the exercise is that the _global memory management_ (Algorithm G with rules (9), (10), and repacking) is unchanged in spirit: it still allocates contiguous blocks to each list and rep...

taocpmathematicsalgorithmsvolume-1hard
TAOCP 2.2.2 Exercise 6

The previous solution failed because it replaced the actual state of the memory in Fig.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 2.2.2 Exercise 7

The variable `OLDTOP[j]` is defined as the value of `TOP[j]` immediately after the previous allocation of memory.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 2.2.2 Exercise 2

We extend the circular array representation $X[1],\ldots,X[M]$ used in (6a) and (7a), with pointers $F$ (front) and $R$ (rear), where the queue is empty exactly when $F = R$.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.2.2 Exercise 3

Let (8) denote the standard MIX sequence that performs table access via a relocatable base and indexing, of the form \texttt{LD1 I},\quad \texttt{LDA BASE},\quad \texttt{STA *+1},\quad \texttt{LDA *,1...

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.2.2 Exercise 5

Let MIX effective address modification be governed by an $I$-field taking values $0,1,\dots,7$, where $0,\dots,6$ are ordinary modifications and $7$ denotes indirect addressing.

taocpmathematicsalgorithmsvolume-1hard
TAOCP 2.2.2 Exercise 4

We adopt the MIX indirect addressing semantics from the extension in Exercise 3: - An effective address $a$ may be modified by index additions $rI_i$.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.2.1 Exercise 13

We correct the solution by fixing the Baxter characterization and by making the deque–Baxter correspondence explicit as a cited theorem rather than an informal claim.

taocpmathematicsalgorithmsvolume-1math-research
TAOCP 2.2.1 Exercise 12

Let $a_n$ denote the number of permutations of $12\ldots n$ obtainable by a stack, as in Exercise 4.

taocpmathematicsalgorithmsvolume-1hm-hard
TAOCP 2.2.1 Exercise 14

A queue is a linear list in which insertions occur at the rear and deletions occur at the front, using Knuth’s notation (4) and (5).

taocpmathematicsalgorithmsvolume-1hard
TAOCP 2.2.2 Exercise 1

The queue is stored in the circular array $X[1], \ldots, X[M]$ with pointers $F$ and $R$, initially $F = R = 1$.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 2.2.1 Exercise 9

Fix a convention.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 2.2.1 Exercise 11

Let $b_n$ denote the number of permutations of $1,2,\dots,n$ obtainable by an output-restricted deque, equivalently by an input-restricted deque, as established in earlier exercises.

taocpmathematicsalgorithmsvolume-1math-project
TAOCP 2.2.1 Exercise 8

The correct resolution is that **every permutation of $1,2,\ldots,n$ is obtainable** using an unrestricted deque, but the proof must explicitly justify why adding the largest element does not restrict...

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.2.1 Exercise 10

A sequence of operations on an output-restricted deque consists of symbols $S$, $Q$, and $X$, applied to the input stream $1,2,\dots,n$ in that order.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 2.2.1 Exercise 7

A deque supports insertions and deletions at its ends.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.2.1 Exercise 6

Every insertion into the queue places the new element at the rear, and every deletion removes the element at the front.

taocpmathematicsalgorithmsvolume-1
TAOCP 2.2.1 Exercise 5

We prove the equivalence carefully from first principles, correcting both directions.

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 2.2.1 Exercise 4

Let $a_n$ denote the number of permutations of ${1,2,\dots,n}$ obtainable by a stack operating as in Exercise 2, where each element is pushed once and popped once.

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 2.2.1 Exercise 1

An output-restricted deque permits insertions at both ends and deletions at only one fixed end.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 2.2.1 Exercise 3

Let a sequence of operations consist of symbols $S$ and $X$, where $S$ inserts the next input car into the stack and $X$ removes the top stack car to the output.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.2.1 Exercise 2

A legal sequence of operations consists of reading cars $1,2,3,4,5,6$ in order, each either being pushed onto the stack or popped to output when it becomes the next required output.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 2.1 Exercise 7

Let TOP denote the word containing the link field of the top node.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 2.1 Exercise 8

The previous solution does not implement any MIX program because it has no access to the content of steps $B1$–$B3$.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.1 Exercise 9

We first restate a clean and valid MIX representation and then give a corrected program that follows MIX conventions consistently.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.1 Exercise 4

Let $TOP$ denote the address of the first card in the pile, and let $NEXT(X)$ denote the link field of card $X$, with value $0$ meaning “no successor.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.1 Exercise 3

Let $TOP$ denote the address of the top card of the pile, with $TOP = 0$ representing an empty pile.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 2.1 Exercise 6

The operation $ \text{CARD} \leftarrow \text{NODE}(\text{TOP}) $ copies each field of the node at $\text{TOP}$ into the corresponding fields of the node-valued variable $\text{CARD}$.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 2.1 Exercise 5

Let the pile be represented as a linked structure of cards.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 2.1 Exercise 2

For any variable $V$, the expression $\mathrm{CONTENTS}(\mathrm{LOC}(V)) = V$ holds whenever $V$ denotes a storage cell in the sense of Section 2.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 2.1 Exercise 1

Let the structure in Figure (3) consist of four nodes linked by the field $NEXT$: TOP \rightarrow N_1 \rightarrow N_2 \rightarrow N_3 \rightarrow N_4 \rightarrow NIL and suppose the figure specifies t...

taocpmathematicsalgorithmsvolume-1simple
TAOCP 1.4.4 Exercise 16

The exercise asks for a formulation of the "green-yellow-red-purple" buffering scheme of Fig.

taocpmathematicsalgorithmsvolume-1hard
TAOCP 1.4.4 Exercise 15

Let the three buffers be `BUF1`, `BUF2`, and `BUF3`, each consisting of 100 words.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 1.4.4 Exercise 14

In the multiple-buffering scheme described in the text, the normal discipline is \cdots\ \text{ASSIGN}\ \cdots\ \text{RELEASE}\ \cdots\ \text{ASSIGN}\ \cdots\ \text{RELEASE}\ \cdots so that the comput...

taocpmathematicsalgorithmsvolume-1medium
TAOCP 1.4.4 Exercise 12

In the multiple buffering scheme, anticipated input is used: whenever a buffer is released for processing, the CONTROL coroutine immediately initiates reading of another card into a free buffer.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 1.4.4 Exercise 10

There are twelve buffer assignments in the computation sequence.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 1.4.4 Exercise 5

Let $n$ be the number of I/O devices referred to by the program, and let $T_i$ denote the time required to perform a complete unbuffered I/O operation on device $i$, for $1 \le i \le n$.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 1.4.4 Exercise 7

Subroutine (4) detects the end of a buffer by placing a sentinel in the 101st word of each buffer.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 1.4.4 Exercise 4

Let a program process $n$ blocks of data using a single I/O device.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 1.4.4 Exercise 1

(a) No.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 1.4.4 Exercise 2

The unbuffered method is \texttt{OUT 1000(6); JBUS *(6).

taocpmathematicsalgorithmsvolume-1simple
TAOCP 1.4.4 Exercise 3

The output analogue of (4) should keep one buffer available for writing by the program while the other buffer is being written onto tape.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 1.4.3.2 Exercise 5

Let $T_A$ and $T_B$ denote two physically distinct copies of the trace routine in Section 1.

taocpmathematicsalgorithmsvolume-1hard
TAOCP 1.4.3.2 Exercise 6

The original solution correctly removed self-exclusion, but it failed to address the essential point: MIX tracing does not inherently create recursive invocations of the tracer.

taocpmathematicsalgorithmsvolume-1project
TAOCP 1.4.3.2 Exercise 3

Writing trace output directly to a printer or similar output device forces the trace routine to compete with the traced program for that device.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 1.4.3.2 Exercise 4

The previous solution fails because it assumes a clean separation between “program being traced” and “tracing mechanism.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 1.4.3.2 Exercise 7

The correct construction is event-based: one output is produced exactly when a jump instruction is executed, and the output records that instruction’s location and its destination.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 1.4.3.1 Exercise 8

We are asked to determine the truth of the statement: _Whenever line 010 of the simulator program is executed, we have $0 \le rI6 < \text{BEGIN}$.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 1.4.3.2 Exercise 2

At each cycle the trace routine reaches location `INST` (line 34) immediately before the simulated execution of the external instruction.

taocpmathematicsalgorithmsvolume-1hard
TAOCP 1.4.3.2 Exercise 1

The critical issue is the incorrect assumption that control can be returned after restoring $rJ$ using `JMP *`.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 1.4.3.1 Exercise 5

The simulator assigns execution time $2$ to the instruction `LDA`, as shown by the entry \texttt{LDA \ \ LOAD(2)} in `OPTABLE`.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 1.4.3.1 Exercise 6

The simulator maintains the state of the simulated MIX machine, including the registers `AREG`, `I1REG`, …, `I6REG`, `XREG`, `JREG`, and the special registers `CLOCK`, `OVTOG`, and `COMPI`, together w...

taocpmathematicsalgorithmsvolume-1hard
TAOCP 1.4.3.1 Exercise 2

The operation code 6 corresponds to the shift group handled by the single routine `SHIFT`, entered from the switching table `OPTABLE` with execution time $2$.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 1.4.3.1 Exercise 4

In the given program the label `BEGIN` performs the initialization that, in the actual MIX machine, is performed by pushing the GO button.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 1.4.2 Exercise 5

In the original linkage (4), only one register-save location is introduced, and only one direction of communication is protected: the contents of register $A$ are saved when control passes from one co...

taocpmathematicsalgorithmsvolume-1simple
TAOCP 1.4.2 Exercise 6

The error in the previous solution is the treatment of the resume labels.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 1.4.2 Exercise 4

Consider a conventional stored-program computer with a program counter $\mathrm{PC}$ and memory cells that can hold addresses.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 1.4.2 Exercise 7

The previous solution fails primarily because it is not a valid MIX program: it uses non-existent instructions, inconsistent state handling, and an incoherent coroutine structure.

taocpmathematicsalgorithmsvolume-1hard
TAOCP 1.4.2 Exercise 3

We address the reviewer’s objections by rebuilding the argument from the actual structural role of the three occurrences of `CMPA PERIOD` inside `OUT`, without assuming anything global beyond what is...

taocpmathematicsalgorithmsvolume-1medium
TAOCP 1.4.2 Exercise 1

Short examples of coroutines tend to collapse either into ordinary sequential programs or into degenerate cases where the coroutine mechanism is not exercised.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 1.4.1 Exercise 7

Self-modifying code is discouraged because modern computer architectures and software systems separate the treatment of instructions and data, and this separation is essential for correctness, perform...

taocpmathematicsalgorithmsvolume-1medium
TAOCP 1.4.2 Exercise 2

The proposed failure analysis is incorrect because it assumes a missing or premature dependency in the initialization of `INX`.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 1.4.1 Exercise 6

The original attempt fails because it tries to update memory-resident variables with `INCX`/`DECX`, which in MIX affect only register $X$.

taocpmathematicsalgorithmsvolume-1hard
TAOCP 1.4.1 Exercise 3

Let the call `JMP MAX100` occur at location $L$.

taocpmathematicsalgorithmsvolume-1math-simple
TAOCP 1.4.1 Exercise 5

In MIX without a J-register, subroutine linkage must be achieved by explicitly storing the return address in a general register or memory cell before transferring control to the subroutine, and then r...

taocpmathematicsalgorithmsvolume-1medium