amd_postorder.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* ========================================================================= */
  2. /* === AMD_postorder ======================================================= */
  3. /* ========================================================================= */
  4. /* ------------------------------------------------------------------------- */
  5. /* AMD, Copyright (c) Timothy A. Davis, */
  6. /* Patrick R. Amestoy, and Iain S. Duff. See ../README.txt for License. */
  7. /* email: davis at cise.ufl.edu CISE Department, Univ. of Florida. */
  8. /* web: http://www.cise.ufl.edu/research/sparse/amd */
  9. /* ------------------------------------------------------------------------- */
  10. /* Perform a postordering (via depth-first search) of an assembly tree. */
  11. #include "amd_internal.h"
  12. GLOBAL void AMD_postorder
  13. (
  14. /* inputs, not modified on output: */
  15. Int nn, /* nodes are in the range 0..nn-1 */
  16. Int Parent [ ], /* Parent [j] is the parent of j, or EMPTY if root */
  17. Int Nv [ ], /* Nv [j] > 0 number of pivots represented by node j,
  18. * or zero if j is not a node. */
  19. Int Fsize [ ], /* Fsize [j]: size of node j */
  20. /* output, not defined on input: */
  21. Int Order [ ], /* output post-order */
  22. /* workspaces of size nn: */
  23. Int Child [ ],
  24. Int Sibling [ ],
  25. Int Stack [ ]
  26. )
  27. {
  28. Int i, j, k, parent, frsize, f, fprev, maxfrsize, bigfprev, bigf, fnext ;
  29. for (j = 0 ; j < nn ; j++)
  30. {
  31. Child [j] = EMPTY ;
  32. Sibling [j] = EMPTY ;
  33. }
  34. /* --------------------------------------------------------------------- */
  35. /* place the children in link lists - bigger elements tend to be last */
  36. /* --------------------------------------------------------------------- */
  37. for (j = nn-1 ; j >= 0 ; j--)
  38. {
  39. if (Nv [j] > 0)
  40. {
  41. /* this is an element */
  42. parent = Parent [j] ;
  43. if (parent != EMPTY)
  44. {
  45. /* place the element in link list of the children its parent */
  46. /* bigger elements will tend to be at the end of the list */
  47. Sibling [j] = Child [parent] ;
  48. Child [parent] = j ;
  49. }
  50. }
  51. }
  52. #ifndef NDEBUG
  53. {
  54. Int nels, ff, nchild ;
  55. AMD_DEBUG1 (("\n\n================================ AMD_postorder:\n"));
  56. nels = 0 ;
  57. for (j = 0 ; j < nn ; j++)
  58. {
  59. if (Nv [j] > 0)
  60. {
  61. AMD_DEBUG1 (( ""ID" : nels "ID" npiv "ID" size "ID
  62. " parent "ID" maxfr "ID"\n", j, nels,
  63. Nv [j], Fsize [j], Parent [j], Fsize [j])) ;
  64. /* this is an element */
  65. /* dump the link list of children */
  66. nchild = 0 ;
  67. AMD_DEBUG1 ((" Children: ")) ;
  68. for (ff = Child [j] ; ff != EMPTY ; ff = Sibling [ff])
  69. {
  70. AMD_DEBUG1 ((ID" ", ff)) ;
  71. ASSERT (Parent [ff] == j) ;
  72. nchild++ ;
  73. ASSERT (nchild < nn) ;
  74. }
  75. AMD_DEBUG1 (("\n")) ;
  76. parent = Parent [j] ;
  77. if (parent != EMPTY)
  78. {
  79. ASSERT (Nv [parent] > 0) ;
  80. }
  81. nels++ ;
  82. }
  83. }
  84. }
  85. AMD_DEBUG1 (("\n\nGo through the children of each node, and put\n"
  86. "the biggest child last in each list:\n")) ;
  87. #endif
  88. /* --------------------------------------------------------------------- */
  89. /* place the largest child last in the list of children for each node */
  90. /* --------------------------------------------------------------------- */
  91. for (i = 0 ; i < nn ; i++)
  92. {
  93. if (Nv [i] > 0 && Child [i] != EMPTY)
  94. {
  95. #ifndef NDEBUG
  96. Int nchild ;
  97. AMD_DEBUG1 (("Before partial sort, element "ID"\n", i)) ;
  98. nchild = 0 ;
  99. for (f = Child [i] ; f != EMPTY ; f = Sibling [f])
  100. {
  101. ASSERT (f >= 0 && f < nn) ;
  102. AMD_DEBUG1 ((" f: "ID" size: "ID"\n", f, Fsize [f])) ;
  103. nchild++ ;
  104. ASSERT (nchild <= nn) ;
  105. }
  106. #endif
  107. /* find the biggest element in the child list */
  108. fprev = EMPTY ;
  109. maxfrsize = EMPTY ;
  110. bigfprev = EMPTY ;
  111. bigf = EMPTY ;
  112. for (f = Child [i] ; f != EMPTY ; f = Sibling [f])
  113. {
  114. ASSERT (f >= 0 && f < nn) ;
  115. frsize = Fsize [f] ;
  116. if (frsize >= maxfrsize)
  117. {
  118. /* this is the biggest seen so far */
  119. maxfrsize = frsize ;
  120. bigfprev = fprev ;
  121. bigf = f ;
  122. }
  123. fprev = f ;
  124. }
  125. ASSERT (bigf != EMPTY) ;
  126. fnext = Sibling [bigf] ;
  127. AMD_DEBUG1 (("bigf "ID" maxfrsize "ID" bigfprev "ID" fnext "ID
  128. " fprev " ID"\n", bigf, maxfrsize, bigfprev, fnext, fprev)) ;
  129. if (fnext != EMPTY)
  130. {
  131. /* if fnext is EMPTY then bigf is already at the end of list */
  132. if (bigfprev == EMPTY)
  133. {
  134. /* delete bigf from the element of the list */
  135. Child [i] = fnext ;
  136. }
  137. else
  138. {
  139. /* delete bigf from the middle of the list */
  140. Sibling [bigfprev] = fnext ;
  141. }
  142. /* put bigf at the end of the list */
  143. Sibling [bigf] = EMPTY ;
  144. ASSERT (Child [i] != EMPTY) ;
  145. ASSERT (fprev != bigf) ;
  146. ASSERT (fprev != EMPTY) ;
  147. Sibling [fprev] = bigf ;
  148. }
  149. #ifndef NDEBUG
  150. AMD_DEBUG1 (("After partial sort, element "ID"\n", i)) ;
  151. for (f = Child [i] ; f != EMPTY ; f = Sibling [f])
  152. {
  153. ASSERT (f >= 0 && f < nn) ;
  154. AMD_DEBUG1 ((" "ID" "ID"\n", f, Fsize [f])) ;
  155. ASSERT (Nv [f] > 0) ;
  156. nchild-- ;
  157. }
  158. ASSERT (nchild == 0) ;
  159. #endif
  160. }
  161. }
  162. /* --------------------------------------------------------------------- */
  163. /* postorder the assembly tree */
  164. /* --------------------------------------------------------------------- */
  165. for (i = 0 ; i < nn ; i++)
  166. {
  167. Order [i] = EMPTY ;
  168. }
  169. k = 0 ;
  170. for (i = 0 ; i < nn ; i++)
  171. {
  172. if (Parent [i] == EMPTY && Nv [i] > 0)
  173. {
  174. AMD_DEBUG1 (("Root of assembly tree "ID"\n", i)) ;
  175. k = AMD_post_tree (i, k, Child, Sibling, Order, Stack
  176. #ifndef NDEBUG
  177. , nn
  178. #endif
  179. ) ;
  180. }
  181. }
  182. }