# include # include # include # include # include # include # include using namespace std; int main ( int argc, char *argv[] ); int adj_bandwidth ( int node_num, int adj_num, int adj_row[], int adj[] ); int adj_perm_bandwidth ( int node_num, int adj_num, int adj_row[], int adj[], int perm[], int perm_inv[] ); int *adj_set_q4_mesh ( int node_num, int element_num, int element_node[], int element_neighbor[], int adj_num, int adj_row[] ); int adj_size_q4_mesh ( int node_num, int element_num, int element_node[], int element_neighbor[], int adj_row[] ); char ch_cap ( char ch ); bool ch_eqi ( char ch1, char ch2 ); int ch_to_digit ( char ch ); void degree ( int root, int adj_num, int adj_row[], int adj[], int mask[], int deg[], int *iccsze, int ls[], int node_num ); int file_column_count ( string filename ); int file_row_count ( string filename ); void genrcm ( int node_num, int adj_num, int adj_row[], int adj[], int perm[] ); int i4_max ( int i1, int i2 ); int i4_min ( int i1, int i2 ); int i4_modp ( int i, int j ); void i4_swap ( int *i, int *j ); int i4_wrap ( int ival, int ilo, int ihi ); int i4col_compare ( int m, int n, int a[], int i, int j ); void i4col_sort_a ( int m, int n, int a[] ); void i4col_swap ( int m, int n, int a[], int irow1, int irow2 ); int *i4mat_data_read ( string input_filename, int m, int n ); void i4mat_header_read ( string input_filename, int *m, int *n ); void i4mat_transpose_print_some ( int m, int n, int a[], int ilo, int jlo, int ihi, int jhi, string title ); void i4mat_write ( string output_filename, int m, int n, int table[] ); void i4vec_heap_d ( int n, int a[] ); void i4vec_print ( int n, int a[], string title ); void i4vec_reverse ( int n, int a[] ); void i4vec_sort_heap_a ( int n, int a[] ); void level_set ( int root, int adj_num, int adj_row[], int adj[], int mask[], int *level_num, int level_row[], int level[], int node_num ); void mesh_base_zero ( int node_num, int element_order, int element_num, int element_node[] ); int *neighbor_elements_q4_mesh ( int element_num, int element_node[] ); bool perm_check ( int n, int p[] ); void perm_inverse3 ( int n, int perm[], int perm_inv[] ); void r8col_permute ( int m, int n, int p[], double a[] ); double *r8mat_data_read ( string input_filename, int m, int n ); void r8mat_header_read ( string input_filename, int *m, int *n ); void r8mat_transpose_print_some ( int m, int n, double a[], int ilo, int jlo, int ihi, int jhi, string title ); void r8mat_write ( string output_filename, int m, int n, double table[] ); void rcm ( int root, int adj_num, int adj_row[], int adj[], int mask[], int perm[], int *iccsze, int node_num ); void root_find ( int *root, int adj_num, int adj_row[], int adj[], int mask[], int *level_num, int level_row[], int level[], int node_num ); int s_len_trim ( string s ); int s_to_i4 ( string s, int *last, bool *error ); bool s_to_i4vec ( string s, int n, int ivec[] ); double s_to_r8 ( string s, int *lchar, bool *error ); bool s_to_r8vec ( string s, int n, double rvec[] ); int s_word_count ( string s ); void sort_heap_external ( int n, int *indx, int *i, int *j, int isgn ); void timestamp ( ); //****************************************************************************80 int main ( int argc, char *argv[] ) //****************************************************************************80 // // Purpose: // // MAIN is the main program for QUAD_MESH_RCM. // // Discussion: // // QUAD_MESH_RCM applies the RCM reordering to a quadrilateral mesh. // // The user supplies a node file and an element file, containing // the coordinates of the nodes, and the indices of the nodes that // make up each element. // // The program reads the data, computes the adjacency information, // carries out the RCM algorithm to get the permutation, applies // the permutation to the nodes and elements, and writes out // new node and element files that correspond to the RCM permutation. // // Note that node data is normally two dimensional, that is, // each node has an X and Y coordinate. In some applications, it // may be desirable to specify more information. This program // will accept node data that includes DIM_NUM entries on each line, // as long as DIM_NUM is the same for each entry. // // Usage: // // quad_mesh_rcm prefix // // where 'prefix' is the common filename prefix: // // * prefix_nodes.txt contains the node coordinates, // * prefix_elements.txt contains the element definitions. // * prefix_rcm_nodes.txt will contain the RCM node coordinates, // * prefix_rcm_elements.txt will contain the RCM element definitions. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 30 September 2009 // // Author: // // John Burkardt // { int *adj; int adj_num; int *adj_row; int bandwidth; int dim_num; string element_filename; int *element_neighbor; int *element_node; int element_num; int element_order; string element_rcm_filename; int i; int j; int node; string node_filename; int node_num; string node_rcm_filename; double *node_xy; int *perm; int *perm_inv; string prefix; cout << "\n"; timestamp ( ); cout << "\n"; cout << "QUAD_MESH_RCM\n"; cout << " C++ version:\n"; cout << " Read a node dataset of NODE_NUM points in 2 dimensions.\n"; cout << " Read an associated quad mesh dataset of ELEMENT_NUM\n"; cout << " 4 node quaderilaterals.\n"; cout << "\n"; cout << " Apply the RCM reordering (Reverse Cuthill-McKee).\n"; cout << "\n"; cout << " Reorder the data and write it out to files.\n"; cout << "\n"; cout << " Compiled on " << __DATE__ << " at " << __TIME__ << ".\n"; // // Get the filename prefix. // if ( argc <= 1 ) { cout << "\n"; cout << "QUAD_MESH_RCM\n"; cout << " Please enter the filename prefix.\n"; cin >> prefix; } else { prefix = argv[1]; } // // Create the filenames. // node_filename = prefix + "_nodes.txt"; element_filename = prefix + "_elements.txt"; node_rcm_filename = prefix + "_rcm_nodes.txt"; element_rcm_filename = prefix + "_rcm_elements.txt"; // // Read the data. // r8mat_header_read ( node_filename, &dim_num, &node_num ); cout << "\n"; cout << " Read the header of \"" << node_filename << "\".\n"; cout << "\n"; cout << " Spatial dimension DIM_NUM = " << dim_num << "\n"; cout << " Number of nodes NODE_NUM = " << node_num << "\n"; node_xy = r8mat_data_read ( node_filename, dim_num, node_num ); cout << "\n"; cout << " Read the data in \"" << node_filename << "\".\n"; r8mat_transpose_print_some ( dim_num, node_num, node_xy, 1, 1, dim_num, 5, " Coordinates of first 5 nodes:" ); i4mat_header_read ( element_filename, &element_order, &element_num ); cout << "\n"; cout << " Read the header of \"" << element_filename << "\".\n"; cout << "\n"; cout << " Element order = " << element_order << "\n"; cout << " Number of elements = " << element_num << "\n"; if ( element_order != 4 ) { cout << "\n"; cout << "QUAD_MESH_RCM - Fatal error!\n"; cout << " Data is not for 4-node quadrilaterals.\n"; exit ( 1 ); } element_node = i4mat_data_read ( element_filename, element_order, element_num ); cout << "\n"; cout << " Read the data in \"" << element_filename << "\".\n"; i4mat_transpose_print_some ( element_order, element_num, element_node, 1, 1, element_order, 5, " First 5 elements:" ); // // Detect and correct 1-based node indexing. // mesh_base_zero ( node_num, element_order, element_num, element_node ); // // Create the element neighbor array. // element_neighbor = neighbor_elements_q4_mesh ( element_num, element_node ); // // Count the number of adjacencies, and set up the ADJ_ROW // adjacency pointer array. // adj_row = new int [node_num+1]; adj_num = adj_size_q4_mesh ( node_num, element_num, element_node, element_neighbor, adj_row ); // // Set up the ADJ adjacency array. // adj = adj_set_q4_mesh ( node_num, element_num, element_node, element_neighbor, adj_num, adj_row ); bandwidth = adj_bandwidth ( node_num, adj_num, adj_row, adj ); cout << "\n"; cout << " ADJ bandwidth = " << bandwidth << "\n"; // // Compute the RCM permutation. // perm = new int[node_num]; // // For now, add 1 to ADJ and ADJ_ROW since GENRCM assumes 1-based indexing. // for ( i = 0; i < node_num + 1; i++ ) { adj_row[i] = adj_row[i] + 1; } for ( i = 0; i < adj_num; i++ ) { adj[i] = adj[i] + 1; } genrcm ( node_num, adj_num, adj_row, adj, perm ); // // On return, subtract 1 from ADJ, ADJROW and PERM. // for ( i = 0; i < node_num; i++ ) { perm[i] = perm[i] - 1; } for ( i = 0; i < node_num + 1; i++ ) { adj_row[i] = adj_row[i] - 1; } for ( i = 0; i < adj_num; i++ ) { adj[i] = adj[i] - 1; } perm_inv = new int[node_num]; perm_inverse3 ( node_num, perm, perm_inv ); bandwidth = adj_perm_bandwidth ( node_num, adj_num, adj_row, adj, perm, perm_inv ); cout << "\n"; cout << " Permuted ADJ bandwidth = " << bandwidth << "\n"; // // Permute the nodes according to the permutation vector. // r8col_permute ( dim_num, node_num, perm, node_xy ); // // Permute the node indices in the element array. // for ( j = 0; j < element_num; j++ ) { for ( i = 0; i < element_order; i++ ) { node = element_node[i+j*element_order]; element_node[i+j*element_order] = perm_inv[node]; } } // // Write out the new data. // r8mat_write ( node_rcm_filename, dim_num, node_num, node_xy ); cout << "\n"; cout << " Created the node file \"" << node_rcm_filename << "\".\n"; i4mat_write ( element_rcm_filename, element_order, element_num, element_node ); cout << "\n"; cout << " Created the element file \"" << element_rcm_filename << "\".\n"; // // Free up memory. // delete [] adj; delete [] adj_row; delete [] element_neighbor; delete [] element_node; delete [] node_xy; delete [] perm; delete [] perm_inv; cout << "\n"; cout << "QUAD_MESH_RCM:\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); return 0; } //****************************************************************************80 int adj_bandwidth ( int node_num, int adj_num, int adj_row[], int adj[] ) //****************************************************************************80 // // Purpose: // // ADJ_BANDWIDTH computes the bandwidth of an adjacency matrix. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 30 September 2009 // // Author: // // John Burkardt // // Reference: // // Alan George, Joseph Liu, // Computer Solution of Large Sparse Positive Definite Systems, // Prentice Hall, 1981. // // Parameters: // // Input, int NODE_NUM, the number of nodes. // // Input, int ADJ_NUM, the number of adjacency entries. // // Input, int ADJ_ROW[NODE_NUM+1]. Information about row I is stored // in entries ADJ_ROW(I) through ADJ_ROW(I+1)-1 of ADJ. // // Input, int ADJ[ADJ_NUM], the adjacency structure. // For each row, it contains the column indices of the nonzero entries. // // Output, int ADJ_BANDWIDTH, the bandwidth of the adjacency // matrix. // { int band_hi; int band_lo; int col; int i; int j; int value; band_lo = 0; band_hi = 0; for ( i = 0; i < node_num; i++ ) { for ( j = adj_row[i]; j <= adj_row[i+1]-1; j++ ) { col = adj[j]; band_lo = i4_max ( band_lo, i - col ); band_hi = i4_max ( band_hi, col - i ); } } value = band_lo + 1 + band_hi; return value; } //****************************************************************************80 int adj_perm_bandwidth ( int node_num, int adj_num, int adj_row[], int adj[], int perm[], int perm_inv[] ) //****************************************************************************80 // // Purpose: // // ADJ_PERM_BANDWIDTH computes the bandwidth of a permuted adjacency matrix. // // Discussion: // // The matrix is defined by the adjacency information and a permutation. // // The routine also computes the bandwidth and the size of the envelope. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 30 September 2009 // // Author: // // John Burkardt // // Reference: // // Alan George, Joseph Liu, // Computer Solution of Large Sparse Positive Definite Systems, // Prentice Hall, 1981. // // Parameters: // // Input, int NODE_NUM, the number of nodes. // // Input, int ADJ_NUM, the number of adjacency entries. // // Input, int ADJ_ROW[NODE_NUM+1]. Information about row I is stored // in entries ADJ_ROW(I) through ADJ_ROW(I+1)-1 of ADJ. // // Input, int ADJ[ADJ_NUM], the adjacency structure. // For each row, it contains the column indices of the nonzero entries. // // Input, int PERM[NODE_NUM], PERM_INV(NODE_NUM), the permutation // and inverse permutation. // // Output, int ADJ_PERM_BANDWIDTH, the bandwidth of the permuted // adjacency matrix. // { int band_hi; int band_lo; int bandwidth; int col; int i; int j; band_lo = 0; band_hi = 0; for ( i = 0; i < node_num; i++ ) { for ( j = adj_row[perm[i]]; j <= adj_row[perm[i]+1] - 1; j++ ) { col = perm_inv[adj[j]]; band_lo = i4_max ( band_lo, i - col ); band_hi = i4_max ( band_hi, col - i ); } } bandwidth = band_lo + 1 + band_hi; return bandwidth; } //****************************************************************************80 int *adj_set_q4_mesh ( int node_num, int element_num, int element_node[], int element_neighbor[], int adj_num, int adj_row[] ) //****************************************************************************80 // // Purpose: // // ADJ_SET_Q4_MESH sets adjacencies in a triangulation. // // Discussion: // // This routine is called to set the adjacencies, after the // appropriate amount of memory has been set aside for storage. // // The mesh is assumed to involve 4-node quadrilaterals. // // Two nodes are "adjacent" if they are both nodes in some element. // Also, a node is considered to be adjacent to itself. // // This routine can be used to create the compressed column storage // for a linear element finite element discretization of // Poisson's equation in two dimensions. // // Diagram: // // side 3 // 4-------3 // s | | s // i | | i // d | | d // e | | e // | | // 4 | | 2 // | | // 1-------2 // // side 1 // // The local node numbering // // // 20-21-22-23-24 // | | | | | // | | | | | // 15-16-17-18-19 // | | | | | // | | | | | // 10-11-12-13-14 // | | | | | // | | | | | // 5--6--7--8--9 // | | | | | // | | | | | // 0--1--2--3--4 // // A sample grid. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 28 September 2009 // // Author: // // John Burkardt // // Parameters // // Input, int NODE_NUM, the number of nodes. // // Input, int ELEMENT_NUM, the number of elements. // // Input, int ELEMENT_NODE[4*ELEMENT_NUM], lists the nodes that // make up each element in counterclockwise order. // // Input, int ELEMENT_NEIGHBOR[4*ELEMENT_NUM], for each side of // an element, lists the neighboring element, or -1 if there is // no neighbor. // // Input, int ADJ_NUM, the number of adjacencies. // // Input, int ADJ_ROW[NODE_NUM+1]. Information about column J is stored // in entries ADJ_ROW(J) through ADJ_ROW(J+1)-1 of ADJ. // // Output, int ADJ_SET_Q4_MESH[ADJ_NUM], the adjacency information. // { int *adj; int *adj_copy; int k; int k1; int k2; int n1; int n2; int n3; int n4; int node; int element; int element2; int element_order = 4; adj = new int[adj_num]; for ( k = 0; k < adj_num; k++ ) { adj[k] = -1; } adj_copy = new int[node_num]; for ( node = 0; node < node_num; node++ ) { adj_copy[node] = adj_row[node]; } // // Set every node to be adjacent to itself. // for ( node = 0; node < node_num; node++ ) { adj[adj_copy[node]] = node; adj_copy[node] = adj_copy[node] + 1; } // // Examine each element. // for ( element = 0; element < element_num; element++ ) { n1 = element_node[0+element*element_order]; n2 = element_node[1+element*element_order]; n3 = element_node[2+element*element_order]; n4 = element_node[3+element*element_order]; // // Add edges (1,3) and (2,4). There is no need to check for redundancy, // since this is the only case when these nodes can share an element. // adj[adj_copy[n1]] = n3; adj_copy[n1] = adj_copy[n1] + 1; adj[adj_copy[n3]] = n1; adj_copy[n3] = adj_copy[n3] + 1; adj[adj_copy[n2]] = n4; adj_copy[n2] = adj_copy[n2] + 1; adj[adj_copy[n4]] = n2; adj_copy[n4] = adj_copy[n4] + 1; // // Add edge (1,2) if this is the first occurrence, // that is, if the edge (1,2) is on a boundary (ELEMENT2 <= 0) // or if this element is the first of the pair in which the edge // occurs (ELEMENT < ELEMENT2). // element2 = element_neighbor[0+element*4]; if ( element2 < 0 || element < element2 ) { adj[adj_copy[n1]] = n2; adj_copy[n1] = adj_copy[n1] + 1; adj[adj_copy[n2]] = n1; adj_copy[n2] = adj_copy[n2] + 1; } // // Add edge (2,3). // element2 = element_neighbor[1+element*4]; if ( element2 < 0 || element < element2 ) { adj[adj_copy[n2]] = n3; adj_copy[n2] = adj_copy[n2] + 1; adj[adj_copy[n3]] = n2; adj_copy[n3] = adj_copy[n3] + 1; } // // Add edge (3,4). // element2 = element_neighbor[2+element*4]; if ( element2 < 0 || element < element2 ) { adj[adj_copy[n4]] = n3; adj_copy[n4] = adj_copy[n4] + 1; adj[adj_copy[n3]] = n4; adj_copy[n3] = adj_copy[n3] + 1; } // // Add edge (4,1). // element2 = element_neighbor[3+element*4]; if ( element2 < 0 || element < element2 ) { adj[adj_copy[n1]] = n4; adj_copy[n1] = adj_copy[n1] + 1; adj[adj_copy[n4]] = n1; adj_copy[n4] = adj_copy[n4] + 1; } } // // Ascending sort the entries for each node. // for ( node = 0; node < node_num; node++ ) { k1 = adj_row[node]; k2 = adj_row[node+1]-1; i4vec_sort_heap_a ( k2+1-k1, adj+k1 ); } delete [] adj_copy; return adj; } //****************************************************************************80 int adj_size_q4_mesh ( int node_num, int element_num, int element_node[], int element_neighbor[], int adj_row[] ) //****************************************************************************80 // // Purpose: // // ADJ_SIZE_Q4_MESH counts adjacencies in a Q4 mesh. // // Discussion: // // This routine is called to count the adjacencies, so that the // appropriate amount of memory can be set aside for storage when // the adjacency structure is created. // // The mesh is assumed to involve 4-node quadrilaterals. // // Two nodes are "adjacent" if they are both nodes in some quadrilateral. // Also, a node is considered to be adjacent to itself. // // Diagram: // // side 3 // 4-------3 // s | | s // i | | i // d | | d // e | | e // | | // 4 | | 2 // | | // 1-------2 // // side 1 // // The local node numbering // // // 20-21-22-23-24 // | | | | | // | | | | | // 15-16-17-18-19 // | | | | | // | | | | | // 10-11-12-13-14 // | | | | | // | | | | | // 5--6--7--8--9 // | | | | | // | | | | | // 0--1--2--3--4 // // A sample grid. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 29 September 2009 // // Author: // // John Burkardt // // Parameters // // Input, int NODE_NUM, the number of nodes. // // Input, int ELEMENT_NUM, the number of elements. // // Input, int ELEMENT_NODE[4*ELEMENT_NUM], lists the nodes that // make up each element, in counterclockwise order. // // Input, int ELEMENT_NEIGHBOR[4*ELEMENT_NUM], for each side of // a element, lists the neighboring elment, or -1 if there is // no neighbor. // // Output, int ADJ_ROW[NODE_NUM+1], Information about column J is stored // in entries ADJ_ROW[J] through ADJ_ROW[J+1]-1 of ADJ. // // Output, int ADJ_SIZE_Q4_MESH, the number of adjacencies. // { int adj_num; int element; int element_order = 4; int element2; int i; int n1; int n2; int n3; int n4; int node; adj_num = 0; // // Set every node to be adjacent to itself. // for ( node = 0; node < node_num; node++ ) { adj_row[node] = 1; } // // Examine each element. // for ( element = 0; element < element_num; element++ ) { n1 = element_node[0+element*element_order]; n2 = element_node[1+element*element_order]; n3 = element_node[2+element*element_order]; n4 = element_node[3+element*element_order]; // // Add edge (1,3). // adj_row[n1] = adj_row[n1] + 1; adj_row[n3] = adj_row[n3] + 1; // // Add edge (2,4). // adj_row[n2] = adj_row[n2] + 1; adj_row[n4] = adj_row[n4] + 1; // // Add edge (1,2) if this is the first occurrence, // that is, if the edge (1,2) is on a boundary (ELEMENT2 <= 0) // or if this element is the first of the pair in which the edge // occurs (ELEMENT < ELEMENT2). // element2 = element_neighbor[0+element*4]; if ( element2 < 0 || element < element2 ) { adj_row[n1] = adj_row[n1] + 1; adj_row[n2] = adj_row[n2] + 1; } // // Add edge (2,3). // element2 = element_neighbor[1+element*4]; if ( element2 < 0 || element < element2 ) { adj_row[n2] = adj_row[n2] + 1; adj_row[n3] = adj_row[n3] + 1; } // // Add edge (3,4). // element2 = element_neighbor[2+element*4]; if ( element2 < 0 || element < element2 ) { adj_row[n3] = adj_row[n3] + 1; adj_row[n4] = adj_row[n4] + 1; } // // Add edge (4,1). // element2 = element_neighbor[3+element*4]; if ( element2 < 0 || element < element2 ) { adj_row[n4] = adj_row[n4] + 1; adj_row[n1] = adj_row[n1] + 1; } } // // We used ADJ_ROW to count the number of entries in each column. // Convert it to pointers into the ADJ array. // for ( node = node_num; 1 <= node; node-- ) { adj_row[node] = adj_row[node-1]; } adj_row[0] = 0; for ( i = 1; i <= node_num; i++ ) { adj_row[i] = adj_row[i] + adj_row[i-1]; } // // Finally, record the total number of adjacencies. // adj_num = adj_row[node_num]; return adj_num; } //****************************************************************************80 char ch_cap ( char ch ) //****************************************************************************80 // // Purpose: // // CH_CAP capitalizes a single character. // // Discussion: // // This routine should be equivalent to the library "toupper" function. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 19 July 1998 // // Author: // // John Burkardt // // Parameters: // // Input, char CH, the character to capitalize. // // Output, char CH_CAP, the capitalized character. // { if ( 97 <= ch && ch <= 122 ) { ch = ch - 32; } return ch; } //****************************************************************************80 bool ch_eqi ( char ch1, char ch2 ) //****************************************************************************80 // // Purpose: // // CH_EQI is true if two characters are equal, disregarding case. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 13 June 2003 // // Author: // // John Burkardt // // Parameters: // // Input, char CH1, CH2, the characters to compare. // // Output, bool CH_EQI, is true if the two characters are equal, // disregarding case. // { if ( 97 <= ch1 && ch1 <= 122 ) { ch1 = ch1 - 32; } if ( 97 <= ch2 && ch2 <= 122 ) { ch2 = ch2 - 32; } return ( ch1 == ch2 ); } //****************************************************************************80 int ch_to_digit ( char ch ) //****************************************************************************80 // // Purpose: // // CH_TO_DIGIT returns the integer value of a base 10 digit. // // Example: // // CH DIGIT // --- ----- // '0' 0 // '1' 1 // ... ... // '9' 9 // ' ' 0 // 'X' -1 // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 13 June 2003 // // Author: // // John Burkardt // // Parameters: // // Input, char CH, the decimal digit, '0' through '9' or blank are legal. // // Output, int CH_TO_DIGIT, the corresponding integer value. If the character was // 'illegal', then DIGIT is -1. // { int digit; if ( '0' <= ch && ch <= '9' ) { digit = ch - '0'; } else if ( ch == ' ' ) { digit = 0; } else { digit = -1; } return digit; } //****************************************************************************80 void degree ( int root, int adj_num, int adj_row[], int adj[], int mask[], int deg[], int *iccsze, int ls[], int node_num ) //****************************************************************************80 // // Purpose: // // DEGREE computes the degrees of the nodes in the connected component. // // Discussion: // // The connected component is specified by MASK and ROOT. // Nodes for which MASK is zero are ignored. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 05 January 2007 // // Author: // // Original FORTRAN77 version by Alan George, Joseph Liu. // C++ version by John Burkardt // // Reference: // // Alan George, Joseph Liu, // Computer Solution of Large Sparse Positive Definite Systems, // Prentice Hall, 1981. // // Parameters: // // Input, int ROOT, the node that defines the connected component. // // Input, int ADJ_NUM, the number of adjacency entries. // // Input, int ADJ_ROW[NODE_NUM+1]. Information about row I is stored // in entries ADJ_ROW(I) through ADJ_ROW(I+1)-1 of ADJ. // // Input, int ADJ[ADJ_NUM], the adjacency structure. // For each row, it contains the column indices of the nonzero entries. // // Input, int MASK[NODE_NUM], is nonzero for those nodes which are // to be considered. // // Output, int DEG[NODE_NUM], contains, for each node in the connected // component, its degree. // // Output, int *ICCSIZE, the number of nodes in the connected component. // // Output, int LS[NODE_NUM], stores in entries 1 through ICCSIZE the nodes // in the connected component, starting with ROOT, and proceeding // by levels. // // Input, int NODE_NUM, the number of nodes. // { int i; int ideg; int j; int jstop; int jstrt; int lbegin; int lvlend; int lvsize; int nbr; int node; // // The sign of ADJ_ROW(I) is used to indicate if node I has been considered. // ls[0] = root; adj_row[root-1] = -adj_row[root-1]; lvlend = 0; *iccsze = 1; // // LBEGIN is the pointer to the beginning of the current level, and // LVLEND points to the end of this level. // for ( ; ; ) { lbegin = lvlend + 1; lvlend = *iccsze; // // Find the degrees of nodes in the current level, // and at the same time, generate the next level. // for ( i = lbegin; i <= lvlend; i++ ) { node = ls[i-1]; jstrt = -adj_row[node-1]; jstop = abs ( adj_row[node] ) - 1; ideg = 0; for ( j = jstrt; j <= jstop; j++ ) { nbr = adj[j-1]; if ( mask[nbr-1] != 0 ) { ideg = ideg + 1; if ( 0 <= adj_row[nbr-1] ) { adj_row[nbr-1] = -adj_row[nbr-1]; *iccsze = *iccsze + 1; ls[*iccsze-1] = nbr; } } } deg[node-1] = ideg; } // // Compute the current level width. // lvsize = *iccsze - lvlend; // // If the current level width is nonzero, generate another level. // if ( lvsize == 0 ) { break; } } // // Reset ADJ_ROW to its correct sign and return. // for ( i = 0; i < *iccsze; i++ ) { node = ls[i] - 1; adj_row[node] = -adj_row[node]; } return; } //****************************************************************************80 int file_column_count ( string filename ) //****************************************************************************80 // // Purpose: // // FILE_COLUMN_COUNT counts the columns in the first line of a file. // // Discussion: // // The file is assumed to be a simple text file. // // Most lines of the file are presumed to consist of COLUMN_NUM words, separated // by spaces. There may also be some blank lines, and some comment lines, // which have a "#" in column 1. // // The routine tries to find the first non-comment non-blank line and // counts the number of words in that line. // // If all lines are blanks or comments, it goes back and tries to analyze // a comment line. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 30 January 2009 // // Author: // // John Burkardt // // Parameters: // // Input, string FILENAME, the name of the file. // // Output, int FILE_COLUMN_COUNT, the number of columns assumed // to be in the file. // { int column_num; ifstream input; bool got_one; char text[255]; // // Open the file. // input.open ( filename.c_str ( ) ); if ( !input ) { column_num = -1; cerr << "\n"; cerr << "FILE_COLUMN_COUNT - Fatal error!\n"; cerr << " Could not open the file:\n"; cerr << " \"" << filename << "\"\n"; return column_num; } // // Read one line, but skip blank lines and comment lines. // got_one = false; for ( ; ; ) { input.getline ( text, sizeof ( text ) ); if ( input.eof ( ) ) { break; } if ( s_len_trim ( text ) == 0 ) { continue; } if ( text[0] == '#' ) { continue; } got_one = true; break; } if ( !got_one ) { input.close ( ); input.open ( filename.c_str ( ) ); for ( ; ; ) { input.getline ( text, sizeof ( text ) ); if ( input.eof ( ) ) { break; } if ( s_len_trim ( text ) == 0 ) { continue; } got_one = true; break; } } input.close ( ); if ( !got_one ) { cerr << "\n"; cerr << "FILE_COLUMN_COUNT - Warning!\n"; cerr << " The file does not seem to contain any data.\n"; return -1; } column_num = s_word_count ( text ); return column_num; } //****************************************************************************80 int file_row_count ( string filename ) //****************************************************************************80 // // Purpose: // // FILE_ROW_COUNT counts the number of row records in a file. // // Discussion: // // It does not count lines that are blank, or that begin with a // comment symbol '#'. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 30 January 2009 // // Author: // // John Burkardt // // Parameters: // // Input, string FILENAME, the name of the input file. // // Output, int FILE_ROW_COUNT, the number of rows found. // { int comment_num; ifstream input; int record_num; int row_num; char text[255]; row_num = 0; comment_num = 0; record_num = 0; input.open ( filename.c_str ( ) ); if ( !input ) { cerr << "\n"; cerr << "FILE_ROW_COUNT - Fatal error!\n"; cerr << " Could not open the file: \"" << filename << "\"\n"; exit ( 1 ); } for ( ; ; ) { input.getline ( text, sizeof ( text ) ); if ( input.eof ( ) ) { break; } record_num = record_num + 1; if ( text[0] == '#' ) { comment_num = comment_num + 1; continue; } if ( s_len_trim ( text ) == 0 ) { comment_num = comment_num + 1; continue; } row_num = row_num + 1; } input.close ( ); return row_num; } //****************************************************************************80 void genrcm ( int node_num, int adj_num, int adj_row[], int adj[], int perm[] ) //****************************************************************************80 // // Purpose: // // GENRCM finds the reverse Cuthill-Mckee ordering for a general graph. // // Discussion: // // For each connected component in the graph, the routine obtains // an ordering by calling RCM. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 05 January 2007 // // Author: // // Original FORTRAN77 version by Alan George, Joseph Liu. // C++ version by John Burkardt. // // Reference: // // Alan George, Joseph Liu, // Computer Solution of Large Sparse Positive Definite Systems, // Prentice Hall, 1981. // // Parameters: // // Input, int NODE_NUM, the number of nodes. // // Input, int ADJ_NUM, the number of adjacency entries. // // Input, int ADJ_ROW[NODE_NUM+1]. Information about row I is stored // in entries ADJ_ROW(I) through ADJ_ROW(I+1)-1 of ADJ. // // Input, int ADJ[ADJ_NUM], the adjacency structure. // For each row, it contains the column indices of the nonzero entries. // // Output, int PERM[NODE_NUM], the RCM ordering. // // Local Parameters: // // Local, int LEVEL_ROW[NODE_NUM+1], the index vector for a level // structure. The level structure is stored in the currently unused // spaces in the permutation vector PERM. // // Local, int MASK[NODE_NUM], marks variables that have been numbered. // { int i; int iccsze; int level_num; int *level_row; int *mask; int num; int OFFSET = 1; int root; level_row = new int[node_num+1]; mask = new int[node_num]; for ( i = 0; i < node_num; i++ ) { mask[i] = 1; } num = 0 + OFFSET; for ( i = 0; i < node_num; i++ ) { // // For each masked connected component... // if ( mask[i] != 0 ) { root = i + OFFSET; // // Find a pseudo-peripheral node ROOT. The level structure found by // ROOT_FIND is stored starting at PERM(NUM). // root_find ( &root, adj_num, adj_row, adj, mask, &level_num, level_row, perm+num-OFFSET, node_num ); cout << "ROOT = " << root << "\n";; // // RCM orders the component using ROOT as the starting node. // rcm ( root, adj_num, adj_row, adj, mask, perm+num-OFFSET, &iccsze, node_num ); num = num + iccsze; // // We can stop once every node is in one of the connected components. // if ( node_num - 1 + OFFSET < num ) { delete [] level_row; delete [] mask; return; } } } delete [] level_row; delete [] mask; return; } //****************************************************************************80 int i4_max ( int i1, int i2 ) //****************************************************************************80 // // Purpose: // // I4_MAX returns the maximum of two I4's. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 13 October 1998 // // Author: // // John Burkardt // // Parameters: // // Input, int I1, I2, are two integers to be compared. // // Output, int I4_MAX, the larger of I1 and I2. // { int value; if ( i2 < i1 ) { value = i1; } else { value = i2; } return value; } //****************************************************************************80 int i4_min ( int i1, int i2 ) //****************************************************************************80 // // Purpose: // // I4_MIN returns the minimum of two I4's. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 13 October 1998 // // Author: // // John Burkardt // // Parameters: // // Input, int I1, I2, two integers to be compared. // // Output, int I4_MIN, the smaller of I1 and I2. // { int value; if ( i1 < i2 ) { value = i1; } else { value = i2; } return value; } //****************************************************************************80 int i4_modp ( int i, int j ) //****************************************************************************80 // // Purpose: // // I4_MODP returns the nonnegative remainder of integer division. // // Discussion: // // If // NREM = I4_MODP ( I, J ) // NMULT = ( I - NREM ) / J // then // I = J * NMULT + NREM // where NREM is always nonnegative. // // The MOD function computes a result with the same sign as the // quantity being divided. Thus, suppose you had an angle A, // and you wanted to ensure that it was between 0 and 360. // Then mod(A,360) would do, if A was positive, but if A // was negative, your result would be between -360 and 0. // // On the other hand, I4_MODP(A,360) is between 0 and 360, always. // // Example: // // I J MOD I4_MODP I4_MODP Factorization // // 107 50 7 7 107 = 2 * 50 + 7 // 107 -50 7 7 107 = -2 * -50 + 7 // -107 50 -7 43 -107 = -3 * 50 + 43 // -107 -50 -7 43 -107 = 3 * -50 + 43 // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 26 May 1999 // // Author: // // John Burkardt // // Parameters: // // Input, int I, the number to be divided. // // Input, int J, the number that divides I. // // Output, int I4_MODP, the nonnegative remainder when I is // divided by J. // { int value; if ( j == 0 ) { cout << "\n"; cout << "I4_MODP - Fatal error!\n"; cout << " I4_MODP ( I, J ) called with J = " << j << "\n"; exit ( 1 ); } value = i % j; if ( value < 0 ) { value = value + abs ( j ); } return value; } //****************************************************************************80 void i4_swap ( int *i, int *j ) //****************************************************************************80 // // Purpose: // // I4_SWAP switches two I4's. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 07 January 2002 // // Author: // // John Burkardt // // Parameters: // // Input/output, int *I, *J. On output, the values of I and // J have been interchanged. // { int k; k = *i; *i = *j; *j = k; return; } //****************************************************************************80 int i4_wrap ( int ival, int ilo, int ihi ) //****************************************************************************80 // // Purpose: // // I4_WRAP forces an I4 to lie between given limits by wrapping. // // Example: // // ILO = 4, IHI = 8 // // I Value // // -2 8 // -1 4 // 0 5 // 1 6 // 2 7 // 3 8 // 4 4 // 5 5 // 6 6 // 7 7 // 8 8 // 9 4 // 10 5 // 11 6 // 12 7 // 13 8 // 14 4 // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 19 August 2003 // // Author: // // John Burkardt // // Parameters: // // Input, int IVAL, an integer value. // // Input, int ILO, IHI, the desired bounds for the integer value. // // Output, int I4_WRAP, a "wrapped" version of IVAL. // { int jhi; int jlo; int value; int wide; jlo = i4_min ( ilo, ihi ); jhi = i4_max ( ilo, ihi ); wide = jhi + 1 - jlo; if ( wide == 1 ) { value = jlo; } else { value = jlo + i4_modp ( ival - jlo, wide ); } return value; } //****************************************************************************80 int i4col_compare ( int m, int n, int a[], int i, int j ) //****************************************************************************80 // // Purpose: // // I4COL_COMPARE compares columns I and J of an I4COL. // // Example: // // Input: // // M = 3, N = 4, I = 2, J = 4 // // A = ( // 1 2 3 4 // 5 6 7 8 // 9 10 11 12 ) // // Output: // // I4COL_COMPARE = -1 // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 12 June 2005 // // Author: // // John Burkardt // // Parameters: // // Input, int M, N, the number of rows and columns. // // Input, int A[M*N], an array of N columns of vectors of length M. // // Input, int I, J, the columns to be compared. // I and J must be between 1 and N. // // Output, int I4COL_COMPARE, the results of the comparison: // -1, column I < column J, // 0, column I = column J, // +1, column J < column I. // { int k; // // Check. // if ( i < 1 ) { cout << "\n"; cout << "I4COL_COMPARE - Fatal error!\n"; cout << " Column index I = " << i << " is less than 1.\n"; exit ( 1 ); } if ( n < i ) { cout << "\n"; cout << "I4COL_COMPARE - Fatal error!\n"; cout << " N = " << n << " is less than column index I = " << i << ".\n"; exit ( 1 ); } if ( j < 1 ) { cout << "\n"; cout << "I4COL_COMPARE - Fatal error!\n"; cout << " Column index J = " << j << " is less than 1.\n"; exit ( 1 ); } if ( n < j ) { cout << "\n"; cout << "I4COL_COMPARE - Fatal error!\n"; cout << " N = " << n << " is less than column index J = " << j << ".\n"; exit ( 1 ); } if ( i == j ) { return 0; } k = 1; while ( k <= m ) { if ( a[k-1+(i-1)*m] < a[k-1+(j-1)*m] ) { return (-1); } else if ( a[k-1+(j-1)*m] < a[k-1+(i-1)*m] ) { return 1; } k = k + 1; } return 0; } //****************************************************************************80 void i4col_sort_a ( int m, int n, int a[] ) //****************************************************************************80 // // Purpose: // // I4COL_SORT_A ascending sorts the columns of an I4COL. // // Discussion: // // In lexicographic order, the statement "X < Y", applied to two // vectors X and Y of length M, means that there is some index I, with // 1 <= I <= M, with the property that // // X(J) = Y(J) for J < I, // and // X(I) < Y(I). // // In other words, X is less than Y if, at the first index where they // differ, the X value is less than the Y value. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 12 June 2005 // // Author: // // John Burkardt // // Parameters: // // Input, int M, the number of rows of A. // // Input, int N, the number of columns of A. // // Input/output, int A[M*N]. // On input, the array of N columns of M vectors; // On output, the columns of A have been sorted in ascending // lexicographic order. // { int i; int indx; int isgn; int j; // // Initialize. // i = 0; indx = 0; isgn = 0; j = 0; // // Call the external heap sorter. // for ( ; ; ) { sort_heap_external ( n, &indx, &i, &j, isgn ); // // Interchange the I and J objects. // if ( 0 < indx ) { i4col_swap ( m, n, a, i, j ); } // // Compare the I and J objects. // else if ( indx < 0 ) { isgn = i4col_compare ( m, n, a, i, j ); } else if ( indx == 0 ) { break; } } return; } //****************************************************************************80 void i4col_swap ( int m, int n, int a[], int icol1, int icol2 ) //****************************************************************************80 // // Purpose: // // I4COL_SWAP swaps two columns of an I4COL. // // Discussion: // // The two dimensional information is stored as a one dimensional // array, by columns. // // The row indices are 1 based, NOT 0 based// However, a preprocessor // variable, called OFFSET, can be reset from 1 to 0 if you wish to // use 0-based indices. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 03 April 2005 // // Author: // // John Burkardt // // Parameters: // // Input, int M, N, the number of rows and columns. // // Input/output, int A[M*N], an array of data. // // Input, int ICOL1, ICOL2, the two columns to swap. // These indices should be between 1 and N. // { # define OFFSET 1 int i; int t; // // Check. // if ( icol1 - OFFSET < 0 || n-1 < icol1 - OFFSET ) { cout << "\n"; cout << "I4COL_SWAP - Fatal error!\n"; cout << " ICOL1 is out of range.\n"; exit ( 1 ); } if ( icol2 - OFFSET < 0 || n-1 < icol2 - OFFSET ) { cout << "\n"; cout << "I4COL_SWAP - Fatal error!\n"; cout << " ICOL2 is out of range.\n"; exit ( 1 ); } if ( icol1 == icol2 ) { return; } for ( i = 0; i < m; i++ ) { t = a[i+(icol1-OFFSET)*m]; a[i+(icol1-OFFSET)*m] = a[i+(icol2-OFFSET)*m]; a[i+(icol2-OFFSET)*m] = t; } return; # undef OFFSET } //****************************************************************************80 int *i4mat_data_read ( string input_filename, int m, int n ) //****************************************************************************80 // // Purpose: // // I4MAT_DATA_READ reads data from an I4MAT file. // // Discussion: // // The file is assumed to contain one record per line. // // Records beginning with '#' are comments, and are ignored. // Blank lines are also ignored. // // Each line that is not ignored is assumed to contain exactly (or at least) // M real numbers, representing the coordinates of a point. // // There are assumed to be exactly (or at least) N such records. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 23 February 2009 // // Author: // // John Burkardt // // Parameters: // // Input, string INPUT_FILENAME, the name of the input file. // // Input, int M, the number of spatial dimensions. // // Input, int N, the number of points. The program // will stop reading data once N values have been read. // // Output, int I4MAT_DATA_READ[M*N], the table data. // { bool error; ifstream input; int i; int j; string line; int *table; int *x; input.open ( input_filename.c_str ( ) ); if ( !input ) { cerr << "\n"; cerr << "I4MAT_DATA_READ - Fatal error!\n"; cerr << " Could not open the input file: \"" << input_filename << "\"\n"; return NULL; } table = new int[m*n]; x = new int[m]; j = 0; while ( j < n ) { getline ( input, line ); if ( input.eof ( ) ) { break; } if ( line[0] == '#' || s_len_trim ( line ) == 0 ) { continue; } error = s_to_i4vec ( line, m, x ); if ( error ) { continue; } for ( i = 0; i < m; i++ ) { table[i+j*m] = x[i]; } j = j + 1; } input.close ( ); delete [] x; return table; } //****************************************************************************80 void i4mat_header_read ( string input_filename, int *m, int *n ) //****************************************************************************80 // // Purpose: // // I4MAT_HEADER_READ reads the header from an I4MAT file. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 23 February 2009 // // Author: // // John Burkardt // // Parameters: // // Input, string INPUT_FILENAME, the name of the input file. // // Output, int *M, the number of spatial dimensions. // // Output, int *N, the number of points // { *m = file_column_count ( input_filename ); if ( *m <= 0 ) { cerr << "\n"; cerr << "I4MAT_HEADER_READ - Fatal error!\n"; cerr << " FILE_COLUMN_COUNT failed.\n"; *n = -1; return; } *n = file_row_count ( input_filename ); if ( *n <= 0 ) { cerr << "\n"; cerr << "I4MAT_HEADER_READ - Fatal error!\n"; cerr << " FILE_ROW_COUNT failed.\n"; return; } return; } //****************************************************************************80 void i4mat_transpose_print_some ( int m, int n, int a[], int ilo, int jlo, int ihi, int jhi, string title ) //****************************************************************************80 // // Purpose: // // I4MAT_TRANSPOSE_PRINT_SOME prints some of an I4MAT, transposed. // // Discussion: // // An I4MAT is an MxN array of I4's, stored by (I,J) -> [I+J*M]. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 14 June 2005 // // Author: // // John Burkardt // // Parameters: // // Input, int M, the number of rows of the matrix. // M must be positive. // // Input, int N, the number of columns of the matrix. // N must be positive. // // Input, int A[M*N], the matrix. // // Input, int ILO, JLO, IHI, JHI, designate the first row and // column, and the last row and column to be printed. // // Input, string TITLE, a title for the matrix. // { # define INCX 10 int i; int i2hi; int i2lo; int j; int j2hi; int j2lo; if ( 0 < s_len_trim ( title ) ) { cout << "\n"; cout << title << "\n"; } // // Print the columns of the matrix, in strips of INCX. // for ( i2lo = ilo; i2lo <= ihi; i2lo = i2lo + INCX ) { i2hi = i2lo + INCX - 1; i2hi = i4_min ( i2hi, m ); i2hi = i4_min ( i2hi, ihi ); cout << "\n"; // // For each row I in the current range... // // Write the header. // cout << " Row: "; for ( i = i2lo; i <= i2hi; i++ ) { cout << setw(6) << i << " "; } cout << "\n"; cout << " Col\n"; cout << "\n"; // // Determine the range of the rows in this strip. // j2lo = i4_max ( jlo, 1 ); j2hi = i4_min ( jhi, n ); for ( j = j2lo; j <= j2hi; j++ ) { // // Print out (up to INCX) entries in column J, that lie in the current strip. // cout << setw(5) << j << " "; for ( i = i2lo; i <= i2hi; i++ ) { cout << setw(6) << a[i-1+(j-1)*m] << " "; } cout << "\n"; } } return; # undef INCX } //****************************************************************************80 void i4mat_write ( string output_filename, int m, int n, int table[] ) //****************************************************************************80 // // Purpose: // // I4MAT_WRITE writes an I4MAT file with no header. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 01 June 2009 // // Author: // // John Burkardt // // Parameters: // // Input, string OUTPUT_FILENAME, the output filename. // // Input, int M, the spatial dimension. // // Input, int N, the number of points. // // Input, int TABLE[M*N], the table data. // { int i; int j; ofstream output; // // Open the file. // output.open ( output_filename.c_str ( ) ); if ( !output ) { cerr << "\n"; cerr << "I4MAT_WRITE - Fatal error!\n"; cerr << " Could not open the output file.\n"; return; } // // Write the data. // for ( j = 0; j < n; j++ ) { for ( i = 0; i < m; i++ ) { output << " " << setw(10) << table[i+j*m]; } output << "\n"; } // // Close the file. // output.close ( ); return; } //****************************************************************************80 void i4vec_heap_d ( int n, int a[] ) //****************************************************************************80 // // Purpose: // // I4VEC_HEAP_D reorders an array of integers into a descending heap. // // Discussion: // // A heap is an array A with the property that, for every index J, // A[J] >= A[2*J+1] and A[J] >= A[2*J+2], (as long as the indices // 2*J+1 and 2*J+2 are legal). // // Diagram: // // A(0) // // A(1) A(2) // // A(3) A(4) A(5) A(6) // // A(7) A(8) A(9) A(10) // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 30 April 1999 // // Author: // // John Burkardt // // Reference: // // Albert Nijenhuis, Herbert Wilf, // Combinatorial Algorithms, // Academic Press, 1978, second edition, // ISBN 0-12-519260-6. // // Parameters: // // Input, int N, the size of the input array. // // Input/output, int A[N]. // On input, an unsorted array. // On output, the array has been reordered into a heap. // { int i; int ifree; int key; int m; // // Only nodes (N/2)-1 down to 0 can be "parent" nodes. // for ( i = (n/2)-1; 0 <= i; i-- ) { // // Copy the value out of the parent node. // Position IFREE is now "open". // key = a[i]; ifree = i; for ( ; ; ) { // // Positions 2*IFREE + 1 and 2*IFREE + 2 are the descendants of position // IFREE. (One or both may not exist because they equal or exceed N.) // m = 2 * ifree + 1; // // Does the first position exist? // if ( n <= m ) { break; } else { // // Does the second position exist? // if ( m + 1 < n ) { // // If both positions exist, take the larger of the two values, // and update M if necessary. // if ( a[m] < a[m+1] ) { m = m + 1; } } // // If the large descendant is larger than KEY, move it up, // and update IFREE, the location of the free position, and // consider the descendants of THIS position. // if ( key < a[m] ) { a[ifree] = a[m]; ifree = m; } else { break; } } } // // When you have stopped shifting items up, return the item you // pulled out back to the heap. // a[ifree] = key; } return; } //****************************************************************************80 void i4vec_print ( int n, int a[], string title ) //****************************************************************************80 // // Purpose: // // I4VEC_PRINT prints an I4VEC. // // Discussion: // // An I4VEC is a vector of I4's. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 14 November 2003 // // Author: // // John Burkardt // // Parameters: // // Input, int N, the number of components of the vector. // // Input, int A[N], the vector to be printed. // // Input, string TITLE, a title. // { int i; cout << "\n"; cout << title << "\n"; cout << "\n"; for ( i = 0; i < n; i++ ) { cout << " " << setw(8) << i << " " << setw(8) << a[i] << "\n"; } return; } //****************************************************************************80 void i4vec_reverse ( int n, int a[] ) //****************************************************************************80 // // Purpose: // // I4VEC_REVERSE reverses the elements of an integer vector. // // Example: // // Input: // // N = 5, // A = ( 11, 12, 13, 14, 15 ). // // Output: // // A = ( 15, 14, 13, 12, 11 ). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 22 September 2005 // // Author: // // John Burkardt // // Parameters: // // Input, int N, the number of entries in the array. // // Input/output, int A(N), the array to be reversed. // { int i; int j; for ( i = 0; i < n / 2; i++ ) { j = a[i]; a[i] = a[n-1-i]; a[n-1-i] = j; } return; } //****************************************************************************80 void i4vec_sort_heap_a ( int n, int a[] ) //****************************************************************************80 // // Purpose: // // I4VEC_SORT_HEAP_A ascending sorts an array of integers using heap sort. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 30 April 1999 // // Author: // // John Burkardt // // Reference: // // Albert Nijenhuis, Herbert Wilf, // Combinatorial Algorithms, // Academic Press, 1978, second edition, // ISBN 0-12-519260-6. // // Parameters: // // Input, int N, the number of entries in the array. // // Input/output, int A[N]. // On input, the array to be sorted; // On output, the array has been sorted. // { int n1; int temp; if ( n <= 1 ) { return; } // // 1: Put A into descending heap form. // i4vec_heap_d ( n, a ); // // 2: Sort A. // // The largest object in the heap is in A[0]. // Move it to position A[N-1]. // temp = a[0]; a[0] = a[n-1]; a[n-1] = temp; // // Consider the diminished heap of size N1. // for ( n1 = n-1; 2 <= n1; n1-- ) { // // Restore the heap structure of the initial N1 entries of A. // i4vec_heap_d ( n1, a ); // // Take the largest object from A[0] and move it to A[N1-1]. // temp = a[0]; a[0] = a[n1-1]; a[n1-1] = temp; } return; } //****************************************************************************80 void level_set ( int root, int adj_num, int adj_row[], int adj[], int mask[], int *level_num, int level_row[], int level[], int node_num ) //****************************************************************************80 // // Purpose: // // LEVEL_SET generates the connected level structure rooted at a given node. // // Discussion: // // Only nodes for which MASK is nonzero will be considered. // // The root node chosen by the user is assigned level 1, and masked. // All (unmasked) nodes reachable from a node in level 1 are // assigned level 2 and masked. The process continues until there // are no unmasked nodes adjacent to any node in the current level. // The number of levels may vary between 2 and NODE_NUM. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 05 January 2007 // // Author: // // Original FORTRAN77 version by Alan George, Joseph Liu. // C++ version by John Burkardt. // // Reference: // // Alan George, Joseph Liu, // Computer Solution of Large Sparse Positive Definite Systems, // Prentice Hall, 1981. // // Parameters: // // Input, int ROOT, the node at which the level structure // is to be rooted. // // Input, int ADJ_NUM, the number of adjacency entries. // // Input, int ADJ_ROW[NODE_NUM+1]. Information about row I is stored // in entries ADJ_ROW(I) through ADJ_ROW(I+1)-1 of ADJ. // // Input, int ADJ[ADJ_NUM], the adjacency structure. // For each row, it contains the column indices of the nonzero entries. // // Input/output, int MASK[NODE_NUM]. On input, only nodes with nonzero // MASK are to be processed. On output, those nodes which were included // in the level set have MASK set to 1. // // Output, int *LEVEL_NUM, the number of levels in the level // structure. ROOT is in level 1. The neighbors of ROOT // are in level 2, and so on. // // Output, int LEVEL_ROW[NODE_NUM+1], LEVEL[NODE_NUM], the rooted // level structure. // // Input, int NODE_NUM, the number of nodes. // { int i; int iccsze; int j; int jstop; int jstrt; int lbegin; int lvlend; int lvsize; int nbr; int node; mask[root-1] = 0; level[0] = root; *level_num = 0; lvlend = 0; iccsze = 1; // // LBEGIN is the pointer to the beginning of the current level, and // LVLEND points to the end of this level. // for ( ; ; ) { lbegin = lvlend + 1; lvlend = iccsze; *level_num = *level_num + 1; level_row[*level_num-1] = lbegin; // // Generate the next level by finding all the masked neighbors of nodes // in the current level. // for ( i = lbegin; i <= lvlend; i++ ) { node = level[i-1]; jstrt = adj_row[node-1]; jstop = adj_row[node] - 1; for ( j = jstrt; j <= jstop; j++ ) { nbr = adj[j-1]; if ( mask[nbr-1] != 0 ) { iccsze = iccsze + 1; level[iccsze-1] = nbr; mask[nbr-1] = 0; } } } // // Compute the current level width (the number of nodes encountered.) // If it is positive, generate the next level. // lvsize = iccsze - lvlend; if ( lvsize <= 0 ) { break; } } level_row[*level_num] = lvlend + 1; // // Reset MASK to 1 for the nodes in the level structure. // for ( i = 0; i < iccsze; i++ ) { mask[level[i]-1] = 1; } return; } //****************************************************************************80 void mesh_base_zero ( int node_num, int element_order, int element_num, int element_node[] ) //****************************************************************************80 // // Purpose: // // MESH_BASE_ZERO ensures that the element definition is zero-based. // // Discussion: // // The ELEMENT_NODE array contains nodes indices that form elements. // The convention for node indexing might start at 0 or at 1. // Since a C++ program will naturally assume a 0-based indexing, it is // necessary to check a given element definition and, if it is actually // 1-based, to convert it. // // This function attempts to detect 1-based node indexing and correct it. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 27 September 2009 // // Author: // // John Burkardt // // Parameters: // // Input, int NODE_NUM, the number of nodes. // // Input, int ELEMENT_ORDER, the order of the elements. // // Input, int ELEMENT_NUM, the number of elements. // // Input/output, int ELEMENT_NODE[ELEMENT_ORDER*ELEMENT_NUM], the element // definitions. // { int element; int node; int node_max; int node_min; int order; node_min = node_num + 1; node_max = -1; for ( element = 0; element < element_num; element++ ) { for ( order = 0; order < element_order; order++ ) { node = element_node[order+element*element_order]; node_min = i4_min ( node_min, node ); node_max = i4_max ( node_max, node ); } } if ( node_min == 1 && node_max == node_num ) { cout << "\n"; cout << "MESH_BASE_ZERO:\n"; cout << " The element indexing appears to be 1-based!\n"; cout << " This will be converted to 0-based.\n"; for ( element = 0; element < element_num; element++ ) { for ( order = 0; order < element_order; order++ ) { element_node[order+element*element_order] = element_node[order+element*element_order] - 1; } } } else if ( node_min == 0 && node_max == node_num - 1 ) { cout << "\n"; cout << "MESH_BASE_ZERO:\n"; cout << " The element indexing appears to be 0-based!\n"; cout << " No conversion is necessary.\n"; } else { cout << "\n"; cout << "MESH_BASE_ZERO - Warning!\n"; cout << " The element indexing is not of a recognized type.\n"; } return; } //****************************************************************************80 int *neighbor_elements_q4_mesh ( int element_num, int element_node[] ) //****************************************************************************80 // // Purpose: // // NEIGHBOR_ELEMENTS_Q4_MESH determines element neighbors in a Q4 mesh. // // Discussion: // // A quadrilateral mesh of a set of nodes can be completely described by // the coordinates of the nodes, and the list of nodes that make up // each element. However, in some cases, it is necessary to know // element adjacency information, that is, which element, if any, // is adjacent to a given element on a particular side. // // This routine creates a data structure recording this information. // // The primary amount of work occurs in sorting a list of 4 * ELEMENT_NUM // data items. // // Note that COL is a work array allocated dynamically inside this // routine. It is possible, for very large values of ELEMENT_NUM, // that the necessary amount of memory will not be accessible, and the // routine will fail. This is a limitation of the implementation of // dynamic arrays in FORTRAN90. One way to get around this would be // to require the user to declare ROW in the calling routine // as an allocatable array, get the necessary memory explicitly with // an ALLOCATE statement, and then pass ROW into this routine. // // Of course, the point of dynamic arrays was to make it easy to // hide these sorts of temporary work arrays from the poor user! // // This routine was revised to store the edge data in a column // array rather than a row array. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 30 September 2009 // // Author: // // John Burkardt // // Parameters: // // Input, int ELEMENT_NUM, the number of elements. // // Input, int ELEMENT_NODE[4*ELEMENT_NUM], the nodes that make up each element. // // Output, int NEIGHBOR_ELEMENTS_Q4_MESH[4*ELEMENT_NUM], the elements that are direct // neighbors of a given element, or -1 if there is no neighbor on that side. // { int *col; int element; int *element_neighbor; int element_order = 4; int element1; int element2; int i; int icol; int j; int k; int l; int side1; int side2; element_neighbor = new int[4*element_num]; col = new int[4*4*element_num]; // // Step 1. // From the list of nodes for element E, of the form: (I,J,K,L) // construct the four neighbor relations: // // (I,J,0,E) or (J,I,0,E), // (J,K,1,E) or (K,J,1,E), // (K,L,2,E) or (L,K,2,E) // (L,I,3,E) or (I,L,3,E) // // where we choose (I,J,0,E) if I < J, or else (J,I,0,E) // for ( element = 0; element < element_num; element++ ) { i = element_node[0+element*element_order]; j = element_node[1+element*element_order]; k = element_node[2+element*element_order]; l = element_node[3+element*element_order]; col[0+0*4+16*element] = i4_min ( i, j ); col[1+0*4+16*element] = i4_max ( i, j ); col[2+0*4+16*element] = 0; col[3+0*4+16*element] = element; col[0+1*4+16*element] = i4_min ( j, k ); col[1+1*4+16*element] = i4_max ( j, k ); col[2+1*4+16*element] = 1; col[3+1*4+16*element] = element; col[0+2*4+16*element] = i4_min ( k, l ); col[1+2*4+16*element] = i4_max ( k, l ); col[2+2*4+16*element] = 2; col[3+2*4+16*element] = element; col[0+3*4+16*element] = i4_min ( l, i ); col[1+3*4+16*element] = i4_max ( l, i ); col[2+3*4+16*element] = 3; col[3+3*4+16*element] = element; } // // Step 2. Perform an ascending dictionary sort on the neighbor relations. // We only intend to sort on rows 1 and 2; the routine we call here // sorts on rows 1 through 4 but that won't hurt us. // // What we need is to find cases where two elements share an edge. // Say they share an edge defined by the nodes I and J. Then there are // two columns of COL that start out ( I, J, ?, ? ). By sorting COL, // we make sure that these two columns occur consecutively. That will // make it easy to notice that the elements are neighbors. // i4col_sort_a ( 4, 4*element_num, col ); // // Step 3. Neighboring elements show up as consecutive columns with // identical first two entries. Whenever you spot this happening, // make the appropriate entries in ELEMENT_NEIGHBOR. // for ( j = 0; j < element_num; j++ ) { for ( i = 0; i < 4; i++ ) { element_neighbor[i+j*4] = -1; } } icol = 0; for ( ; ; ) { if ( 4 * element_num <= icol ) { break; } if ( col[0+icol*4] != col[0+(icol+1)*4] || col[1+icol*4] != col[1+(icol+1)*4] ) { icol = icol + 1; continue; } side1 = col[2+icol*4]; element1 = col[3+icol*4]; side2 = col[2+(icol+1)*4]; element2 = col[3+(icol+1)*4]; element_neighbor[side1+element1*4] = element2; element_neighbor[side2+element2*4] = element1; icol = icol + 2; } delete [] col; return element_neighbor; } //****************************************************************************80 bool perm_check ( int n, int p[] ) //****************************************************************************80 // // Purpose: // // PERM_CHECK checks that a vector represents a permutation. // // Discussion: // // The routine verifies that each of the integers from 1 // to N occurs among the N entries of the permutation. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 13 January 2004 // // Author: // // John Burkardt // // Parameters: // // Input, int N, the number of entries. // // Input, int P[N], the array to check. // // Output, bool PERM_CHECK, is TRUE if the permutation is OK. // { bool found; int i; int seek; for ( seek = 1; seek <= n; seek++ ) { found = false; for ( i = 0; i < n; i++ ) { if ( p[i] == seek ) { found = true; break; } } if ( !found ) { return false; } } return true; } //****************************************************************************80 void perm_inverse3 ( int n, int perm[], int perm_inv[] ) //****************************************************************************80 // // Purpose: // // PERM_INVERSE3 produces the inverse of a given permutation. // // Discussion: // // This function assumes the permutation is 0-based. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 30 September 2009 // // Author: // // John Burkardt // // Parameters: // // Input, int N, the number of items permuted. // // Input, int PERM[N], a permutation. // // Output, int PERM_INV[N], the inverse permutation. // { int i; for ( i = 0; i < n; i++ ) { perm_inv[perm[i]] = i; } return; } //****************************************************************************80 void r8col_permute ( int m, int n, int p[], double a[] ) //****************************************************************************80 // // Purpose: // // R8COL_PERMUTE permutes an R8COL in place. // // Discussion: // // An R8COL is an M by N array of double precision values, regarded // as an array of N columns of length M. // // This routine permutes an array of real "objects", but the same // logic can be used to permute an array of objects of any arithmetic // type, or an array of objects of any complexity. The only temporary // storage required is enough to store a single object. The number // of data movements made is N + the number of cycles of order 2 or more, // which is never more than N + N/2. // // Example: // // Input: // // M = 2 // N = 5 // P = ( 2, 4, 5, 1, 3 ) // A = ( 1.0, 2.0, 3.0, 4.0, 5.0 ) // (11.0, 22.0, 33.0, 44.0, 55.0 ) // // Output: // // A = ( 2.0, 4.0, 5.0, 1.0, 3.0 ) // ( 22.0, 44.0, 55.0, 11.0, 33.0 ). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 09 December 2006 // // Author: // // John Burkardt // // Parameters: // // Input, int M, the length of objects. // // Input, int N, the number of objects. // // Input/output, double A[M*N], the array to be permuted. // // Input, int P[N], the permutation. P(I) = J means // that the I-th element of the output array should be the J-th // element of the input array. P must be a legal permutation // of the integers from 1 to N, otherwise the algorithm will // fail catastrophically. // { double *a_temp; int i; int iget; int iput; int istart; int j; a_temp = new double[m]; // // Need to increment the entries by 1 in order to use the sign trick. // for ( i = 0; i < n; i++ ) { p[i] = p[i] + 1; } // // Search for the next element of the permutation that has not been used. // for ( istart = 1; istart <= n; istart++ ) { if ( p[istart-1] < 0 ) { continue; } else if ( p[istart-1] == istart ) { p[istart-1] = -p[istart-1]; continue; } else { for ( i = 0; i < m; i++ ) { a_temp[i] = a[i+(istart-1)*m]; } iget = istart; // // Copy the new value into the vacated entry. // for ( ; ; ) { iput = iget; iget = p[iget-1]; p[iput-1] = -p[iput-1]; if ( iget < 1 || n < iget ) { cout << "\n"; cout << "R8COL_PERMUTE - Fatal error!\n"; cout << " Entry IPUT = " << iput << " of the permutation has\n"; cout << " an illegal value IGET = " << iget << ".\n"; exit ( 1 ); } if ( iget == istart ) { for ( i = 0; i < m; i++ ) { a[i+(iput-1)*m] = a_temp[i]; } break; } for ( i = 0; i < m; i++ ) { a[i+(iput-1)*m] = a[i+(iget-1)*m]; } } } } // // Restore the signs of the entries. // for ( j = 0; j < n; j++ ) { p[j] = - p[j]; } // // Need to unincrement the entries by 1. // for ( i = 0; i < n; i++ ) { p[i] = p[i] - 1; } delete [] a_temp; return; } //****************************************************************************80 double *r8mat_data_read ( string input_filename, int m, int n ) //****************************************************************************80 // // Purpose: // // R8MAT_DATA_READ reads the data from an R8MAT file. // // Discussion: // // The file is assumed to contain one record per line. // // Records beginning with '#' are comments, and are ignored. // Blank lines are also ignored. // // Each line that is not ignored is assumed to contain exactly (or at least) // M real numbers, representing the coordinates of a point. // // There are assumed to be exactly (or at least) N such records. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 23 February 2009 // // Author: // // John Burkardt // // Parameters: // // Input, string INPUT_FILENAME, the name of the input file. // // Input, int M, the number of spatial dimensions. // // Input, int N, the number of points. The program // will stop reading data once N values have been read. // // Output, double R8MAT_DATA_READ[M*N], the table data. // { bool error; ifstream input; int i; int j; string line; double *table; double *x; input.open ( input_filename.c_str ( ) ); if ( !input ) { cerr << "\n"; cerr << "R8MAT_DATA_READ - Fatal error!\n"; cerr << " Could not open the input file: \"" << input_filename << "\"\n"; return NULL; } table = new double[m*n]; x = new double[m]; j = 0; while ( j < n ) { getline ( input, line ); if ( input.eof ( ) ) { break; } if ( line[0] == '#' || s_len_trim ( line ) == 0 ) { continue; } error = s_to_r8vec ( line, m, x ); if ( error ) { continue; } for ( i = 0; i < m; i++ ) { table[i+j*m] = x[i]; } j = j + 1; } input.close ( ); delete [] x; return table; } //****************************************************************************80 void r8mat_header_read ( string input_filename, int *m, int *n ) //****************************************************************************80 // // Purpose: // // R8MAT_HEADER_READ reads the header from an R8MAT file. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 23 February 2009 // // Author: // // John Burkardt // // Parameters: // // Input, string INPUT_FILENAME, the name of the input file. // // Output, int *M, the number of spatial dimensions. // // Output, int *N, the number of points. // { *m = file_column_count ( input_filename ); if ( *m <= 0 ) { cerr << "\n"; cerr << "R8MAT_HEADER_READ - Fatal error!\n"; cerr << " FILE_COLUMN_COUNT failed.\n"; *n = -1; return; } *n = file_row_count ( input_filename ); if ( *n <= 0 ) { cerr << "\n"; cerr << "R8MAT_HEADER_READ - Fatal error!\n"; cerr << " FILE_ROW_COUNT failed.\n"; return; } return; } //****************************************************************************80 void r8mat_transpose_print_some ( int m, int n, double a[], int ilo, int jlo, int ihi, int jhi, string title ) //****************************************************************************80 // // Purpose: // // R8MAT_TRANSPOSE_PRINT_SOME prints some of an R8MAT, transposed. // // Discussion: // // An R8MAT is a doubly dimensioned array of R8 values, stored as a vector // in column-major order. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 11 August 2004 // // Author: // // John Burkardt // // Parameters: // // Input, int M, N, the number of rows and columns. // // Input, double A[M*N], an M by N matrix to be printed. // // Input, int ILO, JLO, the first row and column to print. // // Input, int IHI, JHI, the last row and column to print. // // Input, string TITLE, an optional title. // { # define INCX 5 int i; int i2; int i2hi; int i2lo; int inc; int j; int j2hi; int j2lo; if ( 0 < s_len_trim ( title ) ) { cout << "\n"; cout << title << "\n"; } for ( i2lo = i4_max ( ilo, 1 ); i2lo <= i4_min ( ihi, m ); i2lo = i2lo + INCX ) { i2hi = i2lo + INCX - 1; i2hi = i4_min ( i2hi, m ); i2hi = i4_min ( i2hi, ihi ); inc = i2hi + 1 - i2lo; cout << "\n"; cout << " Row: "; for ( i = i2lo; i <= i2hi; i++ ) { cout << setw(7) << i << " "; } cout << "\n"; cout << " Col\n"; cout << "\n"; j2lo = i4_max ( jlo, 1 ); j2hi = i4_min ( jhi, n ); for ( j = j2lo; j <= j2hi; j++ ) { cout << setw(5) << j << " "; for ( i2 = 1; i2 <= inc; i2++ ) { i = i2lo - 1 + i2; cout << setw(14) << a[(i-1)+(j-1)*m]; } cout << "\n"; } } return; # undef INCX } //****************************************************************************80 void r8mat_write ( string output_filename, int m, int n, double table[] ) //****************************************************************************80 // // Purpose: // // R8MAT_WRITE writes an R8MAT file with no header. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 29 June 2009 // // Author: // // John Burkardt // // Parameters: // // Input, string OUTPUT_FILENAME, the output filename. // // Input, int M, the spatial dimension. // // Input, int N, the number of points. // // Input, double TABLE[M*N], the table data. // { int i; int j; ofstream output; // // Open the file. // output.open ( output_filename.c_str ( ) ); if ( !output ) { cerr << "\n"; cerr << "R8MAT_WRITE - Fatal error!\n"; cerr << " Could not open the output file.\n"; return; } // // Write the data. // For greater precision, try // // output << " " << setw(24) << setprecision(16) << table[i+j*m]; // for ( j = 0; j < n; j++ ) { for ( i = 0; i < m; i++ ) { output << " " << setw(10) << table[i+j*m]; } output << "\n"; } // // Close the file. // output.close ( ); return; } //****************************************************************************80 void rcm ( int root, int adj_num, int adj_row[], int adj[], int mask[], int perm[], int *iccsze, int node_num ) //****************************************************************************80 // // Purpose: // // RCM renumbers a connected component by the reverse Cuthill McKee algorithm. // // Discussion: // // The connected component is specified by a node ROOT and a mask. // The numbering starts at the root node. // // An outline of the algorithm is as follows: // // X(1) = ROOT. // // for ( I = 1 to N-1) // Find all unlabeled neighbors of X(I), // assign them the next available labels, in order of increasing degree. // // When done, reverse the ordering. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 05 January 2007 // // Author: // // Original FORTRAN77 version by Alan George, Joseph Liu. // C++ version by John Burkardt. // // Reference: // // Alan George, Joseph Liu, // Computer Solution of Large Sparse Positive Definite Systems, // Prentice Hall, 1981. // // Parameters: // // Input, int ROOT, the node that defines the connected component. // It is used as the starting point for the RCM ordering. // // Input, int ADJ_NUM, the number of adjacency entries. // // Input, int ADJ_ROW(NODE_NUM+1). Information about row I is stored // in entries ADJ_ROW(I) through ADJ_ROW(I+1)-1 of ADJ. // // Input, int ADJ(ADJ_NUM), the adjacency structure. // For each row, it contains the column indices of the nonzero entries. // // Input/output, int MASK(NODE_NUM), a mask for the nodes. Only // those nodes with nonzero input mask values are considered by the // routine. The nodes numbered by RCM will have their mask values // set to zero. // // Output, int PERM(NODE_NUM), the RCM ordering. // // Output, int ICCSZE, the size of the connected component // that has been numbered. // // Input, int NODE_NUM, the number of nodes. // // Local Parameters: // // Workspace, int DEG[NODE_NUM], a temporary vector used to hold // the degree of the nodes in the section graph specified by mask and root. // { int *deg; int fnbr; int i; int j; int jstop; int jstrt; int k; int l; int lbegin; int lnbr; int lperm; int lvlend; int nbr; int node; // // Find the degrees of the nodes in the component specified by MASK and ROOT. // deg = new int[node_num]; degree ( root, adj_num, adj_row, adj, mask, deg, iccsze, perm, node_num ); mask[root-1] = 0; if ( *iccsze <= 1 ) { delete [] deg; return; } lvlend = 0; lnbr = 1; // // LBEGIN and LVLEND point to the beginning and // the end of the current level respectively. // while ( lvlend < lnbr ) { lbegin = lvlend + 1; lvlend = lnbr; for ( i = lbegin; i <= lvlend; i++ ) { // // For each node in the current level... // node = perm[i-1]; jstrt = adj_row[node-1]; jstop = adj_row[node] - 1; // // Find the unnumbered neighbors of NODE. // // FNBR and LNBR point to the first and last neighbors // of the current node in PERM. // fnbr = lnbr + 1; for ( j = jstrt; j <= jstop; j++ ) { nbr = adj[j-1]; if ( mask[nbr-1] != 0 ) { lnbr = lnbr + 1; mask[nbr-1] = 0; perm[lnbr-1] = nbr; } } // // If no neighbors, skip to next node in this level. // if ( lnbr <= fnbr ) { continue; } // // Sort the neighbors of NODE in increasing order by degree. // Linear insertion is used. // k = fnbr; while ( k < lnbr ) { l = k; k = k + 1; nbr = perm[k-1]; while ( fnbr < l ) { lperm = perm[l-1]; if ( deg[lperm-1] <= deg[nbr-1] ) { break; } perm[l] = lperm; l = l - 1; } perm[l] = nbr; } } } // // We now have the Cuthill-McKee ordering. Reverse it. // i4vec_reverse ( *iccsze, perm ); delete [] deg; return; } //****************************************************************************80 void root_find ( int *root, int adj_num, int adj_row[], int adj[], int mask[], int *level_num, int level_row[], int level[], int node_num ) //****************************************************************************80 // // Purpose: // // ROOT_FIND finds a pseudo-peripheral node. // // Discussion: // // The diameter of a graph is the maximum distance (number of edges) // between any two nodes of the graph. // // The eccentricity of a node is the maximum distance between that // node and any other node of the graph. // // A peripheral node is a node whose eccentricity equals the // diameter of the graph. // // A pseudo-peripheral node is an approximation to a peripheral node; // it may be a peripheral node, but all we know is that we tried our // best. // // The routine is given a graph, and seeks pseudo-peripheral nodes, // using a modified version of the scheme of Gibbs, Poole and // Stockmeyer. It determines such a node for the section subgraph // specified by MASK and ROOT. // // The routine also determines the level structure associated with // the given pseudo-peripheral node; that is, how far each node // is from the pseudo-peripheral node. The level structure is // returned as a list of nodes LS, and pointers to the beginning // of the list of nodes that are at a distance of 0, 1, 2, ..., // NODE_NUM-1 from the pseudo-peripheral node. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 05 January 2007 // // Author: // // Original FORTRAN77 version by Alan George, Joseph Liu. // C++ version by John Burkardt. // // Reference: // // Alan George, Joseph Liu, // Computer Solution of Large Sparse Positive Definite Systems, // Prentice Hall, 1981. // // Norman Gibbs, William Poole, Paul Stockmeyer, // An Algorithm for Reducing the Bandwidth and Profile of a Sparse Matrix, // SIAM Journal on Numerical Analysis, // Volume 13, pages 236-250, 1976. // // Norman Gibbs, // Algorithm 509: A Hybrid Profile Reduction Algorithm, // ACM Transactions on Mathematical Software, // Volume 2, pages 378-387, 1976. // // Parameters: // // Input/output, int *ROOT. On input, ROOT is a node in the // the component of the graph for which a pseudo-peripheral node is // sought. On output, ROOT is the pseudo-peripheral node obtained. // // Input, int ADJ_NUM, the number of adjacency entries. // // Input, int ADJ_ROW[NODE_NUM+1]. Information about row I is stored // in entries ADJ_ROW(I) through ADJ_ROW(I+1)-1 of ADJ. // // Input, int ADJ[ADJ_NUM], the adjacency structure. // For each row, it contains the column indices of the nonzero entries. // // Input, int MASK[NODE_NUM], specifies a section subgraph. Nodes // for which MASK is zero are ignored by FNROOT. // // Output, int *LEVEL_NUM, is the number of levels in the level structure // rooted at the node ROOT. // // Output, int LEVEL_ROW(NODE_NUM+1), LEVEL(NODE_NUM), the // level structure array pair containing the level structure found. // // Input, int NODE_NUM, the number of nodes. // { int iccsze; int j; int jstrt; int k; int kstop; int kstrt; int level_num2; int mindeg; int nabor; int ndeg; int node; // // Determine the level structure rooted at ROOT. // level_set ( *root, adj_num, adj_row, adj, mask, level_num, level_row, level, node_num ); // // Count the number of nodes in this level structure. // iccsze = level_row[*level_num] - 1; // // Extreme case: // A complete graph has a level set of only a single level. // Every node is equally good (or bad). // if ( *level_num == 1 ) { return; } // // Extreme case: // A "line graph" 0--0--0--0--0 has every node in its only level. // By chance, we've stumbled on the ideal root. // if ( *level_num == iccsze ) { return; } // // Pick any node from the last level that has minimum degree // as the starting point to generate a new level set. // for ( ; ; ) { mindeg = iccsze; jstrt = level_row[*level_num-1]; *root = level[jstrt-1]; if ( jstrt < iccsze ) { for ( j = jstrt; j <= iccsze; j++ ) { node = level[j-1]; ndeg = 0; kstrt = adj_row[node-1]; kstop = adj_row[node] - 1; for ( k = kstrt; k <= kstop; k++ ) { nabor = adj[k-1]; if ( 0 < mask[nabor-1] ) { ndeg = ndeg + 1; } } if ( ndeg < mindeg ) { *root = node; mindeg = ndeg; } } } // // Generate the rooted level structure associated with this node. // level_set ( *root, adj_num, adj_row, adj, mask, &level_num2, level_row, level, node_num ); // // If the number of levels did not increase, accept the new ROOT. // if ( level_num2 <= *level_num ) { break; } *level_num = level_num2; // // In the unlikely case that ROOT is one endpoint of a line graph, // we can exit now. // if ( iccsze <= *level_num ) { break; } } return; } //****************************************************************************80 int s_len_trim ( string s ) //****************************************************************************80 // // Purpose: // // S_LEN_TRIM returns the length of a string to the last nonblank. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 05 July 2009 // // Author: // // John Burkardt // // Parameters: // // Input, string S, a string. // // Output, int S_LEN_TRIM, the length of the string to the last nonblank. // If S_LEN_TRIM is 0, then the string is entirely blank. // { int n; n = s.length ( ); while ( 0 < n ) { if ( s[n-1] != ' ' ) { return n; } n = n - 1; } return n; } //****************************************************************************80 int s_to_i4 ( string s, int *last, bool *error ) //****************************************************************************80 // // Purpose: // // S_TO_I4 reads an I4 from a string. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 05 July 2009 // // Author: // // John Burkardt // // Parameters: // // Input, string S, a string to be examined. // // Output, int *LAST, the last character of S used to make IVAL. // // Output, bool *ERROR is TRUE if an error occurred. // // Output, int *S_TO_I4, the integer value read from the string. // If the string is blank, then IVAL will be returned 0. // { char c; int i; int isgn; int istate; int ival; *error = false; istate = 0; isgn = 1; i = 0; ival = 0; for ( ; ; ) { c = s[i]; i = i + 1; // // Haven't read anything. // if ( istate == 0 ) { if ( c == ' ' ) { } else if ( c == '-' ) { istate = 1; isgn = -1; } else if ( c == '+' ) { istate = 1; isgn = + 1; } else if ( '0' <= c && c <= '9' ) { istate = 2; ival = c - '0'; } else { *error = true; return ival; } } // // Have read the sign, expecting digits. // else if ( istate == 1 ) { if ( c == ' ' ) { } else if ( '0' <= c && c <= '9' ) { istate = 2; ival = c - '0'; } else { *error = true; return ival; } } // // Have read at least one digit, expecting more. // else if ( istate == 2 ) { if ( '0' <= c && c <= '9' ) { ival = 10 * (ival) + c - '0'; } else { ival = isgn * ival; *last = i - 1; return ival; } } } // // If we read all the characters in the string, see if we're OK. // if ( istate == 2 ) { ival = isgn * ival; *last = s_len_trim ( s ); } else { *error = true; *last = 0; } return ival; } //****************************************************************************80 bool s_to_i4vec ( string s, int n, int ivec[] ) //****************************************************************************80 // // Purpose: // // S_TO_I4VEC reads an I4VEC from a string. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 05 July 2009 // // Author: // // John Burkardt // // Parameters: // // Input, string S, the string to be read. // // Input, int N, the number of values expected. // // Output, int IVEC[N], the values read from the string. // // Output, bool S_TO_I4VEC, is TRUE if an error occurred. // { int begin; bool error; int i; int lchar; int length; begin = 0; length = s.length ( ); error = 0; for ( i = 0; i < n; i++ ) { ivec[i] = s_to_i4 ( s.substr(begin,length), &lchar, &error ); if ( error ) { return error; } begin = begin + lchar; length = length - lchar; } return error; } //****************************************************************************80 double s_to_r8 ( string s, int *lchar, bool *error ) //****************************************************************************80 // // Purpose: // // S_TO_R8 reads an R8 from a string. // // Discussion: // // This routine will read as many characters as possible until it reaches // the end of the string, or encounters a character which cannot be // part of the real number. // // Legal input is: // // 1 blanks, // 2 '+' or '-' sign, // 2.5 spaces // 3 integer part, // 4 decimal point, // 5 fraction part, // 6 'E' or 'e' or 'D' or 'd', exponent marker, // 7 exponent sign, // 8 exponent integer part, // 9 exponent decimal point, // 10 exponent fraction part, // 11 blanks, // 12 final comma or semicolon. // // with most quantities optional. // // Example: // // S R // // '1' 1.0 // ' 1 ' 1.0 // '1A' 1.0 // '12,34,56' 12.0 // ' 34 7' 34.0 // '-1E2ABCD' -100.0 // '-1X2ABCD' -1.0 // ' 2E-1' 0.2 // '23.45' 23.45 // '-4.2E+2' -420.0 // '17d2' 1700.0 // '-14e-2' -0.14 // 'e2' 100.0 // '-12.73e-9.23' -12.73 * 10.0**(-9.23) // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 05 July 2009 // // Author: // // John Burkardt // // Parameters: // // Input, string S, the string containing the // data to be read. Reading will begin at position 1 and // terminate at the end of the string, or when no more // characters can be read to form a legal real. Blanks, // commas, or other nonnumeric data will, in particular, // cause the conversion to halt. // // Output, int *LCHAR, the number of characters read from // the string to form the number, including any terminating // characters such as a trailing comma or blanks. // // Output, bool *ERROR, is true if an error occurred. // // Output, double S_TO_R8, the real value that was read from the string. // { char c; int ihave; int isgn; int iterm; int jbot; int jsgn; int jtop; int nchar; int ndig; double r; double rbot; double rexp; double rtop; char TAB = 9; nchar = s_len_trim ( s ); *error = false; r = 0.0; *lchar = -1; isgn = 1; rtop = 0.0; rbot = 1.0; jsgn = 1; jtop = 0; jbot = 1; ihave = 1; iterm = 0; for ( ; ; ) { c = s[*lchar+1]; *lchar = *lchar + 1; // // Blank or TAB character. // if ( c == ' ' || c == TAB ) { if ( ihave == 2 ) { } else if ( ihave == 6 || ihave == 7 ) { iterm = 1; } else if ( 1 < ihave ) { ihave = 11; } } // // Comma. // else if ( c == ',' || c == ';' ) { if ( ihave != 1 ) { iterm = 1; ihave = 12; *lchar = *lchar + 1; } } // // Minus sign. // else if ( c == '-' ) { if ( ihave == 1 ) { ihave = 2; isgn = -1; } else if ( ihave == 6 ) { ihave = 7; jsgn = -1; } else { iterm = 1; } } // // Plus sign. // else if ( c == '+' ) { if ( ihave == 1 ) { ihave = 2; } else if ( ihave == 6 ) { ihave = 7; } else { iterm = 1; } } // // Decimal point. // else if ( c == '.' ) { if ( ihave < 4 ) { ihave = 4; } else if ( 6 <= ihave && ihave <= 8 ) { ihave = 9; } else { iterm = 1; } } // // Exponent marker. // else if ( ch_eqi ( c, 'E' ) || ch_eqi ( c, 'D' ) ) { if ( ihave < 6 ) { ihave = 6; } else { iterm = 1; } } // // Digit. // else if ( ihave < 11 && '0' <= c && c <= '9' ) { if ( ihave <= 2 ) { ihave = 3; } else if ( ihave == 4 ) { ihave = 5; } else if ( ihave == 6 || ihave == 7 ) { ihave = 8; } else if ( ihave == 9 ) { ihave = 10; } ndig = ch_to_digit ( c ); if ( ihave == 3 ) { rtop = 10.0 * rtop + ( double ) ndig; } else if ( ihave == 5 ) { rtop = 10.0 * rtop + ( double ) ndig; rbot = 10.0 * rbot; } else if ( ihave == 8 ) { jtop = 10 * jtop + ndig; } else if ( ihave == 10 ) { jtop = 10 * jtop + ndig; jbot = 10 * jbot; } } // // Anything else is regarded as a terminator. // else { iterm = 1; } // // If we haven't seen a terminator, and we haven't examined the // entire string, go get the next character. // if ( iterm == 1 || nchar <= *lchar + 1 ) { break; } } // // If we haven't seen a terminator, and we have examined the // entire string, then we're done, and LCHAR is equal to NCHAR. // if ( iterm != 1 && (*lchar) + 1 == nchar ) { *lchar = nchar; } // // Number seems to have terminated. Have we got a legal number? // Not if we terminated in states 1, 2, 6 or 7! // if ( ihave == 1 || ihave == 2 || ihave == 6 || ihave == 7 ) { *error = true; return r; } // // Number seems OK. Form it. // if ( jtop == 0 ) { rexp = 1.0; } else { if ( jbot == 1 ) { rexp = pow ( 10.0, jsgn * jtop ); } else { rexp = jsgn * jtop; rexp = rexp / jbot; rexp = pow ( 10.0, rexp ); } } r = isgn * rexp * rtop / rbot; return r; } //****************************************************************************80 bool s_to_r8vec ( string s, int n, double rvec[] ) //****************************************************************************80 // // Purpose: // // S_TO_R8VEC reads an R8VEC from a string. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 05 July 2009 // // Author: // // John Burkardt // // Parameters: // // Input, string S, the string to be read. // // Input, int N, the number of values expected. // // Output, double RVEC[N], the values read from the string. // // Output, bool S_TO_R8VEC, is true if an error occurred. // { int begin; bool error; int i; int lchar; int length; begin = 0; length = s.length ( ); error = 0; for ( i = 0; i < n; i++ ) { rvec[i] = s_to_r8 ( s.substr(begin,length), &lchar, &error ); if ( error ) { return error; } begin = begin + lchar; length = length - lchar; } return error; } //****************************************************************************80 int s_word_count ( string s ) //****************************************************************************80 // // Purpose: // // S_WORD_COUNT counts the number of "words" in a string. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 05 July 2009 // // Author: // // John Burkardt // // Parameters: // // Input, string S, the string to be examined. // // Output, int S_WORD_COUNT, the number of "words" in the string. // Words are presumed to be separated by one or more blanks. // { bool blank; int char_count; int i; int word_count; word_count = 0; blank = true; char_count = s.length ( ); for ( i = 0; i < char_count; i++ ) { if ( isspace ( s[i] ) ) { blank = true; } else if ( blank ) { word_count = word_count + 1; blank = false; } } return word_count; } //****************************************************************************80 void sort_heap_external ( int n, int *indx, int *i, int *j, int isgn ) //****************************************************************************80 // // Purpose: // // SORT_HEAP_EXTERNAL externally sorts a list of items into ascending order. // // Discussion: // // The actual list is not passed to the routine. Hence it may // consist of integers, reals, numbers, names, etc. The user, // after each return from the routine, will be asked to compare or // interchange two items. // // The current version of this code mimics the FORTRAN version, // so the values of I and J, in particular, are FORTRAN indices. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 05 February 2004 // // Author: // // Original FORTRAN77 by Albert Nijenhuis, Herbert Wilf. // C++ version by John Burkardt. // // Parameters: // // Input, int N, the length of the input list. // // Input/output, int *INDX. // The user must set INDX to 0 before the first call. // On return, // if INDX is greater than 0, the user must interchange // items I and J and recall the routine. // If INDX is less than 0, the user is to compare items I // and J and return in ISGN a negative value if I is to // precede J, and a positive value otherwise. // If INDX is 0, the sorting is done. // // Output, int *I, *J. On return with INDX positive, // elements I and J of the user's list should be // interchanged. On return with INDX negative, elements I // and J are to be compared by the user. // // Input, int ISGN. On return with INDX negative, the // user should compare elements I and J of the list. If // item I is to precede item J, set ISGN negative, // otherwise set ISGN positive. // { static int i_save = 0; static int j_save = 0; static int k = 0; static int k1 = 0; static int n1 = 0; // // INDX = 0: This is the first call. // if ( *indx == 0 ) { i_save = 0; j_save = 0; k = n / 2; k1 = k; n1 = n; } // // INDX < 0: The user is returning the results of a comparison. // else if ( *indx < 0 ) { if ( *indx == -2 ) { if ( isgn < 0 ) { i_save = i_save + 1; } j_save = k1; k1 = i_save; *indx = -1; *i = i_save; *j = j_save; return; } if ( 0 < isgn ) { *indx = 2; *i = i_save; *j = j_save; return; } if ( k <= 1 ) { if ( n1 == 1 ) { i_save = 0; j_save = 0; *indx = 0; } else { i_save = n1; j_save = 1; n1 = n1 - 1; *indx = 1; } *i = i_save; *j = j_save; return; } k = k - 1; k1 = k; } // // 0 < INDX: the user was asked to make an interchange. // else if ( *indx == 1 ) { k1 = k; } for ( ;; ) { i_save = 2 * k1; if ( i_save == n1 ) { j_save = k1; k1 = i_save; *indx = -1; *i = i_save; *j = j_save; return; } else if ( i_save <= n1 ) { j_save = i_save + 1; *indx = -2; *i = i_save; *j = j_save; return; } if ( k <= 1 ) { break; } k = k - 1; k1 = k; } if ( n1 == 1 ) { i_save = 0; j_save = 0; *indx = 0; *i = i_save; *j = j_save; } else { i_save = n1; j_save = 1; n1 = n1 - 1; *indx = 1; *i = i_save; *j = j_save; } return; } //****************************************************************************80 void timestamp ( ) //****************************************************************************80 // // Purpose: // // TIMESTAMP prints the current YMDHMS date as a time stamp. // // Example: // // May 31 2001 09:45:54 AM // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 24 September 2003 // // Author: // // John Burkardt // // Parameters: // // None // { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct tm *tm; time_t now; now = time ( NULL ); tm = localtime ( &now ); strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); cout << time_buffer << "\n"; return; # undef TIME_SIZE }