Graph is a nonlinear table data structure.

The elements in the graph are called vertices.
A vertex can establish a connection relationship with any other vertex, which is called edge.
The number of edges connected to a vertex is called the degree of the vertex.

## Undirected graph

A graph whose edges have no direction is called an undirected graph. ## Directed graph

A graph with edges and directions is called a "directed graph". In a directed graph, degrees are divided into in degree and out degree.

The penetration of a vertex indicates how many edges point to the vertex;
The extroversion of a vertex indicates how many edges point to other vertices starting from this vertex.

## Weighted graph

In the weighted graph, each edge has a weight, which can be used to represent the intimacy between QQ friends. ## Storage method

The bottom layer of the adjacency matrix depends on a two-dimensional array,

• Undirected graph
If there is an edge between vertex i and vertex j, we mark A[i][j] and A[j][i] as 1;
• Directed graph
If there is an arrow from vertex i to vertex j pointing to the edge of vertex j, A[i][j] is marked as 1;
If there is an arrow pointing from vertex j to the edge of vertex i, mark A[j][i] as 1;
• In the weighted graph, the corresponding weights are stored in the array. Advantages: simple, intuitive, efficient in obtaining relations and convenient operation

First, in an undirected graph, if A[i][j] is equal to 1, then A[j][i] must also be equal to 1. The upper and lower parts of the diagonal are equal, wasting half of the space;
Secondly, most of the stored matrices are sparse matrices. For example, wechat has hundreds of millions of users, which corresponds to hundreds of millions of vertices on the graph. But most of the matrix is 0.

The structure is similar to a hash table. Each vertex corresponds to a linked list, which stores other vertices connected to this vertex. Similar to an undirected graph, but stored in the linked list of each vertex is the vertex connected to the edge of the vertex.

This method saves space, but it takes more time to use.

If you want to judge whether there is an edge from vertex 2 to vertex 4, you need to traverse the linked list corresponding to vertex 2 to see whether vertex 4 exists in the linked list.

You can change the linked list into red black tree or jump list to increase the search speed.

Take Weibo attention as an example,
Use the adjacency table to find which users a user pays attention to (which other vertices a vertex points to),
However, it is very difficult to know which users pay attention to a user, that is, the user's fan list.

In this case, we need an inverse adjacency table to store the vertices pointing to this vertex. Store the following information: ```
#include<iostream>
using namespace std;
int r;//Adjacency matrix, which is used to store graph information. r[v][u] = 0 indicates that there is no edge (v,u), and R [v] [u] = 1 indicates that there is an edge (v,u);
int main(){
int n;//Number of vertices
int m;//Number of sides
cin>>n>>m;
for(int i=1;i<=m;i++){
int v,u;// (v,u): indicates the edge of V - > u;
cin>>v>>u;//Enter edge information
r[v][u] = 1; //Store side information
}
for(int i=1;i<=n;i++){ // Outputs the vertices i connected to each vertex
cout<<"vertex"<<i<<": ";
for(int j=1;j<=n;j++){
if( r[i][j] == 1){
cout<<j<<' ';
}
}
cout<<endl;
}
return 0;
}
```

```#include<iostream>
using namespace std;
const int MAX = 10005;
int n,m;
int vex[MAX];
typedef struct nodeEdge{ // Edge type
int to;//Point to vertex u
int next;//Points to the position of the previous edge
};
nodeEdge arrPath[2*MAX];

//Construction edge
void creat_graph(int v1,int v2,int curPos){
arrPath[curPos].to = v2;
arrPath[curPos].next = vex[v1];
vex[v1] = curPos;
}

//Output diagram information
void print_Graph(int n){
for(int i=1;i<=n;i++){
cout<<"vertex"<<i<<':';
int index = vex[i];//Gets the number of edges connected to vertex i
while(index!=-1){
cout<<arrPath[index].to<<' ';
index = arrPath[index].next;
}
cout<<endl;
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
vex[i] = -1;
}
for(int i=1;i<=m;i++){
int v1,v2;
cin>>v1>>v2;
creat_graph(v1,v2,i);
}
print_Graph(n);
return 0;
}

```

Input data: in the first row, input the number of vertices n and the number of edges m, and in the next M rows, each line v,u represents a directed edge from vertex v to vertex u;
[input data]
7 12
1 3
1 2
1 4
2 4
2 5
3 6
4 3
4 6
4 7
5 4
5 7
7 6

[output result] Posted on Tue, 21 Sep 2021 01:38:36 -0400 by Sianide