Missed TensorFlow World? Check out the recap. Learn more

Module: nsl.tools.graph_utils

View source on GitHub

Utility functions for manipulating (weighted) graphs.

The functions in this module assume that weighted graphs are represented by nested dictionaries, where the outer dictionary maps each edge source ID to an inner dictionary that maps each edge target ID to that edge's weight. So for example, the graph containing the edges:

A -- 0.5 --> B
A -- 0.9 --> C
B -- 0.4 --> A
B -- 1.0 --> C
C -- 0.8 --> D

would be represented by the dictionary:

{ "A": { "B": 0.5, "C": 0.9 },
  "B": { "A": 0.4, "C": 1.0 },
  "C": { "D": 0.8 }
}

In the documention, we say a graph is represented by a dict: source_id -> (target_id -> weight).

Functions

add_edge(...): Adds an edge to a given graph.

add_undirected_edges(...): Makes all edges of the given graph bi-directional.

read_tsv_graph(...): Reads the file filename containing graph edges in TSV format.

write_tsv_graph(...): Writes the given graph to the file filename in TSV format.