adj_to_incidence: Convert an Adjacency Matrix to an Incidence Matrix

View source: R/utilities.R

adj_to_incidenceR Documentation

Convert an Adjacency Matrix to an Incidence Matrix

Description

This function transforms an adjacency matrix into an incidence matrix.

Usage

adj_to_incidence(A, loops = TRUE, directed = TRUE, weighted = TRUE)

Arguments

A

A square numeric matrix representing the adjacency matrix of a graph. The matrix should have non-negative values, where 'A[i, j]' represents the weight of the edge from node 'i' to node 'j'.

loops

Logical. If 'TRUE', self-loops (edges from a node to itself) are included in the incidence matrix. If 'FALSE', they are removed. Default is 'TRUE'.

directed

Logical. If 'TRUE', the graph is treated as directed, meaning each edge has a specific source and target. If 'FALSE', the graph is treated as undirected, and edges are symmetrically represented. Default is 'TRUE'.

weighted

Logical. If 'TRUE', edge weights from 'A' are included in the incidence matrix. If 'FALSE', all edges are treated as having weight '1'. Default is 'TRUE'.

Value

A numeric matrix where rows represent nodes and columns represent edges. - In a **directed** network, a source node has a negative value (-weight), and a target node has a positive value (+weight). - In an **undirected** network, both nodes involved in an edge share the weight (positive values). - If 'weighted = FALSE', all edges have a weight of '1'.

Examples

# Define an adjacency matrix (directed and weighted)
A <- matrix(c(
  1, 3, 0, 0, 2,
  0, 0, 2, 0, 0,
  5, 0, 0, 0, 0,
  0, 0, 0, 0, 1,
  0, 4, 0, 0, 0
), byrow = TRUE, nrow = 5)

# Convert to an incidence matrix (directed, weighted)
(inc_matrix <- adj_to_incidence(A))

# Undirected, weighted graph
(inc_matrix_undirected <- adj_to_incidence(A, directed = FALSE))

# Directed, unweighted graph
(inc_matrix_unweighted <- adj_to_incidence(A, weighted = FALSE))

# Ignore loops
(inc_matrix_no_loops <- adj_to_incidence(A, loops = FALSE))


anespinosa/netmem documentation built on April 5, 2025, 5:02 p.m.