Graph of Data Structures and Algorithms (C++)

Basic Terminology for Graphs

A graph is usually represented by a two-tuple G=<V, E>, where V represents the vertex set and E represents the edge set. |V| represents the number of elements in the vertex set, that is, the number of vertices, and a graph with n vertices is called a graph of order n. |E| represents the number of elements in the edge set, that is, the number of edges.

Notice:Vertex set V and edge set E are both finite sets, where E can be an empty set, V cannot be an empty set,However, in the operation, it may be generated that V is an empty set. A graph in which V is an empty set is called an empty graph, denoted by φ.

1. Undirected graph

If every edge in a graph G has no direction, it is called an undirected graph. Each edge is an unordered pair of two vertices, such as the edge between vertex v1 and vertex v3, denoted as (v1, v3) or (v3, v1),
insert image description here

2. directed graph

If every edge in a graph G is directed, it is called a directed graph. Directed edges are also called arcs. Each arc is an ordered pair composed of two vertices. For example, an arc from vertex v1 to vertex v3 is denoted as <v1, v3>, v1 is called arc tail, and v3 is called arc tail. Arc head
insert image description here
insert image description here
Note: Angle brackets <vi, vj> indicate ordered pairs, and parentheses (vi, vj) indicate unordered pairs.

3. Simple diagram

A graph that contains neither parallel edges nor cycles is called a simple graph, and the above graphs are all simple graphs.

In an undirected graph, if more than one undirected edge is associated with a pair of vertices, these edges are called parallel edges, and the number of parallel edges is called multiplicity, as shown in Figure (a). In a directed graph, if there is more than one directed edge associated with a pair of vertices, and the start and end points of these edges are the same (in the same direction), these edges are called parallel edges, as shown in Figure (b). Self-loop means that the two vertices associated with an edge are the same vertex, that is to say, there is an edge from itself to itself, as shown in Figure (c). A graph with parallel edges is called a multigraph. The number of parallel sides is called the multiplicity.
insert image description here

4. complete graph

In an undirected graph, if any two points have an edge, the graph is called an undirected complete graph. An undirected graph with n vertices, each vertex has edges to other n-1 vertices, a total of n(n-1)/2 edges.
insert image description here
In a directed graph, if any two points have two arcs in opposite directions, the graph is called a directed complete graph. A directed graph with n vertices, each vertex emits n-1 edges and comes in n-1 edges, for a total of n(n-1) edges.
insert image description here

5. Sparse and dense graphs

A graph with few edges or arcs is called a sparse graph, otherwise, it is called a dense graph. This is a very vague concept. It is difficult to say how much is sparse and how much is dense. Generally speaking, if a graph G satisfies |E|<|V|×log|V|, then G is called a sparse graph.

6. network

In practical applications, values ​​such as distance, time, cost, etc. are often marked on the edge, and this value is called the weight of the edge. A weighted graph is called a net
insert image description here

7. Adjacency and Association

Adjacency refers to the relationship between vertices and vertices, and association refers to the relationship between edges and vertices. The relationship between two vertices connected by an edge/arc, such as an undirected edge (vi, vj), then vi and vj are said to be adjacent to each other; a directed edge <vi, vj>, then vi is said to be adjacent to vj, vj is adjacent to vi. If there is (vi,vj) or <vi,vj>, then the edge or arc is said to be associated with vi and vj. In a graph, each edge is associated (attached) to two vertices.
insert image description here

8. degree of vertex

The degree of a vertex refers to the number of edges associated with that vertex, denoted as TD(v).

