I figured out how to avoid this issue.
I was defining both the nodes and the edges within the subgraphs, so I think it was getting confused when edges included nodes that should not be contained within the subgraph.
Instead, I just only define the nodes (all the nodes) within the subgraphs, and define the edges afterwards all together at the end of the file. Works like a champ :)