0

I am facing a problem that I can't seem to be able to solve, I have a digram of nodes, these node form the shape, as per picture.

enter image description here

The diagram contains 15 nodes. The diagram also has 2 rings. I need to find a solution on which I can find the elements that form a ring in the diagram, So from that diagram I'd get 2 lists of elements {A,Z,B,O,F}, {T,H,R,M,P,F}, each node will be considered a ringelement, while ignoring the rest of the elements that are not included in the rings{ Q,N,C,V }. Keep in mind that for my purposes the diagram will always contain multiple rings.

What I have available is a list of node objects, each node has a property called ConnectedNodes, which is a list containing the nodes connected to it not ordered or anything. Connector nodes {F,P} are connected to 3 nodes. They are sort of shared, they connect to the ring nodes and non ring nodes, while the rest of the nodes are strickly connected to 2 nodes. Can someone please give me ideas, or perhaps suggest alogrithms that might be applied to this problem.

Update:- This is aso valid where you can have more than one ring element as a connector node{V,P,D}. For the updated diagram I now have 4 rings instead of 2 and 4 connector nodes. enter image description here

ZZZ
  • 285
  • 2
  • 15

1 Answers1

1

It's not clear for me what exactly would you like to do. As I understand you want one of these:

  1. You want to find any particular ring (for example: {A,Z,B,O,F}).

You can do it using DFS - just start it from any vertex v, proceed untill you reach vertex u, which has already been visited. The ring is created with vertices which you visited since you have left u.

  1. You want to find every ring in the diagram ({A,Z,B,O,F}, {T,H,R,M,P,F} in the first test case)

This also can be done using DFS - start from any vertex v and look for every mono-vertex-path (path in which every vertex occurs at most once) starting from v. Whenever there is edge (u,w) from end of path to any vertex inside path, you have the ring.

This algorithm will find every ring twice, but it's just technical issue.

Pessimistic time complexity of this algorithm is O(n!). But please note, that in general case one cannot find better solution, because number of rings in general case is n! (for example in full graph).

  1. You want to find every vertex in the diagram, which are part of some ring ({A,Z,B,O,F,T,H,R,M,P} in the first test case)

You can do it by first look for every bridge in graph. https://en.wikipedia.org/wiki/Bridge_(graph_theory)

The vertex u isn't part of any ring if and only if every edge which comes from u is a bridge.

You can do it in linear time.

juggler92
  • 113
  • 10