Graph Theory Algorithms (3): Basic representation of graphs (adjacency matrix, adjacency list, comparison of adjacency matrix and adjacency list)

The content of this chapter is implemented in java, Github code warehouse: https://github.com/ZhekaiLi/Code/tree/main/Graph/src

Viewing the pictures in the article may require scientific Internet access!Because github is used to manage pictures, if there is a situation that cannot be loaded, please turn over the wall

[Reference] imooc Mr. Bobo: Fun Algorithm Series - Graph Theory Essentials for Interview and Promotion (Java Edition)

[Links to previous blogs]
Graph Theory Algorithms (1, 2): Classification of Graphs, Basic Concepts of Graphs (Undirected Graphs and Directed Graphs, Unweighted Graphs, Acyclic Graphs, Complete Graphs, Bipartite Graphs; Simple Graphs, Connected Graphs Components, spanning tree of graphs, subgraphs and parent graphs)
graph theory algorithm (3): basic representation of graphs (adjacency matrix, adjacency list, comparison of adjacency matrix and adjacency list)
graph theory algorithm (4): depth first of graphs Traversal DFS
Graph Theory Algorithm (5): Graph Breadth First Traversal BFS
Graph Theory Algorithm (6): LeetCode Graph Theory Algorithm Exercise (785. Judgment of Bipartite Graph, 695. Maximum Area of ​​Island, Floodfill Algorithm, Union Check)

3. Basic representation of the graph

3.1 Adjacency Matrix

G = (V, E) G = (V, E)G=( V ,E ) adjacency CCC 是如下定义的:
C = ( c i j ) n × n ∈ { 0 , 1 } n × n c i j = { 1 ,      ( i , j ) ∈ E 0 ,      ( i , j ) ∉ E \begin{aligned} C&=(c_{ij})_{n\times n}\in\{0,1\}^{n\times n}\\ c_{ij}&=\begin{cases} 1,\;\;(i,j)\in E\\ 0,\;\;(i,j)\notin E \end{cases} \end{aligned} Ccij=(cij)n×n{ 0,1}n×n={ 1,(i,j)E0,(i,j)/E


Example: Directed Graph and its Adjacency Matrix

For undirected graphs, the adjacency matrix is ​​symmetric ( cij = cji c_{ij}=c_{ji}cij=cj i

An undirected graph can be represented by the two columns of data in the center of the figure below, and then translated into an adjacency matrix



java implementation: AdjMatrix.java

3.2 Adjacency List

Why introduce adjacency list? (complexity of adjacency matrix)



The time complexity of mapping the adjacency matrix is ​​O ( E ) O(E)O ( E ) (traverse each edge), and the time complexity of judging whether two points are adjacent isO ( 1 ) O(1)O ( 1 ) has no room for improvement, but itsspace complexity and time complexity of finding adjacent nodes are relatively large

In particular, for example, for a tree (undirected graph) with 3000 nodes, it has only 2999 edges, and even if it is multiplied by 2, there are only 6000 less than group data, but the space complexity of its corresponding adjacency matrix is ​​O ( 300 0 2 ) O(3000^2)O ( 3 0 0 02 ),nearly a thousand times the gap!

Definition of Adjacency List

The adjacency list of a graph is the set of adjacency lists of all nodes. The adjacency list of
each node consists of:

  1. Adjacent edges (undirected graph)
  2. Arc out (directed graph)

It is composed and listed in a singly linked list. Each unit in the linked list corresponds to an adjacent edge/outgoing arc. In addition, it can also contain the weights on the arc as data fields.

Adjacency list representation

For a directed graph G = ( V , E ) G = (V, E)G=( V ,E ) , general useA (i) A (i)A ( i ) represents nodeiiadjacency list of i , i.e. node iiA set or linked list of all arcs of i

For undirected graphs , A ( i ) A(i)A ( i ) means nodeiiset of adjacent edges of i

The entire adjacency list of the graph can also be represented by an array of pointers. For example: (The first pointer array in the figure below represents, 1 → 2 1\to21The weight of 2 is 8, 1 → 3 1\to313 has a weight of 9)



java implementation: AdjList.java

Adjacency List Complexity



Compared with the complexity of the adjacency matrix, the adjacency list has obvious advantages in terms of space complexity and time complexity of finding adjacent nodes, but the time complexity of its mapping and the time complexity of judging whether two points are adjacent larger .

The key to reducing these two complexities is to quickly see duplicate edges / quickly determine whether two points are adjacent , so we can use a hash table O ( 1 ) O(1)O ( 1 ) or red-black treeO ( log ⁡ V ) O(\log V)O(logV ) instead of linked list(corresponding to HashSet, TreeSet, LinkedList in java respectively)

java implementation: Graph.java (using red-black tree)

3.3 Comparison: Matrices vs Two Tables



Therefore, we finally choose the adjacency list (TreeSet) as the expression form of the graph (of course, HashSet can also be used)

Guess you like

Origin blog.csdn.net/weixin_43728138/article/details/118937568