Identifies the boundary vertices of a specified region in a graph, defined as vertices in the region that have at least one neighbor outside the region.
Value
An integer vector containing the indices of the boundary vertices,
which is a subset of the input region vector. Returns an empty
integer vector if no boundary vertices exist.
Details
This function determines which vertices in a specified region are positioned at the boundary - meaning they have at least one neighbor that is not part of the region. These boundary vertices are often treated differently in graph-based algorithms, such as harmonic smoothing, where their values are typically fixed as constraints.
The boundary definition used matches the one implemented in the C++ harmonic smoothing functions, focusing on vertices that have connections to the outside of the region.
Examples
# Create a simple grid graph adjacency list (5x5 grid)
create_grid_adj_list <- function(n_rows, n_cols) {
n <- n_rows * n_cols
adj_list <- vector("list", n)
for (i in 1:n_rows) {
for (j in 1:n_cols) {
v <- (i-1) * n_cols + j
neighbors <- numeric(0)
# Add neighbors (up, down, left, right)
if (i > 1) neighbors <- c(neighbors, (i-2) * n_cols + j) # up
if (i < n_rows) neighbors <- c(neighbors, i * n_cols + j) # down
if (j > 1) neighbors <- c(neighbors, (i-1) * n_cols + (j-1)) # left
if (j < n_cols) neighbors <- c(neighbors, (i-1) * n_cols + (j+1)) # right
adj_list[[v]] <- neighbors
}
}
return(adj_list)
}
# Create a 5x5 grid graph
grid_adj_list <- create_grid_adj_list(5, 5)
# Define a region (central 3x3 subgrid)
central_region <- c(7:9, 12:14, 17:19)
# Find boundary vertices of the central region
boundary <- get.region.boundary(grid_adj_list, central_region)
print(boundary)
#> [1] 7 8 9 12 14 17 18 19
# Expected output: c(7, 8, 9, 12, 14, 17, 18, 19)
# (all except the center vertex 13)