Skip to content

Instantly share code, notes, and snippets.

@laran
Last active October 1, 2018 03:36
Show Gist options
  • Save laran/9d7460e463296ecc41485b448968cddf to your computer and use it in GitHub Desktop.
Save laran/9d7460e463296ecc41485b448968cddf to your computer and use it in GitHub Desktop.
A Adjacency List representation of a Graph
// The index of the element in the outer Array is the 'id' of the Node in the Graph.
// Each element in the outer Array contains the Set of IDs to which that Node is connected.
//
// e.g. Node 0 has Edges to Nodes 1 and 2
// Node 1 has no Edges
// Node 2 has Edges to Nodes 0, 1 and 3
// Node 3 only has an Edge to Node 2
let graphAsList = [
[1, 2],
[],
[0, 1, 3],
[2]
];
// A Map is another viable way to represent an Adjacency List
let graphAsMap = {
0: [1, 2],
1: [],
2: [0, 1, 3],
3: [2],
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment