Learn R Programming

flownet (version 0.1.2)

normalize_graph: Normalize Graph Node IDs

Description

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.

Usage

normalize_graph(graph_df)

Value

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.

Arguments

graph_df

A data frame representing a graph with columns: from and to (node IDs).

Details

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.

See Also

nodes_from_graph flownet-package

Examples

Run this code
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

Run the code above in your browser using DataLab