Learning data structure (9)-graph

0. Definition of graph

A graph is a data structure composed of a set of vertices and a set of vertex relations:, Graph=(V,E)where the set of vertices V is a finite non-empty set, which contains data elements; E is a finite set of edges, and Path(x,y) means from A one-way path from vertex x to vertex y, the path has a direction, and the graph includes a directed graph and an undirected graph. (1) If there is a pair of vertices <e, v> \ epsilon E, it represents an edge of vertices u and v, and the graph is an undirected graph. (2) If <e, v> \ epsilon E, represents an arc from u to v, and u is the end of the arc, and v is the head of the arc, the graph at this time is called a directed graph. If there are \frac{n(n-1)}{2}edges or arcs, it is called a complete graph.

Sometimes numbers are attached to the edges or arcs of the graph to indicate related quantities. The numbers related to the edges or arcs of the graph are called weights, and weights indicate the distance between vertices. Weighted graphs are called nets. If the vertex and fixed-point relationship of a graph is contained by another graph, the graph is a subgraph of the larger graph. The edge related to the vertex in the undirected graph is called the degree, the edge in the directed graph with the vertex as the arc head is called the in-degree, and the dimension tail is called the out-of-degree. Edge or arc = \frac{1}{2}\sum_{i=1}^{n}TD (the i-th vertex). The edge or arc from one vertex to another is called a path. The number of edges or arcs is called the number of paths. The vertex and the end point are the same as a loop or a loop. Except for the end point (starting point), it is called a simple loop or a simple loop. If there is a path from one point to another, the two points are said to be connected. If any two points are connected, the graph is called a connected graph, and the connected component refers to the maximal connected subgraph in the undirected graph.

1. Storage method

(1) Representation of adjacency matrix

The adjacency matrix of the undirected graph must be a real symmetric matrix. For nets, there are

A.Edge[i][j]=\left\{\begin{matrix} w_{i,j} (v_{i},v_{j}\epsilon E) \\ 0 i=j \\ \infty else \end{matrix}\right.

(2) Representation of adjacency list

The adjacency list is a combination of sequential storage and chain storage.

Each vertex in the graph is linked to its neighbors into a singly linked list: the vertex is the head node, and each node in the linked list represents an edge (edge ​​node), and the node value in the linked list (that is, the value in the data field) ) Is the sequence number of the node adjacent to the head node in the table, and the pointer of the head node points to the first adjacent node (table node).

(3) Cross-linked list representation

The cross-linked list is another storage structure of the directed graph. The purpose is to represent the same arc that appears twice in the adjacency list and the inverse adjacency list of the directed graph with a node, because in the adjacency list and the inverse adjacency list The vertex data of is the same, it only needs to appear once in the cross-linked list, but the pointers to the first "out of arc" and the first "in of arc" need to be kept respectively. (1) The head node (VexNode) includes the data field (store vertex information) the arc pointer field (pointing to the first arc with a vertex as the end of the arc) and the arc pointer field (pointing to the head of the first arc pointing to a certain point). Vertex arc).
(2) The arc node (ArcBox) includes four fields: the two data fields respectively represent the two vertex numbers on which the arc depends, and the two pointer fields link the next arc related to a vertex and the next and The arc associated with this vertex.
(3) The head node sequence constitutes the data of the cross-linked list (OLGraph class).

Similar to the cross-linked list of a directed graph, if two nodes representing the same edge in an undirected graph are combined, several edge-linked lists are merged to obtain another representation method of the undirected graph-adjacent multiple tables .

2. Graph traversal

Depth-first search is similar to tree root traversal, which is a generalization of tree root traversal. Depth-first search is a process of continuous exploration and backtracking.

The breadth-first search of the graph is similar to the hierarchical traversal of the tree.

Guess you like

Origin blog.csdn.net/qq_35789421/article/details/113802081