I recommend approaching problems like this by starting with a base case and working up from there until you see a pattern.
For piping, the base case is a straight case with one input flowing to one output. Depending on the exact specs of the problem, this requires either 0 or 1 valves; it does not require any branching.
I------O (or, I--▷◁--O)
Add an input to the base case:
Add an output to the base case:
Add both an input and an output to the base case:
Here's a nicer-looking diagram, if you prefer.
What you can see from looking at these simple cases is that:
- A single input or output requires no valves.
- If there is more than one input or output, each requires one valve.
- Each input or output beyond the first requires one branch.
Since your problem has multiple inputs and multiple outputs, the minimum number of valves is equal to the total number of inputs and outputs and the minimum number of branches is equal to two fewer than the total number of inputs and outputs.