## Appendix A Forward and Backward Traceability Algorithms  
This appendix provides detailed algorithms for forwards and backwards traceability within the proposed traceability system. These algorithms aim to ensure that all stages of the supply chain can be accurately tracked and verified, from raw materials to final products and vice versa. The forwards traceability algorithm focuses on identifying the subsequent stages that a given node influences, allowing us to follow the progression of materials and components through the supply chain. This algorithm is relevant for quality control, ensuring compliance with standards, and identifying the end products associated with specific batches of raw materials. Conversely, the backwards traceability algorithm is designed to trace the origins of a final product back to its initial raw materials. This capability is vital for verifying the integrity of the supply chain, identifying the sources of defects or contaminants, and ensuring that all components meet regulatory and safety standards. Together, these algorithms enable comprehensive traceability, ensuring transparency and accountability at every step of the supply chain. ```
1:\(\mathit{ToA}\)\(\triangleright\) Table of arcs, where each arc is a tuple \((id_{n},id_{c},d)\)
2:\(id_{t}\)\(\triangleright\) Target node representing a consumer product
3:\(P\)\(\triangleright\) List of arcs leading to \(id_{t}\) from leaf nodes
4:functionFindArcsToTarget(\(A,id_{t}\))
5:\(P\leftarrow[]\)\(\triangleright\) Initialise the list of paths
6:\(D\leftarrow[\,id_{n}:(id_{n},id_{c},d)\in A\,\,]\)\(\triangleright\) Set of all destination nodes
7:\(O\leftarrow[\,id_{c}:(id_{n},id_{c},d)\in A\,\,]\)\(\triangleright\) Set of all origin nodes
8:\(\mathit{LeafNodes}\gets O\setminus D\)\(\triangleright\) Identify leaf nodes
9:forall\(n\in\mathit{LeafNodes}\)do
10:\(\mathit{DFS}(n,id_{t},A,[],P)\)
11:endfor
12:return\(P\)
13:endfunction
14:function\(\mathit{DFS}(current,target,A,path,P)\)
15:if\(current=target\)then
16:\(P\gets P\cup\{path\}\)\(\triangleright\) Add current path to results
17:return
18:endif
19:forall\((id_{n},id_{c},d)\in A\) such that \(id_{c}=current\)do
20:\(\mathit{DFS}(id_{n},target,A,path\cup\{(id_{n},id_{c},d)\},P)\)
21:endfor
22:endfunction
```  
**Algorithm A1** Find all arcs leading to node \(id_{t}\) from leaf nodes in a ToA. ```
1:\(\mathit{ToA}\)\(\triangleright\) Table of arcs, where each arc is a tuple \((id_{n},id_{c},d)\)
2:\(id_{r}\)\(\triangleright\) Leaf node representing a batch of raw materials
3:\(P\)\(\triangleright\) List of distinct paths from \(id_{r}\) to any root node
4:functionFindPathsFromLeaf(\(A,id_{r}\))
5:\(P\leftarrow[]\)\(\triangleright\) Initialise the list of paths
6:\(D\leftarrow[\,n_{d}:(id_{n},id_{c},d)\in A\,\,]\)\(\triangleright\) Set of all destination nodes
7:\(O\leftarrow[\,id_{c}:(n_{d},id_{c},d)\in A\,\,]\)\(\triangleright\) Set of all origin nodes
8:\(RootNodes\gets D\setminus O\)\(\triangleright\) Identify root nodes
9:\(\mathit{DFS}(id_{r},A,[],P,RootNodes)\)
10:return\(P\)
11:endfunction
12:function\(\mathit{DFS}(current,A,path,P,RootNodes)\)
13:if\(current\in RootNodes\)then
14:\(P\gets P\cup\{path\}\)\(\triangleright\) Add current path to results
15:return
16:endif
17:forall\((n_{d},id_{c},d)\in A\) such that \(n_{d}=current\)do
18:\(\mathit{DFS}(id_{c},A,path\cup\{(n_{d},id_{c},d)\},P,RootNodes)\)
19:endfor
20:endfunction
```  
**Algorithm A2** Find all distinct paths from a given leaf node to any root node in a ToA.