2011年8月27日星期六

Prim

...

minimum spanning tree namely a file framework figure is an momentous applying, it requires from a weighted undirected complete diagram, elect n-1 edges and make this figure is still interlocked (namely is to be a spanning trees), but also think the minimum weight tree. In order to win the minimum spanning tree, it is devised a lot of algorithms, most notably the puritanical algorithm and kruskal algorithm. Prim algorithm depicted in textbooks, yet speaks no detailed ample, extra laborious to understand, in order to assist you better comprehend this algorithm, the contents of the writing this was further refined, hope entire of you Help.

directory

algorithm provides the 1st sample of specific data types defined in the following algorithm (Let n be the starting point for the structure) to evaluate the complete code example, suppose that V is the set of graph vertices, E is the edge graph accumulation, TE minimum spanning tree for the set of edges, the prim algorithm via the emulating steps to obtain the minimum spanning tree: 1: Initialization: U = u 0, TE = f. This step is merely set up a knot u 0 of the knot set U and an empty set of edges TE for the initial manner of the minimum spanning tree,www.01626.net, in the subsequent implementation of the algorithm, this form ambition constantly change until you get the minimum spanning tree even now. 2: all u ∈ U, v ∈ V-U edge (u, v) ∈ E, the right to ascertain a minimum edge (u 0, v 0), meantime added to this collection of TE, and this side of the non-U, U of vertices additional. This step function is the set of edges E, find an edge, this edge to encounter the requirements of the following conditions: First, the 2 vertices to every edge in the vertex set U and V-U, the second side of the right to a minimum. After finding this edge, this edge into the edge set TE, and put the U in that the edge is not added to the U of vertices. This step in the algorithm ought be executed multiple periods, once for each execution, set TE and U will change, additional by an edge and a vertex, therefore, TE, and U is the set of two dynamic, which is in understanding the algorithm pay close care. 3: If U = V, then the algorithm ends; otherwise reiterate step 2. This step can be as circulate termination condition. We tin enumerate while U = V, the Step 2 of the implementation of the n-1 periods (Let n be the graph the number of vertices), TE has also increased the n-1 edges, the n-1 edges is the absence to find the minimum spanning tree edge. The algorithm provides a elementary understanding of the idea afterward prim algorithm, here we can look at a characteristic algorithm. In order to be consistent and substances, we are still states: connected web with adjacency phalanx net said that whether there is no edge between 2 vertices, the most permitted weight of the computer, otherwise the corresponding edge weights. First, define the data type type adjmatrix = series [1. . N, 1 .. n] of real; / / define an n * n matrix type adjmatrix, to cache adjacency matrix / / edge = disc appeal, en: 1 .. n; length: real; end; / / define side of the warehouse structure for the edge, which is the side of the starting point of beg, en is the side of the end, length is the edge weight / / treetype = series [1 .. n-1] of edge; / / Define a pedestal type for the edge array type var net: adjmatrix; / / Define a variable of type adjmatrix web, adjacency matrix is ​​placed in the web in the / / tree: treetype; / / define a treetype type of variable tree, tree can be stored in n-1 edge information, including start and end points and weights. In the algorithm after the minimum spanning tree of n-1 edges is placed in the tree in the / / algorithm is as follows (Let n be constructed starting point) program prim (net: adjmatrix; var tree: treetype); / / process first. Parameters The meaning is: net amount parameter passing adjacency matrix, and change parameters minimum spanning tree indicates the storage address / / begin for v: = 1 to n-1 do / / This loop will map the vertex n and the other n-1 vertices to form the n-1 edges stored in the variable tree in / / [tree [v]. beg: = n; tree [v]. en: = v; tree [v]. length: = net [n, v] ] for k: = 1 to n-1 do / / this loop algorithm in the Thought of Step 2, once for each loop,8897.org, TE will join an edge in the algorithm, this edge is stored in a variable boost in tree the k-th ingredient, it can be considered, tree from 1 to k of element stored in the TE and U information. Note: In the algorithm, we prompt the ideas and U TE dynamics, reflected in the algorithm, this dynamic is reflected in changes in the loop variable k. / / [Min: = tree [k]. Length; for j: = k to n-1 do if tree [j]. Length chart [l, j]) and (j in v) and (map [l, j] <> 0) then begin f [j]: = map [l, j]; d [j]: = l; end ; if (f [j] 0) then begin min: = f [j]; minn: = j; end; writeln (d [j]); end; f [ ,],[; s [i, 3]: = min; end; for i: = 1 to 5 do write (s [i, 1], 'to', s [i, 2 ],'=', s [i, 3 ], '->'); readln; end. Further reading: 1

http://hi.baidu.com/kyd_blog

Open Category: the digit of inhibit, teaching, microprocessor, information, algorithm Prim The vt.1. the look solemnly; to ... wore neatly vi.1. appears to me to absolute seriousness

没有评论:

发表评论