When calculating the sum of degrees, each edge is counted twice. If, when calculating degrees, a line is drawn for each degree counted, you can see that each edge is counted twice.
insert image description here
In a directed graph, the degrees of vertices are divided into in-degree and out-degree. The in-degree of vertex v is the number of directed edges that end at v, denoted as ID(v), that is, the number of incoming edges. The out-degree of vertex v is the number of directed edges with v as the starting point, denoted as OD(v), that is, the number of outgoing edges. The degree of vertex v is equal to the sum of its in-degree and out-degree, i.e.
TD(v)=ID(v)+OD(v)
insert image description here
For example, in Figure 7-12, vertex v1 has an in-degree of 1 and an out-degree of 3 , the degree is the sum of the in-degree and the out-degree 4, the sum of the in-degree of all vertices is 8, the sum of the out-degree of all vertices is also 8, and the number of edges in the graph is also 8. Sum of in-degrees of all vertices = sum of out-degrees = number of edges.
insert image description here

9. Path, Path Length, and Distance

Path: A sequence of vertices of consecutive edges.

Path Length: The number of edges or arcs on the path.

Distance: The shortest path length from a vertex to another vertex.
For example, in Figure 7-13,
s, v1, v3, t is a path from s to t with a path length of 3;
s, v2, v4, v3, t is also a path from s to t with a path length of 4 ;
There may be many paths between two vertices, and the shortest path length is the distance between the two vertices, such as the distance from s to t is 3.

Note: In a directed graph, the path must follow the direction of the arrow, and an undirected graph can go as long as it has an edge.
insert image description here

10. Loops (Rings), Simple Paths, and Simple Loops

Loop (ring): The first vertex and the last vertex are the same path. In Figure 7-13, v2, v4, v3, v2 are loops.

Simple path: A path with different vertices, except that the start and end points of the path can be the same. In Figure 7-13, s, v2, v4, v3, t are simple paths, and s, v2, v4, v3, v2, v1 are not simple paths.

Simple loop: A path with different vertices except the start and end points of the path are the same. In Figure 7-13, v2, v4, v3, v2 are simple circuits.

11. Subgraphs and Generating Subgraphs

Subgraph: There are two graphs G=(V, E), G1=(V1, E1). If V1⊆V, E1⊆E, then G1 is a subgraph of G. A graph formed by selecting several vertices and several edges from the graph is called a subgraph of the original graph.

Generated subgraph: Select all vertices from the graph, and the graph composed of several edges is called the generated subgraph of the original graph. As shown in Figure 7-14, (b) and (c) are the subgraphs of (a), and (b) is the generated subgraph of (a).The word "generate" means that all vertices are included.
insert image description here

12. Connected Graphs and Connected Components

Connectivity graph: inundirected graph, if there is a path from vertex vi to vj, then vi and vj are said to be connected. If any two vertices in the graph are connected, then G is called a connected graph. For example, Figure 7-14(a) is a connected graph.

Connected component: The maximally connected subgraph of an undirected graph G is called the connected component of G.Maximally connected subgraph means that the subgraph is a connected subgraph of G, and if another vertex is added, the subgraph is not connected.For example, there are 3 connected components in Figure 7-15, as shown in Figure 7-16.
insert image description here

13. Strongly Connected Graphs and Strongly Connected Components

Strongly connected graph: indirected graph, if any two vertices vi to vj in the graph have a path, and vj to vi also has a path, then G is called a strongly connected graph.

Strongly connected component: The maximally strongly connected subgraph of a directed graph G is called the strongly connected component of G.maximally strongly connected subgraphMeaning: the subgraph is a strongly connected subgraph of G. If another vertex is added, the subgraph is no longer strongly connected. As shown in Figure 7-17, (a) is a strongly connected graph, (b) is not a strongly connected graph, and (c) is a strongly connected component of (b).
insert image description here

14. trees and directed trees

From a graph theory perspective, a tree is an acyclic connected graph. A graph with n vertices and m edges is a tree as long as it satisfies one of the following five conditions:

  • G is a connected graph and m=n-1;
  • G is a connected graph and acyclic;
  • G is a connected graph, but if any edge is deleted, it is not connected;
  • G is acyclic graph, but adding any edge will generate a cycle;
  • There is only one simple path between any pair of vertices in G.

