algorithm - Generating random "break points" in a sequence a minimum distance from each other -


i have sequence n elements, randomly place k "break points" each @ least mindist away each other (and ends). example, n=9, k=2, mindist=2, want generate 1 of following equal probability:

[2 4][2 5][2 6][2 7][3 5][3 6][3 7][4 6][4 7][5 7] 

so far i've come with: placing 1 randomly, "disabling" required amount of nodes around it, picking random number, strikes me unefficient. i'm programming in matlab, language fine.

i using initialize population genetic algorithms, have each of possabilities equally likely, ensure i'm covering whole search space. deterministically distributing break points not enough.

here's function generates possible breakpoint combinations, can store them in list instead of printing them, , uniformly choose 1 of them:

static void breakpointcombinations(list<int> possiblepositions, list<int> breakpointcombination, int remainingbreakpoints, int minspace, int currentpos) {     if (remainingbreakpoints == 0)     {         foreach (int temppos in breakpointcombination)         {             console.write(temppos + " ");         }         console.writeline();     }      else if (remainingbreakpoints * minspace > possiblepositions.count - currentpos)         return;      else     {         if (currentpos >= minspace - 1)         {             breakpointcombination.add(possiblepositions[currentpos]);             breakpointcombinations(possiblepositions, breakpointcombination, remainingbreakpoints - 1, minspace, currentpos + minspace);             breakpointcombination.remove(possiblepositions[currentpos]);         }          breakpointcombinations(possiblepositions, breakpointcombination, remainingbreakpoints, minspace, currentpos + 1);     } } 

the call function (in example, 10 elements, 2 breakpoints, minimal space of 2):

static void main(string[] args) {     list<int> positions = new list<int>();     (int = 0; < 10; i++)     {         positions.add(i);     }      breakpointcombinations(positions, new list<int>(), 2, 2, 0); } 

it implied example space of 2 means there should @ least 1 position between 2 breakpoints (which mean [2, 4] legit example), treated spaces beginning , end same 1 (this wasn't consistent in example), mean if elements [0, ... ,9], first legit breakpoint @ element 1 , last @ element 8.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -