Data structure-6.4 figure
Preface-Data Structure
The data structure needs to be chewed repeatedly, and the answers to the problems encountered in the development can be obtained at any time.
Spanning tree
Figure -> Spanning Tree
-
Depth first search spanning tree breadth first search spanning tree
-
In graph theory, trees are often defined as connected graphs without loops. For example, the following are two connected graphs without loops. At first glance, it does not seem to be a tree, but as long as you select a vertex as the root and orient each edge with a number as the starting point, you can turn them into a normal tree. If there is no circuit diagram, it can be generated as a tree
-
Using depth and breadth first search can become a tree
-
Becomes as follows
Figure->Minimum spanning tree (undirected network)
- Refers to the weighted graph spanning tree in the edge weight and minimum. If the weight on each edge in the spanning tree is the smallest, it is called the minimum spanning tree (prim algorithm and kelusikare algorithm) )
Prim algorithm:
- Kruskal algorithm is suitable for finding the minimum spanning tree of a sparse graph because it is only related to edges
- Take any vertex K as the starting vertex in the graph, set U={k}, W = V (the set of all vertices in the graph)-U (the remaining vertex is the shortest edge, that is, the weight is the smallest and the smallest The edges of the spanning tree are saved). At the beginning, W is all vertices, and the algorithm is completed when W is empty at the end.
- Example: pay attention to the dotted line, ∞
- step1
- step2
- step3 (You can choose 2 or 5 here because the weights of the two sides are the same, and then look again at the root of the graph which weight is the smallest, continue to connect) u = {1,3,6} w = {2,4,5}
- Step last (a total of 6 vertices, 5 edges (because the generated tree) + initialization, a total of six steps)
- step1
Kruskal algorithm
- The prime algorithm is only related to vertices, so it is suitable for finding the minimum spanning tree of a dense graph
- Increase all edges by weight
- Pick the smallest edge that cannot form a loop with the previous edge
- Until n vertices have n-1 edges, the tree at this time is the smallest spanning tree with the smallest weight
- Example: Kruskal's minimum spanning tree process
- The rights are known first
- The rights are known first
example
- The following () algorithm is suitable for forming a minimum spanning tree of a dense graph G
- A Prim
- B Kruskal