1 /* ========================================================================= */
2 /* === AMD_postorder ======================================================= */
3 /* ========================================================================= */
5 /* ------------------------------------------------------------------------- */
6 /* AMD, Copyright (c) Timothy A. Davis, */
7 /* Patrick R. Amestoy, and Iain S. Duff. See ../README.txt for License. */
8 /* email: davis at cise.ufl.edu CISE Department, Univ. of Florida. */
9 /* web: http://www.cise.ufl.edu/research/sparse/amd */
10 /* ------------------------------------------------------------------------- */
12 /* Perform a postordering (via depth-first search) of an assembly tree. */
14 #include "amd_internal.h"
16 GLOBAL void AMD_postorder
18 /* inputs, not modified on output: */
19 Int nn, /* nodes are in the range 0..nn-1 */
20 Int Parent [ ], /* Parent [j] is the parent of j, or EMPTY if root */
21 Int Nv [ ], /* Nv [j] > 0 number of pivots represented by node j,
22 * or zero if j is not a node. */
23 Int Fsize [ ], /* Fsize [j]: size of node j */
25 /* output, not defined on input: */
26 Int Order [ ], /* output post-order */
28 /* workspaces of size nn: */
34 Int i, j, k, parent, frsize, f, fprev, maxfrsize, bigfprev, bigf, fnext ;
36 for (j = 0 ; j < nn ; j++)
42 /* --------------------------------------------------------------------- */
43 /* place the children in link lists - bigger elements tend to be last */
44 /* --------------------------------------------------------------------- */
46 for (j = nn-1 ; j >= 0 ; j--)
50 /* this is an element */
54 /* place the element in link list of the children its parent */
55 /* bigger elements will tend to be at the end of the list */
56 Sibling [j] = Child [parent] ;
64 Int nels, ff, nchild ;
65 AMD_DEBUG1 (("\n\n================================ AMD_postorder:\n"));
67 for (j = 0 ; j < nn ; j++)
71 AMD_DEBUG1 (( ""ID" : nels "ID" npiv "ID" size "ID
72 " parent "ID" maxfr "ID"\n", j, nels,
73 Nv [j], Fsize [j], Parent [j], Fsize [j])) ;
74 /* this is an element */
75 /* dump the link list of children */
77 AMD_DEBUG1 ((" Children: ")) ;
78 for (ff = Child [j] ; ff != EMPTY ; ff = Sibling [ff])
80 AMD_DEBUG1 ((ID" ", ff)) ;
81 ASSERT (Parent [ff] == j) ;
83 ASSERT (nchild < nn) ;
89 ASSERT (Nv [parent] > 0) ;
95 AMD_DEBUG1 (("\n\nGo through the children of each node, and put\n"
96 "the biggest child last in each list:\n")) ;
99 /* --------------------------------------------------------------------- */
100 /* place the largest child last in the list of children for each node */
101 /* --------------------------------------------------------------------- */
103 for (i = 0 ; i < nn ; i++)
105 if (Nv [i] > 0 && Child [i] != EMPTY)
110 AMD_DEBUG1 (("Before partial sort, element "ID"\n", i)) ;
112 for (f = Child [i] ; f != EMPTY ; f = Sibling [f])
114 ASSERT (f >= 0 && f < nn) ;
115 AMD_DEBUG1 ((" f: "ID" size: "ID"\n", f, Fsize [f])) ;
117 ASSERT (nchild <= nn) ;
121 /* find the biggest element in the child list */
126 for (f = Child [i] ; f != EMPTY ; f = Sibling [f])
128 ASSERT (f >= 0 && f < nn) ;
130 if (frsize >= maxfrsize)
132 /* this is the biggest seen so far */
139 ASSERT (bigf != EMPTY) ;
141 fnext = Sibling [bigf] ;
143 AMD_DEBUG1 (("bigf "ID" maxfrsize "ID" bigfprev "ID" fnext "ID
144 " fprev " ID"\n", bigf, maxfrsize, bigfprev, fnext, fprev)) ;
148 /* if fnext is EMPTY then bigf is already at the end of list */
150 if (bigfprev == EMPTY)
152 /* delete bigf from the element of the list */
157 /* delete bigf from the middle of the list */
158 Sibling [bigfprev] = fnext ;
161 /* put bigf at the end of the list */
162 Sibling [bigf] = EMPTY ;
163 ASSERT (Child [i] != EMPTY) ;
164 ASSERT (fprev != bigf) ;
165 ASSERT (fprev != EMPTY) ;
166 Sibling [fprev] = bigf ;
170 AMD_DEBUG1 (("After partial sort, element "ID"\n", i)) ;
171 for (f = Child [i] ; f != EMPTY ; f = Sibling [f])
173 ASSERT (f >= 0 && f < nn) ;
174 AMD_DEBUG1 ((" "ID" "ID"\n", f, Fsize [f])) ;
175 ASSERT (Nv [f] > 0) ;
178 ASSERT (nchild == 0) ;
184 /* --------------------------------------------------------------------- */
185 /* postorder the assembly tree */
186 /* --------------------------------------------------------------------- */
188 for (i = 0 ; i < nn ; i++)
195 for (i = 0 ; i < nn ; i++)
197 if (Parent [i] == EMPTY && Nv [i] > 0)
199 AMD_DEBUG1 (("Root of assembly tree "ID"\n", i)) ;
200 k = AMD_post_tree (i, k, Child, Sibling, Order, Stack