def _get_full_disjoint_col_matrix_cols(col_matrix): """ Find sets of disjoint columns in a column intersection matrix. Parameters ---------- col_matrix : ndarray Column intersection matrix Returns ------- list List of lists of disjoint columns """ color_groups = [] _, ncols = col_matrix.shape # -1 indicates that a column has not been colored colors = np.full(ncols, -1, dtype=get_index_dtype(maxval=ncols)) for col in _order_by_ID(col_matrix): neighbor_colors = set(colors[col_matrix[col]]) for color, grp in enumerate(color_groups): if color not in neighbor_colors: grp.append(col) colors[col] = color break else: colors[col] = len(color_groups) color_groups.append([col]) return color_groups
def _get_full_disjoint_col_matrix_cols(col_matrix): """ Find sets of disjoint columns in a column intersection matrix. Parameters ---------- col_matrix : ndarray Column intersection matrix Returns ------- list List of lists of disjoint columns """ color_groups = [] _, ncols = col_matrix.shape # -1 indicates that a column has not been colored colors = np.full(ncols, -1, dtype=get_index_dtype(maxval=ncols)) for col in _order_by_ID(col_matrix): neighbor_colors = set(colors[col_matrix[col]]) for color, grp in enumerate(color_groups): if color not in neighbor_colors: grp.append(col) colors[col] = color break else: colors[col] = len(color_groups) color_groups.append([col]) return color_groups
def _order_by_ID(col_matrix): """ Return columns in order of incidence degree (ID). ID is the number of already colored neighbors (neighbors are dependent columns). Parameters ---------- col_matrix : ndarray Boolean array of column dependencies. Yields ------ int Column index. """ degrees = _count_nonzeros(col_matrix, axis=0) ncols = degrees.size if ncols == 0: return # use max degree column as a starting point instead of just choosing a random column # since all have incidence degree of 0 when we start. start = degrees.argmax() yield start colored_degrees = np.zeros(degrees.size, dtype=get_index_dtype(maxval=degrees[start])) colored_degrees[col_matrix[start]] += 1 colored_degrees[ start] = -ncols # ensure that this col will never have max degree again for i in range(ncols - 1): col = colored_degrees.argmax() colored_degrees[col_matrix[col]] += 1 colored_degrees[ col] = -ncols # ensure that this col will never have max degree again yield col
def _order_by_ID(col_matrix): """ Return columns in order of incidence degree (ID). ID is the number of already colored neighbors (neighbors are dependent columns). Parameters ---------- col_matrix : ndarray Boolean array of column dependencies. Yields ------ int Column index. """ degrees = _count_nonzeros(col_matrix, axis=0) ncols = degrees.size if ncols == 0: return # use max degree column as a starting point instead of just choosing a random column # since all have incidence degree of 0 when we start. start = degrees.argmax() yield start colored_degrees = np.zeros(degrees.size, dtype=get_index_dtype(maxval=degrees[start])) colored_degrees[col_matrix[start]] += 1 colored_degrees[start] = -ncols # ensure that this col will never have max degree again for i in range(ncols - 1): col = colored_degrees.argmax() colored_degrees[col_matrix[col]] += 1 colored_degrees[col] = -ncols # ensure that this col will never have max degree again yield col