c++ iterate through all neighbor permutations -


i have vector of n objects, , iterate through neighbor permutations of vector. call neighbor permutation permutation 2 elements of original vector changed : if have vector 'a','b','c','d' :

'b','a','c','d' //is 'a','c','b','d' //is 'b','a','d','c' //is not (2 permutations) 

if use std::next_permutation(myvector.begin(), myvector.end() possible permutations, not "neighbor" ones...

do have idea how achieved ?

initially, thought filter permutations have hamming distance greater 2.

however, if need generate vectors resulting swapping 1 pair, more efficient if this:

for(int = 0; < n; i++)     for(int j = + 1; j < n; j++)         // swap , j 

depending on whether need collect results or not, should make copy or vector before swap, or swap again , j after processed current permutation.

collect results:

std::vector< std::vector<t> > neighbor_permutations; for(int = 0; < n; i++) {     for(int j = + 1; j < n; j++) {         std::vector<t> perm(v);         std::swap(perm[i], perm[j]);         neighbor_permutations.push_back(perm);     } } 

faster version - not collect results:

for(int = 0; < n; i++) {     for(int j = + 1; j < n; j++) {         std::swap(v[i], v[j]);         process_permutation(v);         std::swap(v[i], v[j]);     } } 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -