Problem
Submissions

Solution: Paths in Maze That Lead to Same Room

Statement

Naive approach

A naive approach for finding cycles of length 33 in a given set of rooms is based on DFS traversal. We apply DFS on a room and traverse until we visit its neighbors up to a depth of 33. If the next neighbor is the source room, we have found a cycle of length 33. Otherwise, we have not. Repeat this process for all the other rooms, and count all the cycles of length 3.

The time complexity for DFS traversal on a single node is O(V+E)O(V + E), where VV represents the number of vertices and EE represents the number of edges in the graph. Since we perform DFS traversal on every node in the graph, the overall time complexity can be expressed as O(VĂ—(V+E))O(V \times (V + E)). However, the space complexity of this approach is O(V)O(V).

Optimized approach using graph algo

We solve this problem using the adjacency list. We initialize a dictionary, neighbors, containing all rooms as keys and their neighboring rooms as corresponding values. Since corridors[i]=[room1,room2] represents a corridor showing that the two rooms are connected, we update the adjacency list as neighbors[room1] = room2 and neighbors[room2] = room1.

While updating neighbors with the corridor [room1,room2][room1,room2], we know that room1room1 and room2room2 are directly connected. After updating neighbors with this corridor, we can look for any common room that exists in both neighbors[room1] and neighbors[room2]. This can be done by taking the intersection of neighbors[room1] and neighbors[room2]. If we find any common room, say room3room3, we conclude that room3room3 is connected to both room1room1 and room2room2. Therefore, we come to know that room1room1, room2room2, and room3room3 are connected with each other, forming a cycle of length 33.

Let’s look at the following illustration to get a better understanding of the solution:

Problem
Submissions

Solution: Paths in Maze That Lead to Same Room

Statement

Naive approach

A naive approach for finding cycles of length 33 in a given set of rooms is based on DFS traversal. We apply DFS on a room and traverse until we visit its neighbors up to a depth of 33. If the next neighbor is the source room, we have found a cycle of length 33. Otherwise, we have not. Repeat this process for all the other rooms, and count all the cycles of length 3.

The time complexity for DFS traversal on a single node is O(V+E)O(V + E), where VV represents the number of vertices and EE represents the number of edges in the graph. Since we perform DFS traversal on every node in the graph, the overall time complexity can be expressed as O(VĂ—(V+E))O(V \times (V + E)). However, the space complexity of this approach is O(V)O(V).

Optimized approach using graph algo

We solve this problem using the adjacency list. We initialize a dictionary, neighbors, containing all rooms as keys and their neighboring rooms as corresponding values. Since corridors[i]=[room1,room2] represents a corridor showing that the two rooms are connected, we update the adjacency list as neighbors[room1] = room2 and neighbors[room2] = room1.

While updating neighbors with the corridor [room1,room2][room1,room2], we know that room1room1 and room2room2 are directly connected. After updating neighbors with this corridor, we can look for any common room that exists in both neighbors[room1] and neighbors[room2]. This can be done by taking the intersection of neighbors[room1] and neighbors[room2]. If we find any common room, say room3room3, we conclude that room3room3 is connected to both room1room1 and room2room2. Therefore, we come to know that room1room1, room2room2, and room3room3 are connected with each other, forming a cycle of length 33.

Let’s look at the following illustration to get a better understanding of the solution: