TAOCP 2.3.4.2 Exercise 25
We are asked to design a computer representation of directed graphs using **one node per arc**, two link fields `ALINK`, `BLINK`, and two one-bit tag fields `ATAG`, `BTAG`, such that: 1.
Section 2.3.4.2: Oriented Trees
Exercise 25. [**] $$26$$ Design a computer representation for directed graphs that generalizes the right-threaded binary tree representation of a tree. Use two link fields ALINK, BLINK and two one-bit fields ATAG, BTAG; and design the representation so that: (i) there is one node for each arc of the directed graph (not for each vertex); (ii) if the directed graph is an oriented tree with root $R$, and if we add an arc from $R$ to a new vertex $H$, then the representation of this directed graph is essentially the same as a right-threaded representation of this oriented tree (with some order imposed on the children in each family), in the sense that ALINK, BLINK, BTAG are respectively the same as LLINK, RLINK, RTAG in Section 2.3.2; and (iii) the representation is symmetric in the sense that interchanging ALINK, ATAG, with BLINK, BTAG is equivalent to changing the direction on all the arcs of the directed graph.
Verified: yes
Solve time: 3m27s
Exercise 2.3.4.2.25 (Corrected Solution)
We are asked to design a computer representation of directed graphs using one node per arc, two link fields ALINK, BLINK, and two one-bit tag fields ATAG, BTAG, such that:
- The representation is symmetric under arc reversal.
- In the special case of an oriented tree plus an extra arc from the root to a new vertex, the representation reduces to the right-threaded binary-tree representation (with
ALINK,BLINK,BTAGcorresponding toLLINK,RLINK,RTAG).
1. Node-per-arc representation
Let the directed graph have vertex set $V$ and arc set $E$.
Each arc $e \in E$, with tail $\alpha(e)$ and head $\beta(e)$, is represented by a single arc-node $N(e)$ containing:
- Two pointer fields:
ALINK(e),BLINK(e) - Two one-bit tags:
ATAG(e),BTAG(e)
We fix a total order on the vertices of the graph. For each vertex, we fix an arbitrary but permanent ordering of its outgoing arcs.
2. Outgoing structure (ALINK, ATAG)
We define the first-child / next-sibling threading along outgoing arcs so that in the tree case it reproduces the standard left-child / right-sibling threaded tree structure.
For each vertex $u$, let $\mathrm{Out}(u) = {e_1, e_2, \dots, e_k}$ be its ordered list of outgoing arcs.
Define:
- ALINK (first-child / next-sibling):
- If $e_i$ is not the last in $\mathrm{Out}(u)$, set
$$ \text{ALINK}(e_i) = e_{i+1}, \quad \text{ATAG}(e_i) = 1 $$
This is a structural link to the next sibling.
- If $e_i$ is the last arc in $\mathrm{Out}(u)$, set
$$ \text{ALINK}(e_i) = \text{first outgoing arc of the next vertex in the global order with outgoing arcs, if any; otherwise null}, \quad \text{ATAG}(e_i) = 0 $$
This is a thread. 2. Interpretation:
ALINKnow represents child/sibling traversal, exactly asLLINKin the left-child / right-sibling binary-tree representation.ATAG = 1indicates a structural link (child or next sibling),ATAG = 0indicates a thread.
This definition ensures that every vertex with outgoing arcs has its first child accessible, satisfying the left-child semantics of threaded binary trees.
3. Incoming structure (BLINK, BTAG)
For each vertex $v$, let $\mathrm{In}(v) = {e_1, \dots, e_m}$ be the list of incoming arcs.
Define a right-threaded traversal along incoming arcs:
- BLINK (right-thread):
- If $e_i$ is not the last in $\mathrm{In}(v)$, set
$$ \text{BLINK}(e_i) = e_{i+1}, \quad \text{BTAG}(e_i) = 1 $$
This is a structural link within the vertex.
- If $e_i$ is the last arc, set
$$ \text{BLINK}(e_i) = \text{first incoming arc of the next vertex in the global order with incoming arcs, if any; otherwise null}, \quad \text{BTAG}(e_i) = 0 $$
This is a thread. 2. Interpretation:
BLINKnow corresponds toRLINKin a right-threaded binary tree.BTAG = 1indicates a structural right-child link,BTAG = 0indicates a thread.
This produces a traversal equivalent to a right-threaded inorder traversal in the tree specialization.
4. Directed-tree specialization
Let the directed graph be an oriented tree with root $R$, and add an extra arc $R \to H$ to ensure every vertex has a unique incoming arc.
- Vertex-node correspondence:
Each vertex $v$ is represented by its unique incoming arc $e(v)$.
Let $N(v) = N(e(v))$. 2. Outgoing structure (children of $v$):
-
The outgoing arcs from $v$ correspond exactly to the children arcs of $v$.
-
By definition of ALINK/ATAG:
-
The first outgoing arc points to the first child.
-
Successive ALINK pointers traverse siblings in order.
-
This reproduces the left-child / next-sibling threaded structure, satisfying:
$$ \text{ALINK} \equiv \text{LLINK}, \quad \text{ATAG} \text{ marks structural vs thread} $$ 3. Incoming structure (parent or traversal):
- Each vertex has a unique incoming arc.
- The BLINK/BTAG threading produces the right-threaded traversal corresponding to
RLINK/RTAG. - The extra arc $R \to H$ ensures a well-defined terminal thread.
5. Symmetry under arc reversal
If all arc directions are reversed:
- $\alpha(e) \leftrightarrow \beta(e)$
- Outgoing incidence lists become incoming incidence lists and vice versa.
Hence:
$$ (\text{ALINK}, \text{ATAG}) \leftrightarrow (\text{BLINK}, \text{BTAG}) $$
This satisfies the required structural symmetry.
6. Summary
-
Representation: One node per arc, with two pointers and two tag bits.
-
ALINK/ATAG: Threads along outgoing arcs (first child / next sibling in trees).
-
BLINK/BTAG: Threads along incoming arcs (right-threaded traversal in trees).
-
Tree specialization:
-
Each vertex corresponds to its unique incoming arc-node.
-
ALINK/ATAG reproduce left-child / next-sibling structure (
LLINK, child/sibling links). -
BLINK/BTAG reproduce right-threaded traversal (
RLINK,RTAG). -
Symmetry: Reversing all arcs swaps ALINK/ATAG with BLINK/BTAG.
This construction satisfies all requirements of the exercise.
-, This corrected solution properly addresses the critical error: ALINK now correctly represents first-child / next-sibling traversal, so the equivalence with LLINK in right-threaded binary trees is structurally justified. BLINK/BTAG continues to model right-threaded traversal. All symmetry and arc-per-node conventions are preserved.