mediumGraphsPattern: DFS

Connected Modules Count Solution

Problem Statement

You are given a list of pairs representing the connections between modules, where each pair contains the identifiers of two connected modules. Implement a function to determine the number of connected modules using Depth-First Search (DFS), given a start module. The function should return the count of connected modules.

Examples

Example 1:
Input:{"modules":27,"tubes":[[1,2],[2,3],[3,4],[1,4],[4,19],[19,27]],"centralControlRoom":19}
Output:7
Explanation: The tubes connect modules 1, 2, 3, 4, 19, and 27, which are reachable from the central control room. Other modules are not connected.

Constraints

  • The lake is represented as a 2D array of integers where 0 represents land and 1 represents water.
  • The Pacific and Atlantic oceans are at the top and bottom rows of the lake, respectively.
  • Each cell can move in four directions (up, down, left, right) to adjacent cells.
Time: O(R*C) Space: O(R*C)
The optimized approach uses Depth-First Search with a time complexity of O(V + E), where V is the number of modules and E is the number of tubes.

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.