/** Expands the cluster order while adding new neighbor points to the order. * @param db All data points that are to be considered by the algorithm. Changes their values. * @param p The point to be examined. * @param eps The epsilon representing the radius of the epsilon-neighborhood. * @param min_pts The minimum number of points to be found within an epsilon-neigborhood. * @param[out] o_ordered_vector The ordered vector of data points. Elements will be added to this vector. */ void expand_cluster_order( DataVector& db, DataPoint* p, const real eps, const unsigned int min_pts, DataVector& o_ordered_vector) { assert( eps >= 0 && "eps must not be negative"); assert( min_pts > 0 && "min_pts must be greater than 0"); DataVector N_eps = get_neighbors( p, eps, db); p->reachability_distance( OPTICS::UNDEFINED); const real core_dist_p = squared_core_distance( p, min_pts, N_eps); p->processed( true); o_ordered_vector.push_back( p); if( core_dist_p == OPTICS::UNDEFINED) return; DataSet seeds; update_seeds( N_eps, p, core_dist_p, seeds); while( !seeds.empty()) { DataPoint* q = *seeds.begin(); seeds.erase( seeds.begin()); // remove first element from seeds DataVector N_q = get_neighbors( q, eps, db); const real core_dist_q = squared_core_distance( q, min_pts, N_q); q->processed( true); o_ordered_vector.push_back( q); if( core_dist_q != OPTICS::UNDEFINED) { // *** q is a core-object *** update_seeds( N_q, q, core_dist_q, seeds); } } }
void clique::expand_cluster(clique_block & p_block) { p_block.touch(); if (p_block.get_points().size() <= m_density_threshold) { if (!p_block.get_points().empty()) { m_result_ptr->noise().insert(m_result_ptr->noise().end(), p_block.get_points().begin(), p_block.get_points().end()); } return; } m_result_ptr->clusters().push_back({ }); cluster & cur_cluster = m_result_ptr->clusters().back(); cur_cluster.insert(cur_cluster.end(), p_block.get_points().begin(), p_block.get_points().end()); std::list<clique_block *> neighbors; get_neighbors(p_block, neighbors); for (clique_block * neighbor : neighbors) { if (neighbor->get_points().size() > m_density_threshold) { cur_cluster.insert(cur_cluster.end(), neighbor->get_points().begin(), neighbor->get_points().end()); get_neighbors(*neighbor, neighbors); } else if (!neighbor->get_points().empty()) { m_result_ptr->noise().insert(m_result_ptr->noise().end(), neighbor->get_points().begin(), neighbor->get_points().end()); } } }
/* Split a mask into a mask with just the cycles, and another mask with the remaining dots. */ void separate_cycles(mask_t mask, mask_t *cycles, mask_t *no_cycles) { int i, j; *cycles = mask; *no_cycles = 0; for (i = j = 0; i < NUM_DOTS; j = ++i) { if (MASK_CONTAINS(*cycles, j)) { /* Repeatedly remove dots that only have less than two neighbors and add * them to the mask that has no cycles. */ int num_neighbors; neighbors_t neighbors; get_neighbors(*cycles, j, &num_neighbors, neighbors); while (num_neighbors <= 1) { *cycles = REMOVE_FROM_MASK(*cycles, j); *no_cycles = ADD_TO_MASK(*no_cycles, j); if (num_neighbors == 0) { break; } j = neighbors[0]; get_neighbors(*cycles, j, &num_neighbors, neighbors); } } } }
std::vector<int> breadth_first_search(int root, const std::map<int,std::set<int>>& edges) { // Initialize data structures std::unordered_map<int,bool> is_visited; std::vector<int> sorted_nodes; std::queue<int> search_queue; search_queue.push(root); // Visit nodes until search queue is exhausted while (!search_queue.empty()) { const auto& node = search_queue.front(); search_queue.pop(); for (const auto& neighbor : get_neighbors(node, edges)) { if (!is_visited[neighbor]) { is_visited[neighbor] = true; sorted_nodes.push_back(neighbor); search_queue.push(neighbor); } } } // Return list of sorted nodes return sorted_nodes; }
std::vector<int> depth_first_search(int root, const std::map<int,std::set<int>>& edges) { // Initialize data structures std::unordered_map<int,bool> is_visited, is_sorted; std::vector<int> sorted_nodes; std::stack<int> search_stack; search_stack.push(root); // Visit nodes until search stack is exhausted while (!search_stack.empty()) { const auto& node = search_stack.top(); search_stack.pop(); if (!is_sorted[node]) { if (is_visited[node]) { // Add node to sorted list if we have already visited is_sorted[node] = true; sorted_nodes.push_back(node); } else { // Visit node and add neighbors to search stack is_visited[node] = true; search_stack.push(node); for (const auto& neighbor : get_neighbors(node, edges)) { if (!is_visited[neighbor] && !is_sorted[neighbor]) { search_stack.push(neighbor); } } } } } // Return list of sorted nodes return sorted_nodes; }
dev_info_t * i_ddi_create_branch(dev_info_t *pdip, int nid) { char *buf; dev_info_t *dip = NULL; if (pdip == NULL || nid == OBP_NONODE || nid == OBP_BADNODE) return (NULL); buf = kmem_alloc(OBP_MAXPROPNAME, KM_SLEEP); if (getlongprop_buf(nid, OBP_NAME, buf, OBP_MAXPROPNAME) > 0) { if (check_status(nid, buf, pdip) == DDI_SUCCESS) dip = ddi_add_child(pdip, buf, nid, -1); } kmem_free(buf, OBP_MAXPROPNAME); if (dip == NULL) return (NULL); /* * Don't create any siblings of the branch root, just * children. */ (void) get_neighbors(dip, DDI_WALK_PRUNESIB); di_dfs(ddi_get_child(dip), get_neighbors, 0); return (dip); }
void legion_network::neuron_states(const double t, const differ_state<double> & inputs, const differ_extra<void *> & argv, differ_state<double> & outputs) { unsigned int index = *(unsigned int *) argv[1]; const double x = inputs[0]; const double y = inputs[1]; const double p = inputs[2]; double potential_influence = heaviside(p + std::exp(-m_params.alpha * t) - m_params.teta); double stumulus = 0.0; if ((*m_stimulus)[index] > 0) { stumulus = m_params.I; } double dx = 3.0 * x - std::pow(x, 3) + 2.0 - y + stumulus * potential_influence + m_oscillators[index].m_coupling_term + m_oscillators[index].m_noise; double dy = m_params.eps * (m_params.gamma * (1.0 + std::tanh(x / m_params.betta)) - y); std::vector<unsigned int> * neighbors = get_neighbors(index); double potential = 0.0; for (std::vector<unsigned int>::const_iterator index_iterator = neighbors->begin(); index_iterator != neighbors->end(); index_iterator++) { unsigned int index_neighbor = *index_iterator; potential += m_params.T * heaviside(m_oscillators[index_neighbor].m_excitatory - m_params.teta_x); } delete neighbors; double dp = m_params.lamda * (1 - p) * heaviside(potential - m_params.teta_p) - m_params.mu * p; outputs.clear(); outputs.push_back(dx); outputs.push_back(dy); outputs.push_back(dp); }
void legion_network::create_dynamic_connections(const legion_stimulus & stimulus) { for (size_t i = 0; i < size(); i++) { /* re-initialization by 0.0 */ std::fill(m_dynamic_connections[i].begin(), m_dynamic_connections[i].end(), 0.0); std::vector<unsigned int> * neighbors = get_neighbors(i); if (neighbors->size() > 0 && stimulus[i] > 0) { int number_stimulated_neighbors = 0; for (std::vector<unsigned int>::iterator index_iterator = neighbors->begin(); index_iterator != neighbors->end(); index_iterator++) { if (stimulus[*index_iterator] > 0) { number_stimulated_neighbors++; } } if (number_stimulated_neighbors > 0) { double dynamic_weight = m_params.Wt / (double) number_stimulated_neighbors; for (std::vector<unsigned int>::iterator index_iterator = neighbors->begin(); index_iterator != neighbors->end(); index_iterator++) { m_dynamic_connections[i][*index_iterator] = dynamic_weight; } } } if (neighbors != NULL) { delete neighbors; } } }
// The strategy here is, though this is n^2, it's a very small n, just // finely-spaced adjacent cells. bool SpatialHash::isWithinDistanceOfAnything(PointType const& physPt, double distance) const { // (save taking a lot of square roots) double d2 = distance * distance; Index idx = index_of(physPt); Cells nbrs; get_neighbors(idx, nbrs); // 27 <= we include the center cell itself, too. assert(nbrs.size() <= 27); for (Cells::const_iterator itCells = nbrs.begin(), endCells = nbrs.end(); itCells != endCells; ++itCells) { Pts const* pts = *itCells; if (pts) { for (Pts::const_iterator itPts = pts->begin(), endPts = pts->end(); itPts != endPts; ++itPts) { if (itPts->SquaredEuclideanDistanceTo<double>(physPt) < d2) { return true; } } } } return false; }
int find_path(AStar_t astar,int x,int y,int x1,int y1,kn_dlist *path){ AStarNode *from = get_node(astar,x,y); AStarNode *to = get_node(astar,x1,y1); if(!from || !to || from == to || to->block) return 0; minheap_insert(astar->open_list,&from->heap); AStarNode *current_node = NULL; while(1){ struct heapele *e = minheap_popmin(astar->open_list); if(!e){ reset(astar); return 0; } current_node = (AStarNode*)((int8_t*)e-sizeof(kn_dlist_node)); if(current_node == to){ while(current_node) { kn_dlist_remove((kn_dlist_node*)current_node);//可能在close_list中,需要删除 if(current_node != from)//当前点无需加入到路径点中 kn_dlist_push_front(path,(kn_dlist_node*)current_node); AStarNode *t = current_node; current_node = current_node->parent; t->parent = NULL; t->F = t->G = t->H = 0; t->heap.index = 0; } reset(astar); return 1; } //current插入到close表 kn_dlist_push(&astar->close_list,(kn_dlist_node*)current_node); //获取current的相邻节点 kn_dlist *neighbors = get_neighbors(astar,current_node); if(neighbors) { AStarNode *n; while((n = (AStarNode*)kn_dlist_pop(neighbors))){ if(n->heap.index)//在openlist中 { float new_G = current_node->G + cost_2_neighbor(current_node,n); if(new_G < n->G) { //经过当前neighbor路径更佳,更新路径 n->G = new_G; n->F = n->G + n->H; n->parent = current_node; minheap_change(astar->open_list,&n->heap); } continue; } n->parent = current_node; n->G = current_node->G + cost_2_neighbor(current_node,n); n->H = cost_2_goal(n,to); n->F = n->G + n->H; minheap_insert(astar->open_list,&n->heap); } neighbors = NULL; } } }
int IntAutos::automation_is_constant(int64_t start, int64_t end) { Auto *current_auto, *before = 0, *after = 0; int result; result = 1; // default to constant if(!last && !first) return result; // no automation at all // quickly get autos just outside range get_neighbors(start, end, &before, &after); // autos before range if(before) current_auto = before; // try first auto else current_auto = first; // test autos in range for( ; result && current_auto && current_auto->next && current_auto->position < end; current_auto = current_auto->next) { // not constant if(((IntAuto*)current_auto->next)->value != ((IntAuto*)current_auto)->value) result = 0; } return result; }
void Triangulation::calculate_boundaries() { _VERBOSE("Triangulation::calculate_boundaries"); get_neighbors(); // Ensure _neighbors has been created. // Create set of all boundary TriEdges, which are those which do not // have a neighbor triangle. typedef std::set<TriEdge> BoundaryEdges; BoundaryEdges boundary_edges; for (int tri = 0; tri < _ntri; ++tri) { if (!is_masked(tri)) { for (int edge = 0; edge < 3; ++edge) { if (get_neighbor(tri, edge) == -1) { boundary_edges.insert(TriEdge(tri, edge)); } } } } // Take any boundary edge and follow the boundary until return to start // point, removing edges from boundary_edges as they are used. At the same // time, initialise the _tri_edge_to_boundary_map. while (!boundary_edges.empty()) { // Start of new boundary. BoundaryEdges::iterator it = boundary_edges.begin(); int tri = it->tri; int edge = it->edge; _boundaries.push_back(Boundary()); Boundary& boundary = _boundaries.back(); while (true) { boundary.push_back(TriEdge(tri, edge)); boundary_edges.erase(it); _tri_edge_to_boundary_map[TriEdge(tri, edge)] = BoundaryEdge(_boundaries.size()-1, boundary.size()-1); // Move to next edge of current triangle. edge = (edge+1) % 3; // Find start point index of boundary edge. int point = get_triangle_point(tri, edge); // Find next TriEdge by traversing neighbors until find one // without a neighbor. while (get_neighbor(tri, edge) != -1) { tri = get_neighbor(tri, edge); edge = get_edge_in_triangle(tri, point); } if (TriEdge(tri,edge) == boundary.front()) break; // Reached beginning of this boundary, so finished it. else it = boundary_edges.find(TriEdge(tri, edge)); } } }
Pixel soft_dot(const Ink *inky, const SHORT x, const SHORT y) { Rgb3 rgb; Pixel neighbors[9]; average_colors(&rgb, neighbors, get_neighbors(neighbors,x,y), vb.pencel->cmap); return(bclosest_col(&rgb,COLORS,inky->dither)); }
/* Get all unique paths through a color mask with no cycles, starting from an end-point * (a dot with only one neighbor). This is accomplished by searching for all paths that * connect two end-points, and then computing all of its sub-paths. This uses a modified * depth-first search that "fast-forwards" through parts of paths with only one way to go. */ void build_paths( mask_t color_mask, /* The color mask that paths are being found in. */ visited_t visited, /* Matrix of (start,end) pairs representing visited paths. */ int start_index, /* Where to start the path (or where it was started, after recursion). */ int allow_shrinkers, /* Whether to generate paths of length 1, which requires a power-up in-game. */ int *num_moves, /* Number of moves currently stored in moves. */ move_list_t moves, /* Array of moves being generated. */ int current_index, /* Current end position of the path. */ int path_length, /* Current path length. */ path_t path /* Array of indices representing the path. */ ) { int num_neighbors, i; neighbors_t neighbors; /* Follow the path until the end or an intersection is reached, removing * dots as we go to avoid moving backwards. */ get_neighbors(color_mask, current_index, &num_neighbors, neighbors); color_mask = REMOVE_FROM_MASK(color_mask, current_index); path[path_length++] = current_index; while (num_neighbors == 1) { current_index = neighbors[0]; get_neighbors(color_mask, current_index, &num_neighbors, neighbors); color_mask = REMOVE_FROM_MASK(color_mask, current_index); path[path_length++] = current_index; } /* If there are no neighbors left (i.e. the end of the path has been reached), * then yield the resulting path if it hasn't been visited yet. */ if (num_neighbors == 0) { if (!visited[start_index][current_index]) { get_subpaths(num_moves, moves, visited, allow_shrinkers, path_length, path); } return; } /* Recurse on the remaining branches of the intersection. */ for (i = 0; i < num_neighbors; i++) { build_paths(color_mask, visited, start_index, allow_shrinkers, num_moves, moves, neighbors[i], path_length, path); } }
arma::Mat<double> RobotChasing::calculate_probabilities() { calculate_states(); arma::Mat<double> all_probabilities(state_to_ind.size() * m_possible_actions.size(),state_to_ind.size(), arma::fill::zeros); int it = 0; for(unsigned int i = 0; i <= m_max_x; i++) { for(unsigned int j = 0; j <= m_max_y; j++) { for(unsigned int k = 0; k <= m_max_x; k++) { for(unsigned int l = 0; l <= m_max_y; l++) { for(unsigned int a = 0; a < m_possible_actions.size(); a++) { vector<double> s; s.push_back(i); s.push_back(j); s.push_back(k); s.push_back(l); if(i == k && j == l) { int ind = state_to_ind[s]; all_probabilities(it, ind) += 1.0; } else { vector<vector<double> > neighbors = get_neighbors(s,a); for(unsigned int n = 0; n < neighbors.size(); n++) { vector<double> neighbor = neighbors[n]; // for(unsigned int z = 0; z < neighbor.size(); z++) // { // cout << neighbor[z] << ","; // } // cout << endl; int ind = state_to_ind[neighbor]; all_probabilities(it, ind) += 1.0/neighbors.size(); } } it++; } } } } } //all_probabilities.print(); //exit(1); return all_probabilities; }
bool is_closure(const std::set<int>& nodes, const std::map<int,std::set<int>>& edges) { for (const auto& node : nodes) { for (const auto& neighbor : get_neighbors(node, edges)) { if (nodes.count(neighbor) == 0) { return false; } } } return true; }
void print(const std::set<int>& nodes, const std::map<int,std::set<int>>& edges, std::ostream& os) { for (const auto& node : nodes) { os << "node " << node << " neighbors :"; for (const auto& neighbor : get_neighbors(node, edges)) { os << " " << neighbor; } os << "\n"; } }
std::map<int,std::set<int>> induce_subgraph(const std::set<int>& nodes, const std::map<int,std::set<int>>& edges) { std::map<int,std::set<int>> induced_edges; for (const auto& node : nodes) { for (const auto& neighbor : get_neighbors(node, edges)) { if (nodes.count(neighbor) > 0) { induced_edges[node].insert(neighbor); } } } return induced_edges; }
void pcnn::calculate_states(const pcnn_stimulus & stimulus) { std::vector<double> feeding(size(), 0.0); std::vector<double> linking(size(), 0.0); std::vector<double> outputs(size(), 0.0); for (unsigned int index = 0; index < size(); index++) { pcnn_oscillator & current_oscillator = m_oscillators[index]; std::vector<unsigned int> neighbors; get_neighbors(index, neighbors); double feeding_influence = 0.0; double linking_influence = 0.0; for (std::vector<unsigned int>::const_iterator iter = neighbors.begin(); iter != neighbors.end(); iter++) { const double output_neighbor = m_oscillators[(*iter)].output; feeding_influence += output_neighbor * m_params.M; linking_influence += output_neighbor * m_params.W; } feeding_influence *= m_params.VF; linking_influence *= m_params.VL; feeding[index] = m_params.AF * current_oscillator.feeding + stimulus[index] + feeding_influence; linking[index] = m_params.AL * current_oscillator.linking + linking_influence; /* calculate internal activity */ double internal_activity = feeding[index] * (1.0 + m_params.B * linking[index]); /* calculate output of the oscillator */ if (internal_activity > current_oscillator.threshold) { outputs[index] = OUTPUT_ACTIVE_STATE; } else { outputs[index] = OUTPUT_INACTIVE_STATE; } } /* fast linking */ if (m_params.FAST_LINKING) { fast_linking(feeding, linking, outputs); } /* update states of oscillators */ for (unsigned int index = 0; index < size(); index++) { pcnn_oscillator & oscillator = m_oscillators[index]; oscillator.feeding = feeding[index]; oscillator.linking = linking[index]; oscillator.output = outputs[index]; oscillator.threshold = m_params.AT * oscillator.threshold + m_params.VT * outputs[index]; } }
double sync_network::phase_kuramoto(const double t, const double teta, const std::vector<void *> & argv) const { unsigned int index = *(unsigned int *) argv[1]; double phase = 0.0; std::vector<size_t> neighbors; get_neighbors(index, neighbors); for (std::vector<size_t>::const_iterator index_iterator = neighbors.cbegin(); index_iterator != neighbors.cend(); index_iterator++) { unsigned int index_neighbor = (*index_iterator); phase += std::sin(m_oscillators[index_neighbor].phase - teta); } phase = m_oscillators[index].frequency + (phase * weight / size()); return phase; }
void LifeGame::update_cell(int cell, std::vector<bool> &from, std::vector<bool> &to) { int neighbors = get_neighbors(cell%size, cell / size); if (from[cell]) { if ((neighbors < 2) || (neighbors > 3)) to[cell] = false; else to[cell] = true; } else { if (neighbors == 3) to[cell] = true; else to[cell] = false; } }
bool is_topologically_sorted(const std::set<int>& nodes, const std::map<int,std::set<int>>& edges) { if (!is_closure(nodes, edges)) { std::stringstream err; err << __FILE__ << " " << __LINE__ << " :: " << "graph is not a closure"; throw lbann_exception(err.str()); } for (const auto& node : nodes) { const auto& neighbors = get_neighbors(node, edges); if (neighbors.size() > 0 && *neighbors.begin() <= node) { return false; } } return true; }
std::map<int,std::set<int>> transpose(const std::set<int>& nodes, const std::map<int,std::set<int>>& edges) { if (!is_closure(nodes, edges)) { std::stringstream err; err << __FILE__ << " " << __LINE__ << " :: " << "graph is not a closure"; throw lbann_exception(err.str()); } std::map<int,std::set<int>> transpose_edges; for (const auto& node : nodes) { for (const auto& neighbor : get_neighbors(node, edges)) { transpose_edges[neighbor].insert(node); } } return transpose_edges; }
/* Get all possible paths in a color mask without cycles. */ void get_paths(mask_t color_mask, int allow_shrinkers, int *num_moves, move_list_t moves) { int i, num_neighbors; neighbors_t neighbors; path_t path; visited_t visited = {{0}}; /* Repeatedly call build_paths() from each end-point of the color mask */ for (i = 0; i < NUM_DOTS; i++) { if (MASK_CONTAINS(color_mask, i)) { get_neighbors(color_mask, i, &num_neighbors, neighbors); if (num_neighbors <= 1) { build_paths(color_mask, visited, i, allow_shrinkers, num_moves, moves, i, 0, path); } } } }
void register_pe (pos_desc *mypos, int pe_number) { mypos->xpos = pe_number % x_size; mypos->ypos = pe_number / x_size; mypos->neighbor_count = get_neighbors(mypos->xpos, mypos->ypos); memset(mypos->neighbors, -1, MAX_NEIGHBORS * sizeof(int)); if (have_north_neighbor(mypos->ypos)) mypos->neighbors[NORTH] = pe_number - x_size; if (have_south_neighbor(mypos->ypos)) mypos->neighbors[SOUTH] = pe_number + x_size; if (have_east_neighbor(mypos->xpos)) mypos->neighbors[EAST] = pe_number + 1; if (have_west_neighbor(mypos->xpos)) mypos->neighbors[WEST] = pe_number - 1; return; }
double FloatAutos::get_automation_constant(int64_t start, int64_t end) { Auto *current_auto, *before = 0, *after = 0; // quickly get autos just outside range get_neighbors(start, end, &before, &after); // no auto before range so use first if(before) current_auto = before; else current_auto = first; // no autos at all so use default value if(!current_auto) current_auto = default_auto; return ((FloatAuto*)current_auto)->value; }
void pcnn::fast_linking(const std::vector<double> & feeding, std::vector<double> & linking, std::vector<double> & output) { std::vector<double> previous_outputs(output.cbegin(), output.cend()); bool previous_output_change = true; bool current_output_change = false; while (previous_output_change) { for (unsigned int index = 0; index < size(); index++) { pcnn_oscillator & current_oscillator = m_oscillators[index]; std::vector<unsigned int> neighbors; get_neighbors(index, neighbors); double linking_influence = 0.0; for (std::vector<unsigned int>::const_iterator iter = neighbors.begin(); iter != neighbors.end(); iter++) { linking_influence += previous_outputs[(*iter)] * m_params.W; } linking_influence *= m_params.VL; linking[index] = linking_influence; double internal_activity = feeding[index] * (1.0 + m_params.B * linking[index]); if (internal_activity > current_oscillator.threshold) { output[index] = OUTPUT_ACTIVE_STATE; } else { output[index] = OUTPUT_INACTIVE_STATE; } if (output[index] != previous_outputs[index]) { current_output_change = true; } } /* check for changes for avoiding useless operation copy */ if (current_output_change) { std::copy(output.begin(), output.end(), previous_outputs.begin()); } previous_output_change = current_output_change; current_output_change = false; } }
/* Find a path through a mask. Returns the length of the path stored in `path`. The length is 0 if no path was found. */ int _mask_to_path(mask_t mask, mask_t visited, int index, int edges[NUM_DOTS][NUM_DOTS], int path_length, path_t path) { int j, num_neighbors, nowhere_to_go; neighbors_t neighbors; /* All of the branches of this DFS share the same buffer for path, but that's okay because it * quits as soon as it finds a path that uses all of the dots. */ path[path_length++] = index; visited = ADD_TO_MASK(visited, index); nowhere_to_go = 1; get_neighbors(mask, index, &num_neighbors, neighbors); for (j = 0; j < num_neighbors; j++) { int new_index = neighbors[j]; /* Don't follow the same edge twice. */ if (!edges[index][new_index]) { int final_path_length, new_edges[NUM_DOTS][NUM_DOTS]; nowhere_to_go = 0; /* Make a copy of the edges and mark the edge about to be traversed. */ memcpy(new_edges, edges, sizeof(new_edges)); new_edges[index][new_index] = 1; new_edges[new_index][index] = 1; final_path_length= _mask_to_path(mask, visited, new_index, new_edges, path_length, path); if (final_path_length > 0) { return final_path_length; } } } /* End condition: all dots have been visited and there is nowhere left to go. * This works even for cycles where it might visit the same dot twice. */ if (mask == visited && nowhere_to_go) { return path_length; } /* No path was found. */ return 0; }
bool is_cyclic(const std::set<int>& nodes, const std::map<int,std::set<int>>& edges) { // Check that graph is valid if (!is_closure(nodes, edges)) { std::stringstream err; err << __FILE__ << " " << __LINE__ << " :: " << "graph is not a closure"; throw lbann_exception(err.str()); } // Topologically sorted graphs are not cyclic if (is_topologically_sorted(nodes, edges)) { return false; } // Perform depth-first searches to detect cycles std::unordered_map<int,bool> is_visited, is_sorted; std::stack<int> search_stack; for (auto&& it = nodes.rbegin(); it != nodes.rend(); ++it) { search_stack.push(*it); } while (!search_stack.empty()) { const auto& node = search_stack.top(); search_stack.pop(); if (!is_sorted[node]) { if (is_visited[node]) { is_sorted[node] = true; } else { is_visited[node] = true; search_stack.push(node); for (const auto& neighbor : get_neighbors(node, edges)) { if (is_visited[neighbor] && !is_sorted[neighbor]) { return true; } search_stack.push(neighbor); } } } } return false; }
SCM get_living_neighbors (SCM board_smob, SCM cell_smob) { SCM list; struct cell *cell; int i; int count; scm_assert_smob_type(board_tag, board_smob); scm_assert_smob_type(cell_tag, cell_smob); list = get_neighbors(board_smob, cell_smob); count = 0; for (i = 0; i < scm_to_int(scm_length(list)); i++) { cell = (struct cell *) SCM_SMOB_DATA(scm_list_ref(list, scm_from_int(i))); if (cell->status > 0) { count++; } } return scm_from_int(count); }