Minimum spanning tree, shortest path tree

Minimum spanning tree

In graph theory, the spanning tree (English: Spanning Tree) of an undirected graph G is a connected subgraph with all vertices of G but the least number of edges. [1]

There may be multiple spanning trees for a graph.

Among the spanning trees with weighted graphs, the one with the smallest total weight is called the minimum spanning tree.

What application does it have in practice? For example, there are N cities that need to establish interconnected communication networks. How to minimize the total length of communication cables that need to be laid? This requires the use of the idea of ​​minimum spanning tree.

Algorithm for obtaining minimum spanning tree:

Principle of Prim algorithm:

1) Start with a certain point and search for all the edges that can be visited at the current point;
2) Find the smallest edge among the edges that have been searched, this edge must have a point that has not been visited yet, add the point that has not been visited to us To record the added edges;
3) Find all edges accessible to the current set and repeat the process of 2 until no new points can be added;
4) At this time, the tree formed by all edges is the minimum spanning tree.

Reference link: https://wiki.jikexueyuan.com/project/step-by-step-learning-algorithm/prim-algorithm1.html

Principle of Kruskal algorithm:

Now we assume that a graph has m nodes and n edges. First, we need to treat m nodes as m independent spanning trees, and arrange the n edges according to the data from small to large. Among n edges, we take out each edge in turn. If we find that the two nodes of the edge are located on two trees, then merge the two trees into one tree; if the two nodes of the tree are located on the same tree On, then ignore this side and continue running. After all edges are traversed, if all spanning trees can be merged into one spanning tree, then it is the minimum spanning tree we need to find, otherwise there is no minimum spanning tree.

Reference link: https://wiki.jikexueyuan.com/project/step-by-step-learning-algorithm/kruskal-algorithm.html

Comparison of the two algorithms

In general, the Prim algorithm uses points as the object and selects the shortest edge connected to the point to form the minimum spanning tree. The Kruskal algorithm uses edges as the object, and constantly adds new shortest edges that do not form a loop to form a minimum spanning tree.
Insert picture description here

Shortest path tree

The shortest path starts from a specified vertex and calculates the shortest path from this vertex to all other vertices. There are three common shortest path algorithms: dijkstra, floyd, Bellman-Ford and SPFA.
The shortest path tree SPT (Short Path Tree) is a tree composed of the shortest path from the source point of the network to all nodes.

How to define a negative number in the side weight? If there is a negative side, there are two cases.
The first case: If starting from a certain point, you can reach a ring with a negative weight sum, then the shortest distance from this point to other points is negative infinity. Obviously, if there is a negative ring, it can be reached from a certain point This negative ring, then I can walk this negative ring infinitely. Each time I walk, the distance is smaller. In this case, we can define the shortest distance from this point to other points as negative infinity.
The second case: If there is no such negative ring, then it is the same as without negative weight edges, but it is not exactly the same. Among the four algorithms we introduce next, some of them can handle negative weight edges but not negative edges. Some of the loops can handle negative loops, and some can neither handle negative edges nor negative loops.

Dijkstra

  • Not applicable to negative weight graphs
  • Only applicable to the single-source shortest path problem.
    If the weight is negative, pass the algorithm directly. The reason is not shown here.

The single-source shortest path refers to the shortest path problem with only one starting point to other points.

Algorithm flow:
  • S1. Set the dis array, dis[i] represents the distance from the starting point start to i.
  • S2. From the point set V, a point with the smallest dis value and no relaxation operation is performed with this point as the starting point.
  • S3. Update the dis array from each point of relaxation and lead connection from this point, return to S2, and proceed in a loop.
  • S2 can be optimized through the operation of the priority queue, which will be described in detail later.

for example. This example is also a template question for the single-source shortest path in Luogu. Ask for the shortest path from 1 to each point?
Insert picture description here
Obviously, if you look at it with the naked eye, the shortest paths from 1 to 0, 1 to 2, 3, and 4 are 2, 4, and 3 respectively. What is the operation process of dijkstra?

First, we first open a dis array, make the value of the array large enough, dis[i]=0x7fffffff, start from 1, let dis[1]=0, find that there are three points 234 connected to 1, then we proceed one by one Relaxation operation, compare if (dis[1]+w[i]<dis[i]), w represents the weight of each side, if it is less than, let it cover its own dis value, that is, dis[i]=dis[ 1]+w[i], after this wave is updated, the values ​​of 234 are 2, 5, and 4 respectively.
Then, we need to let all 234 into the team, and select the number with the smallest dis value, 2 to continue the relaxation operation, and find that the connection is 3 and 4, continue to update, the end of this wave, the value of 234 is 2, 4, 3 respectively.
Next, it is the point 4 with the second lowest dis value in the previous round, and the operation is performed, but there is no edge out of 4, so no operation is performed.
Finally, there is the remaining 3, 3 and 4 have a weight edge, but the smallest dis value of 4 is still 3.

Next, we find that the algorithm ends here, why? Because in the sentence of S2, the point where slack is not carried out, all have entered the queue as early as the first round of 234 and have been ejected, so they will never again To enter the queue, we can set a bool type vis[i] array to represent whether the i-th point has been visited, if it has been visited, end the loop, or enter the queue directly without pushing.

This is the entire dijkstra algorithm.

to sum up:

The Dijkstra algorithm is an excellent classic shortest path algorithm. Under the optimization of the priority queue, its time complexity is also acceptable. It is widely used in computer networks and other applications. However, it also has its limitations. The limitation is even greater than that of the Floyd algorithm. The Floyd algorithm allows negative weight edges and no negative weight loops. Dijkstra algorithm does not even allow negative weight edges, because Dijkstra's correctness is based on the following assumption: the shortest path is not currently found Among the points, the point with the shortest distance from the source point cannot continue to be relaxed. At this time, he must have relaxed to the shortest state. But this assumption does not hold when there are edges with negative weights. Take a classic example: a graph with three points and three edges, 0->1 distance is 2, 0->2 distance is 3, 2->1 distance It is -2. According to the Dijkstra algorithm, we will definitely add node 1 to the point set that has been relaxed for the first time, but this is not correct. 2 is not the shortest path 0->1, but has been relaxed through point 2. The path: 0->2->1 is the shortest path. Because of the existence of negative weight edges, what we think has been slackened, may be slackened in the future. This is the limitation of Dijkstra, but this does not affect its greatness, because in practical applications, graphs without negative weights are more common. But we still need algorithms that can handle negative weights and even negative loops. At this time, the Bellman-Ford algorithm and the SPFA algorithm come in handy.

Floyd

Freud algorithm is a multi-source shortest path algorithm based on dynamic programming .

https://zhuanlan.zhihu.com/p/34922624
https://zhuanlan.zhihu.com/p/150434472
https://zhuanlan.zhihu.com/p/33162490

Guess you like

Origin blog.csdn.net/Anne033/article/details/108703039