Last active
October 1, 2018 03:36
-
-
Save laran/9d7460e463296ecc41485b448968cddf to your computer and use it in GitHub Desktop.
A Adjacency List representation of a Graph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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