The basic concept of data structure graph

Definition of a picture

  Definition: A graph is composed of a finite non-empty set of vertices and a set of edges between vertices, usually expressed as: G(V,E) , where G represents a graph and V is the set of vertices in graph G , E is the set of edges in the graph G.

  It should be noted in the figure that:

  (1) In the linear table, we call data elements elements, in trees, data elements are called nodes, and in graph data elements, we call them vertices .

  (2) A linear table can have no elements, which is called an empty table; a tree can have no nodes, which is called an empty tree; however, no vertices are allowed in the graph (finite non-empty).

  (3) Each element in the linear table is a linear relationship, each element in the tree is a hierarchical relationship, and the relationship of each vertex in the graph is represented by an edge (the edge set can be empty).

The basic concept of two pictures

  (1) Undirected graph

  If the edge between any two vertices in a graph is an undirected edge (in short, an edge with no direction), the graph is called Undirected graphs.

  (2) Directed graph

  If the edge between any two vertices in a graph is a directed edge (in short, an edge with a direction), the graph is called a directed graph (Directed graphs).

  (3) Complete map

  ① Undirected complete graph: In an undirected graph, if there is an edge between any two vertices, the graph is called an undirected complete graph. (An undirected complete graph with n vertices has (n×(n-1))/2 edges) as shown in the following figure:

  ② Directed complete graph: In a directed graph, if there are two arcs with opposite directions between any two vertices, the graph is called a directed complete graph. (A directed complete graph with n vertices has n×(n-1) edges) as shown in the following figure:

PS: When a graph is close to a complete graph, it is called a dense graph (Dense Graph), and when a graph contains fewer edges, it is called a sparse graph (Spare Graph).

  (4) Degree of vertex

  The degree of a vertex Vi refers to the number of edges associated with Vi in the graph. For a directed graph, there are In-degree and Out-degree. The degree of a vertex of a directed graph is equal to the sum of the in-degree and out-degree of the vertex.

  (5) Adjacency

  ①If there is an edge (V1, V2) between the two vertices V1 and V2 in the undirected graph, then the vertices V1 and V2 are called adjacent (Adjacent);

  ②If there is an edge <V3, V2> in the directed graph, then vertex V3 is said to be adjacent to vertex V2, and V3 is adjacent to V2 or V2 is adjacent to V3 ;

PS: Edges in undirected graphs are represented by parentheses "()", while edges in directed graphs are represented by angle brackets "<>".

  (6) Path

  In an undirected graph, if there is a set of edges from vertex Vi to vertex Vj, the vertex sequence from vertex Vi to vertex Vj is called a path from vertex Vi to vertex Vj.

  (7) Connectivity

  A vertex Vi and a vertex Vj are said to be connected if there is a passable path from Vi to Vj.

  (8) Right

  Some graph edges or arcs have numbers associated with them, which are called weights.

  Some graph edges or arcs have numbers associated with them, which are called weights.

Third, the storage structure of the graph

In addition to storing the information of each vertex in the graph, the storage structure of the graph also needs to store the relationship between the vertices. Therefore, the structure of the graph is also complicated. Commonly used graph storage structures include adjacency matrix and adjacency list.

2.1 Adjacency matrix notation

  The adjacency matrix of the graph is stored in two arrays to represent the graph . A one-dimensional array stores information about vertices in the graph, and a two-dimensional array (called an adjacency matrix) stores information about edges or arcs in the graph.

  (1) Undirected graph:

  We can set two arrays, the vertex array is vertex[4]={v0,v1,v2,v3}, and the edge array arc[4][4] is a matrix like the right side of the above figure. For the values ​​of the main diagonal of the matrix, that is, arc[0][0], arc[1][1], arc[2][2], arc[3][3], all 0 are because they do not exist The edge of the vertex.

  (2) Directed graph:

  Let's look at another example of a directed graph, as shown on the left side of the figure below. The vertex array is vertex[4]={v0,v1,v2,v3}, and the arc array arc[4][4] is a matrix like the right side of the figure below. The value on the main diagonal is still 0. But because it is a directed graph, this matrix is ​​not asymmetric. For example, there is an arc from v1 to v0, and arc[1][0]=1, but there is no arc from v to v, so arc[0][1]=0 .

Disadvantage: Since a graph with n vertices requires n*n array elements for storage, when the graph is a sparse graph, using the adjacency matrix storage method will result in a large number of 0 elements, which will cause a great waste of space . In this case, consider using the adjacency list notation to store the data in the graph.

2.2 Adjacency list notation

  First of all, recalling what we talked about in the linear table, the sequential storage structure has the problem that pre-allocating memory may cause a waste of storage space, so the structure of chain storage is introduced. Similarly, we can also consider using chained storage for edges or arcs to avoid the problem of wasting space.

  The adjacency list consists of a header node and a table node . Each vertex in the graph corresponds to a header node stored in an array. If the vertex corresponding to the header node has adjacent nodes, the adjacent nodes are stored in sequence in the singly linked list pointed to by the header node.

  (1) Undirected graph : The following figure shows the adjacency list structure of an undirected graph.

  From the above figure, we know that each node of the vertex table is represented by two fields, data and firstedge, data is the data field, which stores the information of the vertex, and firstedge is the pointer field, which points to the first node of the edge table, that is, this vertex the first adjacent point of . The edge table node consists of two fields, adjvex and next. adjvex is the adjacency point field, which stores the subscript of the adjacency point of a vertex in the vertex table, and next stores the pointer to the next node in the edge table. For example, if the v1 vertex is adjacent to v0 and v2, in the edge table of v1, adjvex is 0 of v0 and 2 of v2 respectively.

PS: For undirected graphs, data redundancy will also occur when using adjacency lists for storage. For example, in the above figure, there is a colleague pointing to vertex V3 in the linked list pointed to by vertex V0, and there will also be a vertex pointing to V0 in the linked list pointed to by vertex V3.

  (2) Directed graph : If it is a directed graph, the adjacency list structure is similar, but it should be noted that the directed graph has a direction. Therefore, the adjacency list of a directed graph is divided into an outgoing edge list and an incoming edge list (also known as an inverse adjacency list). The table node of the edge table stores a vertex that points to the head node, as shown in the following figure.

  (3) Weighted graph : For a weighted network graph, you can add another weight data field to the edge table node definition, and store the weight information, as shown in the following figure.

Note: For more content, you can view the original blog: http://www.cnblogs.com/edisonchou/p/4672188.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324608985&siteId=291194637