Neighbors Board::getNeighbors(int pos) { // row is current position, elements are its neighbors, at most 4, -1 means null static const int rawData[][4] = { {1,3,8,-1}, {0,2,4,-1}, {1,5,13,-1}, {0,4,6,9}, {1,3,5,-1}, {2,4,7,12}, {3,7,10,-1}, {5,6,11,-1}, {0,9,20,-1}, {3,8,10,17}, {6,9,14,-1}, {7,12,16,-1}, {5,11,13,19}, {2,12,22,-1}, {10,15,17,-1}, {14,16,18,-1}, {11,15,19,-1}, {9,14,18,20}, {15,17,19,21}, {12,16,18,22}, {8,17,21,-1}, {18,20,22,-1}, {13,19,21,-1} }; Neighbors result; for(int j=0; j<4; ++j) if(rawData[pos][j] > -1) result.push_back(rawData[pos][j]); return result; }
Neighbors find_neighbors_bruteforce_impl(const RandomAccessIterator& begin, const RandomAccessIterator& end, Callback callback, IndexType k) { timed_context context("Distance sorting based neighbors search"); typedef std::pair<RandomAccessIterator, ScalarType> DistanceRecord; typedef std::vector<DistanceRecord> Distances; Neighbors neighbors; neighbors.reserve(end-begin); for (RandomAccessIterator iter=begin; iter!=end; ++iter) { Distances distances; for (RandomAccessIterator around_iter=begin; around_iter!=end; ++around_iter) distances.push_back(std::make_pair(around_iter, callback.distance(iter,around_iter))); std::nth_element(distances.begin(),distances.begin()+k+1,distances.end(), distances_comparator<DistanceRecord>()); LocalNeighbors local_neighbors; local_neighbors.reserve(k); for (typename Distances::const_iterator neighbors_iter=distances.begin(); neighbors_iter!=distances.begin()+k+1; ++neighbors_iter) { if (neighbors_iter->first != iter) local_neighbors.push_back(neighbors_iter->first - begin); } neighbors.push_back(local_neighbors); } return neighbors; }
TEMPLATE_HEADER typename LR::Data LR::solveNormalEqn( Coordinate & c, Neighbors & nset ){ if( y.size().rows != Basis::size ){ A.resize( Basis::size, Basis::size ); y.resize( Basis::size, 1); } // find bounds for points Coordinate lb = nset[0].point, rb = nset[0].point, tmp; for( int j = 0; j < Coordinate::Dims ; j++ ){ for( int i = 1; i < nset.size(); i++ ){ lb[j] = fmin(nset[i].point[j],lb[j]); rb[j] = fmax(nset[i].point[j],rb[j]); } } // compute weights std::vector<double> weights(nset.size()); wfunc(weights,nset); // store values for A A = 0; for(int j = 0; j < Basis::size; j++ ){ for( int k = j; k < Basis::size; k++ ){ for( int i = 0; i < nset.size(); i++ ){ scale(lb,rb,nset[i].point,tmp); A(j,k) += basis(k, tmp)*basis(j, tmp)*weights[i]; } } } // store values for y y = 0; for(int j = 0; j < Basis::size; j++ ){ for( int i = 0; i < nset.size(); i++ ){ scale(lb,rb,nset[i].point,tmp); y[j] += basis(j, tmp)*weights[i]*nset[i].data; } } // solve system of equations la::solve(A,y,coef); // compute point Data output = 0; for( int i = 0; i < Basis::size; i++ ){ scale(lb,rb,c,tmp); output += coef[i]*basis(i,tmp); } return output; }
bool Board::isBlocked(int pos) const { Neighbors neighbors = getNeighbors(pos); for(Neighbors::const_iterator it = neighbors.begin(); it != neighbors.end(); ++it) if(chessmen[*it] == 'x') // try to find an empty neighbor return false; return true; }
void Mesh::FindAdjacencies(const aiMesh* paiMesh, vector<unsigned int>& Indices) { // Step 1 - find the two triangles that share every edge for (uint i = 0 ; i < paiMesh->mNumFaces ; i++) { const aiFace& face = paiMesh->mFaces[i]; Face Unique; // If a position vector is duplicated in the VB we fetch the // index of the first occurrence. for (uint j = 0 ; j < 3 ; j++) { uint Index = face.mIndices[j]; aiVector3D& v = paiMesh->mVertices[Index]; if (m_posMap.find(v) == m_posMap.end()) { m_posMap[v] = Index; } else { Index = m_posMap[v]; } Unique.Indices[j] = Index; } m_uniqueFaces.push_back(Unique); Edge e1(Unique.Indices[0], Unique.Indices[1]); Edge e2(Unique.Indices[1], Unique.Indices[2]); Edge e3(Unique.Indices[2], Unique.Indices[0]); m_indexMap[e1].AddNeigbor(i); m_indexMap[e2].AddNeigbor(i); m_indexMap[e3].AddNeigbor(i); } // Step 2 - build the index buffer with the adjacency info for (uint i = 0 ; i < paiMesh->mNumFaces ; i++) { const Face& face = m_uniqueFaces[i]; for (uint j = 0 ; j < 3 ; j++) { Edge e(face.Indices[j], face.Indices[(j + 1) % 3]); assert(m_indexMap.find(e) != m_indexMap.end()); Neighbors n = m_indexMap[e]; uint OtherTri = n.GetOther(i); assert(OtherTri != -1); const Face& OtherFace = m_uniqueFaces[OtherTri]; uint OppositeIndex = OtherFace.GetOppositeIndex(e); Indices.push_back(face.Indices[j]); Indices.push_back(OppositeIndex); } } }
DBSCAN::Neighbors DBSCAN::find_neighbors( const DBSCAN::DistanceMatrix& D, uint32_t pid ) { Neighbors ne; for ( uint32_t j = 0; j < D.size1(); ++j ) { if ( D( pid, j ) <= m_eps ) { ne.push_back( j ); } } return ne; }
// Zhu et al. "A Rank-Order Distance based Clustering Algorithm for Face Tagging", CVPR 2011 // Ob(x) in eq. 1, modified to consider 0/1 as ground truth imposter/genuine. static int indexOf(const Neighbors &neighbors, int i) { for (int j=0; j<neighbors.size(); j++) { const Neighbor &neighbor = neighbors[j]; if (neighbor.first == i) { if (neighbor.second == 0) return neighbors.size()-1; else if (neighbor.second == 1) return 0; else return j; } } return -1; }
int Board::countFreedom(QChar color) const { int result = 0; for(int i=0; i<23; ++i) if(chessmen[i] == color) { Neighbors neighbors = getNeighbors(i); for(Neighbors::const_iterator it = neighbors.begin(); it != neighbors.end(); ++it) if(chessmen[*it] == 'x') // empty result ++; } return result; }
bool Board::closeMorris(int pos) const { Neighbors neighbors = getNeighbors(pos); for(Neighbors::const_iterator it = neighbors.begin(); it != neighbors.end(); ++it) { if(chessmen[*it] == 'x') // for all it's empty neighbor { Board temp(*this); temp.move(pos, *it); // move to this neighbor if(temp.closeMill(*it)) return true; } } return false; }
SparseMatrix neighbors_distances_matrix(RandomAccessIterator begin, RandomAccessIterator end, const Neighbors& neighbors, DistanceCallback callback, ScalarType& average_distance) { const IndexType k = neighbors[0].size(); const IndexType n = neighbors.size(); if ((end-begin)!=n) throw std::runtime_error("Wrong size"); SparseTriplets sparse_triplets; sparse_triplets.reserve(k*n); average_distance = 0; ScalarType current_distance; for (IndexType i = 0; i < n; ++i) { const LocalNeighbors& current_neighbors = neighbors[i]; for (IndexType j = 0; j < k; ++j) { current_distance = callback.distance(begin[i], begin[current_neighbors[j]]); average_distance += current_distance; SparseTriplet triplet(i, current_neighbors[j], current_distance); sparse_triplets.push_back(triplet); } } average_distance /= (k*n); return sparse_matrix_from_triplets(sparse_triplets, n, n); }
void Surface3D::addLayer(const AllNeighbors& totalNeighbors, int sX, int sY, int sZ) { Surface2D surfaceXY; surfaceXY.reserve(sY); for (int y = 0; y < sY; ++y) { Surface1D surfaceX; surfaceX.reserve(sX); for (int x = 0; x < sX; ++x) { Atoms atoms; atoms.reserve(totalNeighbors.size()); for (auto const& neighbors : totalNeighbors) { Neighbors neighbs; // First neighbors count char numberNeighbs = 0; for (int nb = 0; nb < 4; ++nb) { if (x + neighbors[nb].x >= 0 && y + neighbors[nb].y >= 0 && x + neighbors[nb].x < sX && y + neighbors[nb].y < sY) { ++numberNeighbs; AtomType neighb = {x + neighbors[nb].x, y + neighbors[nb].y, sZ + neighbors[nb].z, neighbors[nb].type, false}; neighbs.push_back(neighb); } } AtomInfo atom; atom.neighbors = neighbs; atom.firstNeighborsCount = numberNeighbs; atom.deleted = numberNeighbs == 0; atoms.push_back(atom); } surfaceX.push_back(Cell(atoms)); } surfaceXY.push_back(surfaceX); } push_back(surfaceXY); }
Neighbors find_neighbors_vptree_impl(const RandomAccessIterator& begin, const RandomAccessIterator& end, Callback callback, IndexType k) { timed_context context("VP-Tree based neighbors search"); Neighbors neighbors; neighbors.reserve(end-begin); VantagePointTree<RandomAccessIterator,Callback> tree(begin,end,callback); for (RandomAccessIterator i=begin; i!=end; ++i) { LocalNeighbors local_neighbors = tree.search(i,k+1); std::remove(local_neighbors.begin(),local_neighbors.end(),i-begin); neighbors.push_back(local_neighbors); } return neighbors; }
void add_cofaces (const Graph& graph, Simplex& tau, const Neighbors& neighbors, Complex& complex, const std::size_t dimension) { typedef typename Neighbors::const_iterator Neighbor_iterator; complex.insert_open_cell(tau); if(tau.dimension() >= dimension) { return; } Neighbors lower_neighbors; Neighbors final_neighbors; for(Neighbor_iterator i = neighbors.begin(); i != neighbors.end(); ++i){ lower_neighbors.clear(); Simplex sigma( tau); sigma.insert( *i); get_lower_neighbors(graph, *i, lower_neighbors); final_neighbors.clear(); set_intersection(lower_neighbors.begin(),lower_neighbors.end(), neighbors.begin(),neighbors.end(), back_inserter(final_neighbors)); add_cofaces(graph, sigma, final_neighbors, complex, dimension); } }
int main() { Nodes nodes; Neighbors neighbors; int n = 0, edges = 0, index = 0, nodeX = 0, nodeY = 0; while (true) { cin >> n; if (!n) { break; } nodes.clear(); neighbors.clear(); for (index = 0; index < n; index++) { nodes[index] = Uncolored; } cin >> edges; while (edges) { cin >> nodeX >> nodeY; neighbors[nodeX].push_back(nodeY); neighbors[nodeY].push_back(nodeX); edges--; } cout << (Colorable(nodes, neighbors)? "BICOLORABLE." : "NOT BICOLORABLE.") << endl; } return 0; }
void DBSCAN::dbscan( const DBSCAN::DistanceMatrix& dm ) { std::vector< uint8_t > visited( dm.size1() ); uint32_t cluster_id = 0; for ( uint32_t pid = 0; pid < dm.size1(); ++pid ) { if ( !visited[pid] ) { visited[pid] = 1; Neighbors ne = find_neighbors( dm, pid ); if ( ne.size() >= m_min_elems ) { m_labels[pid] = cluster_id; for ( uint32_t i = 0; i < ne.size(); ++i ) { uint32_t nPid = ne[i]; if ( !visited[nPid] ) { visited[nPid] = 1; Neighbors ne1 = find_neighbors( dm, nPid ); if ( ne1.size() >= m_min_elems ) { for ( const auto& n1 : ne1 ) { ne.push_back( n1 ); } } } if ( m_labels[nPid] == -1 ) { m_labels[nPid] = cluster_id; } } ++cluster_id; } } } }
Neighbors find_neighbors_covertree_impl(RandomAccessIterator begin, RandomAccessIterator end, PairwiseCallback callback, IndexType k) { timed_context context("Covertree-based neighbors search"); typedef CoverTreePoint<RandomAccessIterator> TreePoint; v_array<TreePoint> points; for (RandomAccessIterator iter=begin; iter!=end; ++iter) push(points, TreePoint(iter, callback(*iter,*iter))); node<TreePoint> ct = batch_create(callback, points); v_array< v_array<TreePoint> > res; ++k; // because one of the neighbors will be the actual query point k_nearest_neighbor(callback,ct,ct,res,k); Neighbors neighbors; neighbors.resize(end-begin); assert(end-begin==res.index); for (int i=0; i<res.index; ++i) { LocalNeighbors local_neighbors; local_neighbors.reserve(k); for (IndexType j=1; j<=k; ++j) // j=0 is the query point { // The actual query point is found as a neighbor, just ignore it if (res[i][j].iter_-begin==res[i][0].iter_-begin) continue; local_neighbors.push_back(res[i][j].iter_-begin); } neighbors[res[i][0].iter_-begin] = local_neighbors; free(res[i].elements); }; free(res.elements); free_children(ct); free(points.elements); return neighbors; }
Moves MoveGenerator::generateMove(const Board& board) const { Moves result; for(int from=0; from<23; ++from) if(board.getManAt(from) == board.getSelfColor()) { Neighbors neighbors = Board::getNeighbors(from); for(Neighbors::iterator it = neighbors.begin(); it != neighbors.end(); ++it) if(board.isEmpty(*it)) { Board next = board.makeChild(); next.move(from, *it); if(next.closeMill(*it)) { Moves removeMoves = generateRemove(next, board.getOpponentColor()); copy(removeMoves.begin(), removeMoves.end(), back_inserter(result)); } else result.push_back(next); } } return result; }
int main(int argc, char *argv[]) { std::string input; int width; int height; int row = 0; int attempt = 0; while(std::getline(std::cin, input)) { char c = input.at(0); if(c == '.' || c == 'X' || c == '*') { ++row; neighbors.clear(); for(int pos = 0; pos < (int) input.length(); ++pos) { VertexWeight vertexWeight; vertexWeight.first = pos; char temp = input.at(pos); if(temp == '.') { vertexWeight.second = 1; } else if(temp == '*') { vertexWeight.second = 2; } else { vertexWeight.second = 3; } neighbors.push_back(vertexWeight); } adjList.push_back(neighbors); if(height == row) { visited.clear(); for(int index = 0; index < height; ++index) { visited.push_back(std::vector<bool>(width, false)); } for(int i = 0; i < height; ++i) { for(int j = 0; j < width; ++j) { if((visited[i])[j] == false && ((adjList[i])[j]).second != 1) { area = 0; floodFill(i, j, ((adjList[i])[j]).second); if(area) areas.push_back(area); } } } std::cout << "Throw " << ++attempt << std::endl; std::sort(areas.begin(), areas.end()); int numAreas = (int)areas.size() - 1; for(std::vector<int>::iterator it = areas.begin(); it != areas.end(); ++it) { std::cout << *it; if(numAreas--) { std::cout << " "; } } std::cout << std::endl; std::cout << std::endl; } } else { row = 0; std::stringstream sbuf; sbuf << input; sbuf >> width >> height; adjList.clear(); areas.clear(); if(width == 0 && height == 0) { break; } } } return 0; }
void newDocument() { mask.clear(); etchingAction->setEnabled(true); maskAction->setEnabled(true); saveAct->setEnabled(true); QTime t; z_min = 0; int const xMax = SIZE_X; int const yMax = SIZE_Y; int const zMax = SIZE_Z; z_center = z_min + (zMax - 2 - z_min) / 2; cell = Cell(h, k, l); Xsize = cell.getXSize(); Ysize = cell.getYSize(); Zsize = cell.getZSize(); Vx = cell.getVx(); Vy = cell.getVy(); Vz = cell.getVz(); cell.optimize(); auto const numberOfAtomInCell = static_cast<unsigned int>(cell.size()); neighbors = cell.findNeighbors(Xsize, Ysize, Zsize); surfaceXYZ.reset(new Surface3D(cell)); surfaceXYZ->clear(); surfaceXYZ->reserve(SIZE_Z); for (int z = 0; z < SIZE_Z; ++z) { Surface2D surfaceXY; surfaceXY.reserve(SIZE_Y); for (int y = 0; y < SIZE_Y; ++y) { Surface1D surfaceX; surfaceX.reserve(SIZE_X); for (int x = 0; x < SIZE_X; ++x) { Cell cell; for (unsigned char a = 0; a < numberOfAtomInCell; ++a) { Neighbors neighbs; char numberNeighbs = 0; // number of the first neighbors for (int nb = 0; nb < 4; ++nb) { auto& neighborsANb = neighbors[a][nb]; if (x + neighborsANb.x >= 0 && y + neighborsANb.y >= 0 && z + neighborsANb.z >= 0 && x + neighborsANb.x < xMax && y + neighborsANb.y < yMax && z + neighborsANb.z < zMax + 1) { ++numberNeighbs; AtomType neighb = {x + neighborsANb.x, y + neighborsANb.y, z + neighborsANb.z, neighborsANb.type, false}; neighbs.push_back(neighb); } } AtomInfo atom; atom.neighbors = neighbs; atom.firstNeighborsCount = numberNeighbs; atom.deleted = numberNeighbs == 0; // TODO: atom.type = cell.addAtom(atom); } surfaceX.push_back(cell); } surfaceXY.push_back(surfaceX); } surfaceXYZ->push_back(surfaceXY); } surfaceXYZ->rebuildSurfaceAtoms(); drawResult(); }
bool Board::isNeighbor(int lhs, int rhs) { const Neighbors neighbors = getNeighbors(lhs); return find(neighbors.begin(), neighbors.end(), rhs) != neighbors.end(); }
void ClustererDBSCAN::run_cluster(Points samples) { ClusterId cid = 1; // foreach pid for (PointId pid = 0; pid < samples.size(); pid++) { // not already visited if (!_visited[pid]){ _visited[pid] = true; // get the neighbors Neighbors ne = findNeighbors(pid, _eps); // not enough support -> mark as noise if (ne.size() < _minPts) { _noise[pid] = true; } else { //else it's a core point _core[pid] = true; // Add p to current cluster CCluster c; // a new cluster c.push_back(pid); // assign pid to cluster _pointId_to_clusterId[pid]=cid; // go to neighbors for (unsigned int i = 0; i < ne.size(); i++) { PointId nPid = ne[i]; // not already visited if (!_visited[nPid]) { _visited[nPid] = true; // go to neighbors Neighbors ne1 = findNeighbors(nPid, _eps); // enough support if (ne1.size() >= _minPts) { _core[nPid] = true; // join BOOST_FOREACH(Neighbors::value_type n1, ne1) { // join neighbord ne.push_back(n1); } } } // not already assigned to a cluster if (!_pointId_to_clusterId[nPid]) { // add it to the current cluster c.push_back(nPid); _pointId_to_clusterId[nPid]=cid; } }