| normalize_graph | R Documentation |
Normalize node IDs in a graph to be consecutive integers starting from 1. This is useful for ensuring compatibility with graph algorithms that require sequential node IDs.
normalize_graph(graph_df)
graph_df |
A data frame representing a graph with columns:
|
This function:
Extracts all unique node IDs from both from and to columns
Sorts them in ascending order
Remaps the original node IDs to sequential integers (1, 2, 3, ...)
Updates both from and to columns with the normalized IDs
Normalization is useful when:
Node IDs are non-consecutive (e.g., 1, 5, 10, 20)
Node IDs are non-numeric or contain gaps
Graph algorithms require sequential integer node IDs starting from 1
Note: This function only normalizes the node IDs; it does not modify the graph structure or any other attributes. The mapping preserves the relative ordering of nodes.
A data frame with the same structure as graph_df, but with from
and to columns remapped to consecutive integer IDs starting from 1.
All other columns are preserved unchanged.
nodes_from_graph flownet-package
library(flownet)
# Create graph with non-consecutive node IDs
graph <- data.frame(
from = c(10, 20, 20),
to = c(20, 30, 40),
cost = c(1, 2, 3)
)
# Normalize to consecutive integers (1, 2, 3, 4)
graph_norm <- normalize_graph(graph)
graph_norm
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.