**Algorithm 1** *DETERMINE*\_*RULE*(*rowj*, *pattern*) Returns the precedence rule for row *j* in pattern


**Figure 15.** Yarn order zoom-in at location (6, 5) in the yarn topology graph of Figure 11: (**a**) Simulated knitted pattern. (**b**) Corresponding zoom-in topology graph.

#### *5.2. Contact Neighborhood Order*

As noted in Section 3, a loop in TopoKnit is defined by a list of edges that adhere to certain constraints. An edge defines a yarn connection between two CNs. Thus, the order of loops at a location (*i*, *j*) mirrors the order of head CNs of the loops present at that location. Therefore, our algorithm (Algorithm 2) for determining loop order finds the order of CNs at a given location in the fabric grid. The algorithm begins by finding all of the CNs that end up at the location (if any) (Line 2, Algorithm 2). This is accomplished with the CNS\_AT algorithm, which is a slight modification of the ACNS\_AT algorithm (Algorithm 5 in [1]). Specifically, the CNS\_AT algorithm returns all CNs at a location by omitting the condition that the CN's actualization value be "ACN".


Each CN is created when a loop is formed by the execution of a stitch instruction. Since the order of CNs depends on the stitches that create them, each CN is paired with the corresponding stitch that formed it. For instance, CNs (4, 3) and (5, 3) in Figure 11c were formed by the Back Tuck (BT) stitch in the pattern shown in Figure 11a. The pairings between the CNs and the stitch are specified with the helper function CN\_STITCH\_PAIRS (See Algorithm 3). When a stitch instruction at coordinate m, n) in the stitch pattern is executed, four CN cells are populated in the TopoKnit data structure. Two correspond to two leg CNs ((2m, n), (2m + 1, n)) and two correspond to two head CNs ((2m, n + 1), (2m + 1, n + 1)). See Figure 16. Since only head CNs can be moved in the fabric grid by a stitch, they are considered when determining corresponding stitch instructions. Therefore a head CN located at (*i*, *j*) in the CN grid can be associated with a stitch command located at (*i*/2, *j* − 1), if *i* is even, and at ((*i* − 1)/2, *j* − 1), if *i* is odd. Note that *CNStitchPairs* is a dictionary where the CN (i,j) IDs are the keys and the corresponding stitch codes are the values.


**Figure 16.** Single open loop consisting of four contact neighborhoods. Leg edges highlighted in blue and head edges are highlighted in teal.

CNs whose final location is (*i*, *j*) may be created by stitch instructions within a small neighborhood of the location, possibly from a different row. Since knitting is a sequential process, stitch instructions in lower rows are processed before the ones in higher rows. Therefore, the entries in *CNStitchPairs* are sorted by their row number, with the algorithm SORT\_BY\_J (not included here). Stitch pairs from lower rows come before stitches from higher rows and are stored in *sortedCNStitchPairs*.

Once the CN-Stitch pairs have been created and sorted by their row *j* (Lines 6 and 7), Algorithm 2 determines the CN order at location (*i*, *j*) by calling the function YARN\_ORDER\_RECURSIVE (Algorithm 4), which recursively processes CN-Stitch pairs at location (*i*, *j*) row-by-row, starting with the lowest row. For each row of stitch instructions, which is stored in *currentRow*, *DETERMINE*\_*RULE* returns the appropriate precedence rule for the current row (Line 4, Algorithm 4), as described in Section 5.1. The function ORDER\_ROW\_CNS (Algorithm 5) is called to order the CNs in *currentRow* using the precedence rule in *rule* (Line 13 and 15, Algorithm 4). ORDER\_ROW\_CNS sorts the CNs based on the index of its corresponding stitch in the precedence rule.


**Algorithm 4** *YARN*\_*ORDER*\_*RECURSIVE*(*sortedCNStitchPairs*, *pattern*, *orderedCNs*)


Tuck stitches (Front (FT) and Back (BT)) not only have a precedence relative to the other stitches in their row, but they also affect the precedence of the stitch block that has been ordered in the previous row. As seen in Lines 9 and 10 in Algorithm 1, Front Tuck (FT) has the highest precedence of all the supported stitches, while Back Tuck (BT) has the lowest, seen in Lines 11 and 12. Conversely, when concatenating a new line of stitches (those returned by ORDER\_ROW\_CNS) containing a Back Tuck (BT) with a previous line (*orderedCNs*), as seen in Lines 12 and 13 of Algorithm 4, the block containing the BT has higher precedence than the stitch block from a lower row. The ordered *currentRow* is placed before the stitch block from the previous row (*orderedCNs*). If no BT is present in the current row, the previous stitch block (*orderedCNs*) is placed in front of the current row (Line 15, Algorithm 4).
