Practical Application of Finding All Paths between Two Points in a Graph

Unable to sleep late at night, I began to recall what I had done before. Coincidentally, Zhihu brushed the discussion of depth first and breadth first, and I remembered a good application case in the previous project.

demand

The analog electromechanical laboratory provides a certain number of power supplies, knife switches, motor starters, sliding rheostats, resistors, voltmeters, ammeters, electrical loads and other components. The components have terminals, and each two terminals are connected freely. The user enters the virtual laboratory, connects the required components according to the circuit diagram, and submits after the connection.

After submission, the system will judge the connection status. If it is incorrect, an error will be reported, and the error will display short circuit, open circuit, redundant components, etc. The error message is based on the loop, but should be accurate to a single component. Be flexible in judgment, for example, the knife switch is connected in front of and behind a component, sometimes equivalent, sometimes not equivalent. Or short-circuit a component not connected to the circuit, which will not affect the experiment.

After everything is correct, enter the simulation experiment step. During the experiment, adjust the sliding rheostat, voltage, switch and other interactive components, and the circuit components need to respond (voltmeter, ammeter needle changes, load state changes, etc.).

analysis

The practice in this project is to simulate the current direction, that is, the current starts from the positive pole of the power supply and finally returns to the negative pole (opposite to the direction of the electron).

Each element has its own resistance value. For example, the resistance value of the connecting wire, knife switch, ammeter, etc. is regarded as 0, and the resistance value of the voltmeter is infinite.

Therefore, find out all current paths, and then compare with the resistance value of the correct path and node elements to analyze the connection.

implementation

1. Generate initial adjacency matrix

First, a basic weight list of component terminals is established, and its structure is roughly as follows:

 { Ammeter A terminal a: 0 Ammeter A terminal b: 1 Weight: 100 } { Knife switch A terminal a: 2 Knife switch A terminal b: 3 Weight: 10 } { Resistance A terminal a: 4 Resistance B terminal b: 5 Weight: 200 }

The value behind the terminal is the unique ID of the terminal, and The weight is related to the resistance value but not necessarily equal

Why is the weight related to the resistance value and the two are not necessarily equal? Because we hope to exclude some inconsistent circuits when judging the resistance value (of course, it is impossible to exclude completely), and if the weight is equal to the resistance value, some cases will not be excluded.

For example, the user replaced the knife switch with an ammeter. Assuming that the resistance value is 0, the total resistance value is the same, and the circuit that does not meet the requirements cannot be excluded.

I remember that the weight 10 represents the switch, and the weight 100 represents the ammeter. Of course, the memory may be different.

Then, generate the adjacency matrix of these terminals. By default, the weight between each two terminals is 0, and then fill the above weight into the matrix, so that the initial adjacency matrix is obtained.

2. User connection

When the user connects, the foreground processes the user logic and displays the corresponding performance, and then sends the information of the line to the adjacency table component:

 { Connecting post 1:4 (ID of terminal a of resistance A) Connection column 2:3 (ID of terminal B of knife switch A) Weight: 1 }

The adjacency table changes the weight between the two terminals to 1, that is, the weight of the wire.

3. Obtain all current paths

We use depth first traversal to obtain all paths between the positive and negative poles of the power supply, that is, the physical circuit.

Note that when a component is short circuited, the weight between the two terminals of the component is 1, so the resistance value of the circuit may be different from that of the normal circuit, and the circuit may be ruled out in advance in the following steps.

Under extreme conditions (that is, any two terminals are connected), the time complexity of the algorithm is O (n ^ 2), but according to the operation, no one will spend several hours to interconnect dozens of points in pairs, so the algorithm efficiency is not considered temporarily.

4. Error judgment

This part should be represented by a flow chart, but I was a little sleepy, so I stopped drawing.

First, compare the number of actual circuits with the number of correct circuits.

If the number of circuits is incorrect, compare the weight of the actual component with the standard weight to see if there is a short circuit, and then compare the total weight with the component to report targeted errors.

If the number of loops is correct, compare the total weight value.

If the circuit number is correct and the total weight value is incorrect, first find out whether the component is short circuited, then compare the total weight with the component, and report targeted errors.

If the number of circuits and the total weight are correct, first find out the short circuit of the elements, and then, according to the actual needs of each experiment, specify the order of the comparison elements.

Of course, the actual error detection code is much more complex than the above text, especially when it involves error priority, component access judgment, etc., but the basic principle is clear.

other

Late night chatting, only talking about ideas without mentioning code. As long as you know the idea, you can write code in any language.

Zimiao haunting blog (azimiao. com) All rights reserved. Please note the link when reprinting: https://www.azimiao.com/7357.html
Welcome to the Zimiao haunting blog exchange group: three hundred and thirteen million seven hundred and thirty-two thousand

Comment

*

*