• Nenhum resultado encontrado

CompletePrim algorithm

C.6 OUTPUT tab

4.4 CompletePrim algorithm

4.4. Implementation of Prim’s algorithm

4.4.1 Pseudocode

The pseudocode of our implemenation of Prim’s algorithm for complete geo- metric graphs is listed in Algorithm4.4. One can notice that both the outer and the inner loops executeN iterations whereN is the number of vertices. All the operations inside the loops areO(1). Thus it is immediately clear that this algo- rithm hasO(N2)complexity. Plus, there is no requirement to save all possible edges in memory beforehand, confirming our claims forO(N)space complexity:

aside from a few variables, there are twoN–lengthed arrays (holding vertices and neighbour information.)

Algorithm 4.4:CompletePrim algorithm

CHAPTER 4. IMPLEMENTATION METHODOLOGY

4.5. Implementation of operators

By «connecting to the rest of the tree» we mean that there are at least two other vertices,candb, that are connected toa. In the case ofd, it could not be the starting point of the branch as it connects to two vertices (eanda) making it a middle point of the branch. Thus the starting point ought to havedegree larger than2. On the other hand, the ending vertex of the branch, by being connected to only one other, ought to havedegree equal to1.

Consequently the process of detecting the branches can be narrowed down to calculating the degree of all vertices and marking up those with degree 1.

Measuring&marking

After finding the ending vertices of branches, we can visit the first and follow the edges until a vertex with degree larger than2is found. The indices of all visited vertices but the last, are saved to a list. Of course, if the number of vertices in the branch exceeds the pruning level, then the list is dicarded. Otherwise, we mark all the vertices for cutting.

Then we move on to the next branch (ending vertex) and repeat, until all branches are checked for cutting.

Cutting

After all branches are walked and vertices to be cut are identified, the cut is performed in a trivial way:

1. Create a new list of vertices and edges.

2. Create a mapping between the new list and the old list of vertices, M[i] =j

meaning that thei–th vertex of the old list corresponds to thej–th vertex of the new one.

3. Start from the first vertex of the old list and repeat this: if thei–th vertex was not marked to be cut, then add it to the list and setM[i]equal to the number of previously added vertices

CHAPTER 4. IMPLEMENTATION METHODOLOGY 4. Start from the first edges of the old list of edges and move on: if thei–th edge’s endpoints are both to be kept, then add the edge to the new list of edges.

Though the indices of the endpoint of each appended edge still refer to the old vertex list. Using mappingM transform the indices.

After this process, the cut is complete with edges referring to the updated list of vertices.

4.5.2 Pruning a MSF

We define pruning a minimum spanning forest at a levelp as the result after applying the pruning operator at the same level, on each one of the sub–trees.

Branch–free sub–trees with number of vertices less or equal topare not cut.

OurMSFclass contains a vector of the MSTs, making the above process trivial:

requesting pruning in a for–loop iterating the stored MSTs.

4.5.3 Separating a MST

Our separation implementation works in the opposite direction of pruning: detect the edges to keep and then include only the endpoints of them:

1. Create a new list of vertices and edges. Create an array holding the degrees of the vertices in the separated tree, initialized to0.

2. Iterate through all edges and if the weight of an edge is less than the separation length

• increase its endpoints’ degree by one

• add the edge to the new list of edges

In the end the correct degree of the separated tree will be listed and those vertices not anymore participating in the structure can be recognized by the zero degree.

3. As described in pruning implementation, create a mapping between old and new indices for the vertices, using the zero degree as indicator of which vertices are to be ignored.

4. Now unwanted edges and vertices are cut. Using the above mapping, correct the indices of all the persisting edges to refer to the new list.

The library automates the calculation of some common sample statistics. In the following formulas,xiare the observations in the sample, andN,m,sdenote the size, mean and standard deviation of the sample respectively.

TheNiterms instead ofN, provide the bias correction adjustmentwhen the population parameters are. It is significant whenN is small.

Sample mean

m=

N

X

i=1

xi (4.1)

Sample standard deviation

s=

v u u u u t

PN i=1

(xim)2

N −1 (4.2)

Sample skewness

N

(N −1)(N −2)

PN i=1

(xim)3

s3 (4.3)

Sample kurtosis

N(N + 1)

(N −1)(N −2)(N −3)

N

P

i=1

(xim)4

s4 (4.4)

similar to Bessel’s correction for sample variance and standard deviation

4.7. Statistics

Sample excess kurtosis

N(N + 1)

(N −1)(N −2)(N −3)

PN i=1

(xim)4

s4 − 3(N −1)2

(N −2)(N−3) (4.5) Compensated summation

In computers, representation of floating point numbers is limited. Almost every operation results to arithmetic errors due to rounding, calledround–offerrors. A simple explanation could be given be the following example:

• If we could keep only 5 significant digits, then computing a sum in the form1 +12+13+· · ·+n1 with the naïve approach would take no account of n >10000terms that are less than the accuracy level. Though, a separate addition of those terms would produce a sum that can be added to the first.

• Choosing to start with the smaller numbers would keep the round–offsmall by adding similar in magnitude numbers most of the time.

• The summation of unordered terms is the general case. Sorting the items would be a computationally heavy burden for a summing function.

One of the most commonly used technique is thecompensated summation (Kahan,1971). During the summation of the terms, a separate variable accumu- lates errors, adds them to the total while being smaller in magnitude than the precision of the sum.

Algorithm 4.5:Kahan’s compensated summation