BTF/0000750001170100242240000000000011670213540010646 5ustar davissparseBTF/Include/0000750001170100242240000000000011670213371012233 5ustar davissparseBTF/Include/btf.h0000640001170100242240000002754011670212722013167 0ustar davissparse/* ========================================================================== */
/* === BTF package ========================================================== */
/* ========================================================================== */
/* BTF_MAXTRANS: find a column permutation Q to give A*Q a zero-free diagonal
* BTF_STRONGCOMP: find a symmetric permutation P to put P*A*P' into block
* upper triangular form.
* BTF_ORDER: do both of the above (btf_maxtrans then btf_strongcomp).
*
* Copyright (c) 2004-2007. Tim Davis, University of Florida,
* with support from Sandia National Laboratories. All Rights Reserved.
*/
/* ========================================================================== */
/* === BTF_MAXTRANS ========================================================= */
/* ========================================================================== */
/* BTF_MAXTRANS: finds a permutation of the columns of a matrix so that it has a
* zero-free diagonal. The input is an m-by-n sparse matrix in compressed
* column form. The array Ap of size n+1 gives the starting and ending
* positions of the columns in the array Ai. Ap[0] must be zero. The array Ai
* contains the row indices of the nonzeros of the matrix A, and is of size
* Ap[n]. The row indices of column j are located in Ai[Ap[j] ... Ap[j+1]-1].
* Row indices must be in the range 0 to m-1. Duplicate entries may be present
* in any given column. The input matrix is not checked for validity (row
* indices out of the range 0 to m-1 will lead to an undeterminate result -
* possibly a core dump, for example). Row indices in any given column need
* not be in sorted order. However, if they are sorted and the matrix already
* has a zero-free diagonal, then the identity permutation is returned.
*
* The output of btf_maxtrans is an array Match of size n. If row i is matched
* with column j, then A(i,j) is nonzero, and then Match[i] = j. If the matrix
* is structurally nonsingular, all entries in the Match array are unique, and
* Match can be viewed as a column permutation if A is square. That is, column
* k of the original matrix becomes column Match[k] of the permuted matrix. In
* MATLAB, this can be expressed as (for non-structurally singular matrices):
*
* Match = maxtrans (A) ;
* B = A (:, Match) ;
*
* except of course here the A matrix and Match vector are all 0-based (rows
* and columns in the range 0 to n-1), not 1-based (rows/cols in range 1 to n).
* The MATLAB dmperm routine returns a row permutation. See the maxtrans
* mexFunction for more details.
*
* If row i is not matched to any column, then Match[i] is == -1. The
* btf_maxtrans routine returns the number of nonzeros on diagonal of the
* permuted matrix.
*
* In the MATLAB mexFunction interface to btf_maxtrans, 1 is added to the Match
* array to obtain a 1-based permutation. Thus, in MATLAB where A is m-by-n:
*
* q = maxtrans (A) ; % has entries in the range 0:n
* q % a column permutation (only if sprank(A)==n)
* B = A (:, q) ; % permuted matrix (only if sprank(A)==n)
* sum (q > 0) ; % same as "sprank (A)"
*
* This behaviour differs from p = dmperm (A) in MATLAB, which returns the
* matching as p(j)=i if row i and column j are matched, and p(j)=0 if column j
* is unmatched.
*
* p = dmperm (A) ; % has entries in the range 0:m
* p % a row permutation (only if sprank(A)==m)
* B = A (p, :) ; % permuted matrix (only if sprank(A)==m)
* sum (p > 0) ; % definition of sprank (A)
*
* This algorithm is based on the paper "On Algorithms for obtaining a maximum
* transversal" by Iain Duff, ACM Trans. Mathematical Software, vol 7, no. 1,
* pp. 315-330, and "Algorithm 575: Permutations for a zero-free diagonal",
* same issue, pp. 387-390. Algorithm 575 is MC21A in the Harwell Subroutine
* Library. This code is not merely a translation of the Fortran code into C.
* It is a completely new implementation of the basic underlying method (depth
* first search over a subgraph with nodes corresponding to columns matched so
* far, and cheap matching). This code was written with minimal observation of
* the MC21A/B code itself. See comments below for a comparison between the
* maxtrans and MC21A/B codes.
*
* This routine operates on a column-form matrix and produces a column
* permutation. MC21A uses a row-form matrix and produces a row permutation.
* The difference is merely one of convention in the comments and interpretation
* of the inputs and outputs. If you want a row permutation, simply pass a
* compressed-row sparse matrix to this routine and you will get a row
* permutation (just like MC21A). Similarly, you can pass a column-oriented
* matrix to MC21A and it will happily return a column permutation.
*/
#ifndef _BTF_H
#define _BTF_H
/* make it easy for C++ programs to include BTF */
#ifdef __cplusplus
extern "C" {
#endif
#include "UFconfig.h"
int btf_maxtrans /* returns # of columns matched */
(
/* --- input, not modified: --- */
int nrow, /* A is nrow-by-ncol in compressed column form */
int ncol,
int Ap [ ], /* size ncol+1 */
int Ai [ ], /* size nz = Ap [ncol] */
double maxwork, /* maximum amount of work to do is maxwork*nnz(A); no limit
* if <= 0 */
/* --- output, not defined on input --- */
double *work, /* work = -1 if maxwork > 0 and the total work performed
* reached the maximum of maxwork*nnz(A).
* Otherwise, work = the total work performed. */
int Match [ ], /* size nrow. Match [i] = j if column j matched to row i
* (see above for the singular-matrix case) */
/* --- workspace, not defined on input or output --- */
int Work [ ] /* size 5*ncol */
) ;
/* long integer version (all "int" parameters become "UF_long") */
UF_long btf_l_maxtrans (UF_long, UF_long, UF_long *, UF_long *, double,
double *, UF_long *, UF_long *) ;
/* ========================================================================== */
/* === BTF_STRONGCOMP ======================================================= */
/* ========================================================================== */
/* BTF_STRONGCOMP finds the strongly connected components of a graph, returning
* a symmetric permutation. The matrix A must be square, and is provided on
* input in compressed-column form (see BTF_MAXTRANS, above). The diagonal of
* the input matrix A (or A*Q if Q is provided on input) is ignored.
*
* If Q is not NULL on input, then the strongly connected components of A*Q are
* found. Q may be flagged on input, where Q[k] < 0 denotes a flagged column k.
* The permutation is j = BTF_UNFLIP (Q [k]). On output, Q is modified (the
* flags are preserved) so that P*A*Q is in block upper triangular form.
*
* If Q is NULL, then the permutation P is returned so that P*A*P' is in upper
* block triangular form.
*
* The vector R gives the block boundaries, where block b is in rows/columns
* R[b] to R[b+1]-1 of the permuted matrix, and where b ranges from 1 to the
* number of strongly connected components found.
*/
int btf_strongcomp /* return # of strongly connected components */
(
/* input, not modified: */
int n, /* A is n-by-n in compressed column form */
int Ap [ ], /* size n+1 */
int Ai [ ], /* size nz = Ap [n] */
/* optional input, modified (if present) on output: */
int Q [ ], /* size n, input column permutation */
/* output, not defined on input */
int P [ ], /* size n. P [k] = j if row and column j are kth row/col
* in permuted matrix. */
int R [ ], /* size n+1. block b is in rows/cols R[b] ... R[b+1]-1 */
/* workspace, not defined on input or output */
int Work [ ] /* size 4n */
) ;
UF_long btf_l_strongcomp (UF_long, UF_long *, UF_long *, UF_long *, UF_long *,
UF_long *, UF_long *) ;
/* ========================================================================== */
/* === BTF_ORDER ============================================================ */
/* ========================================================================== */
/* BTF_ORDER permutes a square matrix into upper block triangular form. It
* does this by first finding a maximum matching (or perhaps a limited matching
* if the work is limited), via the btf_maxtrans function. If a complete
* matching is not found, BTF_ORDER completes the permutation, but flags the
* columns of P*A*Q to denote which columns are not matched. If the matrix is
* structurally rank deficient, some of the entries on the diagonal of the
* permuted matrix will be zero. BTF_ORDER then calls btf_strongcomp to find
* the strongly-connected components.
*
* On output, P and Q are the row and column permutations, where i = P[k] if
* row i of A is the kth row of P*A*Q, and j = BTF_UNFLIP(Q[k]) if column j of
* A is the kth column of P*A*Q. If Q[k] < 0, then the (k,k)th entry in P*A*Q
* is structurally zero.
*
* The vector R gives the block boundaries, where block b is in rows/columns
* R[b] to R[b+1]-1 of the permuted matrix, and where b ranges from 1 to the
* number of strongly connected components found.
*/
int btf_order /* returns number of blocks found */
(
/* --- input, not modified: --- */
int n, /* A is n-by-n in compressed column form */
int Ap [ ], /* size n+1 */
int Ai [ ], /* size nz = Ap [n] */
double maxwork, /* do at most maxwork*nnz(A) work in the maximum
* transversal; no limit if <= 0 */
/* --- output, not defined on input --- */
double *work, /* return value from btf_maxtrans */
int P [ ], /* size n, row permutation */
int Q [ ], /* size n, column permutation */
int R [ ], /* size n+1. block b is in rows/cols R[b] ... R[b+1]-1 */
int *nmatch, /* # nonzeros on diagonal of P*A*Q */
/* --- workspace, not defined on input or output --- */
int Work [ ] /* size 5n */
) ;
UF_long btf_l_order (UF_long, UF_long *, UF_long *, double , double *,
UF_long *, UF_long *, UF_long *, UF_long *, UF_long *) ;
/* ========================================================================== */
/* === BTF marking of singular columns ====================================== */
/* ========================================================================== */
/* BTF_FLIP is a "negation about -1", and is used to mark an integer j
* that is normally non-negative. BTF_FLIP (-1) is -1. BTF_FLIP of
* a number > -1 is negative, and BTF_FLIP of a number < -1 is positive.
* BTF_FLIP (BTF_FLIP (j)) = j for all integers j. UNFLIP (j) acts
* like an "absolute value" operation, and is always >= -1. You can test
* whether or not an integer j is "flipped" with the BTF_ISFLIPPED (j)
* macro.
*/
#define BTF_FLIP(j) (-(j)-2)
#define BTF_ISFLIPPED(j) ((j) < -1)
#define BTF_UNFLIP(j) ((BTF_ISFLIPPED (j)) ? BTF_FLIP (j) : (j))
/* ========================================================================== */
/* === BTF version ========================================================== */
/* ========================================================================== */
/* All versions of BTF include these definitions.
* As an example, to test if the version you are using is 1.2 or later:
*
* if (BTF_VERSION >= BTF_VERSION_CODE (1,2)) ...
*
* This also works during compile-time:
*
* #if (BTF >= BTF_VERSION_CODE (1,2))
* printf ("This is version 1.2 or later\n") ;
* #else
* printf ("This is an early version\n") ;
* #endif
*/
#define BTF_DATE "Dec 7, 2011"
#define BTF_VERSION_CODE(main,sub) ((main) * 1000 + (sub))
#define BTF_MAIN_VERSION 1
#define BTF_SUB_VERSION 1
#define BTF_SUBSUB_VERSION 3
#define BTF_VERSION BTF_VERSION_CODE(BTF_MAIN_VERSION,BTF_SUB_VERSION)
#ifdef __cplusplus
}
#endif
#endif
BTF/Include/btf_internal.h0000640001170100242240000000257611164175543015074 0ustar davissparse/* ========================================================================== */
/* === btf_internal include file ============================================ */
/* ========================================================================== */
#ifndef _BTF_INTERNAL_H
#define _BTF_INTERNAL_H
/*
* Copyright (c) 2004-2007. Tim Davis, University of Florida,
* with support from Sandia National Laboratories. All Rights Reserved.
*/
/* Not to be included in any user program. */
#ifdef DLONG
#define Int UF_long
#define Int_id UF_long_id
#define BTF(name) btf_l_ ## name
#else
#define Int int
#define Int_id "%d"
#define BTF(name) btf_ ## name
#endif
/* ========================================================================== */
/* make sure debugging and printing is turned off */
#ifndef NDEBUG
#define NDEBUG
#endif
#ifndef NPRINT
#define NPRINT
#endif
/* To enable debugging and assertions, uncomment this line:
#undef NDEBUG
*/
/* To enable diagnostic printing, uncomment this line:
#undef NPRINT
*/
/* ========================================================================== */
#include
#include
#define ASSERT(a) assert(a)
#undef TRUE
#undef FALSE
#undef PRINTF
#undef MIN
#ifndef NPRINT
#define PRINTF(s) { printf s ; } ;
#else
#define PRINTF(s)
#endif
#define TRUE 1
#define FALSE 0
#define EMPTY (-1)
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#endif
BTF/Source/0000750001170100242240000000000011164175543012116 5ustar davissparseBTF/Source/btf_maxtrans.c0000640001170100242240000003711311164175543014760 0ustar davissparse/* ========================================================================== */
/* === BTF_MAXTRANS ========================================================= */
/* ========================================================================== */
/* Finds a column permutation that maximizes the number of entries on the
* diagonal of a sparse matrix. See btf.h for more information.
*
* This function is identical to cs_maxtrans in CSparse, with the following
* exceptions:
*
* (1) cs_maxtrans finds both jmatch and imatch, where jmatch [i] = j and
* imatch [j] = i if row i is matched to column j. This function returns
* just jmatch (the Match array). The MATLAB interface to cs_maxtrans
* (the single-output cs_dmperm) returns imatch, not jmatch to the MATLAB
* caller.
*
* (2) cs_maxtrans includes a pre-pass that counts the number of non-empty
* rows and columns (m2 and n2, respectively), and computes the matching
* using the transpose of A if m2 < n2. cs_maxtrans also returns quickly
* if the diagonal of the matrix is already zero-free. This pre-pass
* allows cs_maxtrans to be much faster than maxtrans, if the use of the
* transpose is warranted.
*
* However, for square structurally non-singular matrices with one or more
* zeros on the diagonal, the pre-pass is a waste of time, and for these
* matrices, maxtrans can be twice as fast as cs_maxtrans. Since the
* maxtrans function is intended primarily for square matrices that are
* typically structurally nonsingular, the pre-pass is not included here.
* If this maxtrans function is used on a matrix with many more columns
* than rows, consider passing the transpose to this function, or use
* cs_maxtrans instead.
*
* (3) cs_maxtrans can operate as a randomized algorithm, to help avoid
* rare cases of excessive run-time.
*
* (4) this maxtrans function includes an option that limits the total work
* performed. If this limit is reached, the maximum transveral might not
* be found.
*
* Thus, for general usage, cs_maxtrans is preferred. For square matrices that
* are typically structurally non-singular, maxtrans is preferred. A partial
* maxtrans can still be very useful when solving a sparse linear system.
*
* Copyright (c) 2004-2007. Tim Davis, University of Florida,
* with support from Sandia National Laboratories. All Rights Reserved.
*/
#include "btf.h"
#include "btf_internal.h"
/* ========================================================================== */
/* === augment ============================================================== */
/* ========================================================================== */
/* Perform a depth-first-search starting at column k, to find an augmenting
* path. An augmenting path is a sequence of row/column pairs (i1,k), (i2,j1),
* (i3,j2), ..., (i(s+1), js), such that all of the following properties hold:
*
* * column k is not matched to any row
* * entries in the path are nonzero
* * the pairs (i1,j1), (i2,j2), (i3,j3) ..., (is,js) have been
* previously matched to each other
* * (i(s+1), js) is nonzero, and row i(s+1) is not matched to any column
*
* Once this path is found, the matching can be changed to the set of pairs
* path. An augmenting path is a sequence of row/column pairs
*
* (i1,k), (i2,j1), (i3,j2), ..., (i(s+1), js)
*
* Once a row is matched with a column it remains matched with some column, but
* not necessarily the column it was first matched with.
*
* In the worst case, this function can examine every nonzero in A. Since it
* is called n times by maxtrans, the total time of maxtrans can be as high as
* O(n*nnz(A)). To limit this work, pass a value of maxwork > 0. Then at
* most O((maxwork+1)*nnz(A)) work will be performed; the maximum matching might
* not be found, however.
*
* This routine is very similar to the dfs routine in klu_kernel.c, in the
* KLU sparse LU factorization package. It is essentially identical to the
* cs_augment routine in CSparse, and its recursive version (augment function
* in cs_maxtransr_mex.c), except that this routine allows for the search to be
* terminated early if too much work is being performed.
*
* The algorithm is based on the paper "On Algorithms for obtaining a maximum
* transversal" by Iain Duff, ACM Trans. Mathematical Software, vol 7, no. 1,
* pp. 315-330, and "Algorithm 575: Permutations for a zero-free diagonal",
* same issue, pp. 387-390. The code here is a new implementation of that
* algorithm, with different data structures and control flow. After writing
* this code, I carefully compared my algorithm with MC21A/B (ACM Algorithm 575)
* Some of the comparisons are partial because I didn't dig deeply into all of
* the details of MC21A/B, such as how the stack is maintained. The following
* arguments are essentially identical between this code and MC21A:
*
* maxtrans MC21A,B
* -------- -------
* n N identical
* k JORD identical
* Ap IP column / row pointers
* Ai ICN row / column indices
* Ap[n] LICN length of index array (# of nonzeros in A)
* Match IPERM output column / row permutation
* nmatch NUMNZ # of nonzeros on diagonal of permuted matrix
* Flag CV mark a node as visited by the depth-first-search
*
* The following are different, but analogous:
*
* Cheap ARP indicates what part of the a column / row has
* already been matched.
*
* The following arguments are very different:
*
* - LENR # of entries in each row/column (unused in maxtrans)
* Pstack OUT Pstack keeps track of where we are in the depth-
* first-search scan of column j. I think that OUT
* plays a similar role in MC21B, but I'm unsure.
* Istack PR keeps track of the rows in the path. PR is a link
* list, though, whereas Istack is a stack. Maxtrans
* does not use any link lists.
* Jstack OUT? PR? the stack for nodes in the path (unsure)
*
* The following control structures are roughly comparable:
*
* maxtrans MC21B
* -------- -----
* for (k = 0 ; k < n ; k++) DO 100 JORD=1,N
* while (head >= 0) DO 70 K=1,JORD
* for (p = Cheap [j] ; ...) DO 20 II=IN1,IN2
* for (p = head ; ...) DO 90 K=1,JORD
*/
static Int augment
(
Int k, /* which stage of the main loop we're in */
Int Ap [ ], /* column pointers, size n+1 */
Int Ai [ ], /* row indices, size nz = Ap [n] */
Int Match [ ], /* size n, Match [i] = j if col j matched to i */
Int Cheap [ ], /* rows Ai [Ap [j] .. Cheap [j]-1] alread matched */
Int Flag [ ], /* Flag [j] = k if j already visited this stage */
Int Istack [ ], /* size n. Row index stack. */
Int Jstack [ ], /* size n. Column index stack. */
Int Pstack [ ], /* size n. Keeps track of position in adjacency list */
double *work, /* work performed by the depth-first-search */
double maxwork /* maximum work allowed */
)
{
/* local variables, but "global" to all DFS levels: */
Int found ; /* true if match found. */
Int head ; /* top of stack */
/* variables that are purely local to any one DFS level: */
Int j2 ; /* the next DFS goes to node j2 */
Int pend ; /* one past the end of the adjacency list for node j */
Int pstart ;
Int quick ;
/* variables that need to be pushed then popped from the stack: */
Int i ; /* the row tentatively matched to i if DFS successful */
Int j ; /* the DFS is at the current node j */
Int p ; /* current index into the adj. list for node j */
/* the variables i, j, and p are stacked in Istack, Jstack, and Pstack */
quick = (maxwork > 0) ;
/* start a DFS to find a match for column k */
found = FALSE ;
i = EMPTY ;
head = 0 ;
Jstack [0] = k ;
ASSERT (Flag [k] != k) ;
while (head >= 0)
{
j = Jstack [head] ;
pend = Ap [j+1] ;
if (Flag [j] != k) /* a node is not yet visited */
{
/* -------------------------------------------------------------- */
/* prework for node j */
/* -------------------------------------------------------------- */
/* first time that j has been visited */
Flag [j] = k ;
/* cheap assignment: find the next unmatched row in col j. This
* loop takes at most O(nnz(A)) time for the sum total of all
* calls to augment. */
for (p = Cheap [j] ; p < pend && !found ; p++)
{
i = Ai [p] ;
found = (Match [i] == EMPTY) ;
}
Cheap [j] = p ;
/* -------------------------------------------------------------- */
/* prepare for DFS */
if (found)
{
/* end of augmenting path, column j matched with row i */
Istack [head] = i ;
break ;
}
/* set Pstack [head] to the first entry in column j to scan */
Pstack [head] = Ap [j] ;
}
/* ------------------------------------------------------------------ */
/* quick return if too much work done */
/* ------------------------------------------------------------------ */
if (quick && *work > maxwork)
{
/* too much work has been performed; abort the search */
return (EMPTY) ;
}
/* ------------------------------------------------------------------ */
/* DFS for nodes adjacent to j */
/* ------------------------------------------------------------------ */
/* If cheap assignment not made, continue the depth-first search. All
* rows in column j are already matched. Add the adjacent nodes to the
* stack by iterating through until finding another non-visited node.
*
* It is the following loop that can force maxtrans to take
* O(n*nnz(A)) time. */
pstart = Pstack [head] ;
for (p = pstart ; p < pend ; p++)
{
i = Ai [p] ;
j2 = Match [i] ;
ASSERT (j2 != EMPTY) ;
if (Flag [j2] != k)
{
/* Node j2 is not yet visited, start a depth-first search on
* node j2. Keep track of where we left off in the scan of adj
* list of node j so we can restart j where we left off. */
Pstack [head] = p + 1 ;
/* Push j2 onto the stack and immediately break so we can
* recurse on node j2. Also keep track of row i which (if this
* search for an augmenting path works) will be matched with the
* current node j. */
Istack [head] = i ;
Jstack [++head] = j2 ;
break ;
}
}
/* ------------------------------------------------------------------ */
/* determine how much work was just performed */
/* ------------------------------------------------------------------ */
*work += (p - pstart + 1) ;
/* ------------------------------------------------------------------ */
/* node j is done, but the postwork is postponed - see below */
/* ------------------------------------------------------------------ */
if (p == pend)
{
/* If all adjacent nodes of j are already visited, pop j from
* stack and continue. We failed to find a match. */
head-- ;
}
}
/* postwork for all nodes j in the stack */
/* unwind the path and make the corresponding matches */
if (found)
{
for (p = head ; p >= 0 ; p--)
{
j = Jstack [p] ;
i = Istack [p] ;
/* -------------------------------------------------------------- */
/* postwork for node j */
/* -------------------------------------------------------------- */
/* if found, match row i with column j */
Match [i] = j ;
}
}
return (found) ;
}
/* ========================================================================== */
/* === maxtrans ============================================================= */
/* ========================================================================== */
Int BTF(maxtrans) /* returns # of columns in the matching */
(
/* --- input --- */
Int nrow, /* A is nrow-by-ncol in compressed column form */
Int ncol,
Int Ap [ ], /* size ncol+1 */
Int Ai [ ], /* size nz = Ap [ncol] */
double maxwork, /* do at most maxwork*nnz(A) work; no limit if <= 0. This
* work limit excludes the O(nnz(A)) cheap-match phase. */
/* --- output --- */
double *work, /* work = -1 if maxwork > 0 and the total work performed
* reached the maximum of maxwork*nnz(A)).
* Otherwise, work = the total work performed. */
Int Match [ ], /* size nrow. Match [i] = j if column j matched to row i */
/* --- workspace --- */
Int Work [ ] /* size 5*ncol */
)
{
Int *Cheap, *Flag, *Istack, *Jstack, *Pstack ;
Int i, j, k, nmatch, work_limit_reached, result ;
/* ---------------------------------------------------------------------- */
/* get workspace and initialize */
/* ---------------------------------------------------------------------- */
Cheap = Work ; Work += ncol ;
Flag = Work ; Work += ncol ;
/* stack for non-recursive depth-first search in augment function */
Istack = Work ; Work += ncol ;
Jstack = Work ; Work += ncol ;
Pstack = Work ;
/* in column j, rows Ai [Ap [j] .. Cheap [j]-1] are known to be matched */
for (j = 0 ; j < ncol ; j++)
{
Cheap [j] = Ap [j] ;
Flag [j] = EMPTY ;
}
/* all rows and columns are currently unmatched */
for (i = 0 ; i < nrow ; i++)
{
Match [i] = EMPTY ;
}
if (maxwork > 0)
{
maxwork *= Ap [ncol] ;
}
*work = 0 ;
/* ---------------------------------------------------------------------- */
/* find a matching row for each column k */
/* ---------------------------------------------------------------------- */
nmatch = 0 ;
work_limit_reached = FALSE ;
for (k = 0 ; k < ncol ; k++)
{
/* find an augmenting path to match some row i to column k */
result = augment (k, Ap, Ai, Match, Cheap, Flag, Istack, Jstack, Pstack,
work, maxwork) ;
if (result == TRUE)
{
/* we found it. Match [i] = k for some row i has been done. */
nmatch++ ;
}
else if (result == EMPTY)
{
/* augment gave up because of too much work, and no match found */
work_limit_reached = TRUE ;
}
}
/* ---------------------------------------------------------------------- */
/* return the Match, and the # of matches made */
/* ---------------------------------------------------------------------- */
/* At this point, row i is matched to j = Match [i] if j >= 0. i is an
* unmatched row if Match [i] == EMPTY. */
if (work_limit_reached)
{
/* return -1 if the work limit of maxwork*nnz(A) was reached */
*work = EMPTY ;
}
return (nmatch) ;
}
BTF/Source/btf_order.c0000640001170100242240000001171611164175543014237 0ustar davissparse/* ========================================================================== */
/* === BTF_ORDER ============================================================ */
/* ========================================================================== */
/* Find a permutation P and Q to permute a square sparse matrix into upper block
* triangular form. A(P,Q) will contain a zero-free diagonal if A has
* structural full-rank. Otherwise, the number of nonzeros on the diagonal of
* A(P,Q) will be maximized, and will equal the structural rank of A.
*
* Q[k] will be "flipped" if a zero-free diagonal was not found. Q[k] will be
* negative, and j = BTF_UNFLIP (Q [k]) gives the corresponding permutation.
*
* R defines the block boundaries of A(P,Q). The kth block consists of rows
* and columns R[k] to R[k+1]-1.
*
* If maxwork > 0 on input, then the work performed in btf_maxtrans is limited
* to maxwork*nnz(A) (excluding the "cheap match" phase, which can take another
* nnz(A) work). On output, the work parameter gives the actual work performed,
* or -1 if the limit was reached. In the latter case, the diagonal of A(P,Q)
* might not be zero-free, and the number of nonzeros on the diagonal of A(P,Q)
* might not be equal to the structural rank.
*
* See btf.h for more details.
*
* Copyright (c) 2004-2007. Tim Davis, University of Florida,
* with support from Sandia National Laboratories. All Rights Reserved.
*/
#include "btf.h"
#include "btf_internal.h"
/* This function only operates on square matrices (either structurally full-
* rank, or structurally rank deficient). */
Int BTF(order) /* returns number of blocks found */
(
/* input, not modified: */
Int n, /* A is n-by-n in compressed column form */
Int Ap [ ], /* size n+1 */
Int Ai [ ], /* size nz = Ap [n] */
double maxwork, /* do at most maxwork*nnz(A) work in the maximum
* transversal; no limit if <= 0 */
/* output, not defined on input */
double *work, /* work performed in maxtrans, or -1 if limit reached */
Int P [ ], /* size n, row permutation */
Int Q [ ], /* size n, column permutation */
Int R [ ], /* size n+1. block b is in rows/cols R[b] ... R[b+1]-1 */
Int *nmatch, /* # nonzeros on diagonal of P*A*Q */
/* workspace, not defined on input or output */
Int Work [ ] /* size 5n */
)
{
Int *Flag ;
Int nblocks, i, j, nbadcol ;
/* ---------------------------------------------------------------------- */
/* compute the maximum matching */
/* ---------------------------------------------------------------------- */
/* if maxwork > 0, then a maximum matching might not be found */
*nmatch = BTF(maxtrans) (n, n, Ap, Ai, maxwork, work, Q, Work) ;
/* ---------------------------------------------------------------------- */
/* complete permutation if the matrix is structurally singular */
/* ---------------------------------------------------------------------- */
/* Since the matrix is square, ensure BTF_UNFLIP(Q[0..n-1]) is a
* permutation of the columns of A so that A has as many nonzeros on the
* diagonal as possible.
*/
if (*nmatch < n)
{
/* get a size-n work array */
Flag = Work + n ;
for (j = 0 ; j < n ; j++)
{
Flag [j] = 0 ;
}
/* flag all matched columns */
for (i = 0 ; i < n ; i++)
{
j = Q [i] ;
if (j != EMPTY)
{
/* row i and column j are matched to each other */
Flag [j] = 1 ;
}
}
/* make a list of all unmatched columns, in Work [0..nbadcol-1] */
nbadcol = 0 ;
for (j = n-1 ; j >= 0 ; j--)
{
if (!Flag [j])
{
/* j is matched to nobody */
Work [nbadcol++] = j ;
}
}
ASSERT (*nmatch + nbadcol == n) ;
/* make an assignment for each unmatched row */
for (i = 0 ; i < n ; i++)
{
if (Q [i] == EMPTY && nbadcol > 0)
{
/* get an unmatched column j */
j = Work [--nbadcol] ;
/* assign j to row i and flag the entry by "flipping" it */
Q [i] = BTF_FLIP (j) ;
}
}
}
/* The permutation of a square matrix can be recovered as follows: Row i is
* matched with column j, where j = BTF_UNFLIP (Q [i]) and where j
* will always be in the valid range 0 to n-1. The entry A(i,j) is zero
* if BTF_ISFLIPPED (Q [i]) is true, and nonzero otherwise. nmatch
* is the number of entries in the Q array that are non-negative. */
/* ---------------------------------------------------------------------- */
/* find the strongly connected components */
/* ---------------------------------------------------------------------- */
nblocks = BTF(strongcomp) (n, Ap, Ai, Q, P, R, Work) ;
return (nblocks) ;
}
BTF/Source/btf_strongcomp.c0000640001170100242240000005714411164175543015324 0ustar davissparse/* ========================================================================== */
/* === BTF_STRONGCOMP ======================================================= */
/* ========================================================================== */
/* Finds the strongly connected components of a graph, or equivalently, permutes
* the matrix into upper block triangular form. See btf.h for more details.
* Input matrix and Q are not checked on input.
*
* Copyright (c) 2004-2007. Tim Davis, University of Florida,
* with support from Sandia National Laboratories. All Rights Reserved.
*/
#include "btf.h"
#include "btf_internal.h"
#define UNVISITED (-2) /* Flag [j] = UNVISITED if node j not visited yet */
#define UNASSIGNED (-1) /* Flag [j] = UNASSIGNED if node j has been visited,
* but not yet assigned to a strongly-connected
* component (aka block). Flag [j] = k (k in the
* range 0 to nblocks-1) if node j has been visited
* (and completed, with its postwork done) and
* assigned to component k. */
/* This file contains two versions of the depth-first-search, a recursive one
* and a non-recursive one. By default, the non-recursive one is used. */
#ifndef RECURSIVE
/* ========================================================================== */
/* === dfs: non-recursive version (default) ================================= */
/* ========================================================================== */
/* Perform a depth-first-search of a graph, stored in an adjacency-list form.
* The row indices of column j (equivalently, the out-adjacency list of node j)
* are stored in Ai [Ap[j] ... Ap[j+1]-1]. Self-edge (diagonal entries) are
* ignored. Ap[0] must be zero, and thus nz = Ap[n] is the number of entries
* in the matrix (or edges in the graph). The row indices in each column need
* not be in any particular order. If an input column permutation is given,
* node j (in the permuted matrix A*Q) is located in
* Ai [Ap[Q[j]] ... Ap[Q[j]+1]-1]. This Q can be the same as the Match array
* output from the maxtrans routine, for a square matrix that is structurally
* full rank.
*
* The algorithm is from the paper by Robert E. Tarjan, "Depth-first search and
* linear graph algorithms," SIAM Journal on Computing, vol. 1, no. 2,
* pp. 146-160, 1972. The time taken by strongcomp is O(nnz(A)).
*
* See also MC13A/B in the Harwell subroutine library (Iain S. Duff and John
* K. Reid, "Algorithm 529: permutations to block triangular form," ACM Trans.
* on Mathematical Software, vol. 4, no. 2, pp. 189-192, 1978, and "An
* implementation of Tarjan's algorithm for the block triangular form of a
* matrix," same journal, pp. 137-147. This code is implements the same
* algorithm as MC13A/B, except that the data structures are very different.
* Also, unlike MC13A/B, the output permutation preserves the natural ordering
* within each block.
*/
static void dfs
(
/* inputs, not modified on output: */
Int j, /* start the DFS at node j */
Int Ap [ ], /* size n+1, column pointers for the matrix A */
Int Ai [ ], /* row indices, size nz = Ap [n] */
Int Q [ ], /* input column permutation */
/* inputs, modified on output (each array is of size n): */
Int Time [ ], /* Time [j] = "time" that node j was first visited */
Int Flag [ ], /* Flag [j]: see above */
Int Low [ ], /* Low [j]: see definition below */
Int *p_nblocks, /* number of blocks (aka strongly-connected-comp.)*/
Int *p_timestamp, /* current "time" */
/* workspace, not defined on input or output: */
Int Cstack [ ], /* size n, output stack to hold nodes of components */
Int Jstack [ ], /* size n, stack for the variable j */
Int Pstack [ ] /* size n, stack for the variable p */
)
{
/* ---------------------------------------------------------------------- */
/* local variables, and initializations */
/* ---------------------------------------------------------------------- */
/* local variables, but "global" to all DFS levels: */
Int chead ; /* top of Cstack */
Int jhead ; /* top of Jstack and Pstack */
/* variables that are purely local to any one DFS level: */
Int i ; /* edge (j,i) considered; i can be next node to traverse */
Int parent ; /* parent of node j in the DFS tree */
Int pend ; /* one past the end of the adjacency list for node j */
Int jj ; /* column j of A*Q is column jj of the input matrix A */
/* variables that need to be pushed then popped from the stack: */
Int p ; /* current index into the adj. list for node j */
/* the variables j and p are stacked in Jstack and Pstack */
/* local copies of variables in the calling routine */
Int nblocks = *p_nblocks ;
Int timestamp = *p_timestamp ;
/* ---------------------------------------------------------------------- */
/* start a DFS at node j (same as the recursive call dfs (EMPTY, j)) */
/* ---------------------------------------------------------------------- */
chead = 0 ; /* component stack is empty */
jhead = 0 ; /* Jstack and Pstack are empty */
Jstack [0] = j ; /* put the first node j on the Jstack */
ASSERT (Flag [j] == UNVISITED) ;
while (jhead >= 0)
{
j = Jstack [jhead] ; /* grab the node j from the top of Jstack */
/* determine which column jj of the A is column j of A*Q */
jj = (Q == (Int *) NULL) ? (j) : (BTF_UNFLIP (Q [j])) ;
pend = Ap [jj+1] ; /* j's row index list ends at Ai [pend-1] */
if (Flag [j] == UNVISITED)
{
/* -------------------------------------------------------------- */
/* prework at node j */
/* -------------------------------------------------------------- */
/* node j is being visited for the first time */
Cstack [++chead] = j ; /* push j onto the stack */
timestamp++ ; /* get a timestamp */
Time [j] = timestamp ; /* give the timestamp to node j */
Low [j] = timestamp ;
Flag [j] = UNASSIGNED ; /* flag node j as visited */
/* -------------------------------------------------------------- */
/* set Pstack [jhead] to the first entry in column j to scan */
/* -------------------------------------------------------------- */
Pstack [jhead] = Ap [jj] ;
}
/* ------------------------------------------------------------------ */
/* DFS rooted at node j (start it, or continue where left off) */
/* ------------------------------------------------------------------ */
for (p = Pstack [jhead] ; p < pend ; p++)
{
i = Ai [p] ; /* examine the edge from node j to node i */
if (Flag [i] == UNVISITED)
{
/* Node i has not been visited - start a DFS at node i.
* Keep track of where we left off in the scan of adjacency list
* of node j so we can restart j where we left off. */
Pstack [jhead] = p + 1 ;
/* Push i onto the stack and immediately break
* so we can recurse on node i. */
Jstack [++jhead] = i ;
ASSERT (Time [i] == EMPTY) ;
ASSERT (Low [i] == EMPTY) ;
/* break here to do what the recursive call dfs (j,i) does */
break ;
}
else if (Flag [i] == UNASSIGNED)
{
/* Node i has been visited, but still unassigned to a block
* this is a back or cross edge if Time [i] < Time [j].
* Note that i might equal j, in which case this code does
* nothing. */
ASSERT (Time [i] > 0) ;
ASSERT (Low [i] > 0) ;
Low [j] = MIN (Low [j], Time [i]) ;
}
}
if (p == pend)
{
/* If all adjacent nodes of j are already visited, pop j from
* Jstack and do the post work for node j. This also pops p
* from the Pstack. */
jhead-- ;
/* -------------------------------------------------------------- */
/* postwork at node j */
/* -------------------------------------------------------------- */
/* determine if node j is the head of a component */
if (Low [j] == Time [j])
{
/* pop all nodes in this SCC from Cstack */
while (TRUE)
{
ASSERT (chead >= 0) ; /* stack not empty (j in it) */
i = Cstack [chead--] ; /* pop a node from the Cstack */
ASSERT (i >= 0) ;
ASSERT (Flag [i] == UNASSIGNED) ;
Flag [i] = nblocks ; /* assign i to current block */
if (i == j) break ; /* current block ends at j */
}
nblocks++ ; /* one more block has been found */
}
/* update Low [parent], if the parent exists */
if (jhead >= 0)
{
parent = Jstack [jhead] ;
Low [parent] = MIN (Low [parent], Low [j]) ;
}
}
}
/* ---------------------------------------------------------------------- */
/* cleanup: update timestamp and nblocks */
/* ---------------------------------------------------------------------- */
*p_timestamp = timestamp ;
*p_nblocks = nblocks ;
}
#else
/* ========================================================================== */
/* === dfs: recursive version (only for illustration) ======================= */
/* ========================================================================== */
/* The following is a recursive version of dfs, which computes identical results
* as the non-recursive dfs. It is included here because it is easier to read.
* Compare the comments in the code below with the identical comments in the
* non-recursive code above, and that will help you see the correlation between
* the two routines.
*
* This routine can cause stack overflow, and is thus not recommended for heavy
* usage, particularly for large matrices. To help in delaying stack overflow,
* global variables are used, reducing the amount of information each call to
* dfs places on the call/return stack (the integers i, j, p, parent, and the
* return address). Note that this means the recursive code is not thread-safe.
* To try this version, compile the code with -DRECURSIVE or include the
* following line at the top of this file:
#define RECURSIVE
*/
static Int /* for recursive illustration only, not for production use */
chead, timestamp, nblocks, n, *Ap, *Ai, *Flag, *Cstack, *Time, *Low,
*P, *R, *Q ;
static void dfs
(
Int parent, /* came from parent node */
Int j /* at node j in the DFS */
)
{
Int p ; /* current index into the adj. list for node j */
Int i ; /* edge (j,i) considered; i can be next node to traverse */
Int jj ; /* column j of A*Q is column jj of the input matrix A */
/* ---------------------------------------------------------------------- */
/* prework at node j */
/* ---------------------------------------------------------------------- */
/* node j is being visited for the first time */
Cstack [++chead] = j ; /* push j onto the stack */
timestamp++ ; /* get a timestamp */
Time [j] = timestamp ; /* give the timestamp to node j */
Low [j] = timestamp ;
Flag [j] = UNASSIGNED ; /* flag node j as visited */
/* ---------------------------------------------------------------------- */
/* DFS rooted at node j */
/* ---------------------------------------------------------------------- */
/* determine which column jj of the A is column j of A*Q */
jj = (Q == (Int *) NULL) ? (j) : (BTF_UNFLIP (Q [j])) ;
for (p = Ap [jj] ; p < Ap [jj+1] ; p++)
{
i = Ai [p] ; /* examine the edge from node j to node i */
if (Flag [i] == UNVISITED)
{
/* Node i has not been visited - start a DFS at node i. */
dfs (j, i) ;
}
else if (Flag [i] == UNASSIGNED)
{
/* Node i has been visited, but still unassigned to a block
* this is a back or cross edge if Time [i] < Time [j].
* Note that i might equal j, in which case this code does
* nothing. */
Low [j] = MIN (Low [j], Time [i]) ;
}
}
/* ---------------------------------------------------------------------- */
/* postwork at node j */
/* ---------------------------------------------------------------------- */
/* determine if node j is the head of a component */
if (Low [j] == Time [j])
{
/* pop all nodes in this strongly connected component from Cstack */
while (TRUE)
{
i = Cstack [chead--] ; /* pop a node from the Cstack */
Flag [i] = nblocks ; /* assign node i to current block */
if (i == j) break ; /* current block ends at node j */
}
nblocks++ ; /* one more block has been found */
}
/* update Low [parent] */
if (parent != EMPTY)
{
/* Note that this could be done with Low[j] = MIN(Low[j],Low[i]) just
* after the dfs (j,i) statement above, and then parent would not have
* to be an input argument. Putting it here places all the postwork
* for node j in one place, thus making the non-recursive DFS easier. */
Low [parent] = MIN (Low [parent], Low [j]) ;
}
}
#endif
/* ========================================================================== */
/* === btf_strongcomp ======================================================= */
/* ========================================================================== */
#ifndef RECURSIVE
Int BTF(strongcomp) /* return # of strongly connected components */
(
/* input, not modified: */
Int n, /* A is n-by-n in compressed column form */
Int Ap [ ], /* size n+1 */
Int Ai [ ], /* size nz = Ap [n] */
/* optional input, modified (if present) on output: */
Int Q [ ], /* size n, input column permutation. The permutation Q can
* include a flag which indicates an unmatched row.
* jold = BTF_UNFLIP (Q [jnew]) is the permutation;
* this function ingnores these flags. On output, it is
* modified according to the permutation P. */
/* output, not defined on input: */
Int P [ ], /* size n. P [k] = j if row and column j are kth row/col
* in permuted matrix. */
Int R [ ], /* size n+1. kth block is in rows/cols R[k] ... R[k+1]-1
* of the permuted matrix. */
/* workspace, not defined on input or output: */
Int Work [ ] /* size 4n */
)
#else
Int BTF(strongcomp) /* recursive version - same as above except for Work size */
(
Int n_in,
Int Ap_in [ ],
Int Ai_in [ ],
Int Q_in [ ],
Int P_in [ ],
Int R_in [ ],
Int Work [ ] /* size 2n */
)
#endif
{
Int j, k, b ;
#ifndef RECURSIVE
Int timestamp, nblocks, *Flag, *Cstack, *Time, *Low, *Jstack, *Pstack ;
#else
n = n_in ;
Ap = Ap_in ;
Ai = Ai_in ;
Q = Q_in ;
P = P_in ;
R = R_in ;
chead = EMPTY ;
#endif
/* ---------------------------------------------------------------------- */
/* get and initialize workspace */
/* ---------------------------------------------------------------------- */
/* timestamp is incremented each time a new node is visited.
*
* Time [j] is the timestamp given to node j.
*
* Low [j] is the lowest timestamp of any node reachable from j via either
* a path to any descendent of j in the DFS tree, or via a single edge to
* an either an ancestor (a back edge) or another node that's neither an
* ancestor nor a descendant (a cross edge). If Low [j] is equal to
* the timestamp of node j (Time [j]), then node j is the "head" of a
* strongly connected component (SCC). That is, it is the first node
* visited in its strongly connected component, and the DFS subtree rooted
* at node j spans all the nodes of the strongly connected component.
*
* The term "block" and "component" are used interchangebly in this code;
* "block" being a matrix term and "component" being a graph term for the
* same thing.
*
* When a node is visited, it is placed on the Cstack (for "component"
* stack). When node j is found to be an SCC head, all the nodes from the
* top of the stack to node j itself form the nodes in the SCC. This Cstack
* is used for both the recursive and non-recursive versions.
*/
Time = Work ; Work += n ;
Flag = Work ; Work += n ;
Low = P ; /* use output array P as workspace for Low */
Cstack = R ; /* use output array R as workspace for Cstack */
#ifndef RECURSIVE
/* stack for non-recursive dfs */
Jstack = Work ; Work += n ; /* stack for j */
Pstack = Work ; /* stack for p */
#endif
for (j = 0 ; j < n ; j++)
{
Flag [j] = UNVISITED ;
Low [j] = EMPTY ;
Time [j] = EMPTY ;
#ifndef NDEBUG
Cstack [j] = EMPTY ;
#ifndef RECURSIVE
Jstack [j] = EMPTY ;
Pstack [j] = EMPTY ;
#endif
#endif
}
timestamp = 0 ; /* each node given a timestamp when it is visited */
nblocks = 0 ; /* number of blocks found so far */
/* ---------------------------------------------------------------------- */
/* find the connected components via a depth-first-search */
/* ---------------------------------------------------------------------- */
for (j = 0 ; j < n ; j++)
{
/* node j is unvisited or assigned to a block. Cstack is empty. */
ASSERT (Flag [j] == UNVISITED || (Flag [j] >= 0 && Flag [j] < nblocks));
if (Flag [j] == UNVISITED)
{
#ifndef RECURSIVE
/* non-recursive dfs (default) */
dfs (j, Ap, Ai, Q, Time, Flag, Low, &nblocks, ×tamp,
Cstack, Jstack, Pstack) ;
#else
/* recursive dfs (for illustration only) */
ASSERT (chead == EMPTY) ;
dfs (EMPTY, j) ;
ASSERT (chead == EMPTY) ;
#endif
}
}
ASSERT (timestamp == n) ;
/* ---------------------------------------------------------------------- */
/* construct the block boundary array, R */
/* ---------------------------------------------------------------------- */
for (b = 0 ; b < nblocks ; b++)
{
R [b] = 0 ;
}
for (j = 0 ; j < n ; j++)
{
/* node j has been assigned to block b = Flag [j] */
ASSERT (Time [j] > 0 && Time [j] <= n) ;
ASSERT (Low [j] > 0 && Low [j] <= n) ;
ASSERT (Flag [j] >= 0 && Flag [j] < nblocks) ;
R [Flag [j]]++ ;
}
/* R [b] is now the number of nodes in block b. Compute cumulative sum
* of R, using Time [0 ... nblocks-1] as workspace. */
Time [0] = 0 ;
for (b = 1 ; b < nblocks ; b++)
{
Time [b] = Time [b-1] + R [b-1] ;
}
for (b = 0 ; b < nblocks ; b++)
{
R [b] = Time [b] ;
}
R [nblocks] = n ;
/* ---------------------------------------------------------------------- */
/* construct the permutation, preserving the natural order */
/* ---------------------------------------------------------------------- */
#ifndef NDEBUG
for (k = 0 ; k < n ; k++)
{
P [k] = EMPTY ;
}
#endif
for (j = 0 ; j < n ; j++)
{
/* place column j in the permutation */
P [Time [Flag [j]]++] = j ;
}
#ifndef NDEBUG
for (k = 0 ; k < n ; k++)
{
ASSERT (P [k] != EMPTY) ;
}
#endif
/* Now block b consists of the nodes k1 to k2-1 in the permuted matrix,
* where k1 = R [b] and k2 = R [b+1]. Row and column j of the original
* matrix becomes row and column P [k] of the permuted matrix. The set of
* of rows/columns (nodes) in block b is given by P [k1 ... k2-1], and this
* set is sorted in ascending order. Thus, if the matrix consists of just
* one block, P is the identity permutation. */
/* ---------------------------------------------------------------------- */
/* if Q is present on input, set Q = Q*P' */
/* ---------------------------------------------------------------------- */
if (Q != (Int *) NULL)
{
/* We found a symmetric permutation P for the matrix A*Q. The overall
* permutation is thus P*(A*Q)*P'. Set Q=Q*P' so that the final
* permutation is P*A*Q. Use Time as workspace. Note that this
* preserves the negative values of Q if the matrix is structurally
* singular. */
for (k = 0 ; k < n ; k++)
{
Time [k] = Q [P [k]] ;
}
for (k = 0 ; k < n ; k++)
{
Q [k] = Time [k] ;
}
}
/* ---------------------------------------------------------------------- */
/* how to traverse the permuted matrix */
/* ---------------------------------------------------------------------- */
/* If Q is not present, the following code can be used to traverse the
* permuted matrix P*A*P'
*
* // compute the inverse of P
* for (knew = 0 ; knew < n ; knew++)
* {
* // row and column kold in the old matrix is row/column knew
* // in the permuted matrix P*A*P'
* kold = P [knew] ;
* Pinv [kold] = knew ;
* }
* for (b = 0 ; b < nblocks ; b++)
* {
* // traverse block b of the permuted matrix P*A*P'
* k1 = R [b] ;
* k2 = R [b+1] ;
* nk = k2 - k1 ;
* for (jnew = k1 ; jnew < k2 ; jnew++)
* {
* jold = P [jnew] ;
* for (p = Ap [jold] ; p < Ap [jold+1] ; p++)
* {
* iold = Ai [p] ;
* inew = Pinv [iold] ;
* // Entry in the old matrix is A (iold, jold), and its
* // position in the new matrix P*A*P' is (inew, jnew).
* // Let B be the bth diagonal block of the permuted
* // matrix. If inew >= k1, then this entry is in row/
* // column (inew-k1, jnew-k1) of the nk-by-nk matrix B.
* // Otherwise, the entry is in the upper block triangular
* // part, not in any diagonal block.
* }
* }
* }
*
* If Q is present replace the above statement
* jold = P [jnew] ;
* with
* jold = Q [jnew] ;
* or
* jold = BTF_UNFLIP (Q [jnew]) ;
*
* then entry A (iold,jold) in the old (unpermuted) matrix is at (inew,jnew)
* in the permuted matrix P*A*Q. Everything else remains the same as the
* above (simply replace P*A*P' with P*A*Q in the above comments).
*/
/* ---------------------------------------------------------------------- */
/* return # of blocks / # of strongly connected components */
/* ---------------------------------------------------------------------- */
return (nblocks) ;
}
BTF/README.txt0000640001170100242240000001023311517626746012364 0ustar davissparseBTF, by Timothy A. Davis, Copyright (C) 2004-2011, University of Florida
BTF is also available under other licenses; contact the author for details.
http://www.cise.ufl.edu/research/sparse
BTF is a software package for permuting a matrix into block upper triangular
form. It includes a maximum transversal algorithm, which finds a permutation
of a square or rectangular matrix so that it has a zero-free diagonal (if one
exists); otherwise, it finds a maximal matching which maximizes the number of
nonzeros on the diagonal. The package also includes a method for finding the
strongly connected components of a graph. These two methods together give the
permutation to block upper triangular form.
Requires UFconfig, in the ../UFconfig directory relative to this directory.
KLU relies on this package to permute
To compile the libbtf.a library, type "make". The compiled library is located
in BTF/Lib/libbtf.a. Compile code that uses BTF with -IBTF/Include.
Type "make clean" to remove all but the compiled library, and "make distclean"
to remove all files not in the original distribution.
This package does not include a statement coverage test (Tcov directory) or
demo program (Demo directory). See the KLU package for both. The BTF package
does include a MATLAB interface, a MATLAB test suite (in the MATLAB/Test
directory), and a MATLAB demo.
See BTF/Include/btf.h for documentation on how to use the C-callable functions.
Use "help btf", "help maxtrans" and "help strongcomp" in MATLAB, for details on
how to use the MATLAB-callable functions. Additional details on the use of BTF
are given in the KLU User Guide, normally in ../KLU/Doc/KLU_UserGuide.pdf
relative to this directory.
--------------------------------------------------------------------------------
BTF is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This Module is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this Module; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
--------------------------------------------------------------------------------
A full text of the license is in Doc/lesser.txt.
--------------------------------------------------------------------------------
Files and directories in the BTF package:
Doc documentation and license
Include include files
Lib compiled BTF library
Makefile Makefile for C and MATLAB versions
MATLAB MATLAB interface
README.txt this file
Source BTF source code
./Doc:
ChangeLog changes in BTF
lesser.txt license
./Include:
btf.h primary user include file
btf_internal.h internal include file, not for user programs
./Lib:
Makefile Makefile for C library
./MATLAB:
btf.c btf mexFunction
btf_install.m compile and install BTF for use in MATLAB
btf.m btf help
Contents.m contents of MATLAB interface
Makefile Makefile for MATLAB functions
maxtrans.c maxtrans mexFunction
maxtrans.m maxtrans help
strongcomp.c strongcomp mexFunction
strongcomp.m strongcomp help
Test MATLAB test directory
./MATLAB/Test:
checkbtf.m check a BTF ordering
drawbtf.m plot a BTF ordering
test1.m compare maxtrans and cs_dmperm
test2.m compare btf and cs_dmperm
test3.m extensive test (maxtrans, strongcomp, and btf)
test4b.m test btf maxwork option
test4.m test btf maxwork option
test5.m test maxtrans maxwork option
./Source:
btf_maxtrans.c btf_maxtrans C function
btf_order.c btf_order C function
btf_strongcomp.c btf_strongcomp C function
BTF/Doc/0000750001170100242240000000000011670205742011360 5ustar davissparseBTF/Doc/ChangeLog0000640001170100242240000000263311670205742013137 0ustar davissparseDec 7, 2011: version 1.1.3
* fixed the Makefile to better align with CFLAGS and other standards
Jan 25, 2011: version 1.1.2
* minor fix to "make install"
Nov 30, 2009: version 1.1.1
* added "make install" and "make uninstall"
Mar 24, 2009: version 1.1.0
* tabs expanded to 8 spaces; version number updated to stay in sync with KLU
Nov 1, 2007: version 1.0.1
* trivial change to BTF/MATLAB/btf.c mexFunction: unused variable removed.
May 31, 2007: version 1.0 released
* the C application program interface has been modified (see below)
* maxtrans function renamed to btf_maxtrans
* strongcomp function renamed to btf_strongcomp
* full statement coverage tests (KLU/Tcov)
* maxwork parameter added to btf_maxtrans and btf_order
* btf_maxtrans modified; now returns Q[i] = -1 if row i is unmatched;
code to complete the permutation moved to btf_order. This also
changes the maxtrans mexFunction.
* btf_install added for easy MATLAB installation
* illustrative recursive version of maxtrans removed (see the recursive
version of cs_maxtrans in CSparse instead)
* MAXTRANS_* macros renamed BTF_*
* no bug fixes in this release
Dec 12, 2006: version 0.11
* minor MATLAB cleanup
Apr 30, 2006:
* minor editing of comments. dmperm.c moved to MATLAB directory, since
it requires MATLAB. Version number not changed.
BTF/Doc/lesser.txt0000640001170100242240000006364211164200045013417 0ustar davissparse GNU LESSER GENERAL PUBLIC LICENSE
Version 2.1, February 1999
Copyright (C) 1991, 1999 Free Software Foundation, Inc.
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
[This is the first released version of the Lesser GPL. It also counts
as the successor of the GNU Library Public License, version 2, hence
the version number 2.1.]
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
Licenses are intended to guarantee your freedom to share and change
free software--to make sure the software is free for all its users.
This license, the Lesser General Public License, applies to some
specially designated software packages--typically libraries--of the
Free Software Foundation and other authors who decide to use it. You
can use it too, but we suggest you first think carefully about whether
this license or the ordinary General Public License is the better
strategy to use in any particular case, based on the explanations below.
When we speak of free software, we are referring to freedom of use,
not price. Our General Public Licenses are designed to make sure that
you have the freedom to distribute copies of free software (and charge
for this service if you wish); that you receive source code or can get
it if you want it; that you can change the software and use pieces of
it in new free programs; and that you are informed that you can do
these things.
To protect your rights, we need to make restrictions that forbid
distributors to deny you these rights or to ask you to surrender these
rights. These restrictions translate to certain responsibilities for
you if you distribute copies of the library or if you modify it.
For example, if you distribute copies of the library, whether gratis
or for a fee, you must give the recipients all the rights that we gave
you. You must make sure that they, too, receive or can get the source
code. If you link other code with the library, you must provide
complete object files to the recipients, so that they can relink them
with the library after making changes to the library and recompiling
it. And you must show them these terms so they know their rights.
We protect your rights with a two-step method: (1) we copyright the
library, and (2) we offer you this license, which gives you legal
permission to copy, distribute and/or modify the library.
To protect each distributor, we want to make it very clear that
there is no warranty for the free library. Also, if the library is
modified by someone else and passed on, the recipients should know
that what they have is not the original version, so that the original
author's reputation will not be affected by problems that might be
introduced by others.
Finally, software patents pose a constant threat to the existence of
any free program. We wish to make sure that a company cannot
effectively restrict the users of a free program by obtaining a
restrictive license from a patent holder. Therefore, we insist that
any patent license obtained for a version of the library must be
consistent with the full freedom of use specified in this license.
Most GNU software, including some libraries, is covered by the
ordinary GNU General Public License. This license, the GNU Lesser
General Public License, applies to certain designated libraries, and
is quite different from the ordinary General Public License. We use
this license for certain libraries in order to permit linking those
libraries into non-free programs.
When a program is linked with a library, whether statically or using
a shared library, the combination of the two is legally speaking a
combined work, a derivative of the original library. The ordinary
General Public License therefore permits such linking only if the
entire combination fits its criteria of freedom. The Lesser General
Public License permits more lax criteria for linking other code with
the library.
We call this license the "Lesser" General Public License because it
does Less to protect the user's freedom than the ordinary General
Public License. It also provides other free software developers Less
of an advantage over competing non-free programs. These disadvantages
are the reason we use the ordinary General Public License for many
libraries. However, the Lesser license provides advantages in certain
special circumstances.
For example, on rare occasions, there may be a special need to
encourage the widest possible use of a certain library, so that it becomes
a de-facto standard. To achieve this, non-free programs must be
allowed to use the library. A more frequent case is that a free
library does the same job as widely used non-free libraries. In this
case, there is little to gain by limiting the free library to free
software only, so we use the Lesser General Public License.
In other cases, permission to use a particular library in non-free
programs enables a greater number of people to use a large body of
free software. For example, permission to use the GNU C Library in
non-free programs enables many more people to use the whole GNU
operating system, as well as its variant, the GNU/Linux operating
system.
Although the Lesser General Public License is Less protective of the
users' freedom, it does ensure that the user of a program that is
linked with the Library has the freedom and the wherewithal to run
that program using a modified version of the Library.
The precise terms and conditions for copying, distribution and
modification follow. Pay close attention to the difference between a
"work based on the library" and a "work that uses the library". The
former contains code derived from the library, whereas the latter must
be combined with the library in order to run.
GNU LESSER GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License Agreement applies to any software library or other
program which contains a notice placed by the copyright holder or
other authorized party saying it may be distributed under the terms of
this Lesser General Public License (also called "this License").
Each licensee is addressed as "you".
A "library" means a collection of software functions and/or data
prepared so as to be conveniently linked with application programs
(which use some of those functions and data) to form executables.
The "Library", below, refers to any such software library or work
which has been distributed under these terms. A "work based on the
Library" means either the Library or any derivative work under
copyright law: that is to say, a work containing the Library or a
portion of it, either verbatim or with modifications and/or translated
straightforwardly into another language. (Hereinafter, translation is
included without limitation in the term "modification".)
"Source code" for a work means the preferred form of the work for
making modifications to it. For a library, complete source code means
all the source code for all modules it contains, plus any associated
interface definition files, plus the scripts used to control compilation
and installation of the library.
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running a program using the Library is not restricted, and output from
such a program is covered only if its contents constitute a work based
on the Library (independent of the use of the Library in a tool for
writing it). Whether that is true depends on what the Library does
and what the program that uses the Library does.
1. You may copy and distribute verbatim copies of the Library's
complete source code as you receive it, in any medium, provided that
you conspicuously and appropriately publish on each copy an
appropriate copyright notice and disclaimer of warranty; keep intact
all the notices that refer to this License and to the absence of any
warranty; and distribute a copy of this License along with the
Library.
You may charge a fee for the physical act of transferring a copy,
and you may at your option offer warranty protection in exchange for a
fee.
2. You may modify your copy or copies of the Library or any portion
of it, thus forming a work based on the Library, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) The modified work must itself be a software library.
b) You must cause the files modified to carry prominent notices
stating that you changed the files and the date of any change.
c) You must cause the whole of the work to be licensed at no
charge to all third parties under the terms of this License.
d) If a facility in the modified Library refers to a function or a
table of data to be supplied by an application program that uses
the facility, other than as an argument passed when the facility
is invoked, then you must make a good faith effort to ensure that,
in the event an application does not supply such function or
table, the facility still operates, and performs whatever part of
its purpose remains meaningful.
(For example, a function in a library to compute square roots has
a purpose that is entirely well-defined independent of the
application. Therefore, Subsection 2d requires that any
application-supplied function or table used by this function must
be optional: if the application does not supply it, the square
root function must still compute square roots.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Library,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Library, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote
it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Library.
In addition, mere aggregation of another work not based on the Library
with the Library (or with a work based on the Library) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may opt to apply the terms of the ordinary GNU General Public
License instead of this License to a given copy of the Library. To do
this, you must alter all the notices that refer to this License, so
that they refer to the ordinary GNU General Public License, version 2,
instead of to this License. (If a newer version than version 2 of the
ordinary GNU General Public License has appeared, then you can specify
that version instead if you wish.) Do not make any other change in
these notices.
Once this change is made in a given copy, it is irreversible for
that copy, so the ordinary GNU General Public License applies to all
subsequent copies and derivative works made from that copy.
This option is useful when you wish to copy part of the code of
the Library into a program that is not a library.
4. You may copy and distribute the Library (or a portion or
derivative of it, under Section 2) in object code or executable form
under the terms of Sections 1 and 2 above provided that you accompany
it with the complete corresponding machine-readable source code, which
must be distributed under the terms of Sections 1 and 2 above on a
medium customarily used for software interchange.
If distribution of object code is made by offering access to copy
from a designated place, then offering equivalent access to copy the
source code from the same place satisfies the requirement to
distribute the source code, even though third parties are not
compelled to copy the source along with the object code.
5. A program that contains no derivative of any portion of the
Library, but is designed to work with the Library by being compiled or
linked with it, is called a "work that uses the Library". Such a
work, in isolation, is not a derivative work of the Library, and
therefore falls outside the scope of this License.
However, linking a "work that uses the Library" with the Library
creates an executable that is a derivative of the Library (because it
contains portions of the Library), rather than a "work that uses the
library". The executable is therefore covered by this License.
Section 6 states terms for distribution of such executables.
When a "work that uses the Library" uses material from a header file
that is part of the Library, the object code for the work may be a
derivative work of the Library even though the source code is not.
Whether this is true is especially significant if the work can be
linked without the Library, or if the work is itself a library. The
threshold for this to be true is not precisely defined by law.
If such an object file uses only numerical parameters, data
structure layouts and accessors, and small macros and small inline
functions (ten lines or less in length), then the use of the object
file is unrestricted, regardless of whether it is legally a derivative
work. (Executables containing this object code plus portions of the
Library will still fall under Section 6.)
Otherwise, if the work is a derivative of the Library, you may
distribute the object code for the work under the terms of Section 6.
Any executables containing that work also fall under Section 6,
whether or not they are linked directly with the Library itself.
6. As an exception to the Sections above, you may also combine or
link a "work that uses the Library" with the Library to produce a
work containing portions of the Library, and distribute that work
under terms of your choice, provided that the terms permit
modification of the work for the customer's own use and reverse
engineering for debugging such modifications.
You must give prominent notice with each copy of the work that the
Library is used in it and that the Library and its use are covered by
this License. You must supply a copy of this License. If the work
during execution displays copyright notices, you must include the
copyright notice for the Library among them, as well as a reference
directing the user to the copy of this License. Also, you must do one
of these things:
a) Accompany the work with the complete corresponding
machine-readable source code for the Library including whatever
changes were used in the work (which must be distributed under
Sections 1 and 2 above); and, if the work is an executable linked
with the Library, with the complete machine-readable "work that
uses the Library", as object code and/or source code, so that the
user can modify the Library and then relink to produce a modified
executable containing the modified Library. (It is understood
that the user who changes the contents of definitions files in the
Library will not necessarily be able to recompile the application
to use the modified definitions.)
b) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (1) uses at run time a
copy of the library already present on the user's computer system,
rather than copying library functions into the executable, and (2)
will operate properly with a modified version of the library, if
the user installs one, as long as the modified version is
interface-compatible with the version that the work was made with.
c) Accompany the work with a written offer, valid for at
least three years, to give the same user the materials
specified in Subsection 6a, above, for a charge no more
than the cost of performing this distribution.
d) If distribution of the work is made by offering access to copy
from a designated place, offer equivalent access to copy the above
specified materials from the same place.
e) Verify that the user has already received a copy of these
materials or that you have already sent this user a copy.
For an executable, the required form of the "work that uses the
Library" must include any data and utility programs needed for
reproducing the executable from it. However, as a special exception,
the materials to be distributed need not include anything that is
normally distributed (in either source or binary form) with the major
components (compiler, kernel, and so on) of the operating system on
which the executable runs, unless that component itself accompanies
the executable.
It may happen that this requirement contradicts the license
restrictions of other proprietary libraries that do not normally
accompany the operating system. Such a contradiction means you cannot
use both them and the Library together in an executable that you
distribute.
7. You may place library facilities that are a work based on the
Library side-by-side in a single library together with other library
facilities not covered by this License, and distribute such a combined
library, provided that the separate distribution of the work based on
the Library and of the other library facilities is otherwise
permitted, and provided that you do these two things:
a) Accompany the combined library with a copy of the same work
based on the Library, uncombined with any other library
facilities. This must be distributed under the terms of the
Sections above.
b) Give prominent notice with the combined library of the fact
that part of it is a work based on the Library, and explaining
where to find the accompanying uncombined form of the same work.
8. You may not copy, modify, sublicense, link with, or distribute
the Library except as expressly provided under this License. Any
attempt otherwise to copy, modify, sublicense, link with, or
distribute the Library is void, and will automatically terminate your
rights under this License. However, parties who have received copies,
or rights, from you under this License will not have their licenses
terminated so long as such parties remain in full compliance.
9. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Library or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Library (or any work based on the
Library), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Library or works based on it.
10. Each time you redistribute the Library (or any work based on the
Library), the recipient automatically receives a license from the
original licensor to copy, distribute, link with or modify the Library
subject to these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties with
this License.
11. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Library at all. For example, if a patent
license would not permit royalty-free redistribution of the Library by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Library.
If any portion of this section is held invalid or unenforceable under any
particular circumstance, the balance of the section is intended to apply,
and the section as a whole is intended to apply in other circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
12. If the distribution and/or use of the Library is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Library under this License may add
an explicit geographical distribution limitation excluding those countries,
so that distribution is permitted only in or among countries not thus
excluded. In such case, this License incorporates the limitation as if
written in the body of this License.
13. The Free Software Foundation may publish revised and/or new
versions of the Lesser General Public License from time to time.
Such new versions will be similar in spirit to the present version,
but may differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the Library
specifies a version number of this License which applies to it and
"any later version", you have the option of following the terms and
conditions either of that version or of any later version published by
the Free Software Foundation. If the Library does not specify a
license version number, you may choose any version ever published by
the Free Software Foundation.
14. If you wish to incorporate parts of the Library into other free
programs whose distribution conditions are incompatible with these,
write to the author to ask for permission. For software which is
copyrighted by the Free Software Foundation, write to the Free
Software Foundation; we sometimes make exceptions for this. Our
decision will be guided by the two goals of preserving the free status
of all derivatives of our free software and of promoting the sharing
and reuse of software generally.
NO WARRANTY
15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Libraries
If you develop a new library, and you want it to be of the greatest
possible use to the public, we recommend making it free software that
everyone can redistribute and change. You can do so by permitting
redistribution under these terms (or, alternatively, under the terms of the
ordinary General Public License).
To apply these terms, attach the following notices to the library. It is
safest to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least the
"copyright" line and a pointer to where the full notice is found.
Copyright (C)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Also add information on how to contact you by electronic and paper mail.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the library, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the
library `Frob' (a library for tweaking knobs) written by James Random Hacker.
, 1 April 1990
Ty Coon, President of Vice
That's all there is to it!
BTF/Makefile0000640001170100242240000000142011670213537012312 0ustar davissparse#------------------------------------------------------------------------------
# BTF Makefile
#------------------------------------------------------------------------------
VERSION = 1.1.3
default: library
include ../UFconfig/UFconfig.mk
library:
( cd Lib ; $(MAKE) )
clean:
( cd Lib ; $(MAKE) clean )
distclean:
( cd Lib ; $(MAKE) distclean )
( cd MATLAB ; $(MAKE) distclean )
mex:
( cd MATLAB ; $(MAKE) )
purge: distclean
# install BTF
install:
$(CP) Lib/libbtf.a $(INSTALL_LIB)/libbtf.$(VERSION).a
( cd $(INSTALL_LIB) ; ln -sf libbtf.$(VERSION).a libbtf.a )
$(CP) Include/btf.h $(INSTALL_INCLUDE)
chmod 644 $(INSTALL_LIB)/libbtf*.a
chmod 644 $(INSTALL_INCLUDE)/btf.h
# uninstall BTF
uninstall:
$(RM) $(INSTALL_LIB)/libbtf*.a
$(RM) $(INSTALL_INCLUDE)/btf.h
BTF/Lib/0000750001170100242240000000000011672433545011367 5ustar davissparseBTF/Lib/Makefile0000640001170100242240000000222311670172613013021 0ustar davissparsedefault: all
ccode: all
include ../../UFconfig/UFconfig.mk
# for testing only:
# TEST = -DTESTING
C = $(CC) $(CF)
INC = ../Include/btf.h ../Include/btf_internal.h
I = -I../Include -I../../UFconfig
all: library
library: libbtf.a
OBJ = btf_order.o btf_maxtrans.o btf_strongcomp.o \
btf_l_order.o btf_l_maxtrans.o btf_l_strongcomp.o
libbtf.a: $(OBJ)
$(ARCHIVE) libbtf.a $(OBJ)
- $(RANLIB) libbtf.a
$(OBJ): $(INC)
#-------------------------------------------------------------------------------
btf_order.o: ../Source/btf_order.c
$(C) -c $(I) $< -o $@
btf_maxtrans.o: ../Source/btf_maxtrans.c
$(C) -c $(I) $< -o $@
btf_strongcomp.o: ../Source/btf_strongcomp.c
$(C) -c $(I) $< -o $@
#-------------------------------------------------------------------------------
btf_l_order.o: ../Source/btf_order.c
$(C) -c $(I) -DDLONG $< -o $@
btf_l_maxtrans.o: ../Source/btf_maxtrans.c
$(C) -c $(I) -DDLONG $< -o $@
btf_l_strongcomp.o: ../Source/btf_strongcomp.c
$(C) -c $(I) -DDLONG $< -o $@
#-------------------------------------------------------------------------------
purge: distclean
distclean: clean
- $(RM) libbtf.a
clean:
- $(RM) $(CLEAN)
BTF/MATLAB/0000750001170100242240000000000011672433555011622 5ustar davissparseBTF/MATLAB/strongcomp.m0000640001170100242240000000336111164175543014173 0ustar davissparsefunction [p,q,r] = strongcomp (A, qin) %#ok
%STRONGCOMP symmetric permutation to upper block triangular form
% The matrix must be sparse and square.
%
% Example:
% [p,r] = strongcomp (A) ;
% [p,q,r] = strongcomp (A,qin) ;
%
% In the first usage, the permuted matrix is C = A (p,p). In the second usage,
% the matrix A (:,qin) is symmetrically permuted to upper block triangular
% form, where qin is an input column permutation, and the final permuted
% matrix is C = A (p,q). This second usage is equivalent to
%
% [p,r] = strongcomp (A (:,qin)) ;
% q = qin (p) ;
%
% That is, if qin is not present it is assumed to be qin = 1:n.
%
% C is the permuted matrix, with a number of blocks equal to length(r)-1.
% The kth block is from row/col r(k) to row/col r(k+1)-1 of C.
% r(1) is one and the last entry in r is equal to n+1.
% The diagonal of A (or A (:,qin)) is ignored.
%
% strongcomp is normally proceeded by a maximum transversal.
% Assuming A is square and structurally nonsingular,
%
% [p,q,r] = strongcomp (A, maxtrans (A))
%
% is essentially identical to
%
% [p,q,r] = dmperm (A)
%
% except that p, q, and r will differ. Both return an upper block triangular
% form with a zero-free diagonal. The number and sizes of the blocks will be
% identical, but the order of the blocks, and the ordering within the blocks,
% can be different. If the matrix is structurally singular, both strongcomp
% and maxtrans return a vector q containing negative entries. abs(q) is a
% permutation of 1:n, and find(q<0) gives a list of the indices of the
% diagonal of A(p,q) that are zero.
%
% See also btf, maxtrans, dmperm
% Copyright 2004-2007, Tim Davis, University of Florida
error ('strongcomp mexFunction not found') ;
BTF/MATLAB/Makefile0000640001170100242240000000176011276623014013256 0ustar davissparse
include ../../UFconfig/UFconfig.mk
I = -I../Include -I../../UFconfig
MX = $(MEX) $(I) -DDLONG
all: maxtrans.mexglx strongcomp.mexglx btf.mexglx
recursive: strongcomp_recursive.mexglx
maxtrans.mexglx: ../Source/btf_maxtrans.c ../Include/btf.h maxtrans.c \
../Include/btf_internal.h
$(MX) maxtrans.c ../Source/btf_maxtrans.c
strongcomp.mexglx: ../Source/btf_strongcomp.c ../Include/btf.h \
strongcomp.c ../Include/btf_internal.h
$(MX) strongcomp.c ../Source/btf_strongcomp.c
strongcomp_recursive.mexglx: ../Source/btf_strongcomp.c ../Include/btf.h \
strongcomp.c ../Include/btf_internal.h
$(MX) -DRECURSIVE -output strongcomp_recursive \
../Source/btf_strongcomp.c strongcomp.c
btf.mexglx: ../Source/btf_strongcomp.c ../Include/btf.h btf.c \
../Include/btf_internal.h \
../Source/btf_maxtrans.c ../Source/btf_order.c
$(MX) btf.c ../Source/btf_maxtrans.c \
../Source/btf_strongcomp.c ../Source/btf_order.c
distclean: purge
purge: clean
- $(RM) *.o *.mex*
clean:
- $(RM) $(CLEAN)
BTF/MATLAB/Contents.m0000640001170100242240000000116211164175543013572 0ustar davissparse% BTF ordering toolbox:
%
% Primary functions:
%
% btf - permute a square sparse matrix into upper block triangular form
% maxtrans - permute the columns of a sparse matrix so it has a zero-free diagonal
% strongcomp - symmetric permutation to upper block triangular form
%
% Other:
% btf_install - compile and install BTF for use in MATLAB.
% btf_demo - demo for BTF
% drawbtf - plot the BTF form of a matrix
% btf_make - compile BTF for use in MATLAB
%
% Example:
% q = maxtrans (A)
% [p,q,r] = btf (A)
% [p,r] = strongcomp (A)
% Copyright 2004-2007, Tim Davis, University of Florida
BTF/MATLAB/strongcomp.c0000640001170100242240000001324411164175543014162 0ustar davissparse/* ========================================================================== */
/* === stongcomp mexFunction ================================================ */
/* ========================================================================== */
/* STRONGCOMP: Find a symmetric permutation to upper block triangular form of
* a sparse square matrix.
*
* Usage:
*
* [p,r] = strongcomp (A) ;
*
* [p,q,r] = strongcomp (A,qin) ;
*
* In the first usage, the permuted matrix is C = A (p,p). In the second usage,
* the matrix A (:,qin) is symmetrically permuted to upper block triangular
* form, where qin is an input column permutation, and the final permuted
* matrix is C = A (p,q). This second usage is equivalent to
*
* [p,r] = strongcomp (A (:,qin)) ;
* q = qin (p) ;
*
* That is, if qin is not present it is assumed to be qin = 1:n.
*
* C is the permuted matrix, with a number of blocks equal to length(r)-1.
* The kth block is from row/col r(k) to row/col r(k+1)-1 of C.
* r(1) is one and the last entry in r is equal to n+1.
* The diagonal of A (or A (:,qin)) is ignored.
*
* strongcomp is normally proceeded by a maximum transversal:
*
* [p,q,r] = strongcomp (A, maxtrans (A))
*
* if the matrix has full structural rank. This is identical to
*
* [p,q,r] = btf (A)
*
* (except that btf handles the case when A is structurally rank-deficient).
* It essentially the same as
*
* [p,q,r] = dmperm (A)
*
* except that p, q, and r will differ between btf and dmperm. Both return an
* upper block triangular form with a zero-free diagonal. The number and sizes
* of the blocks will be identical, but the order of the blocks, and the
* ordering within the blocks, can be different. For structurally rank
* deficient matrices, dmpmerm returns the maximum matching as a zero-free
* diagonal that is above the main diagonal; btf always returns the matching as
* the main diagonal (which will thus contain zeros).
*
* Copyright (c) 2004-2007. Tim Davis, University of Florida,
* with support from Sandia National Laboratories. All Rights Reserved.
*
* See also maxtrans, btf, dmperm
*/
/* ========================================================================== */
#include "mex.h"
#include "btf.h"
void mexFunction
(
int nargout,
mxArray *pargout[],
int nargin,
const mxArray *pargin[]
)
{
UF_long b, n, i, k, j, *Ap, *Ai, *P, *R, nblocks, *Work, *Q, jj ;
double *Px, *Rx, *Qx ;
/* ---------------------------------------------------------------------- */
/* get inputs and allocate workspace */
/* ---------------------------------------------------------------------- */
if (!((nargin == 1 && nargout <= 2) || (nargin == 2 && nargout <= 3)))
{
mexErrMsgTxt ("Usage: [p,r] = strongcomp (A)"
" or [p,q,r] = strongcomp (A,qin)") ;
}
n = mxGetM (pargin [0]) ;
if (!mxIsSparse (pargin [0]) || n != mxGetN (pargin [0]))
{
mexErrMsgTxt ("strongcomp: A must be sparse, square, and non-empty") ;
}
/* get sparse matrix A */
Ap = (UF_long *) mxGetJc (pargin [0]) ;
Ai = (UF_long *) mxGetIr (pargin [0]) ;
/* get output arrays */
P = mxMalloc (n * sizeof (UF_long)) ;
R = mxMalloc ((n+1) * sizeof (UF_long)) ;
/* get workspace of size 4n (recursive code only needs 2n) */
Work = mxMalloc (4*n * sizeof (UF_long)) ;
/* get the input column permutation Q */
if (nargin == 2)
{
if (mxGetNumberOfElements (pargin [1]) != n)
{
mexErrMsgTxt
("strongcomp: qin must be a permutation vector of size n") ;
}
Qx = mxGetPr (pargin [1]) ;
Q = mxMalloc (n * sizeof (UF_long)) ;
/* connvert Qin to 0-based and check validity */
for (i = 0 ; i < n ; i++)
{
Work [i] = 0 ;
}
for (k = 0 ; k < n ; k++)
{
j = Qx [k] - 1 ; /* convert to 0-based */
jj = BTF_UNFLIP (j) ;
if (jj < 0 || jj >= n || Work [jj] == 1)
{
mexErrMsgTxt
("strongcomp: qin must be a permutation vector of size n") ;
}
Work [jj] = 1 ;
Q [k] = j ;
}
}
else
{
/* no input column permutation */
Q = (UF_long *) NULL ;
}
/* ---------------------------------------------------------------------- */
/* find the strongly-connected components of A */
/* ---------------------------------------------------------------------- */
nblocks = btf_l_strongcomp (n, Ap, Ai, Q, P, R, Work) ;
/* ---------------------------------------------------------------------- */
/* create outputs and free workspace */
/* ---------------------------------------------------------------------- */
/* create P */
pargout [0] = mxCreateDoubleMatrix (1, n, mxREAL) ;
Px = mxGetPr (pargout [0]) ;
for (k = 0 ; k < n ; k++)
{
Px [k] = P [k] + 1 ; /* convert to 1-based */
}
/* create Q */
if (nargin == 2 && nargout > 1)
{
pargout [1] = mxCreateDoubleMatrix (1, n, mxREAL) ;
Qx = mxGetPr (pargout [1]) ;
for (k = 0 ; k < n ; k++)
{
Qx [k] = Q [k] + 1 ; /* convert to 1-based */
}
}
/* create R */
if (nargout == nargin + 1)
{
pargout [nargin] = mxCreateDoubleMatrix (1, nblocks+1, mxREAL) ;
Rx = mxGetPr (pargout [nargin]) ;
for (b = 0 ; b <= nblocks ; b++)
{
Rx [b] = R [b] + 1 ; /* convert to 1-based */
}
}
mxFree (P) ;
mxFree (R) ;
mxFree (Work) ;
if (nargin == 2)
{
mxFree (Q) ;
}
}
BTF/MATLAB/drawbtf.m0000640001170100242240000000100511164175543013422 0ustar davissparsefunction drawbtf (A, p, q, r)
%DRAWBTF plot the BTF form of a matrix
%
% A(p,q) is in BTF form, r the block boundaries
%
% Example:
% [p,q,r] = dmperm (A)
% drawbtf (A, p, q, r)
%
% See also btf, maxtrans, strongcomp, dmperm.
% Copyright 2004-2007, Tim Davis, University of Florida
nblocks = length (r) - 1 ;
hold off
spy (A (p,abs(q)))
hold on
for k = 1:nblocks
k1 = r (k) ;
k2 = r (k+1) ;
nk = k2 - k1 ;
if (nk > 1)
plot ([k1 k2 k2 k1 k1]-.5, [k1 k1 k2 k2 k1]-.5, 'r') ;
end
end
BTF/MATLAB/btf_make.m0000640001170100242240000000157211164175543013552 0ustar davissparsefunction btf_make
%BTF_MAKE compile BTF for use in MATLAB
% Your current working directory must be BTF/MATLAB for this function to work.
%
% Example:
% btf_make
%
% See also btf, maxtrans, stroncomp, dmperm.
% Copyright 2004-2007, Tim Davis, University of Florida
details = 0 ; % if 1, print details of each command
mexcmd = 'mex -O -DDLONG -I../Include -I../../UFconfig ' ;
if (~isempty (strfind (computer, '64')))
mexcmd = [mexcmd '-largeArrayDims '] ;
end
s = [mexcmd 'maxtrans.c ../Source/btf_maxtrans.c'] ;
if (details)
fprintf ('%s\n', s) ;
end
eval (s) ;
s = [mexcmd 'strongcomp.c ../Source/btf_strongcomp.c'] ;
if (details)
fprintf ('%s\n', s) ;
end
eval (s) ;
s = [mexcmd 'btf.c ../Source/btf_maxtrans.c ../Source/btf_strongcomp.c ../Source/btf_order.c'] ;
if (details)
fprintf ('%s\n', s) ;
end
eval (s) ;
fprintf ('BTF successfully compiled.\n') ;
BTF/MATLAB/btf_demo.m0000640001170100242240000000146611672161774013570 0ustar davissparse%BTF_DEMO demo for BTF
%
% Example:
% btf_demo
%
% See also btf, dmperm, strongcomp, maxtrans
% Copyright 2004-2007, Tim Davis, University of Florida
load west0479 ;
A = west0479 ;
clf
subplot (2,3,1) ;
spy (A)
title ('west0479') ;
subplot (2,3,2) ;
[p, q, r] = btf (A) ;
% spy (A (p, abs(q))) ;
drawbtf (A, p, q, r) ;
title ('btf') ;
fprintf ('\nbtf_demo: n %d nnz(A) %d # of blocks %d\n', ...
size (A,1), nnz (A), length (r) - 1) ;
subplot (2,3,3) ;
[p, q, r, s] = dmperm (A) ;
drawbtf (A, p, q, r) ;
title ('dmperm btf')
subplot (2,3,4) ;
[p, r] = strongcomp (A) ;
% spy (A (p, abs(q))) ;
drawbtf (A, p, p, r) ;
title ('strongly conn. comp.') ;
subplot (2,3,5) ;
q = maxtrans (A) ;
spy (A (:,q))
title ('max transversal') ;
subplot (2,3,6) ;
p = dmperm (A) ;
spy (A (p,:))
title ('dmperm maxtrans') ;
BTF/MATLAB/maxtrans.c0000640001170100242240000000610611164175543013623 0ustar davissparse/* ========================================================================== */
/* === maxtrans mexFunction ================================================= */
/* ========================================================================== */
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
/* MAXTRANS: Find a column permutation for a zero-free diagonal.
*
* Usage:
*
* q = maxtrans (A) ;
* q = maxtrans (A,maxwork) ;
*
* A (:,q) has a zero-free diagonal if sprank(A) == n.
* If the matrix is structurally singular, q will contain zeros. Similar
* to p = dmperm (A), except that dmperm returns a row permutation.
*
* An optional second output [q,work] = maxtrans (...) returns the amount of
* work performed, or -1 if the maximum work limit is reached (in which case
* the maximum matching might not have been found).
*
* Copyright (c) 2004-2007. Tim Davis, University of Florida,
* with support from Sandia National Laboratories. All Rights Reserved.
*/
/* ========================================================================== */
#include "mex.h"
#include "btf.h"
void mexFunction
(
int nargout,
mxArray *pargout [ ],
int nargin,
const mxArray *pargin [ ]
)
{
double maxwork, work ;
UF_long nrow, ncol, i, *Ap, *Ai, *Match, nmatch, *Work ;
double *Matchx, *w ;
/* ---------------------------------------------------------------------- */
/* get inputs and allocate workspace */
/* ---------------------------------------------------------------------- */
if (nargin < 1 || nargin > 2 || nargout > 2)
{
mexErrMsgTxt ("Usage: q = maxtrans (A)") ;
}
nrow = mxGetM (pargin [0]) ;
ncol = mxGetN (pargin [0]) ;
if (!mxIsSparse (pargin [0]))
{
mexErrMsgTxt ("maxtrans: A must be sparse, and non-empty") ;
}
/* get sparse matrix A */
Ap = (UF_long *) mxGetJc (pargin [0]) ;
Ai = (UF_long *) mxGetIr (pargin [0]) ;
/* get output array */
Match = mxMalloc (nrow * sizeof (UF_long)) ;
/* get workspace of size 5n (recursive version needs only 2n) */
Work = mxMalloc (5*ncol * sizeof (UF_long)) ;
maxwork = 0 ;
if (nargin > 1)
{
maxwork = mxGetScalar (pargin [1]) ;
}
work = 0 ;
/* ---------------------------------------------------------------------- */
/* perform the maximum transversal */
/* ---------------------------------------------------------------------- */
nmatch = btf_l_maxtrans (nrow, ncol, Ap, Ai, maxwork, &work, Match, Work) ;
/* ---------------------------------------------------------------------- */
/* create outputs and free workspace */
/* ---------------------------------------------------------------------- */
pargout [0] = mxCreateDoubleMatrix (1, nrow, mxREAL) ;
Matchx = mxGetPr (pargout [0]) ;
for (i = 0 ; i < nrow ; i++)
{
Matchx [i] = Match [i] + 1 ; /* convert to 1-based */
}
if (nargout > 1)
{
pargout [1] = mxCreateDoubleMatrix (1, 1, mxREAL) ;
w = mxGetPr (pargout [1]) ;
w [0] = work ;
}
mxFree (Work) ;
mxFree (Match) ;
}
BTF/MATLAB/btf.m0000640001170100242240000000337211164175543012555 0ustar davissparsefunction [p,q,r] = btf (A) %#ok
%BTF permute a square sparse matrix into upper block triangular form
% with a zero-free diagonal, or with a maximum number of nonzeros along the
% diagonal if a zero-free permutation does not exist.
%
% Example:
% [p,q,r] = btf (A) ;
% [p,q,r] = btf (A,maxwork) ;
%
% If the matrix has structural full rank, this is essentially identical to
%
% [p,q,r] = dmperm (A)
%
% except that p, q, and r will differ in trivial ways. Both return an upper
% block triangular form with a zero-free diagonal, if the matrix is
% structurally non-singular. The number and sizes of the blocks will be
% identical, but the order of the blocks, and the ordering within the blocks,
% can be different.
%
% If the matrix is structurally singular, the q from btf will contain negative
% entries. The permuted matrix is C = A(p,abs(q)), and find(q<0) gives a list
% of indices of the diagonal of C that are equal to zero. This differs from
% dmperm, which does not place the maximum matching along the main diagonal
% of C=A(p,q), but places it above the diagonal instead.
%
% The second input limits the maximum amount of work the function does to
% be maxwork*nnz(A), or no limit at all if maxwork <= 0. If the function
% terminates early as a result, a maximum matching may not be found, and the
% diagonal of A(p,abs(q)) might not have the maximum number of nonzeros
% possible. Also, the number of blocks (length(r)-1) may be larger than
% what btf(A) or dmperm(A) would compute.
%
% See also maxtrans, strongcomp, dmperm, sprank
% Copyright 2004-2007, Tim Davis, University of Florida
% with support from Sandia National Laboratories. All Rights Reserved.
error ('btf mexFunction not found') ;
BTF/MATLAB/btf_install.m0000640001170100242240000000074211164175543014301 0ustar davissparsefunction btf_install
%BTF_INSTALL compile and install BTF for use in MATLAB.
% Your current working directory must be BTF/MATLAB for this function to work.
%
% Example:
% btf_install
%
% See also btf, maxtrans, stroncomp, dmperm.
% Copyright 2004-2007, Tim Davis, University of Florida
btf_make
addpath (pwd) ;
fprintf ('BTF has been compiled and installed. The path:\n') ;
disp (pwd) ;
fprintf ('has been added to your path. Use pathtool to add it permanently.\n');
btf_demo
BTF/MATLAB/maxtrans.m0000640001170100242240000000242611164175543013636 0ustar davissparsefunction q = maxtrans (A) %#ok
%MAXTRANS permute the columns of a sparse matrix so it has a zero-free diagonal
% (if it exists). If no zero-free diagonal exists, then a maximum matching is
% found. Note that this differs from p=dmperm(A), which returns a row
% permutation.
%
% Example:
% q = maxtrans (A)
% q = maxtrans (A,maxwork)
%
% If row i and column j are matched, then q(i) = j. Otherwise, if row is
% unmatched, then q(i) = 0. This is similar to dmperm, except that
% p = dmperm(A) returns p(j)=i if row i and column j are matched, or p(j)=0 if
% column j is unmatched.
%
% If A is structurally nonsingular, then A(:,maxtrans(A)) has a zero-free
% diagonal, as does A (dmperm(A),:).
%
% The second input limits the maximum amount of work the function does
% (excluding the O(nnz(A)) cheap match phase) to be maxwork*nnz(A), or no limit
% at all if maxwork <= 0. If the function terminates early as a result, a
% maximum matching may not be found. An optional second output
% [q,work] = maxtrans (...) returns the amount of work performed, or -1 if the
% maximum work limit is reached.
%
% See also: btf, strongcomp, dmperm, sprank
% Copyright 2004-2007, Tim Davis, University of Florida
error ('maxtrans mexfunction not found') ;
BTF/MATLAB/btf.c0000640001170100242240000001076111164175543012543 0ustar davissparse/* ========================================================================== */
/* === btf mexFunction ====================================================== */
/* ========================================================================== */
/* BTF: Permute a square matrix to upper block triangular form with a zero-free
* diagonal, or with a maximum number of nonzeros along the diagonal if a
* zero-free permutation does not exist.
*
* Usage:
*
* [p,q,r] = btf (A) ;
* [p,q,r] = btf (A, maxwork) ;
*
* If the matrix has structural full rank, this is essentially identical to
*
* [p,q,r] = dmperm (A)
*
* except that p, q, and r will differ in trivial ways. Both return an upper
* block triangular form with a zero-free diagonal, if the matrix is
* structurally non-singular. The number and sizes of the blocks will be
* identical, but the order of the blocks, and the ordering within the blocks,
* can be different.
*
* If the matrix is structurally singular, q will contain negative entries.
* The permuted matrix is C = A(p,abs(q)), and find(q<0) gives a list of
* indices of the diagonal of C that are equal to zero. This differs from
* dmperm, which does not place the maximum matching along the main diagonal
* of C=A(p,q), but places it above the diagonal instead.
*
* See maxtrans, or btf.m, for a description of maxwork.
*
* An optional fourth output [p,q,r,work] = btf (...) returns the amount of
* work performed, or -1 if the maximum work limit is reached (in which case
* the maximum matching might not have been found).
*
* Copyright (c) 2004-2007. Tim Davis, University of Florida,
* with support from Sandia National Laboratories. All Rights Reserved.
*
* See also maxtrans, strongcomp, dmperm
*/
/* ========================================================================== */
#include "mex.h"
#include "btf.h"
void mexFunction
(
int nargout,
mxArray *pargout [ ],
int nargin,
const mxArray *pargin [ ]
)
{
double work, maxwork ;
UF_long b, n, k, *Ap, *Ai, *P, *R, nblocks, *Work, *Q, nmatch ;
double *Px, *Rx, *Qx, *w ;
/* ---------------------------------------------------------------------- */
/* get inputs and allocate workspace */
/* ---------------------------------------------------------------------- */
if (nargin < 1 || nargin > 2 || nargout > 4)
{
mexErrMsgTxt ("Usage: [p,q,r] = btf (A)") ;
}
n = mxGetM (pargin [0]) ;
if (!mxIsSparse (pargin [0]) || n != mxGetN (pargin [0]))
{
mexErrMsgTxt ("btf: A must be sparse, square, and non-empty") ;
}
/* get sparse matrix A */
Ap = (UF_long *) mxGetJc (pargin [0]) ;
Ai = (UF_long *) mxGetIr (pargin [0]) ;
/* get output arrays */
Q = mxMalloc (n * sizeof (UF_long)) ;
P = mxMalloc (n * sizeof (UF_long)) ;
R = mxMalloc ((n+1) * sizeof (UF_long)) ;
/* get workspace */
Work = mxMalloc (5*n * sizeof (UF_long)) ;
maxwork = 0 ;
if (nargin > 1)
{
maxwork = mxGetScalar (pargin [1]) ;
}
work = 0 ;
/* ---------------------------------------------------------------------- */
/* find the permutation to BTF */
/* ---------------------------------------------------------------------- */
nblocks = btf_l_order (n, Ap, Ai, maxwork, &work, P, Q, R, &nmatch, Work) ;
/* ---------------------------------------------------------------------- */
/* create outputs and free workspace */
/* ---------------------------------------------------------------------- */
/* create P */
pargout [0] = mxCreateDoubleMatrix (1, n, mxREAL) ;
Px = mxGetPr (pargout [0]) ;
for (k = 0 ; k < n ; k++)
{
Px [k] = P [k] + 1 ; /* convert to 1-based */
}
/* create Q */
if (nargout > 1)
{
pargout [1] = mxCreateDoubleMatrix (1, n, mxREAL) ;
Qx = mxGetPr (pargout [1]) ;
for (k = 0 ; k < n ; k++)
{
Qx [k] = Q [k] + 1 ; /* convert to 1-based */
}
}
/* create R */
if (nargout > 2)
{
pargout [2] = mxCreateDoubleMatrix (1, nblocks+1, mxREAL) ;
Rx = mxGetPr (pargout [2]) ;
for (b = 0 ; b <= nblocks ; b++)
{
Rx [b] = R [b] + 1 ; /* convert to 1-based */
}
}
/* create work output */
if (nargout > 3)
{
pargout [3] = mxCreateDoubleMatrix (1, 1, mxREAL) ;
w = mxGetPr (pargout [3]) ;
w [0] = work ;
}
mxFree (P) ;
mxFree (R) ;
mxFree (Work) ;
mxFree (Q) ;
}
BTF/MATLAB/Test/0000750001170100242240000000000011672162442012533 5ustar davissparseBTF/MATLAB/Test/test6.m0000640001170100242240000000774111164175552013772 0ustar davissparsefunction test6
%TEST6 test for BTF
% Requires UFget
% Example:
% test6
% See also btf, maxtrans, strongcomp, dmperm, UFget,
% test1, test2, test3, test4, test5.
% Copyright 2007, Timothy A. Davis, University of Florida
quick2 = [ ...
1522 -272 1463 1521 460 1507 -838 1533 -1533 -1456 -1512 734 211 ...
-385 -735 394 -397 1109 -744 ...
-734 -375 -1200 -1536 -837 519 -519 520 -520 189 -189 454 385 ...
387 -387 384 -384 386 -386 388 -388 525 -525 526 -526 735 ...
1508 209 210 1243 -1243 1534 -840 1234 -1234 390 -390 392 -392 ...
-394 1472 1242 -1242 389 -389 391 -391 393 -393 1215 -1215 1216 ...
-1216 736 -736 737 -737 455 -455 -224 -839 1426 -1426 -1473 396 ...
-396 398 -398 400 -400 402 -402 404 -404 -1531 395 -395 397 ...
399 -399 401 -401 403 -403 405 -405 -738 -739 1459 -1459 1111 ...
1110 376 -376 284 -284 -740 -742 -741 -743 1293 -1293 452 920 ...
-745 -446 1462 -1461 448 -448 283 -283 1502 -1502 1292 -1292 1503 ...
-1503 1291 -1291 445 -445 -746 -747 1300 -1300 435 -435 -1343 -1345 ...
-1344 1305 -1305 921 -1513 1307 -1307 1369 -1369 1374 -1374 1377 ...
-1377 748 -748 -749 1510 922 -922 ] ;
index = UFget ;
nmat = length (quick2) ;
dopause = 0 ;
h = waitbar (0, 'BTF test 6 of 6') ;
try
for k = 1:nmat
waitbar (k/nmat, h) ;
i = quick2 (k) ;
Prob = UFget (abs (i), index) ;
disp (Prob) ;
if (i < 0)
fprintf ('transposed\n') ;
A = Prob.A' ;
[m n] = size (A) ;
if (m == n)
if (nnz (spones (A) - spones (Prob.A)) == 0)
fprintf ('skip...\n') ;
continue ;
end
end
else
A = Prob.A ;
end
tic
[p1,q1,r1,work1] = btf (A) ;
t1 = toc ;
n1 = length (r1) - 1 ;
m1 = nnz (diag (A (p1, abs (q1)))) ;
limit = work1/nnz(A) ;
fprintf ('full search: %g * nnz(A)\n', limit) ;
works = linspace(0,limit,9) ;
works (1) = eps ;
nw = length (works) ;
T2 = zeros (nw, 1) ;
N2 = zeros (nw, 1) ;
M2 = zeros (nw, 1) ;
T2 (end) = t1 ;
N2 (end) = n1 ;
M2 (end) = m1 ;
fprintf ('full time %10.4f blocks %8d nnz(diag) %8d\n\n', t1, n1, m1) ;
subplot (3,4,4) ;
drawbtf (A, p1, abs (q1), r1) ;
title (Prob.name, 'Interpreter', 'none') ;
for j = 1:nw-1
maxwork = works (j) ;
tic
[p2,q2,r2,work2] = btf (A, maxwork) ;
t2 = toc ;
n2 = length (r2) - 1 ;
m2 = nnz (diag (A (p2, abs (q2)))) ;
T2 (j) = t2 ;
N2 (j) = n2 ;
M2 (j) = m2 ;
fprintf ('%9.1f %10.4f blocks %8d nnz(diag) %8d\n', ...
maxwork, t2, n2, m2) ;
subplot (3,4,4+j) ;
drawbtf (A, p2, abs (q2), r2) ;
title (sprintf ('%g', maxwork)) ;
ss = [1:j nw] ;
subplot (3,4,1) ;
plot (works(ss), T2(ss), 'o-') ; title ('time vs work') ;
axis ([0 limit 0 max(0.1,max(T2))]) ;
subplot (3,4,2) ;
plot (works(ss), N2(ss), 'o-') ; title ('blocks vs work') ;
axis ([0 limit 0 n1]) ;
subplot (3,4,3) ;
plot (works(ss), M2(ss), 'o-') ; title ('nnz(diag) vs work') ;
axis ([0 limit 0 m1]) ;
drawnow
end
fprintf ('full time %10.4f blocks %8d nnz(diag) %8d\n', t1, n1, m1) ;
if (dopause)
input ('hit enter: ') ;
end
end
catch
% out-of-memory is OK, other errors are not
disp (lasterr) ;
if (isempty (strfind (lasterr, 'Out of memory')))
error (lasterr) ; %#ok
else
fprintf ('test terminated early, but otherwise OK\n') ;
end
end
close (h) ;
BTF/MATLAB/Test/test4.m0000640001170100242240000000407311164175552013763 0ustar davissparsefunction test4 (nmat)
%TEST4 test for BTF
% Requires UFget
% Example:
% test4
% See also btf, maxtrans, strongcomp, dmperm, UFget,
% test1, test2, test3, test4, test5.
% Copyright 2007, Timothy A. Davis, University of Florida
index = UFget ;
f = find (index.nrows == index.ncols) ;
[ignore i] = sort (index.nnz (f)) ;
f = f (i) ;
% time intensive
skip_costly = [1514 1297 1876 1301] ;
f = setdiff (f, skip_costly) ;
if (nargin < 1)
nmat = 1000 ;
end
nmat = min (nmat, length (f)) ;
f = f (1:nmat) ;
h = waitbar (0, 'BTF test 4 of 6') ;
try
for k = 1:nmat
Prob = UFget (f (k), index) ;
A = Prob.A ;
waitbar (k/nmat, h) ;
for tr = [1 -1]
if (tr == -1)
AT = A' ;
[m n] = size (A) ;
if (m == n)
if (nnz (spones (AT) - spones (A)) == 0)
fprintf ('skip transpose\n') ;
continue ;
end
end
A = AT ;
end
tic
[p1,q1,r1,work1] = btf (A) ;
t1 = toc ;
n1 = length (r1) - 1 ;
tic
[p2,q2,r2,work2] = btf (A, 10) ;
t2 = toc ;
n2 = length (r2) - 1 ;
fprintf (...
'%4d %4d : %10.4f %8d %8g : %10.4f %8d %8g :', ...
k, f(k), t1, n1, work1, t2, n2, work2) ;
if (t2 ~= 0)
fprintf (' rel %8.4f %8.4f' , t1 / t2, n2 / (max (1, n1))) ;
end
fprintf ('\n') ;
if (n1 ~= n2 | work1 ~= work2) %#ok
disp (Prob) ;
fprintf ('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n') ;
end
end
end
catch
% out-of-memory is OK, other errors are not
disp (lasterr) ;
if (isempty (strfind (lasterr, 'Out of memory')))
error (lasterr) ; %#ok
else
fprintf ('test terminated early, but otherwise OK\n') ;
end
end
close (h) ;
BTF/MATLAB/Test/checkbtf.m0000640001170100242240000000163011164175551014464 0ustar davissparsefunction checkbtf (A, p, q, r)
%CHECKBTF ensure A(p,q) is in BTF form
%
% A(p,q) is in BTF form, r the block boundaries
%
% Example:
% [p,q,r] = dmperm (A)
% checkbtf (A, p, q, r)
%
% See also drawbtf, maxtrans, strongcomp.
% Copyright 2007, Timothy A. Davis, University of Florida
[m n] = size (A) ;
if (m ~= n)
error ('A must be square') ;
end
if (any (sort (p) ~= 1:n))
error ('p not a permutation') ;
end
if (any (sort (q) ~= 1:n))
error ('q not a permutation') ;
end
nblocks = length (r) - 1 ;
if (r (1) ~= 1)
error ('r(1) not one') ;
end
if (r (end) ~= n+1)
error ('r(end) not n+1') ;
end
if (nblocks < 1 | nblocks > n) %#ok
error ('nblocks wrong') ;
end
nblocks = length (r) - 1 ;
rdiff = r (2:(nblocks+1)) - r (1:nblocks) ;
if (any (rdiff < 1) | any (rdiff > n)) %#ok
error ('r bad')
end
BTF/MATLAB/Test/test5.m0000640001170100242240000000371411164175552013765 0ustar davissparsefunction test5 (nmat)
%TEST5 test for BTF
% Requires UFget
% Example:
% test5
% See also btf, maxtrans, strongcomp, dmperm, UFget,
% test1, test2, test3, test4, test5.
% Copyright 2007, Timothy A. Davis, University of Florida
index = UFget ;
[ignore f] = sort (index.nnz) ;
% time intensive
skip_costly = [1514 1297 1876 1301] ;
f = setdiff (f, skip_costly) ;
if (nargin < 1)
nmat = 1000 ;
end
nmat = min (nmat, length (f)) ;
f = f (1:nmat) ;
h = waitbar (0, 'BTF test 5 of 6') ;
try
for k = 1:nmat
i = f(k) ;
Prob = UFget (i, index) ;
A = Prob.A ;
waitbar (k/nmat, h) ;
for tr = [1 -1]
if (tr == -1)
AT = A' ;
[m n] = size (A) ;
if (m == n)
if (nnz (spones (AT) - spones (A)) == 0)
fprintf ('skip test with transpose\n') ;
continue ;
end
end
A = AT ;
end
tic
q1 = maxtrans (A) ;
t1 = toc ;
r1 = sum (q1 > 0) ;
tic
q2 = maxtrans (A, 10) ;
t2 = toc ;
r2 = sum (q2 > 0) ;
fprintf (...
'%4d %4d : %10.4f %8d : %10.4f %8d', k, f(k), t1, r1, t2, r2) ;
fprintf (' rel sprank %8.4f', r2 / (max (1, r1))) ;
if (t2 ~= 0)
fprintf (': rel time %8.4f', t1 / t2) ;
end
fprintf ('\n') ;
if (r1 ~= r2)
disp (Prob) ;
fprintf ('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n') ;
end
end
end
catch
% out-of-memory is OK, other errors are not
disp (lasterr) ;
if (isempty (strfind (lasterr, 'Out of memory')))
error (lasterr) ; %#ok
else
fprintf ('test terminated early, but otherwise OK\n') ;
end
end
close (h) ;
BTF/MATLAB/Test/test1.m0000640001170100242240000000647211672162432013762 0ustar davissparsefunction test1 (nmat)
%TEST1 test for BTF
% Requires CSparse and UFget
% Example:
% test1
% See also btf, maxtrans, strongcomp, dmperm, UFget,
% test1, test2, test3, test4, test5.
% Copyright 2007, Timothy A. Davis, University of Florida
index = UFget ;
% f = find (index.sprank < min (index.nrows, index.ncols)) ;
f = 1:length (index.nrows) ;
% too much time:
skip = [1514 1297 1876 1301] ;
f = setdiff (f, skip) ;
[ignore i] = sort (index.nnz (f)) ;
f = f (i) ;
if (nargin < 1)
nmat = 1000 ;
end
nmat = min (nmat, length (f)) ;
f = f (1:nmat) ;
T0 = zeros (nmat,1) ;
T1 = zeros (nmat,1) ;
Anz = zeros (nmat,1) ;
MN = zeros (nmat, 2) ;
Nzdiag = zeros (nmat,1) ;
clf
% warmup
p = maxtrans (sparse (1)) ; %#ok
p = cs_dmperm (sparse (1)) ; %#ok
a = cs_transpose (sparse (1)) ; %#ok
h = waitbar (0, 'BTF test 1 of 6') ;
try
for k = 1:nmat
Prob = UFget (f (k), index) ;
A = Prob.A ;
clear Prob
t = 0 ;
waitbar (k/nmat, h) ;
r = full (sum (spones (A), 2)) ;
c = full (sum (spones (A))) ;
m2 = length (find (r > 0)) ;
n2 = length (find (c > 0)) ;
if (m2 < n2)
tic
A = cs_transpose (A) ;
t = toc ;
end
Nzdiag (k) = nnz (diag (A)) ;
[m n] = size (A) ;
Anz (k) = nnz (A) ;
MN (k,:) = [m n] ;
tic
q = maxtrans (A) ;
t0 = toc ;
s0 = sum (q > 0) ;
T0 (k) = max (1e-9, t0) ;
tic
p = cs_dmperm (A) ;
t1 = toc ;
s1 = sum (p > 0) ;
T1 (k) = max (1e-9, t1) ;
fprintf (...
'%4d maxtrans %10.6f %10.6f cs_dmperm %10.6f m/n %8.2f', ...
f(k), t, t0, t1, m/n) ;
if (t1 ~= 0)
fprintf (' rel: %8.4f', t0 / t1) ;
end
fprintf ('\n') ;
if (s0 ~= s1)
error ('!') ;
end
if (s0 == n & m == n) %#ok
B = A (:, q) ;
subplot (2,2,1) ;
cspy (B) ;
if (nnz (diag (B)) ~= n)
error ('?')
end
clear B
else
cspy (0) ;
end
maxnz = nnz (A) ;
zfree = find (MN (1:k,1) == MN (1:k,2) & Nzdiag (1:k) == MN(1:k,1)) ;
square = find (MN (1:k,1) == MN (1:k,2) & Nzdiag (1:k) ~= MN(1:k,1)) ;
tall = find (MN (1:k,1) > MN (1:k,2)) ;
squat = find (MN (1:k,1) < MN (1:k,2)) ;
subplot (2,2,2) ;
loglog (Anz (square), T0 (square) ./ T1 (square), ...
'o', [1 maxnz], [1 1], 'r-') ;
title ('square') ;
subplot (2,2,3) ;
loglog (Anz (tall), T0 (tall) ./ T1 (tall), ...
'o', [1 maxnz], [1 1], 'r-') ;
title ('tall') ;
subplot (2,2,4) ;
title ('square, intially zero-free') ;
loglog (Anz (zfree), T0 (zfree) ./ T1 (zfree), ...
'o', [1 maxnz], [1 1], 'r-') ;
title ('square, zero-free diag') ;
drawnow
end
catch
% out-of-memory is OK, other errors are not
disp (lasterr) ;
if (isempty (strfind (lasterr, 'Out of memory')))
error (lasterr) ; %#ok
else
fprintf ('test terminated early, but otherwise OK\n') ;
end
end
close (h) ;
BTF/MATLAB/Test/test2.m0000640001170100242240000000442411672162426013761 0ustar davissparsefunction test2 (nmat)
%TEST2 test for BTF
% Requires CSparse and UFget
% Example:
% test2
% See also btf, maxtrans, strongcomp, dmperm, UFget,
% test1, test2, test3, test4, test5.
% Copyright 2007, Timothy A. Davis, University of Florida
index = UFget ;
f = find (index.nrows == index.ncols) ;
% too much time:
skip = [1514 1297 1876 1301] ;
f = setdiff (f, skip) ;
[ignore i] = sort (index.nnz (f)) ;
f = f (i) ;
if (nargin < 1)
nmat = 1000 ;
end
nmat = min (nmat, length (f)) ;
f = f (1:nmat) ;
T0 = zeros (nmat,1) ;
T1 = zeros (nmat,1) ;
Anz = zeros (nmat,1) ;
MN = zeros (nmat, 2) ;
Nzdiag = zeros (nmat,1) ;
clf
% warmup
p = maxtrans (sparse (1)) ; %#ok
p = btf (sparse (1)) ; %#ok
p = cs_dmperm (sparse (1)) ; %#ok
a = cs_transpose (sparse (1)) ; %#ok
h = waitbar (0, 'BTF test 2 of 6') ;
try
for k = 1:nmat
Prob = UFget (f (k), index) ;
A = Prob.A ;
waitbar (k/nmat, h) ;
Nzdiag (k) = nnz (diag (A)) ;
[m n] = size (A) ;
Anz (k) = nnz (A) ;
MN (k,:) = [m n] ;
tic
[p,q,r] = btf (A) ;
t0 = toc ;
s0 = sum (q > 0) ;
T0 (k) = max (1e-9, t0) ;
tic
[p2,q2,r2] = cs_dmperm (A) ;
t1 = toc ;
s1 = sum (dmperm (A) > 0) ;
T1 (k) = max (1e-9, t1) ;
fprintf ('%4d btf %10.6f cs_dmperm %10.6f', f(k), t0, t1) ;
if (t1 ~= 0)
fprintf (' rel: %8.4f', t0 / t1) ;
end
fprintf ('\n') ;
if (s0 ~= s1)
error ('!') ;
end
C = A (p, abs (q)) ;
subplot (1,2,1) ;
cspy (C) ;
z = find (q < 0) ;
zd = nnz (diag (C (z,z))) ;
if (zd > 0)
error ('?') ;
end
minnz = Anz (1) ;
maxnz = nnz (A) ;
subplot (1,2,2) ;
loglog (Anz (1:k), T0 (1:k) ./ T1 (1:k), ...
'o', [minnz maxnz], [1 1], 'r-') ;
drawnow
clear C A Prob
end
catch
% out-of-memory is OK, other errors are not
disp (lasterr) ;
if (isempty (strfind (lasterr, 'Out of memory')))
error (lasterr) ; %#ok
else
fprintf ('test terminated early, but otherwise OK\n') ;
end
end
close (h) ;
BTF/MATLAB/Test/test3.m0000640001170100242240000003773611672162441013773 0ustar davissparsefunction test3 (nmat)
%TEST3 test for BTF
% Requires UFget
% Example:
% test3
% See also btf, maxtrans, strongcomp, dmperm, UFget,
% test1, test2, test3, test4, test5.
% Copyright 2007, Timothy A. Davis, Univ. of Florida
doplot = 1 ;
dopause = 0 ;
dostrong = 1 ;
index = UFget ;
f = find (index.nrows == index.ncols) ;
[ignore i] = sort (index.nnz (f)) ;
f = f (i) ;
clear i
% short test set: seg faults, lots of blocks, lots of work, and so on:
nasty = [
% --- various test matrices (no seg fault, quick run time)
-(1:8)' % generated matrices
904 % vanHeukelum/cage3 (5-by-5)
819 % Simon/raefsky6 (permuted triangular matrix)
%
% --- older seg faults:
264 % HB/west0156, causes older strongcomp_recursive to fail
824 % TOKAMAK/utm300 (300-by-300), causes older code to fail
868 % Pothen/bodyy4
%
% --- seg faults in old MATLAB dmperm
290 % Averous/epb3
983 % Sanghavi/ecl32
885 % Pothen/tandem_dual
879 % Pothen/onera_dual
955 % Schenk_IBMSDS/2D_54019_highK
957 % Schenk_IBMSDS/3D_51448_3D
958 % Schenk_IBMSDS/ibm_matrix_2
912 % vanHeukelum/cage11
924 % Andrews/Andrews
960 % Schenk_IBMSDS/matrix-new_3
862 % Kim/kim1
544 % Hamm/scircuit
897 % Norris/torso2
801 % Ronis/xenon1
53 % HB/bcsstk31
958 % Schenk_IBMSDS/matrix_9
844 % Cunningham/qa8fk
845 % Cunningham/qa8fk
821 % Simon/venkat25
822 % Simon/venkat50
820 % Simon/venkat01
812 % Simon/bbmat
804 % Rothberg/cfd1
54 % HB/bcsstk32
913 % vanHeukelum/cage12
846 % Boeing/bcsstk39
972 % Schenk_IBMSDS/para-10
974 % Schenk_IBMSDS/para-5
975 % Schenk_IBMSDS/para-6
976 % Schenk_IBMSDS/para-7
977 % Schenk_IBMSDS/para-8
978 % Schenk_IBMSDS/para-9
961 % Schenk_ISEI/barrier2-10
962 % Schenk_ISEI/barrier2-11
963 % Schenk_ISEI/barrier2-12
964 % Schenk_ISEI/barrier2-1
965 % Schenk_ISEI/barrier2-2
966 % Schenk_ISEI/barrier2-3
967 % Schenk_ISEI/barrier2-4
968 % Schenk_ISEI/barrier2-9
851 % Chen/pkustk05
979 % Kamvar/Stanford
374 % Bova/rma10
%
% --- lots of time:
395 % DRIVCAV/cavity16
396 % DRIVCAV/cavity17
397 % DRIVCAV/cavity18
398 % DRIVCAV/cavity19
399 % DRIVCAV/cavity20
400 % DRIVCAV/cavity21
401 % DRIVCAV/cavity22
402 % DRIVCAV/cavity23
403 % DRIVCAV/cavity24
404 % DRIVCAV/cavity25
405 % DRIVCAV/cavity26
1109 % Sandia/mult_dcop_01
1110 % Sandia/mult_dcop_02
1111 % Sandia/mult_dcop_03
376 % Brethour/coater2
284 % ATandT/onetone2
588 % Hollinger/mark3jac100
589 % Hollinger/mark3jac100sc
452 % Grund/bayer01
920 % Hohn/sinc12
590 % Hollinger/mark3jac120
591 % Hollinger/mark3jac120sc
809 % Shyy/shyy161
448 % Graham/graham1
283 % ATandT/onetone1
445 % Garon/garon2
541 % Hamm/bcircuit
592 % Hollinger/mark3jac140
593 % Hollinger/mark3jac140sc
435 % FIDAP/ex40
912 % Hohn/sinc15
894 % Norris/lung2
542 % Hamm/hcircuit
752 % Mulvey/finan512
753 % Mulvey/pfinan512
564 % Hollinger/g7jac180
565 % Hollinger/g7jac180sc
566 % Hollinger/g7jac200
567 % Hollinger/g7jac200sc
748 % Mallya/lhr34
749 % Mallya/lhr34c
922 % Hohn/sinc18
447 % Goodwin/rim
807 % Rothberg/struct3
286 % ATandT/twotone
982 % Tromble/language
953 % Schenk_IBMNA/c-73
890 % Norris/heart1
750 % Mallya/lhr71
751 % Mallya/lhr71c
925 % FEMLAB/ns3Da
827 % Vavasis/av41092
931 % FEMLAB/sme3Db
1297 % GHS_index/boyd2
1301 % GHS_indef/cont-300
%
% --- lots of time, and seg faults:
285 % ATandT/pre2
% --- huge matrix, turn off plotting
940 % Shenk/af_shell1, memory leak in plot, after call to btf, once.
% ----
]' ;
% maxtrans_recursive causes a seg fault on these matrices, because of
% stack overflow (this is expected)
skip_list_maxtrans_recursive = 285 ;
% p = dmperm (A) in MATLAB 7.4 causes a seg fault on these matrices:
skip_list_dmperm = [285 1301 1231 1251 1232 1241] ;
% [p,q,r] = dmperm (A) in MATLAB 7.4 causes a seg fault on these matrices:
skip_list_dmperm_btf = ...
[ 285 879 885 290 955 957 958 924 960 897 959 844 845 ...
821 822 820 804 913 846 972 974:978 961:968 979 940 ...
1422 1513 1412 1510 1301 1231 1251 1434 1213 1232 1241 1357 1579 1431 1281] ;
% length(skip_list_dmperm_btf)
% time intensive
skip_costly = [1514 1297 1876 1301] ;
% strongcomp (recursive) causes a seg fault on these matrices because of
% stack overflow (this is expected).
skip_list_strongcomp_recursive = ...
[983 285 879 885 290 955 957 958 912 924 960 862 544 897 801 53 959 844 845 ...
821 822 820 812 804 54 913 846 972 974:978 961:968 851 374 940] ;
skip_list_strongcomp_recursive = ...
[ skip_list_strongcomp_recursive 592 593 752 753 807 286 982 855 566 567 ] ;
% matrices with the largest # of nonzeros in the set (untested)
toobig = [
928 853 852 356 761 368 973 895 805 849 932 ...
803 854 936 802 850 537 856 898 857 859 971 937 ...
914 858 980 896 806 538 863 369 938 860 941 942 ...
943 944 945 946 947 948 915 939 916 ] ;
f = [ -(1:8) f ] ;
% f = nasty ;
h = waitbar (0, 'BTF test 3 of 6') ;
if (nargin < 1)
nmat = 1000 ;
end
nmat = min (nmat, length (f)) ;
f = f (1:nmat) ;
try
for matnum = 1:nmat
waitbar (matnum/nmat, h) ;
j = f (matnum) ;
if (any (j == toobig) | any (j == skip_costly)) %#ok
fprintf ('\n%4d: %3d %s/%s too big\n', ...
matnum, j, index.Group{j}, index.Name{j}) ;
continue ;
end
rand ('state', 0) ;
% clear all unused variables.
% nothing here is left that is proportional to the matrix size
clear A p1 p2 p3 q3 r3 match1 match2 match4 pa ra sa qa B C pb rb pc rc
clear jumble B11 B12 B13 B21 B22 B23 B31 B32 B33 pjumble qjumble ans
clear c kbad kgood
% whos
% pause
if (j > 0)
Problem = UFget (j, index) ;
name = Problem.name ;
A = Problem.A ;
clear Problem
else
% construct the jth test matrix
j = -j ;
if (j == 1 | j == 2) %#ok
B11 = UFget ('Grund/b1_ss') ; % 7-by-7 diagonal block
B11 = B11.A ;
B12 = sparse (zeros (7,2)) ;
B12 (3,2) = 1 ;
B13 = sparse (ones (7,5)) ;
B21 = sparse (zeros (2,7)) ;
B22 = sparse (ones (2,2)) ; % 2-by-2 diagonal block
B23 = sparse (ones (2,5)) ;
B31 = sparse (zeros (5,7)) ;
B32 = sparse (zeros (5,2)) ;
B33 = UFget ('vanHeukelum/cage3') ; % 5-by-5 diagonal block
B33 = B33.A ;
A = [ B11 B12 B13 ; B21 B22 B23 ; B31 B32 B33 ] ;
name = '(j=1 test matrix)' ;
end
if (j == 2)
pjumble = [ 10 7 11 1 13 12 8 2 5 14 9 6 4 3 ] ;
qjumble = [ 3 14 2 11 1 8 5 7 10 12 4 13 9 6 ] ;
A = A (pjumble, qjumble) ;
name = '(j=2 test matrix)' ;
elseif (j == 3)
A = sparse (1) ;
elseif (j == 4)
A = sparse (0) ;
elseif (j == 5)
A = sparse (ones (2)) ;
elseif (j == 6)
A = sparse (2,2) ;
elseif (j == 7)
A = speye (2) ;
elseif (j == 8)
A = sparse (2,2) ;
A (2,1) = 1 ;
end
if (j > 2)
full (A)
end
end
[m n] = size (A) ;
if (m ~= n)
continue ;
end
fprintf ('\n%4d: ', matnum) ;
fprintf (' =========================== Matrix: %3d %s\n', j, name) ;
fprintf ('n: %d nz: %d\n', n, nnz (A)) ;
if (nnz (A) > 6e6)
doplot = 0 ;
end
%-----------------------------------------------------------------------
% now try maxtrans
tic
match1 = maxtrans (A) ;
t = toc ;
s1 = sum (match1 > 0) ;
fprintf ('n-sprank: %d\n', n-s1) ;
fprintf ('maxtrans: %8.2f seconds\n', t) ;
singular = s1 < n ;
if (doplot)
clf
subplot (2,4,1)
spy (A)
title (name) ;
end
p1 = match1 ;
if (any (p1 <= 0))
% complete the permutation
badrow = find (p1 <= 0) ;
badcol = ones (1,n) ;
badcol (p1 (p1 > 0)) = 0 ;
badcol = find (badcol) ;
p1 (badrow) = badcol ;
% construct the older form of match1
match1 (badrow) = -p1 (badrow) ;
end
if (any (sort (p1) ~= 1:n))
error ('!!') ;
end
B = A (:,p1) ;
if (doplot)
subplot (2,4,2)
hold off
spy (B)
hold on
badcol = find (match1 < 0) ;
Junk = sparse (badcol, badcol, ones (length (badcol), 1), n, n) ;
% if (~isempty (A))
% spy (Junk, 'ro') ;
% end
title ('maxtrans') ;
end
d = nnz (diag (B)) ;
if (d ~= s1)
error ('bad sprank') ;
end
clear B
%-----------------------------------------------------------------------
% try p = dmperm(A)
skip_dmperm = any (j == skip_list_dmperm) ;
if (~skip_dmperm)
tic
match4 = dmperm (A) ;
t = toc ;
fprintf ('p=dmperm(A): %8.2f seconds\n', t) ;
s4 = sum (match4 > 0) ;
singular4 = (s4 < n) ;
if (doplot)
if (~singular4)
subplot (2,4,3)
spy (A (match4,:))
title ('dmperm') ;
end
end
if (singular ~= singular4)
error ('s4?') ;
end
if (s1 ~= s4)
error ('bad sprank') ;
end
else
fprintf ('p=dmperm(A): skip\n') ;
end
%-----------------------------------------------------------------------
nblocks = -1 ;
skip_dmperm_btf = any (j == skip_list_dmperm_btf) ;
if (~skip_dmperm_btf)
% get btf form
tic
[pa,qa,ra,sa] = dmperm (A) ;
t = toc ;
fprintf ('[p,q,r,s]=dmperm(A): %8.2f seconds\n', t) ;
nblocks = length (ra) - 1 ;
fprintf ('nblocks: %d\n', nblocks) ;
if (~singular4)
checkbtf (A, pa, qa, ra) ;
if (doplot)
subplot (2,4,4)
drawbtf (A, pa, qa, ra)
title ('dmperm blocks')
end
end
else
fprintf ('[p,q,r,s]=dmperm(A): skip\n') ;
end
jumble = randperm (n) ;
%-----------------------------------------------------------------------
% try strongcomp, non-recursive version
%-------------------------------------------------------------------
% try strongcomp on original matrix
B = A (:,p1) ;
tic ;
[pb,rb] = strongcomp (B) ;
t = toc ;
fprintf ('strongcomp %8.2f seconds\n', t) ;
if (~singular & ~skip_dmperm_btf & (length (rb) ~= nblocks+1)) %#ok
error ('BTF:invalid (rb)') ;
end
checkbtf (B, pb, pb, rb) ;
if (doplot)
subplot (2,4,5)
drawbtf (B, pb, pb, rb) ;
title ('strongcomp') ;
end
%-------------------------------------------------------------------
% try btf on original matrix
tic ;
[pw,qw,rw] = btf (A) ;
t = toc ;
fprintf ('btf %8.2f seconds nblocks %d\n', ...
t, length (rw)-1) ;
if (any (pw ~= pb))
error ('pw') ;
end
if (any (rw ~= rb))
error ('rw') ;
end
if (any (abs (qw) ~= p1 (pw)))
error ('qw') ;
end
c = diag (A (pw,abs (qw))) ;
if (~singular & ~skip_dmperm_btf & (length (rw) ~= nblocks+1)) %#ok
error ('BTF:invalid (rw)') ;
end
checkbtf (A, pw, abs (qw), rw) ;
kbad = find (qw < 0) ;
kgood = find (qw > 0) ;
if (any (c (kbad) ~= 0))
error ('kbad') ;
end
if (any (c (kgood) == 0)) %#ok
error ('kgood') ;
end
if (doplot)
subplot (2,4,6)
drawbtf (A, pw, abs (qw), rw) ;
if (n < 500)
for k = kbad
plot ([k (k+1) (k+1) k k]-.5, ...
[k k (k+1) (k+1) k]-.5, 'r') ;
end
end
title ('btf') ;
end
%-------------------------------------------------------------------
% try [p,q,r] = strongcomp (A, qin) form
tic
[pz,qz,rz] = strongcomp (A, match1) ;
t = toc ;
fprintf ('[p,q,r]=strongcomp(A,qin)%8.2f seconds\n', t) ;
if (any (pz ~= pb))
error ('pz') ;
end
if (any (rz ~= rb))
error ('rz') ;
end
if (any (abs (qz) ~= p1 (pz)))
error ('qz') ;
end
c = diag (A (pz,abs (qz))) ;
if (~singular & ~skip_dmperm_btf & (length (rz) ~= nblocks+1)) %#ok
error ('BTF:invalid (rz)') ;
end
checkbtf (A, pz, abs (qz), rz) ;
kbad = find (qz < 0) ;
kgood = find (qz > 0) ;
if (any (c (kbad) ~= 0))
error ('kbad') ;
end
if (any (c (kgood) == 0)) %#ok
error ('kgood') ;
end
if (doplot)
subplot (2,4,7)
drawbtf (A, pz, abs (qz), rz) ;
if (n < 500)
for k = kbad
plot ([k (k+1) (k+1) k k]-.5, ...
[k k (k+1) (k+1) k]-.5, 'r') ;
end
end
title ('strongcomp(A,qin)') ;
end
%-------------------------------------------------------------------
% try strongcomp again, on a randomly jumbled matrix
C = sparse (B (jumble, jumble)) ;
tic ;
[pc,rc] = strongcomp (C) ;
t = toc ;
fprintf ('strongcomp (rand) %8.2f seconds\n', t) ;
if (~singular & ~skip_dmperm_btf & (length (rc) ~= nblocks+1)) %#ok
error ('BTF:invalid (rc)') ;
end
checkbtf (C, pc, pc, rc) ;
if (doplot)
subplot (2,4,8)
drawbtf (C, pc, pc, rc) ;
title ('strongcomp(rand)') ;
end
if (length (rc) ~= length (rb))
error ('strongcomp random mismatch') ;
end
%-----------------------------------------------------------------------
if (doplot)
drawnow
end
if (matnum ~= nmat & dopause) %#ok
input ('Hit enter: ') ;
end
end
catch
% out-of-memory is OK, other errors are not
disp (lasterr) ;
if (isempty (strfind (lasterr, 'Out of memory')))
error (lasterr) ; %#ok
else
fprintf ('test terminated early, but otherwise OK\n') ;
end
end
close (h) ;
BTF/MATLAB/Test/btf_test.m0000640001170100242450000000052111670232522013754 0ustar davisfacfunction btf_test (nmat)
%BTF_TEST test for BTF
% Requires CSparse (or CXSparse) and UFget
% Example:
% btf_test
% See also btf, maxtrans, strongcomp, dmperm, UFget,
% test1, test2, test3, test4, test5, test6.
if (nargin < 1)
nmat = 200 ;
end
test1 (nmat) ;
test2 (nmat) ;
test3 (nmat) ;
test4 (nmat) ;
test5 (nmat) ;
test6 ;