Construct 2D Grid Matching Graph Layout Problem
Description
LeetCode Problem 3311.
You are given a 2D integer array edges representing an undirected graph having n nodes, where edges[i] = [u_i, v_i] denotes an edge between nodes u_i and v_i.
Construct a 2D grid that satisfies these conditions:
- The grid contains all nodes from
0ton - 1in its cells, with each node appearing exactly once. - Two nodes should be in adjacent grid cells (horizontally or vertically) if and only if there is an edge between them in
edges.
It is guaranteed that edges can form a 2D grid that satisfies the conditions.
Return a 2D integer array satisfying the conditions above. If there are multiple solutions, return any of them.
Example 1:
Input: n = 4, edges = [[0,1],[0,2],[1,3],[2,3]]
Output: [[3,1],[2,0]]
Explanation:

Example 2:
Input: n = 5, edges = [[0,1],[1,3],[2,3],[2,4]]
Output: [[4,2,3,1,0]]
Explanation:

Example 3:
Input: n = 9, edges = [[0,1],[0,4],[0,5],[1,7],[2,3],[2,4],[2,5],[3,6],[4,6],[4,7],[6,8],[7,8]]
Output: [[8,6,3],[7,4,2],[1,0,5]]
Explanation:

Constraints:
2 <= n <= 5 * 10^41 <= edges.length <= 10^5edges[i] = [u_i, v_i]0 <= u_i < v_i < n- All the edges are distinct.
- The input is generated such that
edgescan form a 2D grid that satisfies the conditions.
Solution
Approach: Find the First Row, Then Peel Rows Off
Every node has degree 1 to 4 in the graph: corners have degree 2 (or 1 in a single row), border nodes 3, inner nodes 4. The first row of the grid can be found from the degrees:
- If a node has degree 1, the grid is a single row and that node is its end. The first row is just
[node], and the later steps rebuild the path. - If there is no node of degree 4, the grid has two rows or two columns. Pick a corner
x(degree 2) and its neighborythat also has degree 2. The first row is[x, y]. - Otherwise start at a corner and walk along the border: keep moving to a neighbor that is not the previous node and does not have degree 4, until the next corner is reached. This gives a full first row.
The width is now known, so the number of rows is n / width. Build each following row from the previous one: mark the previous row as visited, and for each node in it take its one unvisited neighbor. Because the grid is guaranteed to exist, that neighbor is the node just below.
Complexity: O(n + m) time and O(n + m) space, where m is the number of edges.
class Solution:
def constructGridLayout(self, n: int, edges: List[List[int]]) -> List[List[int]]:
g = [[] for _ in range(n)]
for u, v in edges:
g[u].append(v)
g[v].append(u)
deg = [-1] * 5
for x, ys in enumerate(g):
deg[len(ys)] = x
if deg[1] != -1:
row = [deg[1]]
elif deg[4] == -1:
x = deg[2]
for y in g[x]:
if len(g[y]) == 2:
row = [x, y]
break
else:
x = deg[2]
row = [x]
pre = x
x = g[x][0]
while len(g[x]) > 2:
row.append(x)
for y in g[x]:
if y != pre and len(g[y]) < 4:
pre = x
x = y
break
row.append(x)
ans = [row]
vis = [False] * n
for _ in range(n // len(row) - 1):
for x in row:
vis[x] = True
nxt = []
for x in row:
for y in g[x]:
if not vis[y]:
nxt.append(y)
break
ans.append(nxt)
row = nxt
return ans