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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
|
/*! \file
Copyright (c) 2003, The Regents of the University of California, through
Lawrence Berkeley National Laboratory (subject to receipt of any required
approvals from U.S. Dept. of Energy)
All rights reserved.
The source code is distributed under BSD license, see the file License.txt
at the top-level directory.
*/
/*! @file sp_coletree.c
* \brief Tree layout and computation routines
*
*<pre>
* -- SuperLU routine (version 3.1) --
* Univ. of California Berkeley, Xerox Palo Alto Research Center,
* and Lawrence Berkeley National Lab.
* August 1, 2008
*
* Copyright (c) 1994 by Xerox Corporation. All rights reserved.
*
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY
* EXPRESSED OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
*
* Permission is hereby granted to use or copy this program for any
* purpose, provided the above notices are retained on all copies.
* Permission to modify the code and to distribute modified code is
* granted, provided the above notices are retained, and a notice that
* the code was modified is included with the above copyright notice.
* </pre>
*/
/* Elimination tree computation and layout routines */
#include <stdio.h>
#include <stdlib.h>
#include "slu_ddefs.h"
/*
* Implementation of disjoint set union routines.
* Elements are integers in 0..n-1, and the
* names of the sets themselves are of type int.
*
* Calls are:
* initialize_disjoint_sets (n) initial call.
* s = make_set (i) returns a set containing only i.
* s = link (t, u) returns s = t union u, destroying t and u.
* s = find (i) return name of set containing i.
* finalize_disjoint_sets final call.
*
* This implementation uses path compression but not weighted union.
* See Tarjan's book for details.
* John Gilbert, CMI, 1987.
*
* Implemented path-halving by XSL 07/05/95.
*/
static
int *mxCallocInt(int n)
{
register int i;
int *buf;
buf = (int *) SUPERLU_MALLOC( n * sizeof(int) );
if ( !buf ) {
ABORT("SUPERLU_MALLOC fails for buf in mxCallocInt()");
}
for (i = 0; i < n; i++) buf[i] = 0;
return (buf);
}
static
void initialize_disjoint_sets (
int n,
int **pp
)
{
(*pp) = mxCallocInt(n);
}
static
int make_set (
int i,
int *pp
)
{
pp[i] = i;
return i;
}
static
int link (
int s,
int t,
int *pp
)
{
pp[s] = t;
return t;
}
/* PATH HALVING */
static
int find (
int i,
int *pp
)
{
register int p, gp;
p = pp[i];
gp = pp[p];
while (gp != p) {
pp[i] = gp;
i = gp;
p = pp[i];
gp = pp[p];
}
return (p);
}
#if 0
/* PATH COMPRESSION */
static
int find (
int i
)
{
if (pp[i] != i)
pp[i] = find (pp[i]);
return pp[i];
}
#endif
static
void finalize_disjoint_sets (
int *pp
)
{
SUPERLU_FREE(pp);
}
/*
* Find the elimination tree for A'*A.
* This uses something similar to Liu's algorithm.
* It runs in time O(nz(A)*log n) and does not form A'*A.
*
* Input:
* Sparse matrix A. Numeric values are ignored, so any
* explicit zeros are treated as nonzero.
* Output:
* Integer array of parents representing the elimination
* tree of the symbolic product A'*A. Each vertex is a
* column of A, and nc means a root of the elimination forest.
*
* John R. Gilbert, Xerox, 10 Dec 1990
* Based on code by JRG dated 1987, 1988, and 1990.
*/
/*
* Nonsymmetric elimination tree
*/
int
sp_coletree(
int *acolst, int *acolend, /* column start and end past 1 */
int *arow, /* row indices of A */
int nr, int nc, /* dimension of A */
int *parent /* parent in elim tree */
)
{
int *root; /* root of subtee of etree */
int *firstcol; /* first nonzero col in each row*/
int rset, cset;
int row, col;
int rroot;
int p;
int *pp;
root = mxCallocInt (nc);
initialize_disjoint_sets (nc, &pp);
/* Compute firstcol[row] = first nonzero column in row */
firstcol = mxCallocInt (nr);
for (row = 0; row < nr; firstcol[row++] = nc);
for (col = 0; col < nc; col++)
for (p = acolst[col]; p < acolend[col]; p++) {
row = arow[p];
firstcol[row] = SUPERLU_MIN(firstcol[row], col);
}
/* Compute etree by Liu's algorithm for symmetric matrices,
except use (firstcol[r],c) in place of an edge (r,c) of A.
Thus each row clique in A'*A is replaced by a star
centered at its first vertex, which has the same fill. */
for (col = 0; col < nc; col++) {
cset = make_set (col, pp);
root[cset] = col;
parent[col] = nc; /* Matlab */
for (p = acolst[col]; p < acolend[col]; p++) {
row = firstcol[arow[p]];
if (row >= col) continue;
rset = find (row, pp);
rroot = root[rset];
if (rroot != col) {
parent[rroot] = col;
cset = link (cset, rset, pp);
root[cset] = col;
}
}
}
SUPERLU_FREE (root);
SUPERLU_FREE (firstcol);
finalize_disjoint_sets (pp);
return 0;
}
/*
* q = TreePostorder (n, p);
*
* Postorder a tree.
* Input:
* p is a vector of parent pointers for a forest whose
* vertices are the integers 0 to n-1; p[root]==n.
* Output:
* q is a vector indexed by 0..n-1 such that q[i] is the
* i-th vertex in a postorder numbering of the tree.
*
* ( 2/7/95 modified by X.Li:
* q is a vector indexed by 0:n-1 such that vertex i is the
* q[i]-th vertex in a postorder numbering of the tree.
* That is, this is the inverse of the previous q. )
*
* In the child structure, lower-numbered children are represented
* first, so that a tree which is already numbered in postorder
* will not have its order changed.
*
* Written by John Gilbert, Xerox, 10 Dec 1990.
* Based on code written by John Gilbert at CMI in 1987.
*/
static
/*
* Depth-first search from vertex v.
*/
void etdfs (
int v,
int first_kid[],
int next_kid[],
int post[],
int *postnum
)
{
int w;
for (w = first_kid[v]; w != -1; w = next_kid[w]) {
etdfs (w, first_kid, next_kid, post, postnum);
}
/* post[postnum++] = v; in Matlab */
post[v] = (*postnum)++; /* Modified by X. Li on 08/10/07 */
}
static
/*
* Depth-first search from vertex n. No recursion.
* This routine was contributed by Cédric Doucet, CEDRAT Group, Meylan, France.
*/
void nr_etdfs (int n, int *parent,
int *first_kid, int *next_kid,
int *post, int postnum)
{
int current = n, first, next;
while (postnum != n){
/* no kid for the current node */
first = first_kid[current];
/* no first kid for the current node */
if (first == -1){
/* numbering this node because it has no kid */
post[current] = postnum++;
/* looking for the next kid */
next = next_kid[current];
while (next == -1){
/* no more kids : back to the parent node */
current = parent[current];
/* numbering the parent node */
post[current] = postnum++;
/* get the next kid */
next = next_kid[current];
}
/* stopping criterion */
if (postnum==n+1) return;
/* updating current node */
current = next;
}
/* updating current node */
else {
current = first;
}
}
}
/*
* Post order a tree
*/
int *TreePostorder(
int n,
int *parent
)
{
int *first_kid, *next_kid; /* Linked list of children. */
int *post, postnum;
int v, dad;
/* Allocate storage for working arrays and results */
first_kid = mxCallocInt (n+1);
next_kid = mxCallocInt (n+1);
post = mxCallocInt (n+1);
/* Set up structure describing children */
for (v = 0; v <= n; first_kid[v++] = -1);
for (v = n-1; v >= 0; v--) {
dad = parent[v];
next_kid[v] = first_kid[dad];
first_kid[dad] = v;
}
/* Depth-first search from dummy root vertex #n */
postnum = 0;
#if 0
/* recursion */
etdfs (n, first_kid, next_kid, post, &postnum);
#else
/* no recursion */
nr_etdfs(n, parent, first_kid, next_kid, post, postnum);
#endif
SUPERLU_FREE (first_kid);
SUPERLU_FREE (next_kid);
return post;
}
/*
* p = spsymetree (A);
*
* Find the elimination tree for symmetric matrix A.
* This uses Liu's algorithm, and runs in time O(nz*log n).
*
* Input:
* Square sparse matrix A. No check is made for symmetry;
* elements below and on the diagonal are ignored.
* Numeric values are ignored, so any explicit zeros are
* treated as nonzero.
* Output:
* Integer array of parents representing the etree, with n
* meaning a root of the elimination forest.
* Note:
* This routine uses only the upper triangle, while sparse
* Cholesky (as in spchol.c) uses only the lower. Matlab's
* dense Cholesky uses only the upper. This routine could
* be modified to use the lower triangle either by transposing
* the matrix or by traversing it by rows with auxiliary
* pointer and link arrays.
*
* John R. Gilbert, Xerox, 10 Dec 1990
* Based on code by JRG dated 1987, 1988, and 1990.
* Modified by X.S. Li, November 1999.
*/
/*
* Symmetric elimination tree
*/
int
sp_symetree(
int *acolst, int *acolend, /* column starts and ends past 1 */
int *arow, /* row indices of A */
int n, /* dimension of A */
int *parent /* parent in elim tree */
)
{
int *root; /* root of subtree of etree */
int rset, cset;
int row, col;
int rroot;
int p;
int *pp;
root = mxCallocInt (n);
initialize_disjoint_sets (n, &pp);
for (col = 0; col < n; col++) {
cset = make_set (col, pp);
root[cset] = col;
parent[col] = n; /* Matlab */
for (p = acolst[col]; p < acolend[col]; p++) {
row = arow[p];
if (row >= col) continue;
rset = find (row, pp);
rroot = root[rset];
if (rroot != col) {
parent[rroot] = col;
cset = link (cset, rset, pp);
root[cset] = col;
}
}
}
SUPERLU_FREE (root);
finalize_disjoint_sets (pp);
return 0;
} /* SP_SYMETREE */
|