[Data structure] Minimum spanning tree (Prim algorithm, Prim algorithm, Prim), shortest path (Dijkstra algorithm, Dijkstra algorithm, single-source shortest path)

pre-question

insert image description here
insert image description here

FAQ

insert image description here

1. Basic concepts: the definition and properties of the minimum spanning tree

(1) Definition of Minimal Spanning Tree

  • 生成树的代价:任G ( V , E ) G(V,E)G ( V ,E ) is an undirected connected network graph, andthe sum of the weights of each edgeis calledthe cost of the spanning tree.
  • 最小生成树: in graph GGAmong all spanning trees in G , the spanning tree with the least costisthe minimum spanning tree.

(2) The nature of the minimum spanning tree (MST)

Setting G = ( V , E ) G=(V,E)G=(V,E ) is an undirected connected network graph,UUU is a non-empty subset of the vertex set. If( u , v ) (u,v)(u,v ) is an edge with minimum weight, whereu ∈ U , v ∈ V − U u\in U,v\in VUuU,vVU , then there must be a tree containing edgesu , vu,vu,The minimum spanning tree for v .

insert image description here

2. How to use the MST property to find the minimum spanning tree

  • Find the edge ( u , v ) of minimum weight between two point sets (u,v)(u,v ) , let ( u , v ) (u,v)with minimum weight(u,v ) becomes part of the minimum spanning tree and will be greater than the minimum weight( u , v ) (u,v)(u,v ) delete.

Next, there are two ideas:

  • Starting from one point, adding points at a time to form a point set(Prim算法)
  • Starting from the edge, merge the point sets to avoid forming a cycle(Kruskal算法)

3. Prim algorithm

(1) Prim algorithm idea

Do operations on points, maintain a vertex set A of points in the minimum spanning tree, and a vertex set B of points to be processed, find the shortest edge connecting these two sets each time, and add both vertices to it Set A until all vertices are processed.

Abstract description: (think abstract skip)

insert image description here

(2) The detailed process of Prim algorithm to form the minimum spanning tree

insert image description here
Legend:

  • The red line represents the minimum spanning tree
  • The blue circle represents the set UUU , the set of other vertices isV − U VUVU
  • The blue line represents UUUV sumV − U VUVAdjacent side of U

insert image description here

Calculate the cost between each point in U and its adjacent points, find the point V 5 with the smallest cost, and include V 5 into the U set. Calculate the cost between each point in U and its adjacent points, find out the point V5 with the smallest cost, and include V5 into the U set.Calculate the cost between each point in U and its adjacent points, find out the point V 5 with the smallest cost , and include V 5 into the U set.

insert image description here


















insert image description here


insert image description here
insert image description here


insert image description here


insert image description here

(3) C++ and python implementation of Prim's algorithm

4. Dijkstra algorithm

(1) Connection with Prim algorithm

Dijkstra's algorithm and Prim's algorithm are both shortest path algorithms , which are mainly used to find the shortest path of a graph .

The difference is that Dijkstra's algorithm is suitable for the shortest path from the starting point of a directed graph to other points , while Prim's algorithm is suitable for finding the minimum spanning tree of an undirected graph . Their solution procedures are also slightly different. The Dijkstra algorithm selects the point closest to the starting point each time as a new access point, and updates the shortest distance from other points to the starting point until all points are visited. Prim's algorithm starts from a starting point and continuously selects the point connected to the visited point and has the smallest edge weight until all points on the graph are visited.

(2) Dijkstra algorithm idea

insert image description here
insert image description here

Calculate the path length from point A to every point in the graph, select a shortest path: A->B, and add vertex B to set S.

After adding a shortest path, is there a shorter path from vertex A to other points?

Update the shortest path:
insert image description here


A->C,A->D,A->ESelect the shortest path in: , A->Dand add the D vertices to the S set. Update all shortest paths.
insert image description here


insert image description here


insert image description here

Guess you like

Origin blog.csdn.net/verse_armour/article/details/128982805