I have a c# unity game (using quikGraph, but I don't believe it fits my needs here). I'm adding wires which connect power sources to power destinations. All devices and wires will be nodes in a graph. I'm looking for a graph structure / accompanying algorithm to efficiently maintain incrementally updated components
Operations which should be quick:
- When an edge is added, any merged components ('circuits') should be updated
- When an edge is removed, any separated components ('circuits') should be updated
- When components are 'updated,' I'll want to operate on each components, then each member of the component
- I.e. Whenever components merge or separate within the graph, I need to take each affected component, compute the new wattage and update individual devices accordingly
- As such, maintaining a distinct object/enumerable collection for graph components is strongly preferred
I've considered using quikGraph's IncrementalConnectedComponentsAlgorithm, but it does not support removing edges and the data structure it returns doesn't allow me to easily single out components which need updates (instead, just giving me a mapping of nodes to arbitrary circuits)
Additional info: This is all simple undirected edges, wires are all the same, 'wattage' is always the same for circuits (not trying to do a realistic simulation of electricity here)
First idea I had:
- Maintain
HashSet<Circuit>
where 'Circuit' is aHashSet<Node>
and have everyNode
point to itsCircuit
- On edge add, if the nodes have different circuits, make them the same, iterate over the smaller circuit adding all of them to the new one
- On edge remove, bidirectional bfs to rebuild each circuit, (terminate early if frontiers touch?)
- THIS IS THE PART that I'm worried about being inefficient