Directed tree:A directed graph with only one vertex in-degree 0 and all other vertices having in-degree 1, as shown in Figure 7-18.
insert image description here

15. Spanning Trees and Spanning Forests

minimal connected subgraph: This subgraph is a connected subgraph of G. If any edge is deleted from this subgraph, the subgraph is no longer connected. For example, in Figure 7-19, (b) is a minimally connected subgraph of (a), and (c) is not a minimally connected subgraph of (a).
insert image description here
Spanning tree: A minimal connected subgraph containing all vertices of an undirected graph G.As shown in Figure 7-19(b). Because spanning trees contain all vertices, only connected graphs have spanning trees, and non-connected graphs have a spanning tree for each connected component.

Spanning Forest: For disconnected graphs, a collection of spanning trees for each connected component. For example, for the three connected components in Figure 7-15, each connected component gets a spanning tree, called a spanning forest, as shown in Figure 7-20.
insert image description here

Graph storage structure

The structure of the graph is complex, and there may be a relationship between any two vertices. If usingsequential storage, you need to use a two-dimensional array to represent the relationship between elements, that isAdjacency matrix, you can also use an edge set array to store each edge sequentially. If usingchain storage, then there areadjacency list, cross-linked list and adjacency multi-list and other representation methods. Among them, adjacency matrix and adjacency list are the simplest and most commonly used storage methods.

adjacency matrix

An adjacency matrix is ​​a matrix that represents the relationship between vertices. The adjacency matrix storage method requires a one-dimensional array to store the information of the vertices in the graph, and a two-dimensional array to store the adjacency relationship between the vertices in the graph. The two-dimensional array that stores the adjacency relationship between the vertices is called an adjacency matrix.

(1) Adjacency matrix of undirected graphinsert image description here

insert image description here
insert image description here
The characteristics of an undirected graph adjacency matrix are as follows.

  • 1) The adjacency matrix of an undirected graph is symmetric and unique.
  • 2) The number of non-zero elements in the i-th row or i-th column is exactly the degree of the i-th vertex.
    In the adjacency matrix in Figure 7-23, the number of non-zero elements in the third column is 2, indicating that the degree of the third vertex (c) is 2.

(2) Adjacency matrix of directed graph

insert image description here
For example, for the directed graph shown in Figure 7-24, its vertex information and adjacency matrix are shown in Figure 7-25. In Figure 7-24, a to b have edges, and the storage positions of a and b in the one-dimensional array are 0 and 1, respectively, so M[0][1]=1. A directed graph is a directed edge, a to b has an edge, b to a does not necessarily have an edge, so the adjacency matrix of a directed graph is not necessarily symmetric.
insert image description here
insert image description here
The characteristics of a directed graph adjacency matrix are as follows:

  • 1) The adjacency matrix of a directed graph is not necessarily symmetric.
  • 2)The number of non-zero elements in the i-th row is exactly the out-degree of the i-th vertex, and the number of non-zero elements in the i-th column is exactly the in-degree of the i-th vertex.

In the adjacency matrix shown in Figure 7-25, the number of non-zero elements in the third row is 2, and the number of non-zero elements in the third column is also 2, indicating that the out-degree and in-degree of the third vertex (c) are both 2 .

(3) Adjacency matrix of the network

insert image description here
insert image description here

Data Structure Definition of Adjacency Matrix

insert image description here

How to store adjacency matrix

Algorithm step

1) Enter the number of vertices and edges.
2) Input the vertex information in turn and store it in the vertex array Vex[].
3) Initialize the adjacency matrix, if it is a graph, it is initialized to 0; if it is a network, it is initialized to ∞.
4) Enter the two vertices attached to each edge in turn. If it is a net, you also need to enter the weight of the edge.

  • · If it is an undirected graph, input two vertices a and b, query the storage subscripts i and j of a and b in the vertex array Vex[], and set Edge[i][j]=Edge[j][i ]=1.
  • · If it is a directed graph, input two vertices a and b, query the storage subscripts i and j of a and b in the vertex array Ve x[], and set Edge[i][j]=1.
  • · If it is an undirected network, input two vertices and weights a, b, w, query the storage subscripts i, j of a and b in the vertex array Vex[], and set Edge[i][j]=Edge [j][i]=w.
  • · If it is a directed network, input two vertices and weights a, b, w, query the storage subscripts i, j of a and b in the vertex array Vex[], and set Edge[i][j]=w .

