[ADT notes] graph (graph)

definition

In fact, it is similar to a tree, but the tree emphasizes the nodes (data), while the graph emphasizes both nodes and edges.

Classification

  • Directed graph (edges have directions)
  • Undirected graph (edges have no direction)

For example, <j,i> and <i,j> are the same edge in an undirected graph. In a directed graph, <j,i> represents an edge from node j to node i. Obviously, <j,i> and <i,j> are not the same edge.

the term

Every time

The number of edges associated with a vertex.

Directed graphs additionally distinguish between in-degree and out-degree.

  • In-degree: the number of edges ending at this vertex
  • Out-degree: the number of edges starting from this vertex
  • Degree: the sum of out-degree and in-degree

The sum of the degrees of all vertices in the graph is twice the number of edges.

complete graph

Every vertex in the graph has a connection with all other vertices, which means that there are two lines between every two nodes in the directed graph, and one back and forth.

A complete graph of order k has k*(k-1)/2 edges

path

A path is a sequence of vertices (since a pair of vertices can represent an edge, then two adjacent vertices of the sequence are an edge, and the entire sequence is multiple edges connected end to end), and the number of edges contained in the sequence is the length of the path .

If the vertices on a path are different except for the start point and the end point (note that it is possible, but not necessarily the same), the path is called a simple path .

connected

In an undirected graph G, two vertices are said to be connected if there is a path between them.

If any two vertices in an undirected graph G are connected, then the graph G is called a connected graph.

There is a bit of a detour here. In fact, it means that no matter how many paths one point goes to another point, it is said that the two are connected.


Connected subgraph:

Connected component : refers to the maximum connected subgraph in the undirected graph G. Therefore, the connected component of a connected graph has only one itself, while the non-connected graph has multiple .


Here we want to rap about maximally connected subgraphs and minimally connected subgraphs:

  • A maximally connected subgraph is a connected component.
  • A minimally connected subgraph uses the least number of edges to achieve all edge connectivity.

It is said that there is a path between two vertices, and the two vertices are said to be connected, but it does not say how many paths there are (extremely, I hope that there are as many paths as possible). In contrast, a maximally connected subgraph is the opposite of a minimally connected subgraph. It should incorporate as many edges as possible into the original graph and make the graph connected.

You can also directly refer to the connectivity of graphs

Quanhe.com

A graph with side weights is called a weighted graph, also called a net.

spanning tree

The minimum spanning tree is actually the abbreviation of the minimum weight spanning tree.

The spanning tree of a connected graph is a minimal connected subgraph containing all the vertices of the graph and n-1 edges that make up the tree.

The reason for emphasizing connected graphs is to prevent some special cases where there are isolated and unconnected vertices

See the basic operation below for the specific algorithm

Difference between minimum spanning tree and shortest path

The minimum spanning tree is only connected and ensures that the sum of edge weights is the least, but it does not guarantee the shortest path between any two points

The problem solved by the shortest path is the shortest path to a single source point , which ensures that for a specified point (the source point) the distance to any other point is the shortest.


Two algorithms are generally mentioned for processing the minimum spanning tree (see below for details):

  • Prim
  • Kruskal

The results of the two methods are the same, but the process is different.

Two algorithms are generally mentioned to deal with the shortest path problem (see below for details):

  • Dijkstra algorithm
  • Floyd algorithm

Among them, the Floyd algorithm can handle the case where the weight is negative

Dijkstra cannot handle the case where the weight is negative, see below for the reason

The Weight of Dijkstra's Shortest Path Algorithm Cannot Be Negative and Its Reasons

storage method

Thinking about it ahead, the implementation of stacks and queues before learning data structures are all implemented around the two structures of arrays and linked lists, and the data structure of the graph is no exception.

The method implemented using an array is called an adjacency matrix storage method, while the method implemented using a linked list is an adjacency list storage method.

Dense graphs are stored with an adjacency matrix

Sparse graphs are stored using adjacency lists

Reason: First of all, it is clear that the adjacency list is a linked list, which means that the storage efficiency itself will be lost. Because a part of the space is used to represent the pointer field, that is, to maintain the relationship between the nodes. 存储效率=有用数据占用空间/总占用空间, non-zero nodes are useful data, the adjacency list only stores non-zero nodes, and the sparse graph has more zero nodes. Dense graphs are similar, with fewer zero nodes, and an adjacency matrix is ​​used.

Adjacency Matrix Storage Method

Adjacency list storage method

basic operations

traverse

Algorithm for Minimum Spanning Tree

kruskal

Select some edges in the original undirected weighted graph to form a new graph (obviously also a tree), the selection principle is:

  • The edge with the smallest total selection weight is added to the resulting new graph
  • If the selected side and the previously selected side will form a ring, discard and select the second smallest weight

Prim

See the Prim algorithm in this article

shortest path

Dijkstra gives Floyd

To be honest, the written narrative is really abstract. My suggestion is to watch the video, and I happened to see this video which is very good:

Watch this B station video

topological sequence

Properties/requirements: For an edge or path <i, j>, there must be i in front of j in the topological sequence

Premise: The graph is a directed graph. Only directed acyclic graphs have topological order, so directed acyclic graphs are also called topological graphs. Directed cyclic graphs can also output topological order, but the output vertex sequence is a part of all vertices of the graph.

The process of finding a topological sequence in a directed graph is called topological sorting

The steps of topological sorting are:

  1. Select a vertex with no predecessor (in-degree 0) from the graph (if there are more than one, choose 1) and output
  2. Remove the vertex and all edges from it in the graph
  3. Then repeat the above steps

(obviously known from 1 above) the topological sequence may not be unique

At this time, I have to mention AOV network (Activity on Vertex Network) and AOE network (Activity On Edge)

AOV and AOE

AOE network

AOV AOE
edge representation sequence event
Vertex representation event state
Does the side have the right no yes

The AOV network has no weights, it just expresses the priority of things . For example, it can be seen from the above picture (ignoring the edge weights), first complete V 1 V_1V1Only then can V 2 V_2V2and V 3 V_3V3Expressed content

The AOE network has a value, indicating that the completion of the side represents the cost corresponding to the event (such as the required time), then for example, from V 1 V_1V1This state to V 2 V_2V2Need to complete time a 1 a_1a1, which took 3 hours, is one possible implication.


In the AOE network, the state ai a_iaiis unlocked if and only if all its predecessor nodes are unlocked

For example, the above picture only has V 2 V_2V2and V 3 V_3V3V 4 V_4 must be completedV4

AOE diagram 1
Look at the timeline diagram below, needless to say, immediately understand the meaning of the critical path (the first line)

Critical Path Timeline Diagram

There can be multiple critical paths

For example, a1+a4+a8+a11 in the above picture is also

critical path method

ve与vl

Obviously for a certain key event athere isve(a)=vl(a)

The definition is this:

ve(v)is the maximum value from the source point xto vall paths, namely

v l ( v ) = v e ( y ) − M A X { c ( p ) } vl(v)=ve(y)-MAX\{c(p)\} vl(v)=ve(y)MAX{ c(p)}

reference

Li Chunbao's "Data Structure Tutorial"
Graph connectivity, connected graphs, connected components, strongly connected components
Talking about what is graph topology sorting
7.1 Definition and basic terms of graphs (this dear friend's data structure column looks okay)

Guess you like

Origin blog.csdn.net/qq_39377889/article/details/124573264