I had just started doing a fun project about the classical DC circuit experiments. I have designed the UI part and the back-end for series circuit. Actually what happens is, user can spawn as many (up to infinite) set of resistors, bulbs, voltage source (which is 1 for now), ammeter and voltmeter, and connect them in any manner (permutation) they want. So in back-end I am storing these connections as edges of a graph where voltage sources, resistors, bulbs, ammeter and voltmeter are the nodes. Now, I am traversing the circuit as DFS traversal as soon as user taps "on" the voltage source. So being a connected graph in series I can able to easily sum up all the resistors and calculate the electric current flow throughout the circuit and display it on the ammeter. But the problem is, if user connects the circuit in parallel then how to solve this problem.
I need some advice/suggestions from the stack community to help me solve the problem.
Change the representation of your circuit. Connections should be nodes, components should be edges labelled with their resistance.
First let's consider a circuit with a single current/voltage source.
Create a new set of nodes by merging together the nodes connected through a 0 ohm edge.
Apply Millman's theorem to each node to get a set of linear equations whose solution will give you the potential of all nodes.
If the source is a current source, there is no difficulty. If the source is a voltage source, you may have to fix the potential of the two nodes to which it is connected. When solving the equations you may then find that the ground node is not at the 0 potential. This is ok, just substract this potential to all the nodes to get the correct solution relative to your ground.
If your circuit has got more than one source, use the superposition theorem to compute the final solution from the solutions with each source enabled in turn.