aboutsummaryrefslogtreecommitdiffstats
path: root/test/monniaux/glpk-4.65/src/amd/amd_postorder.c
blob: a3ece915cfd816eb9e7a7c4658fb99b2c47fb3ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* ========================================================================= */
/* === AMD_postorder ======================================================= */
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
/* AMD, Copyright (c) Timothy A. Davis,                                      */
/* Patrick R. Amestoy, and Iain S. Duff.  See ../README.txt for License.     */
/* email: davis at cise.ufl.edu    CISE Department, Univ. of Florida.        */
/* web: http://www.cise.ufl.edu/research/sparse/amd                          */
/* ------------------------------------------------------------------------- */

/* Perform a postordering (via depth-first search) of an assembly tree. */

#include "amd_internal.h"

GLOBAL void AMD_postorder
(
    /* inputs, not modified on output: */
    Int nn,             /* nodes are in the range 0..nn-1 */
    Int Parent [ ],     /* Parent [j] is the parent of j, or EMPTY if root */
    Int Nv [ ],         /* Nv [j] > 0 number of pivots represented by node j,
                         * or zero if j is not a node. */
    Int Fsize [ ],      /* Fsize [j]: size of node j */

    /* output, not defined on input: */
    Int Order [ ],      /* output post-order */

    /* workspaces of size nn: */
    Int Child [ ],
    Int Sibling [ ],
    Int Stack [ ]
)
{
    Int i, j, k, parent, frsize, f, fprev, maxfrsize, bigfprev, bigf, fnext ;

    for (j = 0 ; j < nn ; j++)
    {
        Child [j] = EMPTY ;
        Sibling [j] = EMPTY ;
    }

    /* --------------------------------------------------------------------- */
    /* place the children in link lists - bigger elements tend to be last */
    /* --------------------------------------------------------------------- */

    for (j = nn-1 ; j >= 0 ; j--)
    {
        if (Nv [j] > 0)
        {
            /* this is an element */
            parent = Parent [j] ;
            if (parent != EMPTY)
            {
                /* place the element in link list of the children its parent */
                /* bigger elements will tend to be at the end of the list */
                Sibling [j] = Child [parent] ;
                Child [parent] = j ;
            }
        }
    }

#ifndef NDEBUG
    {
        Int nels, ff, nchild ;
        AMD_DEBUG1 (("\n\n================================ AMD_postorder:\n"));
        nels = 0 ;
        for (j = 0 ; j < nn ; j++)
        {
            if (Nv [j] > 0)
            {
                AMD_DEBUG1 (( ""ID" :  nels "ID" npiv "ID" size "ID
                    " parent "ID" maxfr "ID"\n", j, nels,
                    Nv [j], Fsize [j], Parent [j], Fsize [j])) ;
                /* this is an element */
                /* dump the link list of children */
                nchild = 0 ;
                AMD_DEBUG1 (("    Children: ")) ;
                for (ff = Child [j] ; ff != EMPTY ; ff = Sibling [ff])
                {
                    AMD_DEBUG1 ((ID" ", ff)) ;
                    ASSERT (Parent [ff] == j) ;
                    nchild++ ;
                    ASSERT (nchild < nn) ;
                }
                AMD_DEBUG1 (("\n")) ;
                parent = Parent [j] ;
                if (parent != EMPTY)
                {
                    ASSERT (Nv [parent] > 0) ;
                }
                nels++ ;
            }
        }
    }
    AMD_DEBUG1 (("\n\nGo through the children of each node, and put\n"
                 "the biggest child last in each list:\n")) ;
#endif

    /* --------------------------------------------------------------------- */
    /* place the largest child last in the list of children for each node */
    /* --------------------------------------------------------------------- */

    for (i = 0 ; i < nn ; i++)
    {
        if (Nv [i] > 0 && Child [i] != EMPTY)
        {

#ifndef NDEBUG
            Int nchild ;
            AMD_DEBUG1 (("Before partial sort, element "ID"\n", i)) ;
            nchild = 0 ;
            for (f = Child [i] ; f != EMPTY ; f = Sibling [f])
            {
                ASSERT (f >= 0 && f < nn) ;
                AMD_DEBUG1 (("      f: "ID"  size: "ID"\n", f, Fsize [f])) ;
                nchild++ ;
                ASSERT (nchild <= nn) ;
            }
#endif

            /* find the biggest element in the child list */
            fprev = EMPTY ;
            maxfrsize = EMPTY ;
            bigfprev = EMPTY ;
            bigf = EMPTY ;
            for (f = Child [i] ; f != EMPTY ; f = Sibling [f])
            {
                ASSERT (f >= 0 && f < nn) ;
                frsize = Fsize [f] ;
                if (frsize >= maxfrsize)
                {
                    /* this is the biggest seen so far */
                    maxfrsize = frsize ;
                    bigfprev = fprev ;
                    bigf = f ;
                }
                fprev = f ;
            }
            ASSERT (bigf != EMPTY) ;

            fnext = Sibling [bigf] ;

            AMD_DEBUG1 (("bigf "ID" maxfrsize "ID" bigfprev "ID" fnext "ID
                " fprev " ID"\n", bigf, maxfrsize, bigfprev, fnext, fprev)) ;

            if (fnext != EMPTY)
            {
                /* if fnext is EMPTY then bigf is already at the end of list */

                if (bigfprev == EMPTY)
                {
                    /* delete bigf from the element of the list */
                    Child [i] = fnext ;
                }
                else
                {
                    /* delete bigf from the middle of the list */
                    Sibling [bigfprev] = fnext ;
                }

                /* put bigf at the end of the list */
                Sibling [bigf] = EMPTY ;
                ASSERT (Child [i] != EMPTY) ;
                ASSERT (fprev != bigf) ;
                ASSERT (fprev != EMPTY) ;
                Sibling [fprev] = bigf ;
            }

#ifndef NDEBUG
            AMD_DEBUG1 (("After partial sort, element "ID"\n", i)) ;
            for (f = Child [i] ; f != EMPTY ; f = Sibling [f])
            {
                ASSERT (f >= 0 && f < nn) ;
                AMD_DEBUG1 (("        "ID"  "ID"\n", f, Fsize [f])) ;
                ASSERT (Nv [f] > 0) ;
                nchild-- ;
            }
            ASSERT (nchild == 0) ;
#endif

        }
    }

    /* --------------------------------------------------------------------- */
    /* postorder the assembly tree */
    /* --------------------------------------------------------------------- */

    for (i = 0 ; i < nn ; i++)
    {
        Order [i] = EMPTY ;
    }

    k = 0 ;

    for (i = 0 ; i < nn ; i++)
    {
        if (Parent [i] == EMPTY && Nv [i] > 0)
        {
            AMD_DEBUG1 (("Root of assembly tree "ID"\n", i)) ;
            k = AMD_post_tree (i, k, Child, Sibling, Order, Stack
#ifndef NDEBUG
                , nn
#endif
                ) ;
        }
    }
}