Amesos Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
amesos_amd_l_postorder.c
Go to the documentation of this file.
1 /* ========================================================================= */
2 /* === AMD_postorder ======================================================= */
3 /* ========================================================================= */
4 
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 /* ------------------------------------------------------------------------- */
11 
12 /* Perform a postordering (via depth-first search) of an assembly tree. */
13 
14 /* This file should make the long int version of AMD */
15 #define DLONG 1
16 
17 #include "amesos_amd_internal.h"
18 
20 (
21  /* inputs, not modified on output: */
22  Int nn, /* nodes are in the range 0..nn-1 */
23  Int Parent [ ], /* Parent [j] is the parent of j, or EMPTY if root */
24  Int Nv [ ], /* Nv [j] > 0 number of pivots represented by node j,
25  * or zero if j is not a node. */
26  Int Fsize [ ], /* Fsize [j]: size of node j */
27 
28  /* output, not defined on input: */
29  Int Order [ ], /* output post-order */
30 
31  /* workspaces of size nn: */
32  Int Child [ ],
33  Int Sibling [ ],
34  Int Stack [ ]
35 )
36 {
37  Int i, j, k, parent, frsize, f, fprev, maxfrsize, bigfprev, bigf, fnext ;
38 
39  for (j = 0 ; j < nn ; j++)
40  {
41  Child [j] = EMPTY ;
42  Sibling [j] = EMPTY ;
43  }
44 
45  /* --------------------------------------------------------------------- */
46  /* place the children in link lists - bigger elements tend to be last */
47  /* --------------------------------------------------------------------- */
48 
49  for (j = nn-1 ; j >= 0 ; j--)
50  {
51  if (Nv [j] > 0)
52  {
53  /* this is an element */
54  parent = Parent [j] ;
55  if (parent != EMPTY)
56  {
57  /* place the element in link list of the children its parent */
58  /* bigger elements will tend to be at the end of the list */
59  Sibling [j] = Child [parent] ;
60  Child [parent] = j ;
61  }
62  }
63  }
64 
65 #ifndef NDEBUG
66  {
67  Int nels, ff, nchild ;
68  AMD_DEBUG1 (("\n\n================================ AMD_postorder:\n"));
69  nels = 0 ;
70  for (j = 0 ; j < nn ; j++)
71  {
72  if (Nv [j] > 0)
73  {
74  AMD_DEBUG1 (( ""ID" : nels "ID" npiv "ID" size "ID
75  " parent "ID" maxfr "ID"\n", j, nels,
76  Nv [j], Fsize [j], Parent [j], Fsize [j])) ;
77  /* this is an element */
78  /* dump the link list of children */
79  nchild = 0 ;
80  AMD_DEBUG1 ((" Children: ")) ;
81  for (ff = Child [j] ; ff != EMPTY ; ff = Sibling [ff])
82  {
83  AMD_DEBUG1 ((ID" ", ff)) ;
84  ASSERT (Parent [ff] == j) ;
85  nchild++ ;
86  ASSERT (nchild < nn) ;
87  }
88  AMD_DEBUG1 (("\n")) ;
89  parent = Parent [j] ;
90  if (parent != EMPTY)
91  {
92  ASSERT (Nv [parent] > 0) ;
93  }
94  nels++ ;
95  }
96  }
97  }
98  AMD_DEBUG1 (("\n\nGo through the children of each node, and put\n"
99  "the biggest child last in each list:\n")) ;
100 #endif
101 
102  /* --------------------------------------------------------------------- */
103  /* place the largest child last in the list of children for each node */
104  /* --------------------------------------------------------------------- */
105 
106  for (i = 0 ; i < nn ; i++)
107  {
108  if (Nv [i] > 0 && Child [i] != EMPTY)
109  {
110 
111 #ifndef NDEBUG
112  Int nchild ;
113  AMD_DEBUG1 (("Before partial sort, element "ID"\n", i)) ;
114  nchild = 0 ;
115  for (f = Child [i] ; f != EMPTY ; f = Sibling [f])
116  {
117  ASSERT (f >= 0 && f < nn) ;
118  AMD_DEBUG1 ((" f: "ID" size: "ID"\n", f, Fsize [f])) ;
119  nchild++ ;
120  ASSERT (nchild <= nn) ;
121  }
122 #endif
123 
124  /* find the biggest element in the child list */
125  fprev = EMPTY ;
126  maxfrsize = EMPTY ;
127  bigfprev = EMPTY ;
128  bigf = EMPTY ;
129  for (f = Child [i] ; f != EMPTY ; f = Sibling [f])
130  {
131  ASSERT (f >= 0 && f < nn) ;
132  frsize = Fsize [f] ;
133  if (frsize >= maxfrsize)
134  {
135  /* this is the biggest seen so far */
136  maxfrsize = frsize ;
137  bigfprev = fprev ;
138  bigf = f ;
139  }
140  fprev = f ;
141  }
142  ASSERT (bigf != EMPTY) ;
143 
144  fnext = Sibling [bigf] ;
145 
146  AMD_DEBUG1 (("bigf "ID" maxfrsize "ID" bigfprev "ID" fnext "ID
147  " fprev " ID"\n", bigf, maxfrsize, bigfprev, fnext, fprev)) ;
148 
149  if (fnext != EMPTY)
150  {
151  /* if fnext is EMPTY then bigf is already at the end of list */
152 
153  if (bigfprev == EMPTY)
154  {
155  /* delete bigf from the element of the list */
156  Child [i] = fnext ;
157  }
158  else
159  {
160  /* delete bigf from the middle of the list */
161  Sibling [bigfprev] = fnext ;
162  }
163 
164  /* put bigf at the end of the list */
165  Sibling [bigf] = EMPTY ;
166  ASSERT (Child [i] != EMPTY) ;
167  ASSERT (fprev != bigf) ;
168  ASSERT (fprev != EMPTY) ;
169  Sibling [fprev] = bigf ;
170  }
171 
172 #ifndef NDEBUG
173  AMD_DEBUG1 (("After partial sort, element "ID"\n", i)) ;
174  for (f = Child [i] ; f != EMPTY ; f = Sibling [f])
175  {
176  ASSERT (f >= 0 && f < nn) ;
177  AMD_DEBUG1 ((" "ID" "ID"\n", f, Fsize [f])) ;
178  ASSERT (Nv [f] > 0) ;
179  nchild-- ;
180  }
181  ASSERT (nchild == 0) ;
182 #endif
183 
184  }
185  }
186 
187  /* --------------------------------------------------------------------- */
188  /* postorder the assembly tree */
189  /* --------------------------------------------------------------------- */
190 
191  for (i = 0 ; i < nn ; i++)
192  {
193  Order [i] = EMPTY ;
194  }
195 
196  k = 0 ;
197 
198  for (i = 0 ; i < nn ; i++)
199  {
200  if (Parent [i] == EMPTY && Nv [i] > 0)
201  {
202  AMD_DEBUG1 (("Root of assembly tree "ID"\n", i)) ;
203  k = AMD_post_tree (i, k, Child, Sibling, Order, Stack
204 #ifndef NDEBUG
205  , nn
206 #endif
207  ) ;
208  }
209  }
210 }
void f()
#define EMPTY
#define GLOBAL
#define AMD_DEBUG1(params)
#define Int
#define ASSERT(expression)
#define NDEBUG
#define AMD_post_tree
#define ID
GLOBAL void AMD_postorder(Int nn, Int Parent[], Int Nv[], Int Fsize[], Int Order[], Int Child[], Int Sibling[], Int Stack[])