1

This problem has proven to be tougher than I originally thought. I have a list of connected rooms. Each of the rooms can have either a closed or open door. At any given time I would like to know which rooms are connected together via open doors, and label them Connected Rooms Group 1, Connected Rooms Group 2, and etc.

There is a post on stackoverflow discussing how to generate randomly connected rooms and keep track of which rooms are connected. Basically define a matrix/array conn[a][b]. You can then populate the array with true or false values if a room is connected. So if rooms 2 and 4 are connected then conn[2][4] = true and similarly conn[4][2] = true. The Matrix would look like the following:

+--------+--------+--------+--------+--------+
| Rooms  | Room 1 | Room 2 | Room 3 | Room 4 |
+--------+--------+--------+--------+--------+
| Room 1 | N/A    | false  | false  | false  |
| Room 2 | false  | N/A    | false  | true   |
| Room 3 | false  | false  | N/A    | false  |
| Room 4 | false  | true   | false  | N/A    |
+--------+--------+--------+--------+--------+

A more complex situation would be the following (Room 2 and Room 3 have a closed door):

+----+----+----+
| R1   R2 | R3 |
+    +----+    +
| R5 |    | R4 |
+----+    +----+

The Matrix would look like the following:

+--------+--------+--------+--------+--------+--------+
| Rooms  | Room 1 | Room 2 | Room 3 | Room 4 | Room 5 |
+--------+--------+--------+--------+--------+--------+
| Room 1 | N/A    | true   | false  | false  | true   |
| Room 2 | true   | N/A    | false  | false  | false  |
| Room 3 | false  | false  | N/A    | true   | false  |
| Room 4 | false  | false  | true   | N/A    | false  |
| Room 5 | true   | false  | false  | false  | N/A    |
+--------+--------+--------+--------+--------+--------+

I thought I had a clever idea by grouping the connected rooms by looping through only the cells that were located above the N/As and treating each row as a different 'Connected Rooms Group':

+--------+--------+--------+--------+--------+--------+
| Rooms  | Room 1 | Room 2 | Room 3 | Room 4 | Room 5 |
+--------+--------+--------+--------+--------+--------+
| Room 1 | N/A    | true   | false  | false  | true   |
| Room 2 |        | N/A    | false  | false  | false  |
| Room 3 |        |        | N/A    | true   | false  |
| Room 4 |        |        |        | N/A    | false  |
| Room 5 |        |        |        |        | N/A    |
+--------+--------+--------+--------+--------+--------+

Would yield:

Connected Group 1: [1, 2, 5]
Connected Group 2: [3, 4]

However, this technique doesn't work for more complex situations (Connecting Rooms 4 and 5):

+----+----+----+
| R1   R2 | R3 |
+    +----+    +
| R5        R4 |
+----+----+----+

Matrix:

+--------+--------+--------+--------+--------+--------+
| Rooms  | Room 1 | Room 2 | Room 3 | Room 4 | Room 5 |
+--------+--------+--------+--------+--------+--------+
| Room 1 | N/A    | true   | false  | false  | true   |
| Room 2 |        | N/A    | false  | false  | false  |
| Room 3 |        |        | N/A    | true   | false  |
| Room 4 |        |        |        | N/A    | true   |
| Room 5 |        |        |        |        | N/A    |
+--------+--------+--------+--------+--------+--------+

Would yield:

Connected Group 1: [1, 2, 5]
Connected Group 2: [3, 4]
Connected Group 3: [4, 5]

Whereas the actual volume would all be connected, so there should only be one Connected Group 1: [1,2,3,4,5]

I've decided to talk my problem using rooms and doors, but my actual problem is slightly different. At a high level I'm making an analysis tool where the user can isolate volumes via opening/closing a valve between volumes. The tool needs to group individual volumes together for different reasons such as, visually indicating which volumes are connected and running pressure equalization calculations on each of the grouped volumes.

Every schema I have thought about for grouping connected volumes seems to fail. Any brilliant stack overflow users out there have an intelligent solution to group connected rooms?

I'm leaning on programming this in python, but I am well fluent in multiple languages. Feel free to answer the question conceptually or via your favorite non-archaic language.

Mitch
  • 3,342
  • 2
  • 21
  • 31
  • For those who are looking at this topic in the future. I also found a [helpful article](https://breakingcode.wordpress.com/2013/04/08/finding-connected-components-in-a-graph/) on connected components using python. :) – Fly Dangerous o7 Mar 26 '20 at 15:27

1 Answers1

3

I believe what you are doing here is looking for connected components in an adjacency matrix. Your rooms are nodes and if they are connected they share an edge.

Here is a link to some pseudo code for an algorithm that should solve your problem in both a DFS and BFS fashion.

Mitch
  • 3,342
  • 2
  • 21
  • 31