Algorithm diagram

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

Adjacency list pros and cons

Advantages:
insert image description here
Disadvantages:
insert image description here

adjacency list

Adjacency List is a kind of graphChained storage method.The adjacency list consists of two parts: vertices and adjacencies. Vertices include vertex information and a pointer to the first neighbor. The adjacency includes the storage index of the adjacency and a pointer to the next adjacency. All adjacent points of vertex vi form a singly linked list.
insert image description here

(1) Adjacency list of undirected graph

For example, an undirected graph is shown in Figure 7-37, and its adjacency list is shown in Figure 7-38.
insert image description here
The adjacent points of a are b and d, and the storage subscripts of the adjacent points are 1 and 3, which are put into the singly linked list after a according to the head insertion method (reverse order).

The adjacent points of b are a, c, and d, and the storage subscripts of the adjacent points are 0, 2, and 3, and they are placed in the singly linked list behind b.

The adjacent points of c are b and d, and the storage subscripts of the adjacent points are 1 and 3, which are placed in the singly linked list behind c.

The adjacent points of d are a, b, and c, and the storage subscripts of the adjacent points are 0, 1, and 2, and they are placed in the singly linked list after d.

The characteristics of an undirected graph adjacency list are as follows.

  • 1) If the undirected graph has n vertices and e edges, then the vertex table has n nodes, and the adjacency table has 2e nodes.
  • 2) The degree of a vertex is the number of nodes in the singly linked list behind the vertex.

(2) Adjacency list of directed graph (outgoing edge)

For example, a directed graph is shown in Figure 7-39, and its adjacency list is shown in Figure 7-40.
insert image description here
insert image description here
The adjacent points of a (only see the edge, that is, the arc) are b, c, and e, and the storage subscripts of the adjacent points are 1, 2, and 4, and they are placed in the order after a according to the head insertion method (reverse order). in the linked list.

The adjacent point of b is c, the storage subscript of its adjacent point is 2, and it is put into the singly linked list behind b.

The adjacent points of c are d and e, and the storage subscripts of the adjacent points are 3 and 4, which are put into the singly linked list behind c according to the head insertion method.

The adjacent point of d is e, the storage subscript of its adjacent point is 4, and it is put into the singly linked list after d.

e has no adjacent points, and the singly linked list behind it is empty.

Note: For the adjacency point of a vertex in a directed graph, only the outgoing edge (outgoing arc) of the vertex is looked at.

The characteristics of a directed graph adjacency list are as follows

  • 1) If the directed graph has n vertices and e edges, then the vertex table has n nodes, and the adjacency table has e nodes.
  • 2) The out-degree of a vertex is the number of nodes in the singly linked list behind the vertex.

In a directed graph adjacency list, it is easy to find the out-degree of a vertex, butIt is difficult to find the in-degree. You need to traverse all the nodes in the adjacency table to find out how many times the vertex appears, and the in-degree is the number.
As shown in Figure 7-41, the subscript of vertex c is 2, and there are two nodes with 2 in the adjacency list, so the in-degree of c is 2; the subscript of vertex e is 4, and there are two nodes in the adjacency list. 3 nodes of 4, so e has an in-degree of 3.
insert image description here

(3) Inverse Adjacency List of Directed Graph (Incoming Edge)

Sometimes in order to easily obtain the in-degree of a vertex, an inverse adjacency list of a directed graph can be established. The inverse adjacency list of Figure 7-42 is shown in Figure 7-43.
insert image description here
insert image description here
a has no inverse adjacency (only see the incoming edge, that is, the incoming arc), and the singly linked list behind it is empty.

