Skip to content

Graph

Module Collections.Graphs

Graphs in ixfx are immutable, any changes to the graph produce a new graph.

Each node, or vertex of the graph has an associated string key. Thus, it’s not possible to put data directly into the these graphs. Rather, you put in a key that can later identify the data. This is helpful if the data you are storing is likewise immutable, since otherwise the graph would be storing stale references. The principle being that the key can always point back to the right object, even if its identity has changed.

// Make a new map and a helper function to store people by name
const store = new Map();
const storePeople = (...persons) => {
for (const p of persons) {
store.set(p.name, p);
}
}
// Add two people
storePerson(
{ name: `Sue`, size: 12 },
{ name: `Bob`, size: 9 }
)
const Dg = Graphs.Directed;
let g = Dg.graph();
g = Dg.connect(g, { from: `Bob`, to: `Sue` });

Directed graph

Graphs.Directed is set of functions for working with directed graphs. A directed graph means that connections have ‘from’ and ‘to’, like an arrow drawn between two shapes.

import { Graphs } from "https://unpkg.com/ixfx/dist/collections.js"
const Dg = Graphs.Directed;
let g = Dg.graph();
g = Dg.connect(g, { from: `a`, to: `b` });
g = Dg.connect(g, { from: `b`, to: `c` });
g = Dg.connect(g, { from: `c`, to: `a` });
Dg.dumpGraph(g);
// A -> B, B -> C, C -> A

The graph is made up of nodes or vertices (Vertex ), connected via edges (Edge ).

Each vertex keeps track of its outgoing edges.

Undirected graph

Graphs.Undirected is a set of functions for working with undirected graphs.

Undirected graphs have no sense of ‘from’ or ‘to’. Vertices are connected, but with no sense of direction.

import { Graphs } from "https://unpkg.com/ixfx/dist/collections.js"
const G = Graphs.Undirected;
let g = G.graph();
g = G.connect(g, `a`, `b`);
g = G.connect(g, `b`, `c`);
g = G.connect(g, `c`, `a`);
G.dumpGraph(g);
// A - B, B - C, C - A

Type Undirected.Edge can have a weight.