Possible Subspace Intersection Dimensions
Math & Bit ManipulationAuto-graded · 23 test casesLast updated
Computes all valid integer dimensions for the intersection of two vector subspaces given ambient and subspace dimensions.
Given two vector subspaces $U$ and $W$ embedded inside an ambient vector space $V$, determine all possible values for the dimension of their intersection, $\dim(U \cap W)$.
You are given three integers:
dim_u: The dimension of subspace $U$ ($\dim(U)$)dim_w: The dimension of subspace $W$ ($\dim(W)$)dim_v: The dimension of the ambient space $V$ ($\dim(V)$)
By Grassmann's dimension formula, the linear sum $U + W$ satisfies: $$\dim(U + W) = \dim(U) + \dim(W) - \dim(U \cap W)$$
Since $U \cap W \subseteq U$ and $U \cap W \subseteq W$, the intersection dimension cannot exceed either subspace's dimension. Additionally, since $U + W$ is a subspace of $V$, its dimension cannot exceed $\dim(V)$.
Return a sorted list of all valid non-negative integer dimensions that $\dim(U \cap W)$ can take.
Constraints
- $0 \le \text{dim_u} \le \text{dim_v}$
- $0 \le \text{dim_w} \le \text{dim_v}$
- $1 \le \text{dim_v} \le 10^9$
Examples
Example 1
Input: possible_intersection_dimensions(4, 4, 7)
Output: [1, 2, 3, 4]
Example 2
Input: possible_intersection_dimensions(4, 4, 8)
Output: [0, 1, 2, 3, 4]
Example 3
Input: possible_intersection_dimensions(2, 3, 4)
Output: [1, 2]
Hints
Hint 1
Apply Grassmann's formula: dim(U + W) = dim(U) + dim(W) - dim(U ∩ W).
Hint 2
What are the structural upper and lower bounds on dim(U + W)?
Hint 3
Use dim(U + W) ≤ dim(V) to find the minimum possible intersection dimension, and min(dim(U), dim(W)) for the maximum.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
To find all valid values for $k = \dim(U \cap W)$, we derive strict upper and lower bounds on $k$ using linear algebra principles:
-
Upper Bound: Since $U \cap W$ is contained within both $U$ and $W$, its dimension cannot be larger than the dimension of either subspace: $$k \le \min(\dim(U), \dim(W))$$
-
Lower Bound: By Grassmann's dimension formula: $$\dim(U + W) = \dim(U) + \dim(W) - k$$ Because $U + W$ is a subspace of $V$, we know that $\dim(U + W) \le \dim(V)$. Rearranging gives: $$k \ge \dim(U) + \dim(W) - \dim(V)$$ Since the dimension of a subspace must also be non-negative, we enforce: $$k \ge \max(0, \dim(U) + \dim(W) - \dim(V))$$
Every integer dimension in the closed range $[\max(0, \dim(U) + \dim(W) - \dim(V)), \min(\dim(U), \dim(W))]$ is achievable by appropriately selecting basis vectors.
Complexity
- Time Complexity: $O(M)$ where $M = \min(\dim(U), \dim(W)) - \max(0, \dim(U) + \dim(W) - \dim(V)) + 1$. Since $M \le \dim(V)$, generating the output list takes linear time in the number of possible dimensions.
- Space Complexity: $O(M)$ to construct and return the result list.
from typing import List
def possible_intersection_dimensions(dim_u: int, dim_w: int, dim_v: int) -> List[int]:
min_dim = max(0, dim_u + dim_w - dim_v)
max_dim = min(dim_u, dim_w)
return list(range(min_dim, max_dim + 1))