I know this is a question which is more about logic than an actual programming issue, but I hope that someone may guide me into the correct direction to solve this. Look at it like the advent programming riddles :)
I have nodes, that are connected with each other over a port. I know the nodes and I know all relations. Now I need to get all possible networks.
To further clarify what I need, I made the following draft: Image of the network layout: https://ibb.co/51zmkfj
Legend:
- A-F: Nodes
- 1-5: Relations
- 6: Redundant relation (In case node D is out of order)
- I-IV: Networks I need to get in a List (All nodes per network, therefore a node can be in multiple networks)
- V: Redundant network which I must get in a separate list (With source and target node)
Please note, that the network can be much bigger and there may be more than one of these redundant networks
As written, I need these networks I-V. I tried various things and got a version working for I-IV, but it is a lot of code and I am probably overthinking the whole thing.
My approach:
- looping through the relations and creating a
Dictionary<int, List<String>>
with a random int identifier and the id+portNumber as a unique string of all relations that are connected with each other. - Then I do a nested foreach with the Dictionary to compare each entry with the other entries to find any duplicates and Groups that are not complete.
- Then I remove these wrong groups from the Dictionary.
- After that, I remove the portNumber from all values in the list of the dictionary.
I also made a .NET Fiddle which is prepared with the data exactly as in the image above. https://dotnetfiddle.net/SQnZYs
As someone with a bit of networking experience I'd first like to point out that you can't plug multiple cables into the same port like that
I can see a couple of approaches here but you'll need to add some more information to your data model, such as a way of labelling a backup link as a seperate connection that doesn't get involved in a 'normal' network and therefore needs to be segregated.
The big thing though is that your terms need more refined definitions:
Node
Port
s (includingBackup
versions of thosePort
s).Port
Network
and has an arbitrary number ofConnection
s.Connection
Port
s, contained by a singleNetwork
.Network
Port
s and theConnection
s that link them together.Backup
Port
,Connection
orNetwork
that is separate from the normal set such thanBackup Port
s andBackup Connection
s exist exclusively inBackup Network
s.Ok that last one is a bit clumsy, but I think it's a necessary part of the problem in this case. Withou some distinction between a standard link and a backup link you can't separate network
V
from the other networks. Of course not all of those objects are actual classes, they're concepts. The useful minimum seems to be:Node
,Port
andConnection
, withNetwork
andBackup
becoming attributes... although in fairness you could probably replacePort
with some logic, but it's useful in some ways.With those definitions it should be possible to solve the problem. A psudo-code algorithm for it might be something like this:
We could merge the two inner loops into one with the right data structure of course.
And since I'm fond of overly-engineered objects that make my top-level code easier to read (if not always easier to understand), the fact that I had some time on my hands today, and probably in the spirit of Xmas... I wrote a fun implementation for you.
Caution: not for production use!
(Seriously, don't even look at all the fun I had with
ToString()
overrides.)