Object cloning with cyclic reference chain

185 Views Asked by At

I'm running some operations on a graph in memory, which modify the graph itself. I need to repeat these operations some number of times, and subsequent repetitions must work on a fresh copy of the graph. Since the graph is generated by parsing a file, I don't really want to spend time re-parsing the file for every repetition, so I've gone down the route of cloning the original graph.

My node, edge and graph types are all implementing ICloneable, but I have an issue with a cyclic reference chain.

Node stores a list of directed Edges coming from it. Edge stores the weight and the Node it points to. When two nodes share a bi-directional edge, I get a StackOverflowException when I attempt to clone either node, since cloning the list of edges of either node invariably means cloning the other node.

What would be the best approach to resolve this cyclic reference for the purposes of cloning?

0

There are 0 best solutions below