The inverse adjacency of b is a, and its storage subscript is 0, which is placed in the singly linked list behind b.

The inverse adjacent points of c are a and b, and their storage subscripts are 0 and 1. They are put into the singly linked list after c according to the head insertion method.

The inverse adjacency of d is c, and its storage subscript is 2, which is placed in the singly linked list after d.

The inverse adjacent points of e are a, c, and d, and their storage subscripts are 0, 2, and 3, and they are put into the singly linked list after e according to the head insertion method (reverse order).

Note: The inverse adjacency of a vertex of a directed graph only looks at the incoming edge (incoming arc) of the vertex.

The characteristics of a directed graph inverse adjacency list are as follows

  • 1) If the directed graph has n vertices and e edges, then the vertex table has n nodes, and the adjacency table has e nodes.
  • 2) The in-degree of a vertex is the number of nodes in the singly linked list behind the vertex.

Adjacency List Data Structure Definition

The adjacency list uses 2 data structures

  • 1) Vertex nodes, including vertex information and a pointer to the first adjacent point, can be stored in a one-dimensional array.
  • 2) Adjacent point node, including the storage subscript of the adjacent point and the pointer to the next adjacent point. All adjacent points of vertex vi form a singly linked list.

The adjacency node contains the adjacency index and a pointer to the next adjacency, as shown in Figure 7-44. If it is an adjacent point of the network, a weight domain w needs to be added, as shown in Figure 7-45.
insert image description here

Advantages and disadvantages of adjacency lists

insert image description here
insert image description here

cross linked list

Orthogonal List is another chain storage structure for directed graphs. It combines the features of the adjacency list and the inverse adjacency list, and can quickly access the out-arc and in-arc to get the out-degree and in-degree. The cross-linked list also contains two parts: vertex nodes and arc nodes. The vertex node includes vertex information and two pointers (pointing to the first incoming arc and the first outgoing arc respectively), and the arc node includes two data fields (arc tail, arc head) and two pointer fields (pointing to the same arc head respectively) and the same arc tail).

For example, a directed graph is shown in Figure 7-64, and its adjacency table is shown in Figure 7-65.
insert image description here
insert image description here
The arcs of a are ab, ac, and ae, and the storage subscripts corresponding to the arc tail and arc head are 01, 02, and 04. , put the arcs into the singly linked list after a in reverse order.

The arc exit of b is bc, the storage subscript corresponding to the arc head of the arc tail is 12, and the arc exit is put into the singly linked list behind b.

The arc exits of c are cd and ce, and the storage subscripts corresponding to the arc tail and arc head are 23 and 24. The arc exits are put into the singly linked list behind c in reverse order.

The arc exit of d is de, and the storage subscript corresponding to the arc head of the arc tail is 34, and the arc exit is put into the singly linked list after d.

e does not exit the arc, and the arc exit field is empty.
insert image description here
insert image description here

adjacency multilist

An adjacency multilist is another chained storage structure for undirected graphs. The focus of the adjacency list is vertices, while the focus of the adjacency multi-list is the edge, which is suitable for operations such as access marking and edge deletion. The adjacency multilist is similar to the cross-linked list, and also contains two parts: vertex nodes and edge nodes. The vertex node includes vertex information and a pointer (pointing to the first edge attached to the vertex), and the edge node includes two data fields (vertex i, vertex j) and two pointer fields (pointing to the next attached to i, j respectively). one side). If you need to mark whether it has been visited, the edge node can also add a flag field.
insert image description here
insert image description here
insert image description here

I will continue to update it in the future. If you like my articles, please remember to click three times in a row. Like, follow, and collect. Every like, every follow, and every collection will be the infinite motivation for me to move forward! ! ! ↖(▔▽▔)↗Thank you for your support!

Guess you like

Origin blog.csdn.net/qq_44631615/article/details/120385753