Skip to contents

Compute a distance matrix for all node pairs in a graph using igraph.

Usage

distances_from_graph(graph_df, directed = FALSE, cost.column = "cost", ...)

Arguments

graph_df

A data frame representing a graph with columns: from, to, and a cost column (see cost.column).

directed

Logical (default: FALSE). If TRUE, treats the graph as directed; if FALSE, treats it as undirected.

cost.column

Character string (optional). Name of the cost column in graph_df. Alternatively, a numeric vector of edge costs with length equal to nrow(graph_df).

...

Additional arguments passed to distances(), such as v (from) and to to compute paths between specific nodes.

Value

A matrix of shortest path distances (based on the edge costs) between node pairs. Rows and columns are named by node ID, in ascending order of node ID.

Details

This function:

  • Converts the graph data frame to an igraph graph object, with the sorted unique node IDs as vertex names

  • Computes the shortest path distances with distances(mode = "out"), by default for all node pairs

Examples

library(flownet)

# Create a simple graph
graph <- data.frame(
  from = c(1, 2, 2, 3),
  to = c(2, 3, 4, 4),
  cost = c(1, 2, 3, 1)
)

# Compute distance matrix
dmat <- distances_from_graph(graph, cost.column = "cost")
dmat
#>   1 2 3 4
#> 1 0 1 3 4
#> 2 1 0 2 3
#> 3 3 2 0 1
#> 4 4 3 1 0