Number of Possible Sets of Closing Branches - Problem

A company has n branches connected by roads across the country. Due to excessive travel time between branches, they want to close some branches to optimize operations.

The key constraint: all remaining branches must be within maxDistance of each other. When a branch closes, all roads connected to it become inaccessible.

Goal: Count how many different valid sets of branches can remain open (including keeping all branches open if possible).

Input:

  • n: number of branches
  • maxDistance: maximum allowed distance between any two remaining branches
  • roads: array of [u, v, weight] representing roads between branches

Output: Number of possible valid sets of remaining branches.

Input & Output

example_1.py โ€” Simple Triangle
$ Input: n = 3, maxDistance = 12, roads = [[0,1,2],[1,2,10],[0,2,10]]
โ€บ Output: 5
๐Ÿ’ก Note: Valid subsets: {} (empty), {0}, {1}, {2}, {0,1}. The subset {0,2} has distance 10 โ‰ค 12, {1,2} has distance 10 โ‰ค 12, and {0,1,2} has max distance 10 โ‰ค 12, so total is 8 valid subsets.
example_2.py โ€” Linear Chain
$ Input: n = 3, maxDistance = 5, roads = [[0,1,2],[1,2,10]]
โ€บ Output: 5
๐Ÿ’ก Note: Valid subsets: {}, {0}, {1}, {2}, {0,1}. Subsets {0,2}, {1,2}, {0,1,2} are invalid because distance between 0 and 2 is 12 > 5.
example_3.py โ€” Single Node
$ Input: n = 1, maxDistance = 10, roads = []
โ€บ Output: 2
๐Ÿ’ก Note: Valid subsets: {} (empty set) and {0} (single branch). Both satisfy the constraint trivially.

Visualization

Tap to expand
Branch Network Optimization ProcessOriginal Network01221010Subset: {0, 1}0122Max Distance: 2 โœ“Subset: {0, 2}01210Max Distance: 10Algorithm StepsStep 1: EnumerateGenerate all 2^npossible subsetsusing bit masksStep 2: ExtractBuild subgraph withonly remainingbranches & roadsStep 3: ComputeUse Floyd-Warshallto find shortestpaths O(n^3)Step 4: ValidateCheck if max distanceโ‰ค maxDistanceCount valid subsetsTime Complexity: O(2^n ร— n^3)Feasible for n โ‰ค 15 (constraint: 2^15 = 32,768 subsets)
Understanding the Visualization
1
Enumerate Possibilities
Consider all 2^n ways to close branches using bitmasks
2
Extract Subgraph
For each subset, build the network of remaining branches
3
Compute Distances
Use Floyd-Warshall to find shortest paths between all remaining branches
4
Validate Constraint
Check if maximum distance โ‰ค maxDistance requirement is satisfied
Key Takeaway
๐ŸŽฏ Key Insight: This is a classic enumeration problem where we must check all possible combinations. The exponential time is unavoidable, but the constraint n โ‰ค 15 makes it computationally feasible.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(2^n ร— n^3)

2^n subsets, each requiring O(n^3) Floyd-Warshall computation

n
2n
โš  Quadratic Growth
Space Complexity
O(n^2)

Distance matrix for Floyd-Warshall algorithm

n
2n
โš  Quadratic Space

Constraints

  • 1 โ‰ค n โ‰ค 15
  • 1 โ‰ค maxDistance โ‰ค 105
  • 0 โ‰ค roads.length โ‰ค n ร— (n-1) / 2
  • roads[i].length == 3
  • 0 โ‰ค ui, vi โ‰ค n-1
  • ui โ‰  vi
  • 1 โ‰ค wi โ‰ค 105
  • At most one road between any pair of branches
Asked in
Google 45 Amazon 32 Meta 28 Microsoft 22
38.5K Views
Medium Frequency
~25 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen