Proofs: Forward star
These are the human proofs of Forward star, at full length.
BUILD makes a valid forward and reverse star and the queries list the arcs of each vertex full
This proof shows: correctness, termination.
Let be the input list of arcs with ; repeats are allowed. Write and for the number of arcs of with tail and with head . The invariant (F) is (F1), arcs sorted by tail with marking the blocks, and (F2), split by into increasing segments of the names of the arcs into each vertex.
Lemma 1 (prefix counts). If is the number of keys less than for , then , is the number of keys, is non-decreasing, and is the number of keys equal to .
Proof. Direct from the definition, since each key is in .
Lemma 2 (line L1). After line L1, the arcs are a permutation of , named , with non-decreasing tails, and arcs with the same tail are in their input order.
Proof. Line L1 is a counting sort by tail. Let be the number of arcs with tail less than . The arcs with tail are written, in input order, into the slots , since the counter of tail starts at and is increased after each write. By Lemma 1 these slots form the block of length , and the blocks of different tails are disjoint and cover . So each slot is written once, the result is a permutation of , tails increase from block to block, and each block keeps the input order. The loop has steps, so it stops.
By Lemma 2 and Lemma 1 applied to the tails, line L2 sets , the start of the block of tail , and the arcs out of are the names in . So (F1) holds.
Lemma 3 (lines L3 to L6). After line L6, (F2) holds.
Proof. By Lemma 1 applied to the heads, line L3 gives with , , non-decreasing, and segments of lengths . We claim that before step of the loop of line L5, for each , , where is the number of names below with head , and slots hold these names in increasing order. Line L4 makes this true for . Step writes , which is larger than all names written before, into slot for , and increases , which keeps the claim for . Since , each write stays in the segment of . After step , for each , so each segment is full, holds exactly the names of the arcs into , each once, in increasing order. The loop has steps, so it stops.
Theorem. After BUILD, (F) holds; OUT-ARCS outputs exactly the names of the arcs out of , each once; IN-ARCS outputs exactly the names of the arcs into , each once; and OUT-DEGREE and IN-DEGREE return and . Each query stops.
Proof. (F) holds by Lemma 2 with line L2, and Lemma 3. Line L7 outputs the names in , which by (F1) are the arcs out of . Line L8 outputs the segment of of , which by (F2) lists the arcs into , each once. Lines L9 and L10 return the lengths of the block and of the segment, which by Lemma 1 are and . Each query runs over a finite range. Since both stars return names and the data of an arc is stored once, at its name, a write to the data of arc is read back the same way through either star.