Representation (Adjacency list/matrix)
Representation (Adjacency list/matrix) Adjacency list An adjacency list is a data structure that represents a graph as a two-dimensional list of element...
Representation (Adjacency list/matrix) Adjacency list An adjacency list is a data structure that represents a graph as a two-dimensional list of element...
Representation (Adjacency list/matrix)
Adjacency list
An adjacency list is a data structure that represents a graph as a two-dimensional list of elements. Each element in the adjacency list corresponds to a node in the graph, and its value corresponds to the neighboring nodes of that node.
Example:
[
["A", "B", "C"],
["D", "E", "F"],
["G", "H", "I"]
]
Matrix
A matrix is a rectangular array of elements that represents a graph. Each element in the matrix corresponds to a node in the graph, and its value corresponds to the degree of that node. A node with degree 0 is a sink node, and a node with degree 1 is an source node.
Example:
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
Key Differences
Adjacency list:
Nodes are represented by elements in a list.
Neighboring nodes are represented by the elements in the list.
The order of elements in the list does not matter.
Matrix:
Nodes are represented by elements in a rectangular array.
The degree of a node is represented by the number of elements in the row corresponding to that node.
The order of elements in the matrix does matter