yog_io/list

Adjacency List format support.

This module provides functions to serialize and deserialize graphs in adjacency list format (.list). This format is simple and human-readable, where each line represents a node and its successors.

Format Overview

Each line starts with a node ID, followed by a delimiter (default: :), and then a space-separated list of successor node IDs.

For weighted graphs, successors are represented as id,weight.

Example (Unweighted)

1: 2 3
2: 3
3:

Example (Weighted)

1: 2,0.5 3,1.2
2: 3,0.8
3:

Usage

import yog/model.{Directed}
import yog_io/list as list_io

pub fn main() {
  let assert Ok(graph) = list_io.read("graph.list", Directed)
}

Types

Errors that can occur during Adjacency List operations

pub type ListError {
  ReadError(path: String, error: String)
  WriteError(path: String, error: String)
  ParseError(line: Int, content: String)
}

Constructors

  • ReadError(path: String, error: String)

    File not found or couldn’t be read

  • WriteError(path: String, error: String)

    File couldn’t be written

  • ParseError(line: Int, content: String)

    Invalid line format

Options for Adjacency List import/export

pub type ListOptions {
  ListOptions(weighted: Bool, delimiter: String)
}

Constructors

  • ListOptions(weighted: Bool, delimiter: String)

    Arguments

    weighted

    Whether neighbors include weights (e.g., “neighbor,weight”)

    delimiter

    Delimiter between node and neighbors (default: “:”)

Values

pub fn default_options() -> ListOptions

Returns default options for unweighted adjacency lists

pub fn from_string(
  content: String,
  graph_type: model.GraphType,
  options: ListOptions,
) -> Result(model.Graph(Nil, Float), ListError)

Parses an adjacency list from a string into a graph.

pub fn read(
  path: String,
  graph_type: model.GraphType,
) -> Result(model.Graph(Nil, Float), ListError)

Reads a graph from an adjacency list file.

pub fn read_with(
  path: String,
  graph_type: model.GraphType,
  options: ListOptions,
) -> Result(model.Graph(Nil, Float), ListError)

Reads a graph from an adjacency list file with custom options.

pub fn serialize(
  graph: model.Graph(n, Float),
  options: ListOptions,
) -> String

Converts a graph to an adjacency list string.

pub fn write(
  path: String,
  graph: model.Graph(n, Float),
) -> Result(Nil, ListError)

Writes a graph to an adjacency list file.

pub fn write_with(
  path: String,
  graph: model.Graph(n, Float),
  options: ListOptions,
) -> Result(Nil, ListError)

Writes a graph to an adjacency list file with custom options.

Search Document