$search
00001 #include "cs.h" 00002 /* column counts of LL'=A or LL'=A'A, given parent & post ordering */ 00003 #define HEAD(k,j) (ata ? head [k] : j) 00004 #define NEXT(J) (ata ? next [J] : -1) 00005 static void init_ata (cs *AT, const int *post, int *w, int **head, int **next) 00006 { 00007 int i, k, p, m = AT->n, n = AT->m, *ATp = AT->p, *ATi = AT->i ; 00008 *head = w+4*n, *next = w+5*n+1 ; 00009 for (k = 0 ; k < n ; k++) w [post [k]] = k ; /* invert post */ 00010 for (i = 0 ; i < m ; i++) 00011 { 00012 for (k = n, p = ATp[i] ; p < ATp[i+1] ; p++) k = CS_MIN (k, w [ATi[p]]); 00013 (*next) [i] = (*head) [k] ; /* place row i in linked list k */ 00014 (*head) [k] = i ; 00015 } 00016 } 00017 int *cs_counts (const cs *A, const int *parent, const int *post, int ata) 00018 { 00019 int i, j, k, n, m, J, s, p, q, jleaf, *ATp, *ATi, *maxfirst, *prevleaf, 00020 *ancestor, *head = NULL, *next = NULL, *colcount, *w, *first, *delta ; 00021 cs *AT ; 00022 if (!CS_CSC (A) || !parent || !post) return (NULL) ; /* check inputs */ 00023 m = A->m ; n = A->n ; 00024 s = 4*n + (ata ? (n+m+1) : 0) ; 00025 delta = colcount = cs_malloc (n, sizeof (int)) ; /* allocate result */ 00026 w = cs_malloc (s, sizeof (int)) ; /* get workspace */ 00027 AT = cs_transpose (A, 0) ; /* AT = A' */ 00028 if (!AT || !colcount || !w) return (cs_idone (colcount, AT, w, 0)) ; 00029 ancestor = w ; maxfirst = w+n ; prevleaf = w+2*n ; first = w+3*n ; 00030 for (k = 0 ; k < s ; k++) w [k] = -1 ; /* clear workspace w [0..s-1] */ 00031 for (k = 0 ; k < n ; k++) /* find first [j] */ 00032 { 00033 j = post [k] ; 00034 delta [j] = (first [j] == -1) ? 1 : 0 ; /* delta[j]=1 if j is a leaf */ 00035 for ( ; j != -1 && first [j] == -1 ; j = parent [j]) first [j] = k ; 00036 } 00037 ATp = AT->p ; ATi = AT->i ; 00038 if (ata) init_ata (AT, post, w, &head, &next) ; 00039 for (i = 0 ; i < n ; i++) ancestor [i] = i ; /* each node in its own set */ 00040 for (k = 0 ; k < n ; k++) 00041 { 00042 j = post [k] ; /* j is the kth node in postordered etree */ 00043 if (parent [j] != -1) delta [parent [j]]-- ; /* j is not a root */ 00044 for (J = HEAD (k,j) ; J != -1 ; J = NEXT (J)) /* J=j for LL'=A case */ 00045 { 00046 for (p = ATp [J] ; p < ATp [J+1] ; p++) 00047 { 00048 i = ATi [p] ; 00049 q = cs_leaf (i, j, first, maxfirst, prevleaf, ancestor, &jleaf); 00050 if (jleaf >= 1) delta [j]++ ; /* A(i,j) is in skeleton */ 00051 if (jleaf == 2) delta [q]-- ; /* account for overlap in q */ 00052 } 00053 } 00054 if (parent [j] != -1) ancestor [j] = parent [j] ; 00055 } 00056 for (j = 0 ; j < n ; j++) /* sum up delta's of each child */ 00057 { 00058 if (parent [j] != -1) colcount [parent [j]] += colcount [j] ; 00059 } 00060 return (cs_idone (colcount, AT, w, 1)) ; /* success: free workspace */ 00061 }