Amesos Package Browser (Single Doxygen Collection) Development
amesos_amd_post_tree.c
Go to the documentation of this file.
00001 /* ========================================================================= */
00002 /* === AMD_post_tree ======================================================= */
00003 /* ========================================================================= */
00004 
00005 /* ------------------------------------------------------------------------- */
00006 /* AMD, Copyright (c) Timothy A. Davis,              */
00007 /* Patrick R. Amestoy, and Iain S. Duff.  See ../README.txt for License.     */
00008 /* email: davis at cise.ufl.edu    CISE Department, Univ. of Florida.        */
00009 /* web: http://www.cise.ufl.edu/research/sparse/amd                          */
00010 /* ------------------------------------------------------------------------- */
00011 
00012 /* Post-ordering of a supernodal elimination tree.  */
00013 
00014 #include "amesos_amd_internal.h"
00015 
00016 GLOBAL Int AMD_post_tree
00017 (
00018     Int root,     /* root of the tree */
00019     Int k,      /* start numbering at k */
00020     Int Child [ ],    /* input argument of size nn, undefined on
00021          * output.  Child [i] is the head of a link
00022          * list of all nodes that are children of node
00023          * i in the tree. */
00024     const Int Sibling [ ],  /* input argument of size nn, not modified.
00025          * If f is a node in the link list of the
00026          * children of node i, then Sibling [f] is the
00027          * next child of node i.
00028          */
00029     Int Order [ ],    /* output order, of size nn.  Order [i] = k
00030          * if node i is the kth node of the reordered
00031          * tree. */
00032     Int Stack [ ]   /* workspace of size nn */
00033 #ifndef NDEBUG
00034     , Int nn      /* nodes are in the range 0..nn-1. */
00035 #endif
00036 )
00037 {
00038     Int f, head, h, i ;
00039 
00040 #if 0
00041     /* --------------------------------------------------------------------- */
00042     /* recursive version (Stack [ ] is not used): */
00043     /* --------------------------------------------------------------------- */
00044 
00045     /* this is simple, but can caouse stack overflow if nn is large */
00046     i = root ;
00047     for (f = Child [i] ; f != EMPTY ; f = Sibling [f])
00048     {
00049   k = AMD_post_tree (f, k, Child, Sibling, Order, Stack, nn) ;
00050     }
00051     Order [i] = k++ ;
00052     return (k) ;
00053 #endif
00054 
00055     /* --------------------------------------------------------------------- */
00056     /* non-recursive version, using an explicit stack */
00057     /* --------------------------------------------------------------------- */
00058 
00059     /* push root on the stack */
00060     head = 0 ;
00061     Stack [0] = root ;
00062 
00063     while (head >= 0)
00064     {
00065   /* get head of stack */
00066   ASSERT (head < nn) ;
00067   i = Stack [head] ;
00068   AMD_DEBUG1 (("head of stack "ID" \n", i)) ;
00069   ASSERT (i >= 0 && i < nn) ;
00070 
00071   if (Child [i] != EMPTY)
00072   {
00073       /* the children of i are not yet ordered */
00074       /* push each child onto the stack in reverse order */
00075       /* so that small ones at the head of the list get popped first */
00076       /* and the biggest one at the end of the list gets popped last */
00077       for (f = Child [i] ; f != EMPTY ; f = Sibling [f])
00078       {
00079     head++ ;
00080     ASSERT (head < nn) ;
00081     ASSERT (f >= 0 && f < nn) ;
00082       }
00083       h = head ;
00084       ASSERT (head < nn) ;
00085       for (f = Child [i] ; f != EMPTY ; f = Sibling [f])
00086       {
00087     ASSERT (h > 0) ;
00088     Stack [h--] = f ;
00089     AMD_DEBUG1 (("push "ID" on stack\n", f)) ;
00090     ASSERT (f >= 0 && f < nn) ;
00091       }
00092       ASSERT (Stack [h] == i) ;
00093 
00094       /* delete child list so that i gets ordered next time we see it */
00095       Child [i] = EMPTY ;
00096   }
00097   else
00098   {
00099       /* the children of i (if there were any) are already ordered */
00100       /* remove i from the stack and order it.  Front i is kth front */
00101       head-- ;
00102       AMD_DEBUG1 (("pop "ID" order "ID"\n", i, k)) ;
00103       Order [i] = k++ ;
00104       ASSERT (k <= nn) ;
00105   }
00106 
00107 #ifndef NDEBUG
00108   AMD_DEBUG1 (("\nStack:")) ;
00109   for (h = head ; h >= 0 ; h--)
00110   {
00111       Int j = Stack [h] ;
00112       AMD_DEBUG1 ((" "ID, j)) ;
00113       ASSERT (j >= 0 && j < nn) ;
00114   }
00115   AMD_DEBUG1 (("\n\n")) ;
00116   ASSERT (head < nn) ;
00117 #endif
00118 
00119     }
00120     return (k) ;
00121 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines