Home >Web Front-end >JS Tutorial >Get a gist of graph data structure here...
This blog is for developers who want to get the gist of a concept and not beat around the bush. We will understand graph data structure in this post. Let's start.
Graphs are non-linear data structures with 2 components namely edges and nodes.
A node is: -
An edge is:-
There are 2 ways:
The adjacent matrix is a 2-D matrix representing graph data structure. The rows and columns represent nodes and the value in the matrix represents the edges.
In the above image, 4 nodes are connected by the edges. Node A is connected to all the nodes and thus the value is added as 1 in the cells where A (either row or column) intersects with rows or columns of other nodes. Node B is only connected to Node A resulting in a value 1 in the cell where node B intersects with node A and other cells have a value 0.
The time complexity to add or delete an edge for the adjacent matrix is O(1). It can be used when: -
The adjacent list is storing the graph with the help of multiple linked lists or arrays. The rows represent the node (check the image below), and the values present in the rows represent neighbour.
In the above image, there are 5 nodes. Node A is connected to node B and node D which is why the first array has those values. Similarly, node B is connected to node A, node C, node D, and node E and so the second array has those nodes in the second array.
The time complexity for retrieving or deleting an edge is O(n) and the time complexity for adding an edge is O(1). The adjacent list can be used when: -
One popular example of graph data structure is: in a field of football each player can be considered a node and their interactions represent edges.
Graphs are used in similar scenarios as previous example, such as: -
The above is the detailed content of Get a gist of graph data structure here.... For more information, please follow other related articles on the PHP Chinese website!