1 /* reorder_phylo.c 2012-08-01 */
3 /* Copyright 2008-2012 Emmanuel Paradis */
5 /* This file is part of the R-package `ape'. */
6 /* See the file ../COPYING for licensing issues. */
10 void neworder_cladewise(int *n, int *edge1, int *edge2,
11 int *N, int *neworder)
12 /* n: nb of tips, N: nb of edges */
14 int i, j, k, node, *done, dn, *node_back, eb;
18 /* done: indicates whether an edge has been collected
19 node_back: the series of node from the root to `node'
20 node: the current node */
22 done = (int*)R_alloc(*N, sizeof(int));
23 node_back = (int*)R_alloc(*N + 2 - *n, sizeof(int));
24 memset(done, 0, *N * sizeof(int));
29 for (i = 0; i < *N; i++) {
30 if (done[i] || edge1[i] != node) continue;
38 /* if found a new node, reset the loop */
42 /* if arrived at the end of `edge', go down one node */
48 #define DO_NODE_PRUNING\
49 /* go back down in `edge' to set `neworder' */\
50 for (j = 0; j <= i; j++) {\
51 /* if find the edge where `node' is */\
52 /* the descendant, make as ready */\
53 if (edge2[j] == node) ready[j] = 1;\
54 if (edge1[j] != node) continue;\
55 neworder[nextI] = j + 1;\
56 ready[j] = 0; /* mark the edge as done */\
60 void neworder_pruningwise(int *ntip, int *nnode, int *edge1,
61 int *edge2, int *nedge, int *neworder)
63 int *ready, *Ndegr, i, j, node, nextI, n;
65 nextI = *ntip + *nnode;
66 Ndegr = (int*)R_alloc(nextI, sizeof(int));
67 memset(Ndegr, 0, nextI*sizeof(int));
68 for (i = 0; i < *nedge; i++) (Ndegr[edge1[i] - 1])++;
70 ready = (int*)R_alloc(*nedge, sizeof(int));
72 /* `ready' indicates whether an edge is ready to be */
73 /* collected; only the terminal edges are initially ready */
74 for (i = 0; i < *nedge; i++)
75 ready[i] = (edge2[i] <= *ntip) ? 1 : 0;
77 /* `n' counts the number of times a node has been seen. */
78 /* This algo will work if the tree is in cladewise order, */
79 /* so that the nodes of "cherries" will be contiguous in `edge'. */
82 while (nextI < *nedge - Ndegr[*ntip]) {
83 for (i = 0; i < *nedge; i++) {
84 if (!ready[i]) continue;
86 /* if found an edge ready, initialize `node' and start counting */
89 } else { /* else counting has already started */
90 if (edge1[i] == node) n++;
92 /* if the node has changed we checked that all edges */
93 /* from `node' have been found */
94 if (n == Ndegr[node - 1]) {
97 /* in all cases reset `n' and `node' and carry on */
101 } /* go to the next edge */
102 /* if at the end of `edge', check that we can't do a node */
103 if (n == Ndegr[node - 1]) {
109 for (i = 0; i < *nedge; i++) {
110 if (!ready[i]) continue;
111 neworder[nextI] = i + 1;