Building the electricity or piping network in game

Hi guys!

I am making a project that we can build an electricity network and piping network in game like the function in Cities Skylines. The system allows player to build the pipes and pump and finally connect it to a building. But I have no idea how to check if the object (Pipe/building) is connecting the source(Pump) or not. The figure below shows the idea I would like to build.

Any Ideas?

Thanks!!