JointJS+ toAdjacencyList

graphUtils.toAdjacencyList


toAdjacencyList(graph)

Converts graph to an adjacency list. The method returns an object with element ids as properties and an array of connected element ids as values (the array includes indirect connections i.e. the path between connected elements includes a link-link connection).

Example:

const graph = new joint.dia.Graph({}, { cellNamespace: joint.shapes });

const a = new joint.shapes.standard.Ellipse({ id: 'a' });
const b = new joint.shapes.standard.Ellipse({ id: 'b' });
const c = new joint.shapes.standard.Ellipse({ id: 'c' });

const ab = new joint.shapes.standard.Link({
    id: `a-b`,
    source: { id: 'a' },
    target: { id: 'b' },
});
const bc = new joint.shapes.standard.Link({
    id: `b-c`,
    source: { id: 'b' },
    target: { id: 'c' },
});
const indirectLink = new joint.shapes.standard.Link({
    id: `indirectLink`,
    source: { id: 'a' },
    target: { id: 'b-c' },
});

graph.addCells(a, b, c, ab, bc, indirectLink);

// Returns an object:
// {
//   "a": ["b", "c"],
//   "b": ["c"],
//   "c": []
// }
joint.graphUtils.toAdjacencyList(graph);