Algorithm Express (15) Graph "minimum spanning tree" and "Shortest Path"

Source: Internet
Author: User

Today is the end of the big picture, the last thing of "figure", "minimum spanning tree" and "Shortest path".

One: Minimum spanning tree

1. The concept

First look at the following figure, do not know what you can sum up.

For a connected graph G, if all its vertices and portions of the edges constitute a subgraph G1, when G1 satisfies:

① just connects all the vertices in the graph. ② vertices do not exist loops. The G1 is called the "spanning tree" of G.

In fact, a summary of the sentence is: the spanning tree is the entire vertex of the original image with the smallest edge connected to the subgraph, which does not, such as the next connected graph can get the following two spanning trees.

② for a connected graph with weights, when the resulting tree is different, the weights on each side are different, and if the weight of a spanning tree is minimal, it is the "minimum spanning tree".

2. Scene

The actual application of the "minimum spanning tree" is quite practical value, textbooks have such a sentence, if the diagram represents a traffic system, each vertex represents a city,

The side represents the distance between two cities, when there are N cities, there may be N (n-1)/2 edge, then how to choose (n-1) edge to make the total distance between the city is the smallest, in fact it

The abstract model is to find the "minimum spanning tree" problem.

3. Prim algorithm

Of course how to seek the "minimum spanning tree" problem, predecessors have been summed up for us, we just divert is,

The first step: we build the set "V,u", all the vertices in the graph are poured into the V set, and the U set is initially empty.

Step two: We put the V1 in the U collection and mark the V1 vertex as visited. At this point: U (V1).

Step three: We look for the V1 adjacency Point (V2,V3,V5), the weights found (v1,v2) between the smallest weights, at this time we will V2 into the U set and Mark V2 as visited,

At this time is U (V1,V2).

Fourth step: We find the V1 of the U set and the adjacent edge of the V2, after a spasm, found (V1,V5) The weight of the smallest, at this time the V5 added to the U set merge marked as visited, at this time

The set elements of U are (V1,v2, V5).

Fifth step: At this time, we take (V1,V2,V5) as the datum to look around for the minimum weight of the adjacent edge, found (V5, V4) The weight of the smallest, at this time, add V4 to the U set merge Mark

is accessed, at which point U's collection element is (V1,V2,V5, V4).

Sixth step: As in the fifth step form, found (v1,v3) The weight of the smallest, the V3 added to the U set and marked as visited, the final U element is (v1,v2,v5,v4,v3),

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.